[struct_json][feat]support custom optional(struct_pack compatible) (#781)

This commit is contained in:
qicosmos 2024-09-23 16:34:23 +08:00 committed by GitHub
parent fc773e61cb
commit 0766d839fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 6 deletions

View File

@ -159,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<T>::value;
}
};
IGUANA_INLINE std::shared_ptr<base> create_instance(std::string_view name) {

View File

@ -12,8 +12,9 @@ template <bool Is_writing_escape = true, typename Stream, typename T,
std::enable_if_t<ylt_refletable_v<T>, int> = 0>
IGUANA_INLINE void to_json(T &&t, Stream &s);
namespace detail {
template <bool Is_writing_escape = true, typename Stream, typename T>
IGUANA_INLINE void to_json_impl(Stream &ss, const std::optional<T> &val);
template <bool Is_writing_escape = true, typename Stream, typename T,
std::enable_if_t<optional_v<T>, int> = 0>
IGUANA_INLINE void to_json_impl(Stream &ss, const T &val);
template <bool Is_writing_escape = true, typename Stream, typename T,
std::enable_if_t<fixed_array_v<T>, int> = 0>
@ -151,8 +152,9 @@ IGUANA_INLINE void to_json_impl(Stream &ss, T val) {
}
}
template <bool Is_writing_escape, typename Stream, typename T>
IGUANA_INLINE void to_json_impl(Stream &ss, const std::optional<T> &val) {
template <bool Is_writing_escape, typename Stream, typename T,
std::enable_if_t<optional_v<T>, int>>
IGUANA_INLINE void to_json_impl(Stream &ss, const T &val) {
if (!val) {
ss.append("null");
}

View File

@ -47,8 +47,7 @@ template <typename T>
inline constexpr bool enum_v = std::is_enum_v<std::decay_t<T>>;
template <typename T>
constexpr inline bool optional_v =
is_template_instant_of<std::optional, std::remove_cvref_t<T>>::value;
constexpr inline bool optional_v = ylt::reflection::optional<T>;
template <class, class = void>
struct is_container : std::false_type {};