DAW JSON Link
Loading...
Searching...
No Matches
daw_json_link_describe.h
Go to the documentation of this file.
1// Copyright (c) Darrell Wright
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5//
6// Official repository: https://github.com/beached/daw_json_link_describe
7//
8
9#pragma once
10
11#include <daw/daw_consteval.h>
12#include <daw/daw_traits.h>
14
15#if not __has_include( <boost/describe.hpp> ) and __has_include( <boost/mp11.hpp> )
16#error \
17 "In order to use daw_json_link_describe.h Boost.Describe and Boost.Mp11 must be available"
18#endif
19
20#include <boost/describe.hpp>
21#include <boost/mp11.hpp>
22#include <cstddef>
23#include <tuple>
24#include <utility>
25
26namespace daw::json {
31 template<typename, typename = void>
32 inline constexpr bool use_boost_describe_v = true;
33
34 namespace describe_impl {
35 template<typename, typename>
36 struct describe_member_impl;
37
40 template<typename T, std::size_t... Is>
41 struct describe_member_impl<T, std::index_sequence<Is...>> {
42 static constexpr char const name[sizeof...( Is )]{ T::name[Is]... };
43 using type =
44 json_link<name,
45 daw::traits::member_type_of_t<DAW_TYPEOF( T::pointer )>>;
46 };
47
48 template<typename T>
49 using describe_member =
50 describe_member_impl<T, std::make_index_sequence<
51 std::char_traits<char>::length( T::name ) + 1>>;
52
53 template<typename>
54 struct member_list;
55
56 template<template<typename...> typename List, typename... Ts>
57 struct member_list<List<Ts...>> {
58 template<typename U>
59 using desc_t = typename describe_member<U>::type;
60
61 using type = json_member_list<desc_t<Ts>...>;
62 };
63 } // namespace describe_impl
64
65 template<typename T>
66 DAW_JSON_REQUIRES( boost::describe::has_describe_members<T>::value
67 and use_boost_describe_v<T> )
68 struct json_data_contract<T> {
69 private:
70 using pub_desc_t =
71 boost::describe::describe_members<T, boost::describe::mod_public>;
72 using pri_desc_t =
73 boost::describe::describe_members<T, boost::describe::mod_private>;
74 using pro_desc_t =
75 boost::describe::describe_members<T, boost::describe::mod_protected>;
76 static_assert( boost::mp11::mp_empty<pri_desc_t>::value,
77 "Classes with private member variables are not supported. "
78 "Must use a manual mapping." );
79 static_assert( boost::mp11::mp_empty<pro_desc_t>::value,
80 "Classes with protected member variables are not supported. "
81 "Must use a manual mapping." );
82
83 template<typename U>
84 using desc_t = typename describe_impl::describe_member<U>::type;
85
86 template<template<typename...> typename List, typename... Ts>
87 static inline constexpr auto
88 to_json_data_impl( T const &value, List<Ts...> const & ) noexcept {
89 return std::forward_as_tuple( value.*Ts::pointer... );
90 }
91
92 public:
93 using type = typename describe_impl::member_list<pub_desc_t>::type;
94
95 static constexpr auto to_json_data( T const &value ) noexcept {
96 return to_json_data_impl( value, pub_desc_t{ } );
97 }
98 };
99} // namespace daw::json
#define DAW_JSON_REQUIRES(...)
Customization point traits.
constexpr bool use_boost_describe_v
Types that use Boost.Describe need to specialize use_boost_describe_v for their type with a bool valu...
Mapping class for JSON data structures to C++. It must be specialized in order to parse to a user cla...