DAW JSON Link
Loading...
Searching...
No Matches
daw_json_boost_pfr_mapping.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
7//
8
9#pragma once
10
11#include "daw/daw_tuple_forward.h"
12#include "daw_json_link.h"
13
14#include <daw/daw_attributes.h>
15#include <daw/daw_consteval.h>
16
17#include <boost/pfr.hpp>
18#include <daw/stdinc/tuple_traits.h>
19#include <type_traits>
20#include <utility>
21
22namespace daw::json {
25 template<typename T>
26 requires( std::is_aggregate_v<T> and std::is_class_v<T> and
27 not std::is_empty_v<T> ) //
28 inline constexpr bool use_boost_pfr = false;
29
30 namespace pfr_details {
31 template<typename T, std::size_t Idx>
32 DAW_CONSTEVAL auto get_member_name( ) {
33 constexpr auto name = boost::pfr::get_name<Idx, T>( );
34 static_assert( not name.empty( ) );
35 return json_name<name.size( ) + 1>(
36 name.data( ), std::make_index_sequence<name.size( ) + 1>{ } );
37 }
38
39 template<typename, typename>
40 struct make_data_contract;
41
42 template<typename T, std::size_t... Is>
43 struct make_data_contract<T, std::index_sequence<Is...>> {
44 private:
45 using member_types = DAW_TYPEOF(
46 boost::pfr::structure_to_tuple( std::declval<T const &>( ) ) );
47
48 public:
49 using type =
51 std::tuple_element_t<Is, member_types>>...>;
52
53 DAW_ATTRIB_INLINE static constexpr auto to_json_data( T const &value ) {
54 return boost::pfr::structure_tie( value );
55 }
56 };
57 } // namespace pfr_details
58
59 template<typename T>
60 requires use_boost_pfr<T> //
62 : pfr_details::make_data_contract<
63 T, std::make_index_sequence<boost::pfr::tuple_size_v<T>>> {};
64} // namespace daw::json
Customization point traits.
constexpr bool use_boost_pfr
Enable Boost PFR mappings for a type.
Mapping class for JSON data structures to C++. It must be specialized in order to parse to a user cla...