parcel 0.2.2
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
defaults.h File Reference

The default_cell_for<T> trait that drives FieldsBuilder field-type inference. More...

#include <parcel/list.h>
#include <parcel/map.h>
#include <parcel/primitive.h>
#include <parcel/union.h>
#include <array>
#include <cstddef>
#include <deque>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
Include dependency graph for defaults.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  parcel::default_cell_for< T, typename >
 Maps a raw field type to its default ICell wrapper. More...
 
struct  parcel::default_cell_for< T >
 Any PrimitiveStorage type → PrimitiveCell<T>. More...
 
struct  parcel::default_cell_for< std::vector< T > >
 std::vector<T> (with a default-wrappable T) → TypedListCell<wrapper>. More...
 
struct  parcel::default_cell_for< std::map< std::string, T > >
 std::map<std::string, T> (with a default-wrappable T) → TypedMapCell<wrapper>. More...
 
struct  parcel::default_cell_for< std::unordered_map< std::string, T > >
 std::unordered_map<std::string, T>TypedMapCell<wrapper> (sorted on the wire). More...
 
struct  parcel::default_cell_for< std::array< T, N > >
 std::array<T, N>TypedListCell<wrapper> (size erased on the wire). More...
 
struct  parcel::default_cell_for< std::deque< T > >
 std::deque<T>TypedListCell<wrapper>. More...
 
struct  parcel::default_cell_for< std::list< T > >
 std::list<T>TypedListCell<wrapper>. More...
 
struct  parcel::default_cell_for< std::set< T > >
 std::set<T>TypedListCell<wrapper> (the wire form is a list — sort/unique is the user's responsibility). More...
 
struct  parcel::default_cell_for< std::variant< Ts... > >
 std::variant<Ts...> (with default-wrappable Ts) → UnionCell<wrappers...>. More...
 
struct  parcel::default_cell_for< std::optional< T > >
 std::optional<T> (with a default-wrappable T) → the inner wrapper itself. More...
 

Concepts

concept  parcel::HasDefaultCellWrapper
 Concept matching types for which a default_cell_for mapping exists.
 

Macros

#define PARCEL_DEFAULT_CELL(CellT)
 Register a cell type as the default wrapper for its payload type.
 

Typedefs

template<typename T >
using parcel::default_cell_for_t = default_cell_for< T >::type
 Convenience alias for default_cell_for<T>::type.
 

Functions

template<typename T >
requires HasDefaultCellWrapper<detail::cell_arg_t<T>>
auto parcel::cell (T &&v)
 Wrap a raw value into its default cell, returning a shared_ptr to the cell.
 
template<typename... Ts>
requires (HasDefaultCellWrapper<detail::cell_arg_t<Ts>> && ...)
std::shared_ptr< ListCellparcel::make_list (Ts &&... xs)
 Build a heterogeneous ListCell from raw values, wrapping each via parcel::cell(...).
 
std::shared_ptr< MapCellparcel::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.
 

Detailed Description

The default_cell_for<T> trait that drives FieldsBuilder field-type inference.

default_cell_for<T> maps a raw field type to the cell wrapper used for it on the wire. Out of the box every PrimitiveStorage type maps to PrimitiveCell<T>, std::vector<T> to TypedListCell, std::map<std::string, T> to TypedMapCell, std::variant<Ts...> to UnionCell, and std::optional<T> to the inner wrapper. User code can specialize the trait for custom field types so the parameter-less FieldsBuilder::field<MemberPtr>(key) works. See the README "Default cell wrappers" section.

Macro Definition Documentation

◆ PARCEL_DEFAULT_CELL

#define PARCEL_DEFAULT_CELL (   CellT)
Value:
template <> \
struct parcel::default_cell_for<typename CellT::storage_t> { \
using type = CellT; \
}
Maps a raw field type to its default ICell wrapper.
Definition defaults.h:51

Register a cell type as the default wrapper for its payload type.

Expands to a parcel::default_cell_for specialization mapping CellT::storage_t to CellT, so FieldsBuilder::field<MemberPtr>(key) can infer the wrapper from the field type alone (and the free parcel::cell(value) factory picks it up too).

Use once per user-defined cell, at namespace scope (typically just below the class definition, outside any namespace):

struct Address { ... };
class AddressCell : public parcel::StructCell<AddressCell, Address, "address"> { ... };
PARCEL_DEFAULT_CELL(AddressCell);
CRTP base for user-defined struct cells.
Definition struct.h:685
#define PARCEL_DEFAULT_CELL(CellT)
Register a cell type as the default wrapper for its payload type.
Definition defaults.h:269

Equivalent hand-rolled form:

template <>
struct parcel::default_cell_for<Address> {
using type = AddressCell;
};

The cell type must be complete and expose a storage_t typedef — every cell derived from parcel::BaseCell (including StructCell) does.

Parameters
CellTFully-qualified cell type (e.g. demo::AddressCell).

Function Documentation

◆ cell()

template<typename T >
requires HasDefaultCellWrapper<detail::cell_arg_t<T>>
auto parcel::cell ( T &&  v)

Wrap a raw value into its default cell, returning a shared_ptr to the cell.

Picks the wrapper via default_cell_for_t, with a small normalization layer so cell("hi") resolves to StringCell rather than tripping over const char*.

Template Parameters
TArgument type — its decayed form must satisfy HasDefaultCellWrapper.
Parameters
vRaw value.
Returns
std::shared_ptr<Cell> owning the new cell.

◆ make_list()

template<typename... Ts>
requires (HasDefaultCellWrapper<detail::cell_arg_t<Ts>> && ...)
std::shared_ptr< ListCell > parcel::make_list ( Ts &&...  xs)

Build a heterogeneous ListCell from raw values, wrapping each via parcel::cell(...).

auto l = parcel::make_list(1, std::string("hi"), true);
Template Parameters
TsArgument types — each must satisfy HasDefaultCellWrapper after cell_arg normalization.
Returns
std::shared_ptr<ListCell> owning the new list.

◆ make_map()

std::shared_ptr< MapCell > parcel::make_map ( const std::initializer_list< std::pair< const std::string, cell_t > >  entries)
inline

Build a heterogeneous MapCell from a brace list of {key, cell} pairs.

For homogeneous maps prefer TypedMapCell<T> directly. This helper is for the heterogeneous case where each value is already a cell_t (typically built via parcel::cell(value)).

auto m = parcel::make_map({{"x", parcel::cell(1)},
{"y", parcel::cell(std::string("hi"))}});