include/depthai-shared/common/optional.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include "tl/optional.hpp"
5 
6 // tl::optional serialization for nlohmann json
7 // partial specialization (full specialization works too)
8 namespace nlohmann {
9 template <typename T>
10 struct adl_serializer<tl::optional<T>> {
11  static void to_json(json& j, const tl::optional<T>& opt) { // NOLINT this is a specialization, naming conventions don't apply
12  if(opt == tl::nullopt) {
13  j = nullptr;
14  } else {
15  j = *opt; // this will call adl_serializer<T>::to_json which will
16  // find the free function to_json in T's namespace!
17  }
18  }
19 
20  static void from_json(const json& j, tl::optional<T>& opt) { // NOLINT this is a specialization, naming conventions don't apply
21  if(j.is_null()) {
22  opt = tl::nullopt;
23  } else {
24  opt = j.get<T>(); // same as above, but with
25  // adl_serializer<T>::from_json
26  }
27  }
28 };
29 } // namespace nlohmann
30 
31 // tl::optional serialization for libnop
32 namespace nop {
33 
34 //
35 // Optional<T> encoding formats:
36 //
37 // Empty Optional<T>:
38 //
39 // +-----+
40 // | NIL |
41 // +-----+
42 //
43 // Non-empty Optional<T>
44 //
45 // +---//----+
46 // | ELEMENT |
47 // +---//----+
48 //
49 // Element must be a valid encoding of type T.
50 //
51 
52 template <typename T>
53 struct Encoding<tl::optional<T>> : EncodingIO<tl::optional<T>> {
55 
56  static constexpr EncodingByte Prefix(const Type& value) {
57  return value ? Encoding<T>::Prefix(*value) : EncodingByte::Empty;
58  }
59 
60  static constexpr std::size_t Size(const Type& value) {
61  return value ? Encoding<T>::Size(*value) : BaseEncodingSize(EncodingByte::Empty);
62  }
63 
64  static constexpr bool Match(EncodingByte prefix) {
65  return prefix == EncodingByte::Empty || Encoding<T>::Match(prefix);
66  }
67 
68  template <typename Writer>
69  static constexpr Status<void> WritePayload(EncodingByte prefix, const Type& value, Writer* writer) {
70  if(value) {
71  return Encoding<T>::WritePayload(prefix, *value, writer);
72  } else {
73  return {};
74  }
75  }
76 
77  template <typename Reader>
78  static constexpr Status<void> ReadPayload(EncodingByte prefix, Type* value, Reader* reader) {
79  if(prefix == EncodingByte::Empty) {
80  value->reset();
81  } else {
82  T temp;
83  auto status = Encoding<T>::ReadPayload(prefix, &temp, reader);
84  if(!status) return status;
85 
86  *value = std::move(temp);
87  }
88 
89  return {};
90  }
91 };
92 
93 } // namespace nop
nop
Definition: include/depthai-shared/common/optional.hpp:32
tl
Definition: 3rdparty/tl/optional.hpp:106
optional.hpp
nlohmann
Definition: include/depthai-shared/common/optional.hpp:8
nop::Encoding< tl::optional< T > >::WritePayload
static constexpr Status< void > WritePayload(EncodingByte prefix, const Type &value, Writer *writer)
Definition: include/depthai-shared/common/optional.hpp:69
tl::nullopt
static constexpr nullopt_t nullopt
Represents an empty optional.
Definition: 3rdparty/tl/optional.hpp:663
nop::Encoding< tl::optional< T > >::Prefix
static constexpr EncodingByte Prefix(const Type &value)
Definition: include/depthai-shared/common/optional.hpp:56
nlohmann::adl_serializer< tl::optional< T > >::from_json
static void from_json(const json &j, tl::optional< T > &opt)
Definition: include/depthai-shared/common/optional.hpp:20
nlohmann::adl_serializer< tl::optional< T > >::to_json
static void to_json(json &j, const tl::optional< T > &opt)
Definition: include/depthai-shared/common/optional.hpp:11
Serialization.hpp
nop::Encoding< tl::optional< T > >::ReadPayload
static constexpr Status< void > ReadPayload(EncodingByte prefix, Type *value, Reader *reader)
Definition: include/depthai-shared/common/optional.hpp:78
nanorpc::core::detail::pack::meta::status
status
Definition: pack_meta.h:33
tl::optional
Definition: 3rdparty/tl/optional.hpp:120
tl::optional::reset
void reset() noexcept
Destroys the stored value if one exists, making the optional empty.
Definition: 3rdparty/tl/optional.hpp:1329
nop::Encoding< tl::optional< T > >::Match
static constexpr bool Match(EncodingByte prefix)
Definition: include/depthai-shared/common/optional.hpp:64
nop::Encoding< tl::optional< T > >::Size
static constexpr std::size_t Size(const Type &value)
Definition: include/depthai-shared/common/optional.hpp:60


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19