parcel 0.2.2
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
defaults.h
Go to the documentation of this file.
1#pragma once
2
16#include <parcel/list.h>
17#include <parcel/map.h>
18#include <parcel/primitive.h>
19#include <parcel/union.h>
20
21#include <array>
22#include <cstddef>
23#include <deque>
24#include <list>
25#include <map>
26#include <memory>
27#include <optional>
28#include <set>
29#include <string>
30#include <type_traits>
31#include <unordered_map>
32#include <utility>
33#include <variant>
34#include <vector>
35
36namespace parcel {
37
50template <typename T, typename = void>
52
54template <PrimitiveStorage T>
56 using type = PrimitiveCell<T>;
57};
58
60template <typename T>
62
68template <typename T>
69concept HasDefaultCellWrapper = requires { typename default_cell_for<T>::type; };
70
79template <typename T>
81struct default_cell_for<std::vector<T>> {
83};
84
86template <typename T>
88struct default_cell_for<std::map<std::string, T>> {
90};
91
93template <typename T>
95struct default_cell_for<std::unordered_map<std::string, T>> {
97};
98
100template <typename T, std::size_t N>
102struct default_cell_for<std::array<T, N>> {
104};
105
107template <typename T>
109struct default_cell_for<std::deque<T>> {
111};
112
114template <typename T>
116struct default_cell_for<std::list<T>> {
118};
119
122template <typename T>
124struct default_cell_for<std::set<T>> {
126};
127
129template <typename... Ts>
130 requires(HasDefaultCellWrapper<Ts> && ...)
131struct default_cell_for<std::variant<Ts...>> {
133};
134
142template <typename T>
144struct default_cell_for<std::optional<T>> {
145 using type = default_cell_for_t<T>;
146};
147
148namespace detail {
149// Normalize raw call-site types into the canonical storage type that
150// default_cell_for is keyed on. Without this, string literals (`const char[N]`)
151// and `const char*` would not resolve to StringCell.
152template <typename T>
153struct cell_arg {
154 using type = T;
155};
156template <std::size_t N>
157struct cell_arg<char[N]> { // NOLINT(modernize-avoid-c-arrays) — must match decay-from string
158 // literal
159 using type = std::string;
160};
161template <std::size_t N>
162struct cell_arg<const char[N]> { // NOLINT(modernize-avoid-c-arrays) — must match decay-from string
163 // literal
164 using type = std::string;
165};
166template <>
167struct cell_arg<char*> {
168 using type = std::string;
169};
170template <>
171struct cell_arg<const char*> {
172 using type = std::string;
173};
174
175template <typename T>
176using cell_arg_t = typename cell_arg<std::remove_cvref_t<T>>::type;
177} // namespace detail
178
190template <typename T>
191 requires HasDefaultCellWrapper<detail::cell_arg_t<T>>
192auto cell(T&& v) {
194 return Cell::of(std::forward<T>(v));
195}
196
209template <typename... Ts>
210 requires(HasDefaultCellWrapper<detail::cell_arg_t<Ts>> && ...)
211[[nodiscard]] std::shared_ptr<ListCell> make_list(Ts&&... xs) {
212 std::vector<cell_t> elems;
213 elems.reserve(sizeof...(Ts));
214 (elems.push_back(cell(std::forward<Ts>(xs))), ...);
215 return ListCell::of(std::move(elems));
216}
217
230[[nodiscard]] inline std::shared_ptr<MapCell>
231make_map(const std::initializer_list<std::pair<const std::string, cell_t>> entries) {
232 std::map<std::string, cell_t> data(entries.begin(), entries.end());
233 return MapCell::of(std::move(data));
234}
235
236} // namespace parcel
237
269#define PARCEL_DEFAULT_CELL(CellT) \
270 template <> \
271 struct parcel::default_cell_for<typename CellT::storage_t> { \
272 using type = CellT; \
273 }
Leaf cell wrapping a single scalar (or complex) value of type T.
Definition primitive.h:316
Homogeneous list of element cells of type T.
Definition list.h:106
Homogeneous string-keyed map of values of type T.
Definition map.h:106
Closed-set polymorphic cell — exactly one of Ts at runtime.
Definition union.h:100
Concept matching types for which a default_cell_for mapping exists.
Definition defaults.h:69
std::shared_ptr< MapCell > make_map(const std::initializer_list< std::pair< const std::string, cell_t > > entries)
Build a heterogeneous MapCell from a brace list of {key, cell} pairs.
Definition defaults.h:231
default_cell_for< T >::type default_cell_for_t
Convenience alias for default_cell_for<T>::type.
Definition defaults.h:61
std::shared_ptr< ListCell > make_list(Ts &&... xs)
Build a heterogeneous ListCell from raw values, wrapping each via parcel::cell(......
Definition defaults.h:211
auto cell(T &&v)
Wrap a raw value into its default cell, returning a shared_ptr to the cell.
Definition defaults.h:192
TypedListCell<T> and heterogeneous ListCell with their descriptors.
TypedMapCell<T> and heterogeneous MapCell with their descriptors.
PrimitiveCell<T> plus the per-storage PrimitiveTraits<T> specializations.
static std::shared_ptr< ListCell > of(Args &&... args)
Construct a shared_ptr<Derived> forwarding the arguments.
Definition cell.h:385
Maps a raw field type to its default ICell wrapper.
Definition defaults.h:51
UnionCell<Ts...> closed-set polymorphic cell and its descriptor.