From 5bd83d3c91cd180ebdd351cdb690e5ac1054072a Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 23 Sep 2024 15:07:28 +0800 Subject: [PATCH 1/2] some improvement --- include/ylt/standalone/iguana/dynamic.hpp | 7 ++++++- include/ylt/standalone/iguana/json_writer.hpp | 10 ++++++---- include/ylt/standalone/iguana/util.hpp | 3 +-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/ylt/standalone/iguana/dynamic.hpp b/include/ylt/standalone/iguana/dynamic.hpp index 47cf20154..898c53461 100644 --- a/include/ylt/standalone/iguana/dynamic.hpp +++ b/include/ylt/standalone/iguana/dynamic.hpp @@ -12,7 +12,12 @@ constexpr inline uint8_t ENABLE_ALL = 0x0F; template struct base_impl : public base { - base_impl() { [[maybe_unused]] static bool r = register_type(); } + base_impl() { + // make sure init T before main, and can register_type before main. + [[maybe_unused]] static auto t = + ylt::reflection::internal::wrapper::value; + [[maybe_unused]] static bool r = register_type(); + } void to_pb(std::string& str) const override { if constexpr ((ENABLE_FLAG & ENABLE_PB) != 0) { to_pb_adl((iguana_adl_t*)nullptr, *(static_cast(this)), str); diff --git a/include/ylt/standalone/iguana/json_writer.hpp b/include/ylt/standalone/iguana/json_writer.hpp index 1901d565b..75d4a70e9 100644 --- a/include/ylt/standalone/iguana/json_writer.hpp +++ b/include/ylt/standalone/iguana/json_writer.hpp @@ -12,8 +12,9 @@ template , int> = 0> IGUANA_INLINE void to_json(T &&t, Stream &s); namespace detail { -template -IGUANA_INLINE void to_json_impl(Stream &ss, const std::optional &val); +template , int> = 0> +IGUANA_INLINE void to_json_impl(Stream &ss, const T &val); template , int> = 0> @@ -151,8 +152,9 @@ IGUANA_INLINE void to_json_impl(Stream &ss, T val) { } } -template -IGUANA_INLINE void to_json_impl(Stream &ss, const std::optional &val) { +template , int>> +IGUANA_INLINE void to_json_impl(Stream &ss, const T &val) { if (!val) { ss.append("null"); } diff --git a/include/ylt/standalone/iguana/util.hpp b/include/ylt/standalone/iguana/util.hpp index 614040cc0..8c3fca9e2 100644 --- a/include/ylt/standalone/iguana/util.hpp +++ b/include/ylt/standalone/iguana/util.hpp @@ -47,8 +47,7 @@ template inline constexpr bool enum_v = std::is_enum_v>; template -constexpr inline bool optional_v = - is_template_instant_of>::value; +constexpr inline bool optional_v = ylt::reflection::optional; template struct is_container : std::false_type {}; From 8e684e4ebaffec13544215949555566579d1e7aa Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 23 Sep 2024 16:04:28 +0800 Subject: [PATCH 2/2] fix --- include/ylt/standalone/iguana/dynamic.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/ylt/standalone/iguana/dynamic.hpp b/include/ylt/standalone/iguana/dynamic.hpp index 898c53461..5b134baf6 100644 --- a/include/ylt/standalone/iguana/dynamic.hpp +++ b/include/ylt/standalone/iguana/dynamic.hpp @@ -12,12 +12,7 @@ constexpr inline uint8_t ENABLE_ALL = 0x0F; template struct base_impl : public base { - base_impl() { - // make sure init T before main, and can register_type before main. - [[maybe_unused]] static auto t = - ylt::reflection::internal::wrapper::value; - [[maybe_unused]] static bool r = register_type(); - } + base_impl() { [[maybe_unused]] static bool r = register_type(); } void to_pb(std::string& str) const override { if constexpr ((ENABLE_FLAG & ENABLE_PB) != 0) { to_pb_adl((iguana_adl_t*)nullptr, *(static_cast(this)), str); @@ -164,6 +159,13 @@ struct base_impl : public base { virtual ~base_impl() {} mutable size_t cache_size = 0; + + private: + virtual void dummy() { + // make sure init T before main, and can register_type before main. + [[maybe_unused]] static auto t = + ylt::reflection::internal::wrapper::value; + } }; IGUANA_INLINE std::shared_ptr create_instance(std::string_view name) {