From 8cc0d689d19501ab95fb2809506121513d79fd13 Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Sun, 16 Jun 2024 15:22:52 +0200 Subject: [PATCH] Fix build on MSVC --- libbroker/broker/convert.hh | 5 +++-- libbroker/broker/detail/sqlite_backend.cc | 3 +-- libbroker/broker/detail/type_traits.hh | 15 +++++++++++++++ libbroker/broker/logger.hh | 3 +++ libbroker/broker/master.test.cc | 21 +++++++++++---------- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/libbroker/broker/convert.hh b/libbroker/broker/convert.hh index 0fe5bb8c..41177ddd 100644 --- a/libbroker/broker/convert.hh +++ b/libbroker/broker/convert.hh @@ -130,8 +130,9 @@ template std::enable_if_t< detail::has_convert_v // must have a convert function && !std::is_arithmetic_v // avoid ambiguitiy with default overloads - && !std::is_convertible_v, // avoid ambiguity with - // conversion-to-string + && !std::is_convertible_v // avoid ambiguity with + // conversion-to-string + && !std::is_same_v, std::ostream&> operator<<(std::ostream& os, const T& x) { std::string str; diff --git a/libbroker/broker/detail/sqlite_backend.cc b/libbroker/broker/detail/sqlite_backend.cc index bc0d1e64..d3a7dbe9 100644 --- a/libbroker/broker/detail/sqlite_backend.cc +++ b/libbroker/broker/detail/sqlite_backend.cc @@ -282,8 +282,7 @@ struct sqlite_backend::impl { if (!dir.empty()) { if (!detail::is_directory(dir) && !detail::mkdirs(dir)) { log::store::error("sqlite-create-dir-failed", - "failed to create directory for database: {}", - dir.c_str()); + "failed to create directory for database: {}", dir); return false; } } diff --git a/libbroker/broker/detail/type_traits.hh b/libbroker/broker/detail/type_traits.hh index f727c82e..97b8dc8e 100644 --- a/libbroker/broker/detail/type_traits.hh +++ b/libbroker/broker/detail/type_traits.hh @@ -248,6 +248,21 @@ public: static constexpr bool value = std::is_same_v; }; +// Trait that checks whether a type has a .string() member function. +template +class has_string_member_fn { +private: + template + static auto sfinae(const U& x) -> decltype(x.string()); + + static void sfinae(...); + + using result = std::decay_t()))>; + +public: + static constexpr bool value = std::is_same_v; +}; + } // namespace broker::detail #define BROKER_DEF_HAS_ENCODE_IN_NS(ns_name) \ diff --git a/libbroker/broker/logger.hh b/libbroker/broker/logger.hh index 7df2f9ec..c5118c2d 100644 --- a/libbroker/broker/logger.hh +++ b/libbroker/broker/logger.hh @@ -97,6 +97,9 @@ OutputIterator fmt_to(OutputIterator out, std::string_view fmt, const T& arg, } else if constexpr (has_to_string::value) { auto str = to_string(arg); out = std::copy(str.begin(), str.end(), out); + } else if constexpr (has_string_member_fn::value) { + auto&& str = arg.string(); + out = std::copy(str.begin(), str.end(), out); } else { static_assert(std::is_convertible_v); for (auto cstr = arg; *cstr != '\0'; ++cstr) diff --git a/libbroker/broker/master.test.cc b/libbroker/broker/master.test.cc index 9016586f..f0e48ac3 100644 --- a/libbroker/broker/master.test.cc +++ b/libbroker/broker/master.test.cc @@ -85,7 +85,7 @@ bool operator==(const string_list& xs, const pattern_list& ys) { } struct fixture : base_fixture { - string_list log; + string_list log_lines; worker logger; caf::timespan tick_interval = defaults::store::tick_interval; @@ -100,11 +100,11 @@ struct fixture : base_fixture { [this](data_message msg) { auto content = get_data(msg).to_data(); if (auto insert = store_event::insert::make(content)) - log.emplace_back(to_string(insert)); + log_lines.emplace_back(to_string(insert)); else if (auto update = store_event::update::make(content)) - log.emplace_back(to_string(update)); + log_lines.emplace_back(to_string(update)); else if (auto erase = store_event::erase::make(content)) - log.emplace_back(to_string(erase)); + log_lines.emplace_back(to_string(erase)); else FAIL("unknown event: " << to_string(content)); }, @@ -162,12 +162,13 @@ TEST(local_master) { CHECK_EQUAL(unbox(ds.put_unique("bar", "unicorn")), data{false}); // check log run(tick_interval); - CHECK_EQUAL(log, pattern_list({ - "insert\\(foo, hello, world, (null|none), .+\\)", - "update\\(foo, hello, world, universe, (null|none), .+\\)", - "erase\\(foo, hello, .+\\)", - "insert\\(foo, bar, baz, .+\\)", - })); + CHECK_EQUAL(log_lines, + pattern_list({ + "insert\\(foo, hello, world, (null|none), .+\\)", + "update\\(foo, hello, world, universe, (null|none), .+\\)", + "erase\\(foo, hello, .+\\)", + "insert\\(foo, bar, baz, .+\\)", + })); // done caf::anon_send_exit(core, caf::exit_reason::user_shutdown); }