GMP 0.4.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
traits.hpp
Go to the documentation of this file.
1// ___ __ __ ___
2// / __| \/ | _ \ GMP(Generative Metaprogramming)
3// | (_ | |\/| | _/ version 0.4.0
4// \___|_| |_|_| https://github.com/lkimuk/gmp
5//
6// SPDX-FileCopyrightText: 2023-2026 Miles Li <https://www.cppmore.com/>
7// SPDX-License-Identifier: MIT
8//
9// This file is part of the GMP (Generative Metaprogramming) library.
10// Full project source: https://github.com/lkimuk/gmp
11
12#ifndef GMP_SERIALIZATION_TRAITS_HPP_
13#define GMP_SERIALIZATION_TRAITS_HPP_
14
15#include <array>
16#include <concepts>
17#include <cstddef>
18#include <string_view>
19#include <tuple>
20#include <type_traits>
21#include <utility>
22
24
25namespace gmp {
26
27template <typename T> struct serialization_traits {};
28
29template <typename T> struct serialization_schema {};
30
31template <typename T> struct type_tag {
32 using type = T;
33};
34
35namespace detail {
36
37struct no_serialization_default {
38 friend constexpr bool operator==(no_serialization_default, no_serialization_default) = default;
39};
40
41inline constexpr no_serialization_default no_default{};
42
43} // namespace detail
44
45template <fixed_string Member, fixed_string Wire = Member, bool Transient = false,
46 auto Default = detail::no_default, fixed_string... Aliases>
48 static constexpr auto member = Member;
49 static constexpr auto wire_name = Wire;
50 static constexpr bool is_transient = Transient;
51 static constexpr auto default_provider = Default;
52 static constexpr bool has_default =
53 !std::same_as<std::remove_cv_t<decltype(Default)>, detail::no_serialization_default>;
54
55 template <fixed_string Name> consteval auto name() const {
57 }
58
59 template <fixed_string Name> consteval auto alias() const {
61 }
62
63 consteval auto transient() const {
64 return serialization_field<Member, Wire, true, Default, Aliases...>{};
65 }
66
67 template <auto Value> consteval auto defaulted() const {
69 }
70
71 template <auto Provider> consteval auto default_with() const {
72 static_assert(std::is_invocable_v<decltype(Provider)>);
74 }
75
76 [[nodiscard]] static constexpr bool matches(std::string_view input) {
77 return input == Wire.to_string_view() || ((input == Aliases.to_string_view()) || ...);
78 }
79
80 template <typename Other> [[nodiscard]] static constexpr bool overlaps() {
81 return Other::matches(Wire.to_string_view()) ||
82 (Other::matches(Aliases.to_string_view()) || ...);
83 }
84
85 [[nodiscard]] static consteval bool names_unique() {
86 constexpr std::array names{Wire.to_string_view(), Aliases.to_string_view()...};
87 for (std::size_t i = 0; i < names.size(); ++i) {
88 for (std::size_t j = i + 1; j < names.size(); ++j) {
89 if (names[i] == names[j]) {
90 return false;
91 }
92 }
93 }
94 return true;
95 }
96
97 template <typename T> static constexpr T make_default() {
98 static_assert(has_default);
99 if constexpr (std::is_invocable_v<decltype(Default)>) {
100 return static_cast<T>(Default());
101 } else {
102 return static_cast<T>(Default);
103 }
104 }
105};
106
107template <fixed_string Member> inline constexpr serialization_field<Member> field{};
108
109template <typename... Fields> consteval auto define_schema(Fields... fields) {
110 return std::tuple<Fields...>{fields...};
111}
112
113} // namespace gmp
114
115#endif // GMP_SERIALIZATION_TRAITS_HPP_
fixed_string(char const(&)[N]) -> fixed_string< N - 1 >
Deduce fixed_string length from a string literal.
consteval auto enum_values()
Get all enumerator values of an enumeration type at compile-time.
Definition meta.hpp:193
Definition lock.hpp:21
constexpr serialization_field< Member > field
Definition traits.hpp:107
consteval auto define_schema(Fields... fields)
Definition traits.hpp:109
static constexpr auto default_provider
Definition traits.hpp:51
static constexpr bool overlaps()
Definition traits.hpp:80
static constexpr bool is_transient
Definition traits.hpp:50
consteval auto transient() const
Definition traits.hpp:63
static consteval bool names_unique()
Definition traits.hpp:85
static constexpr bool matches(std::string_view input)
Definition traits.hpp:76
static constexpr auto wire_name
Definition traits.hpp:49
consteval auto defaulted() const
Definition traits.hpp:67
consteval auto alias() const
Definition traits.hpp:59
static constexpr bool has_default
Definition traits.hpp:52
consteval auto name() const
Definition traits.hpp:55
consteval auto default_with() const
Definition traits.hpp:71
static constexpr T make_default()
Definition traits.hpp:97
static constexpr auto member
Definition traits.hpp:48