Sen API
Sen Libraries
Loading...
Searching...
No Matches
basic_traits.h
Go to the documentation of this file.
1// === basic_traits.h ==================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_META_BASIC_TRAITS_H
9#define SEN_CORE_META_BASIC_TRAITS_H
10
11// sen
13#include "sen/core/io/util.h"
14#include "sen/core/meta/type.h"
15#include "sen/core/meta/var.h"
16
17// std
18#include <cstdint>
19#include <string>
20
21namespace sen
22{
23
24struct Var;
25
28
37template <typename T>
39{
40 static constexpr bool available = true;
41
42 static void variantToValue(const Var& var, T& val) { val = getCopyAs<T>(var); }
43 static void valueToVariant(T val, Var& var) { var = val; }
44 static constexpr uint32_t serializedSize(T val) noexcept { return impl::getSerializedSize(val); }
45
46 static std::string toJsonString(const T& val)
47 {
48 Var var;
49 valueToVariant(val, var);
50 return toJson(var);
51 }
52
53 static void fromJsonString(const std::string& str, T& val)
54 {
55 const Var var = fromJson(str);
56 variantToValue(var, val);
57 }
58};
59
61
62} // namespace sen
63
64#endif // SEN_CORE_META_BASIC_TRAITS_H
To getCopyAs(const Var &var)
Tries to transform the stored value to T. For expensive types, like strings, maps or lists is better ...
Definition var.h:325
std::string toJson(const Var &var, int indent=2)
Converts a variant into its Json representation. See https://www.Json.org/Json-en....
Var fromJson(const std::string &str)
Inverse as toJson.
Definition assert.h:17
Helper class that traits classes can inherit from when the type at hand is basic (native or trivial).
Definition basic_traits.h:39
static constexpr uint32_t serializedSize(T val) noexcept
Definition basic_traits.h:44
static constexpr bool available
Definition basic_traits.h:40
static std::string toJsonString(const T &val)
Definition basic_traits.h:46
static void variantToValue(const Var &var, T &val)
Definition basic_traits.h:42
static void fromJsonString(const std::string &str, T &val)
Definition basic_traits.h:53
static void valueToVariant(T val, Var &var)
Definition basic_traits.h:43
Can hold any supported value type. Wraps std::variant to allow recursion and implements some helpers.
Definition var.h:119