Skip to content

Commit

Permalink
[struct_json][feat]support custom optional(struct_pack compatible) (#781
Browse files Browse the repository at this point in the history
)
  • Loading branch information
qicosmos authored Sep 23, 2024
1 parent fc773e6 commit 0766d83
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 7 additions & 0 deletions include/ylt/standalone/iguana/dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions include/ylt/standalone/iguana/json_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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");
}
Expand Down
3 changes: 1 addition & 2 deletions include/ylt/standalone/iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down

0 comments on commit 0766d83

Please sign in to comment.