Ice 3.8
C++ API Reference
Loading...
Searching...
No Matches
StreamableTraits.h
1// Copyright (c) ZeroC, Inc.
2
3#ifndef ICE_STREAMABLE_TRAITS_H
4#define ICE_STREAMABLE_TRAITS_H
5
6#include "ValueF.h"
7
8#include <optional>
9#include <string_view>
10
11namespace Ice
12{
13 class ObjectPrx;
14 class UserException;
15
16 /// The stream helper category allows to select a StreamHelper specialization for a specific category of types.
17 /// The possible values are #StreamHelperCategoryUnknown, #StreamHelperCategoryBuiltinValue,
18 /// #StreamHelperCategoryBuiltin, #StreamHelperCategoryStruct, #StreamHelperCategoryEnum,
19 /// #StreamHelperCategorySequence, #StreamHelperCategoryDictionary, #StreamHelperCategoryProxy,
20 /// #StreamHelperCategoryClass, or #StreamHelperCategoryUserException. You can also define your own value if you
21 /// want to add support for a new type category.
23
24 /// Types with no StreamHelper specialization.
26
27 /// Built-in types usually passed by value.
29
30 /// Built-in types usually passed by reference.
32
33 /// Generated struct types.
35
36 /// Generated enum types.
38
39 /// Sequence types.
41
42 /// Dictionary types.
44
45 /// Proxy types.
47
48 /// Generated class types.
50
51 /// User exception types.
53
54 /// The optional format, used for marshaling optional fields and arguments.
55 /// It describes how the data is marshaled and how it can be skipped by the unmarshaling code if the optional isn't
56 /// known to the receiver.
57 enum class OptionalFormat : std::uint8_t
58 {
59 /// Fixed 1-byte encoding.
60 F1 = 0,
61
62 /// Fixed 2-byte encoding.
63 F2 = 1,
64
65 /// Fixed 4-byte encoding.
66 F4 = 2,
67
68 /// Fixed 8-byte encoding.
69 F8 = 3,
70
71 /// "Size encoding" using either 1 or 5 bytes. Used by enums, class identifiers, etc.
72 Size = 4,
73
74 /// Variable "size encoding" using either 1 or 5 bytes followed by data.
75 /// Used by strings, fixed size structs, and containers whose size can be computed prior to marshaling.
76 VSize = 5,
77
78 /// Fixed "size encoding" using 4 bytes followed by data.
79 /// Used by variable-size structs, and containers whose sizes can't be computed prior to unmarshaling.
80 FSize = 6,
81
82 /// Class instance. No longer supported.
84 };
85
86 /// @private
87 /// Determines whether the provided type is a container. The implementation checks if there is a @p T::iterator
88 /// typedef using SFINAE.
89 /// @tparam T The type to check
90 template<typename T> struct IsContainer
91 {
92 template<typename C> static char test(typename C::iterator*) noexcept;
93
94 template<typename C> static long test(...) noexcept;
95
96 static constexpr bool value = sizeof(test<T>(nullptr)) == sizeof(char);
97 };
98
99 /// @private
100 /// Determines whether the provided type is a map. The implementation checks if there is a @p T::mapped_type typedef
101 /// using SFINAE.
102 /// @tparam T The type to check
103 template<typename T> struct IsMap
104 {
105 template<typename C> static char test(typename C::mapped_type*) noexcept;
106
107 template<typename C> static long test(...) noexcept;
108
109 static constexpr bool value = IsContainer<T>::value && sizeof(test<T>(nullptr)) == sizeof(char);
110 };
111
112#ifdef ICE_DOXYGEN
113 /// Provides traits for a type that can be marshaled or unmarshaled to/from a stream of bytes using the Slice
114 /// encoding. This template is specialized for each type that can be marshaled/unmarshaled.
115 /// @tparam T The type to provide the traits for.
116 /// @tparam Enabler A type used to enable a partial specialization for several types. It should not be used in the
117 /// partial specialization itself.
118 /// @remark Streamable traits for enumeration types provide two additional traits: `minValue` and `maxValue`.
119 /// @headerfile Ice/Ice.h
120 template<typename T, typename Enabler = void> struct StreamableTraits
121 {
122 /// The category trait, used for selecting the appropriate StreamHelper.
123 static constexpr StreamHelperCategory helper;
124
125 /// The minimum number of bytes needed to marshal this type.
126 static constexpr int minWireSize;
127
128 /// Indicates if the type is always encoded on a fixed number of bytes.
129 // Only used for marshaling/unmarshaling optional data members and parameters.
130 static constexpr bool fixedLength;
131 };
132#endif
133
134 /// @cond INTERNAL
135
136 /// Base traits template. Types with no specialized trait use this trait.
137 template<typename T, typename Enabler = void> struct StreamableTraits
138 {
139 static constexpr StreamHelperCategory helper = StreamHelperCategoryUnknown;
140 };
141
142 /// Partial specialization for sequence and dictionary types.
143 template<typename T> struct StreamableTraits<T, std::enable_if_t<IsMap<T>::value || IsContainer<T>::value>>
144 {
145 static constexpr StreamHelperCategory helper =
146 IsMap<T>::value ? StreamHelperCategoryDictionary : StreamHelperCategorySequence;
147
148 static constexpr int minWireSize = 1;
149 static constexpr bool fixedLength = false;
150 };
151
152 /// Partial specialization for user exceptions.
153 template<typename T> struct StreamableTraits<T, std::enable_if_t<std::is_base_of_v<UserException, T>>>
154 {
156
157 // There is no sequence/dictionary of UserException (so no need for minWireSize) and no optional UserException
158 // (so no need for fixedLength)
159 };
160
161 /// Partial specialization for arrays (std::pair<const T*, const T*>).
162 template<typename T> struct StreamableTraits<std::pair<T*, T*>>
163 {
164 static constexpr StreamHelperCategory helper = StreamHelperCategorySequence;
165 static constexpr int minWireSize = 1;
166 static constexpr bool fixedLength = false;
167 };
168
169 //
170 // Specialization for built-in types (this is needed for sequence marshaling to figure out the minWireSize of each
171 // type).
172 //
173
174 template<> struct StreamableTraits<bool>
175 {
177 static constexpr int minWireSize = 1;
178 static constexpr bool fixedLength = true;
179 };
180
181 template<> struct StreamableTraits<std::byte>
182 {
184 static constexpr int minWireSize = 1;
185 static constexpr bool fixedLength = true;
186 };
187
188 template<> struct StreamableTraits<std::uint8_t>
189 {
191 static constexpr int minWireSize = 1;
192 static constexpr bool fixedLength = true;
193 };
194
195 template<> struct StreamableTraits<std::int16_t>
196 {
198 static constexpr int minWireSize = 2;
199 static constexpr bool fixedLength = true;
200 };
201
202 template<> struct StreamableTraits<std::int32_t>
203 {
205 static constexpr int minWireSize = 4;
206 static constexpr bool fixedLength = true;
207 };
208
209 template<> struct StreamableTraits<std::int64_t>
210 {
212 static constexpr int minWireSize = 8;
213 static constexpr bool fixedLength = true;
214 };
215 template<> struct StreamableTraits<float>
216 {
218 static constexpr int minWireSize = 4;
219 static constexpr bool fixedLength = true;
220 };
221
222 template<> struct StreamableTraits<double>
223 {
225 static constexpr int minWireSize = 8;
226 static constexpr bool fixedLength = true;
227 };
228
229 template<> struct StreamableTraits<std::string>
230 {
231 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
232 static constexpr int minWireSize = 1;
233 static constexpr bool fixedLength = false;
234 };
235
236 template<> struct StreamableTraits<std::string_view>
237 {
239 static constexpr int minWireSize = 1;
240 static constexpr bool fixedLength = false;
241 };
242
243 template<> struct StreamableTraits<std::wstring>
244 {
245 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
246 static constexpr int minWireSize = 1;
247 static constexpr bool fixedLength = false;
248 };
249
250 template<> struct StreamableTraits<std::wstring_view>
251 {
253 static constexpr int minWireSize = 1;
254 static constexpr bool fixedLength = false;
255 };
256
257 /// vector<bool> is a special type in C++: the streams handle it like a built-in type.
258 template<> struct StreamableTraits<std::vector<bool>>
259 {
260 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
261 static constexpr int minWireSize = 1;
262 static constexpr bool fixedLength = false;
263 };
264
265 //
266 // Partial specialization for proxies and classes.
267 //
268
269 template<typename T> struct StreamableTraits<std::optional<T>, std::enable_if_t<std::is_base_of_v<ObjectPrx, T>>>
270 {
271 static constexpr StreamHelperCategory helper = StreamHelperCategoryProxy;
272 static constexpr int minWireSize = 2;
273 static constexpr bool fixedLength = false;
274 };
275
276 template<typename T> struct StreamableTraits<std::shared_ptr<T>, std::enable_if_t<std::is_base_of_v<Value, T>>>
277 {
278 static constexpr StreamHelperCategory helper = StreamHelperCategoryClass;
279 static constexpr int minWireSize = 1;
280 static constexpr bool fixedLength = false;
281 };
282
283 template<typename T, StreamHelperCategory st> struct StreamHelper;
284
285 /// @endcond
286
287 /// @private
288 template<typename T, StreamHelperCategory st, bool fixedLength> struct StreamOptionalHelper;
289}
290
291#endif
The base class for all Ice proxies.
Definition Proxy.h:232
Abstract base class for all exceptions defined in Slice.
constexpr StreamHelperCategory StreamHelperCategoryBuiltin
Built-in types usually passed by reference.
OptionalFormat
The optional format, used for marshaling optional fields and arguments.
@ VSize
Variable "size encoding" using either 1 or 5 bytes followed by data.
@ F8
Fixed 8-byte encoding.
@ Size
"Size encoding" using either 1 or 5 bytes. Used by enums, class identifiers, etc.
@ Class
Class instance. No longer supported.
@ F1
Fixed 1-byte encoding.
@ FSize
Fixed "size encoding" using 4 bytes followed by data.
@ F4
Fixed 4-byte encoding.
@ F2
Fixed 2-byte encoding.
constexpr StreamHelperCategory StreamHelperCategoryDictionary
Dictionary types.
constexpr StreamHelperCategory StreamHelperCategoryEnum
Generated enum types.
constexpr StreamHelperCategory StreamHelperCategoryProxy
Proxy types.
int StreamHelperCategory
The stream helper category allows to select a StreamHelper specialization for a specific category of ...
constexpr StreamHelperCategory StreamHelperCategoryClass
Generated class types.
constexpr StreamHelperCategory StreamHelperCategoryStruct
Generated struct types.
constexpr StreamHelperCategory StreamHelperCategorySequence
Sequence types.
constexpr StreamHelperCategory StreamHelperCategoryBuiltinValue
Built-in types usually passed by value.
constexpr StreamHelperCategory StreamHelperCategoryUnknown
Types with no StreamHelper specialization.
constexpr StreamHelperCategory StreamHelperCategoryUserException
User exception types.
The Ice RPC framework.
Definition SampleEvent.h:60
static constexpr bool fixedLength
Indicates if the type is always encoded on a fixed number of bytes.
static constexpr int minWireSize
The minimum number of bytes needed to marshal this type.
static constexpr StreamHelperCategory helper
The category trait, used for selecting the appropriate StreamHelper.
Provides traits for a type that can be marshaled or unmarshaled to/from a stream of bytes using the S...