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
12
13#include <daw/daw_consteval.h>
14#include <daw/daw_enable_requires.h>
15#include <daw/daw_traits.h>
16
17#if not __has_include( <boost/describe.hpp> ) and __has_include( <boost/mp11.hpp> )
18#error \
19 "In order to use daw_json_link_describe.h Boost.Describe and Boost.Mp11 must be available"
20#endif
21
22#include <boost/describe.hpp>
23#include <boost/mp11.hpp>
24#include <cstddef>
25#include <tuple>
26#include <utility>
27
28namespace daw::json {
33 template<typename, typename = void>
34 inline constexpr bool use_boost_describe_v = true;
35
36 namespace describe_impl {
37 template<typename, typename>
38 struct describe_member_impl;
39
42 template<typename T, std::size_t... Is>
43 struct describe_member_impl<T, std::index_sequence<Is...>> {
44 static constexpr char const name[sizeof...( Is )]{ T::name[Is]... };
45 using type =
46 json_link<name,
47 daw::traits::member_type_of_t<DAW_TYPEOF( T::pointer )>>;
48 };
49
50 template<typename T>
51 using describe_member =
52 describe_member_impl<T, std::make_index_sequence<
53 std::char_traits<char>::length( T::name ) + 1>>;
54
55 template<typename>
56 struct member_list;
57
58 template<template<typename...> typename List, typename... Ts>
59 struct member_list<List<Ts...>> {
60 template<typename U>
61 using desc_t = typename describe_member<U>::type;
62
63 using type = json_member_list<desc_t<Ts>...>;
64 };
65 } // namespace describe_impl
66
67 template<typename T>
68 DAW_REQUIRES( boost::describe::has_describe_members<T>::value
69 and use_boost_describe_v<T> )
70 struct json_data_contract<T> {
71 private:
72 using pub_desc_t =
73 boost::describe::describe_members<T, boost::describe::mod_public>;
74 using pri_desc_t =
75 boost::describe::describe_members<T, boost::describe::mod_private>;
76 using pro_desc_t =
77 boost::describe::describe_members<T, boost::describe::mod_protected>;
78 static_assert( boost::mp11::mp_empty<pri_desc_t>::value,
79 "Classes with private member variables are not supported. "
80 "Must use a manual mapping." );
81 static_assert( boost::mp11::mp_empty<pro_desc_t>::value,
82 "Classes with protected member variables are not supported. "
83 "Must use a manual mapping." );
84
85 template<typename U>
86 using desc_t = typename describe_impl::describe_member<U>::type;
87
88 template<template<typename...> typename List, typename... Ts>
89 static constexpr auto to_json_data_impl( T const &value,
90 List<Ts...> const & ) noexcept {
91 return std::forward_as_tuple( value.*Ts::pointer... );
92 }
93
94 public:
95 using type = typename describe_impl::member_list<pub_desc_t>::type;
96
97 static constexpr auto to_json_data( T const &value ) noexcept {
98 return to_json_data_impl( value, pub_desc_t{ } );
99 }
100 };
101} // namespace daw::json
Customization point traits.
DAW_REQUIRES(boost::describe::has_describe_members< T >::value and use_boost_describe_v< T >) struct json_data_contract< T >
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...