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