TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_serialize.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-27 22:48:07 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include <cstdint>
29#include <string>
30#include <type_traits>
31
32#include "tec/tec_def.hpp" // IWYU pragma: keep
33
34
35namespace tec {
36
37/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38 *
39 * Binary serialization
40 *
41 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
42
50class NetData;
51
61
62 Serializable() = default;
63 virtual ~Serializable() = default;
64
75 virtual NetData& store(NetData& s) const = 0;
76
87 virtual NetData& load(NetData& s) = 0;
88};
89
103template <typename T>
104inline constexpr bool is_serializable_v = std::is_base_of<Serializable, T>::value;
105
106
108using rpcid_t = uint16_t;
109
119struct NdRoot: public Serializable {
120private:
121 rpcid_t id_;
122
123public:
131 explicit NdRoot(rpcid_t _id)
132 : id_{_id}
133 {}
134
143 constexpr rpcid_t id() const { return id_; }
144};
145
154template <typename T>
155inline constexpr bool is_root_v = std::is_base_of<NdRoot, T>::value;
156
157/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158 *
159 * JSON serialization
160 *
161 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
162
170
171 JsonSerializable() = default;
172 virtual ~JsonSerializable() = default;
173
183 virtual std::string to_json() const = 0;
184};
185
194template <typename T>
195inline constexpr bool is_json_serializable_v = std::is_base_of<JsonSerializable, T>::value;
196
197
198} // namespace tec
Lightweight binary serialization container optimized for network communication.
Definition tec_net_data.hpp:51
Base interface for objects that support JSON serialization.
Definition tec_serialize.hpp:169
virtual std::string to_json() const =0
Convert this object to a JSON string representation.
Root class for serializable objects used in RPC communications.
Definition tec_serialize.hpp:119
NdRoot(rpcid_t _id)
Constructs an NdRoot instance with the specified RPC identifier.
Definition tec_serialize.hpp:131
constexpr rpcid_t id() const
Retrieves the RPC identifier.
Definition tec_serialize.hpp:143
Base interface for objects that support binary serialization.
Definition tec_serialize.hpp:60
virtual NetData & load(NetData &s)=0
Deserialize this object from a binary stream.
virtual NetData & store(NetData &s) const =0
Serialize this object into a binary stream.
Common definitions and utilities for the tec namespace.
constexpr bool is_serializable_v
Variable template to check at compile-time whether a type is serializable.
Definition tec_serialize.hpp:104
uint16_t rpcid_t
Type alias for RPC identifiers, represented as a 16-bit unsigned integer.
Definition tec_serialize.hpp:108
constexpr bool is_json_serializable_v
Variable template to check at compile-time whether a type is JSON serializable.
Definition tec_serialize.hpp:195
constexpr bool is_root_v
Variable template to check at compile-time whether a type is a root object for binary serizalization.
Definition tec_serialize.hpp:155