changeset 720:34af864668fa

Core: use more C++17
author David Demelier <markand@malikania.fr>
date Mon, 16 Jul 2018 13:14:10 +0200
parents fbb0c95724c3
children 2fa1f2c898ee
files irccdctl/rule_add_cli.cpp libirccd-core/irccd/acceptor.hpp libirccd-core/irccd/connector.hpp libirccd-core/irccd/fs_util.hpp libirccd-core/irccd/ini_util.hpp libirccd-core/irccd/socket_acceptor.hpp libirccd-core/irccd/socket_connector.hpp libirccd-core/irccd/socket_stream.hpp libirccd-core/irccd/stream.hpp libirccd-core/irccd/string_util.cpp libirccd-core/irccd/string_util.hpp libirccd-core/irccd/system.cpp libirccd-core/irccd/system.hpp libirccd-core/irccd/tls_acceptor.hpp libirccd-core/irccd/tls_connector.hpp libirccd-core/irccd/tls_stream.hpp
diffstat 16 files changed, 73 insertions(+), 117 deletions(-) [+]
line wrap: on
line diff
--- a/irccdctl/rule_add_cli.cpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/irccdctl/rule_add_cli.cpp	Mon Jul 16 13:14:10 2018 +0200
@@ -74,7 +74,7 @@
     }
 
     // Index.
-    boost::optional<unsigned> index;
+    std::optional<unsigned> index;
 
     if (result.count("-i") > 0 && !(index = to_uint(result.find("-i")->second)))
         throw std::invalid_argument("invalid index argument");
--- a/libirccd-core/irccd/acceptor.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/acceptor.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -28,9 +28,7 @@
 #include <memory>
 #include <system_error>
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 class stream;
 
@@ -70,8 +68,6 @@
     virtual void accept(accept_handler handler) = 0;
 };
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_ACCEPTOR_HPP
--- a/libirccd-core/irccd/connector.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/connector.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -28,9 +28,7 @@
 #include <memory>
 #include <system_error>
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 class stream;
 
@@ -72,8 +70,6 @@
     virtual void connect(connect_handler handler) = 0;
 };
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_CONNECTOR_HPP
--- a/libirccd-core/irccd/fs_util.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/fs_util.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -29,12 +29,10 @@
 
 #include <boost/filesystem.hpp>
 
-namespace irccd {
-
 /**
  * \brief Filesystem utilities.
  */
-namespace fs_util {
+namespace irccd::fs_util {
 
 // {{{ base_name
 
@@ -142,8 +140,6 @@
 
 // }}}
 
-} // !fs_util
-
-} // !irccd
+} // !irccd::fs_util
 
 #endif // !IRCCD_COMMON_FS_UTIL_HPP
--- a/libirccd-core/irccd/ini_util.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/ini_util.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -24,7 +24,7 @@
  * \brief Ini utilities.
  */
 
-#include <boost/optional.hpp>
+#include <optional>
 
 #include "ini.hpp"
 #include "string_util.hpp"
@@ -44,7 +44,7 @@
  * \return the value or none if not able to convert
  */
 template <typename Int>
-inline boost::optional<Int> get_uint(const ini::section& sc, const std::string& name) noexcept
+inline std::optional<Int> get_uint(const ini::section& sc, const std::string& name) noexcept
 {
     return string_util::to_uint<Int>(sc.get(name).value());
 }
@@ -78,7 +78,7 @@
  * \return the value or none if not able to convert
  */
 template <typename Int>
-inline boost::optional<Int> optional_uint(const ini::section& sc,
+inline std::optional<Int> optional_uint(const ini::section& sc,
                                           const std::string& name,
                                           Int def) noexcept
 {
--- a/libirccd-core/irccd/socket_acceptor.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/socket_acceptor.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -29,9 +29,7 @@
 #include "acceptor.hpp"
 #include "socket_stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief Socket stream acceptor interface.
@@ -155,8 +153,6 @@
 
 #endif
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_SOCKET_ACCEPTOR_HPP
--- a/libirccd-core/irccd/socket_connector.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/socket_connector.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -31,9 +31,7 @@
 #include "connector.hpp"
 #include "socket_stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief Socket connection interface.
@@ -169,8 +167,6 @@
 
 #endif
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_SOCKET_CONNECTOR_HPP
--- a/libirccd-core/irccd/socket_stream.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/socket_stream.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -36,9 +36,7 @@
 
 #include "stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \cond HIDDEN_SYMBOLS
@@ -239,8 +237,6 @@
 
 #endif
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_SOCKET_STREAM_HPP
--- a/libirccd-core/irccd/stream.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/stream.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -29,9 +29,7 @@
 
 #include "json.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief Read completion handler.
@@ -84,8 +82,6 @@
     virtual void write(const nlohmann::json& json, write_handler handler) = 0;
 };
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_COMMON_STREAM_HPP
--- a/libirccd-core/irccd/string_util.cpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/string_util.cpp	Mon Jul 16 13:14:10 2018 +0200
@@ -30,6 +30,7 @@
 
 #include <cassert>
 #include <iomanip>
+#include <regex>
 
 #include "string_util.hpp"
 
@@ -43,7 +44,7 @@
 
 namespace {
 
-const std::unordered_map<std::string, int> irc_colors{
+const std::unordered_map<std::string_view, int> irc_colors{
     { "white",      0   },
     { "black",      1   },
     { "blue",       2   },
@@ -62,7 +63,7 @@
     { "lightgrey",  15  }
 };
 
-const std::unordered_map<std::string, char> irc_attributes{
+const std::unordered_map<std::string_view, char> irc_attributes{
     { "bold",       '\x02'  },
     { "italic",     '\x09'  },
     { "strike",     '\x13'  },
@@ -72,7 +73,7 @@
     { "reverse",    '\x16'  }
 };
 
-const std::unordered_map<std::string, unsigned> shell_colors{
+const std::unordered_map<std::string_view, unsigned> shell_colors{
     { "black",      30  },
     { "red",        31  },
     { "green",      32  },
@@ -84,7 +85,7 @@
     { "default",    39  },
 };
 
-const std::unordered_map<std::string, unsigned> shell_attributes{
+const std::unordered_map<std::string_view, unsigned> shell_attributes{
     { "bold",       1   },
     { "dim",        2   },
     { "underline",  4   },
@@ -93,12 +94,12 @@
     { "hidden",     8   }
 };
 
-inline bool is_reserved(char token) noexcept
+auto is_reserved(char token) noexcept -> bool
 {
     return token == '#' || token == '@' || token == '$' || token == '!';
 }
 
-std::string subst_date(const std::string& text, const subst& params)
+auto subst_date(const std::string& text, const subst& params) -> std::string
 {
     std::ostringstream oss;
 
@@ -119,9 +120,9 @@
     return oss.str();
 }
 
-std::string subst_keywords(const std::string& content, const subst& params)
+auto subst_keywords(const std::string& content, const subst& params) -> std::string
 {
-    auto value = params.keywords.find(content);
+    auto value = params.keywords.find(std::string(content));
 
     if (value != params.keywords.end())
         return value->second;
@@ -383,7 +384,7 @@
 
 std::string strip(std::string str) noexcept
 {
-    const auto test = [] (auto c) { return !std::isspace(c); };
+    const auto test = [] (auto c) noexcept { return !std::isspace(c); };
 
     str.erase(str.begin(), std::find_if(str.begin(), str.end(), test));
     str.erase(std::find_if(str.rbegin(), str.rend(), test).base(), str.end());
@@ -395,7 +396,7 @@
 
 // {{{ split
 
-std::vector<std::string> split(const std::string& list, const std::string& delimiters, int max)
+auto split(std::string_view list, const std::string& delimiters, int max) -> std::vector<std::string>
 {
     std::vector<std::string> result;
     std::size_t next = -1, current;
@@ -428,11 +429,22 @@
 
 // }}}
 
+// {{{ is_identifier
+
+auto is_identifier(std::string_view name) noexcept -> bool
+{
+    static const std::regex regex("[A-Za-z0-9-_]+");
+
+    return std::regex_match(std::string(name), regex);
+}
+
+// }}}
+
 // {{{ is_boolean
 
-bool is_boolean(std::string value) noexcept
+auto is_boolean(std::string value) noexcept -> bool
 {
-    std::transform(value.begin(), value.end(), value.begin(), [] (auto c) {
+    std::transform(value.begin(), value.end(), value.begin(), [] (auto c) noexcept {
         return toupper(c);
     });
 
--- a/libirccd-core/irccd/string_util.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/string_util.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -29,23 +29,20 @@
 #include <ctime>
 #include <initializer_list>
 #include <limits>
-#include <regex>
+#include <optional>
 #include <sstream>
 #include <stdexcept>
 #include <string>
+#include <string_view>
 #include <type_traits>
 #include <unordered_map>
 
 #include <boost/format.hpp>
-#include <boost/optional.hpp>
-
-namespace irccd {
 
 /**
- * \file string_util.hpp
  * \brief String utilities.
  */
-namespace string_util {
+namespace irccd::string_util {
 
 // {{{ subst
 
@@ -190,7 +187,7 @@
  *   - <strong>\@{white,black,bold,underline}</strong>: will write white text on
  *     black in both bold and underline.
  */
-std::string format(std::string text, const subst& params = {});
+auto format(std::string text, const subst& params = {}) -> std::string;
 
 // }}}
 
@@ -202,7 +199,7 @@
  * \param str the string
  * \return the removed white spaces
  */
-std::string strip(std::string str) noexcept;
+auto strip(std::string str) noexcept -> std::string;
 
 // }}}
 
@@ -216,7 +213,7 @@
  * \param max max number of split
  * \return a list of string splitted
  */
-std::vector<std::string> split(const std::string& list, const std::string& delimiters, int max = -1);
+auto split(std::string_view list, const std::string& delimiters, int max = -1) -> std::vector<std::string>;
 
 // }}}
 
@@ -231,7 +228,7 @@
  * \return the string
  */
 template <typename InputIt, typename DelimType = char>
-std::string join(InputIt first, InputIt last, DelimType delim = ':')
+auto join(InputIt first, InputIt last, DelimType delim = ':') -> std::string
 {
     std::ostringstream oss;
 
@@ -253,7 +250,7 @@
  * \return the string
  */
 template <typename Container, typename DelimType = char>
-std::string join(const Container& c, DelimType delim = ':')
+auto join(const Container& c, DelimType delim = ':') -> std::string
 {
     return join(c.begin(), c.end(), delim);
 }
@@ -266,7 +263,7 @@
  * \return the string
  */
 template <typename T, typename DelimType = char>
-inline std::string join(std::initializer_list<T> list, DelimType delim = ':')
+auto join(std::initializer_list<T> list, DelimType delim = ':') -> std::string
 {
     return join(list.begin(), list.end(), delim);
 }
@@ -281,10 +278,7 @@
  * \param name the identifier name
  * \return true if is valid
  */
-inline bool is_identifier(const std::string& name)
-{
-    return std::regex_match(name, std::regex("[A-Za-z0-9-_]+"));
-}
+auto is_identifier(std::string_view name) noexcept -> bool;
 
 // }}}
 
@@ -297,7 +291,7 @@
  * \return true if is boolean
  * \note this function is case-insensitive
  */
-bool is_boolean(std::string value) noexcept;
+auto is_boolean(std::string value) noexcept -> bool;
 
 // }}}
 
@@ -336,7 +330,7 @@
  * \return the string
  */
 template <typename Format, typename... Args>
-std::string sprintf(const Format& format, const Args&... args)
+auto sprintf(const Format& format, const Args&... args) -> std::string
 {
     boost::format fmter(format);
 
@@ -358,9 +352,9 @@
  * \return the value or boost::none if not convertible
  */
 template <typename T = int>
-boost::optional<T> to_int(const std::string& str,
-                          T min = std::numeric_limits<T>::min(),
-                          T max = std::numeric_limits<T>::max()) noexcept
+auto to_int(const std::string& str,
+            T min = std::numeric_limits<T>::min(),
+            T max = std::numeric_limits<T>::max()) noexcept -> std::optional<T>
 {
     static_assert(std::is_signed<T>::value, "must be signed");
 
@@ -368,7 +362,7 @@
     auto v = std::strtoll(str.c_str(), &end, 10);
 
     if (*end != '\0' || v < min || v > max)
-        return boost::none;
+        return std::nullopt;
 
     return static_cast<T>(v);
 }
@@ -387,9 +381,9 @@
  * \return the value or boost::none if not convertible
  */
 template <typename T = unsigned>
-boost::optional<T> to_uint(const std::string& str,
-                           T min = std::numeric_limits<T>::min(),
-                           T max = std::numeric_limits<T>::max()) noexcept
+auto to_uint(const std::string& str,
+             T min = std::numeric_limits<T>::min(),
+             T max = std::numeric_limits<T>::max()) noexcept -> std::optional<T>
 {
     static_assert(std::is_unsigned<T>::value, "must be unsigned");
 
@@ -397,15 +391,13 @@
     auto v = std::strtoull(str.c_str(), &end, 10);
 
     if (*end != '\0' || v < min || v > max)
-        return boost::none;
+        return std::nullopt;
 
     return static_cast<T>(v);
 }
 
 // }}}
 
-} // !string_util
-
-} // !irccd
+} // !irccd::string_util
 
 #endif // !IRCCD_COMMON_STRING_UTIL_HPP
--- a/libirccd-core/irccd/system.cpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/system.cpp	Mon Jul 16 13:14:10 2018 +0200
@@ -58,9 +58,7 @@
 #include "string_util.hpp"
 #include "xdg.hpp"
 
-namespace irccd {
-
-namespace sys {
+namespace irccd::sys {
 
 namespace {
 
@@ -500,6 +498,4 @@
 
 // }}}
 
-} // !sys
-
-} // !irccd
+} // !irccd::sys
--- a/libirccd-core/irccd/system.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/system.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -33,12 +33,10 @@
 
 #include "sysconfig.hpp"
 
-namespace irccd {
-
 /**
  * \brief Namespace for system functions.
  */
-namespace sys {
+namespace irccd::sys {
 
 /**
  * Set the program name, needed for some functions or some systems.
@@ -160,8 +158,6 @@
 std::vector<std::string> plugin_filenames(const std::string& name,
                                           const std::vector<std::string>& extensions);
 
-} // !sys
-
-} // !irccd
+} // !irccd::sys
 
 #endif // !IRCCD_COMMON_SYSTEM_HPP
--- a/libirccd-core/irccd/tls_acceptor.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/tls_acceptor.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -31,9 +31,7 @@
 #include "socket_acceptor.hpp"
 #include "tls_stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief TLS/SSL acceptors.
@@ -87,9 +85,7 @@
     });
 }
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_HAVE_SSL
 
--- a/libirccd-core/irccd/tls_connector.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/tls_connector.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -31,9 +31,7 @@
 #include "socket_connector.hpp"
 #include "tls_stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief TLS/SSL connectors.
@@ -86,9 +84,7 @@
     });
 }
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_HAVE_SSL
 
--- a/libirccd-core/irccd/tls_stream.hpp	Tue Jul 10 21:44:00 2018 +0200
+++ b/libirccd-core/irccd/tls_stream.hpp	Mon Jul 16 13:14:10 2018 +0200
@@ -32,9 +32,7 @@
 
 #include "socket_stream.hpp"
 
-namespace irccd {
-
-namespace io {
+namespace irccd::io {
 
 /**
  * \brief TLS/SSL streams.
@@ -55,9 +53,7 @@
     }
 };
 
-} // !io
-
-} // !irccd
+} // !irccd::io
 
 #endif // !IRCCD_HAVE_SSL