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 when the tag 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 1 to 5 bytes, e.g., enum, class identifier.
72 Size = 4,
73
74 /// "Size encoding" using 1 to 5 bytes followed by data, e.g., string, fixed size struct, or containers whose
75 /// size can be computed prior to marshaling.
76 VSize = 5,
77
78 /// Fixed size using 4 bytes followed by data, e.g., variable-size struct, container.
79 FSize = 6,
80
81 /// Class instance. Not longer supported.
83 };
84
85 /// @private
86 /// Determines whether the provided type is a container. The implementation checks if there is a @p T::iterator
87 /// typedef using SFINAE.
88 /// @tparam T The type to check
89 template<typename T> struct IsContainer
90 {
91 template<typename C> static char test(typename C::iterator*) noexcept;
92
93 template<typename C> static long test(...) noexcept;
94
95 static constexpr bool value = sizeof(test<T>(nullptr)) == sizeof(char);
96 };
97
98 /// @private
99 /// Determines whether the provided type is a map. The implementation checks if there is a @p T::mapped_type typedef
100 /// using SFINAE.
101 /// @tparam T The type to check
102 template<typename T> struct IsMap
103 {
104 template<typename C> static char test(typename C::mapped_type*) noexcept;
105
106 template<typename C> static long test(...) noexcept;
107
108 static constexpr bool value = IsContainer<T>::value && sizeof(test<T>(nullptr)) == sizeof(char);
109 };
110
111#ifdef ICE_DOXYGEN
112 /// Provides traits for a type that can be marshaled or unmarshaled to/from a stream of bytes using the Slice
113 /// encoding. This template is specialized for each type that can be marshaled/unmarshaled.
114 /// @tparam T The type to provide the traits for.
115 /// @tparam Enabler A type used to enable a partial specialization for several types. It should not be used in the
116 /// partial specialization itself.
117 /// @remark Streamable traits for enumeration types provide two additional traits: `minValue` and `maxValue`.
118 /// @headerfile Ice/Ice.h
119 template<typename T, typename Enabler = void> struct StreamableTraits
120 {
121 /// The category trait, used for selecting the appropriate StreamHelper.
122 static constexpr StreamHelperCategory helper;
123
124 /// The minimum number of bytes needed to marshal this type.
125 static constexpr int minWireSize;
126
127 /// Indicates if the type is always encoded on a fixed number of bytes.
128 // Only used for marshaling/unmarshaling optional data members and parameters.
129 static constexpr bool fixedLength;
130 };
131#endif
132
133 /// @cond INTERNAL
134
135 /// Base traits template. Types with no specialized trait use this trait.
136 template<typename T, typename Enabler = void> struct StreamableTraits
137 {
138 static constexpr StreamHelperCategory helper = StreamHelperCategoryUnknown;
139 };
140
141 /// Partial specialization for sequence and dictionary types.
142 template<typename T> struct StreamableTraits<T, std::enable_if_t<IsMap<T>::value || IsContainer<T>::value>>
143 {
144 static constexpr StreamHelperCategory helper =
145 IsMap<T>::value ? StreamHelperCategoryDictionary : StreamHelperCategorySequence;
146
147 static constexpr int minWireSize = 1;
148 static constexpr bool fixedLength = false;
149 };
150
151 /// Partial specialization for user exceptions.
152 template<typename T> struct StreamableTraits<T, std::enable_if_t<std::is_base_of_v<UserException, T>>>
153 {
155
156 // There is no sequence/dictionary of UserException (so no need for minWireSize) and no optional UserException
157 // (so no need for fixedLength)
158 };
159
160 /// Partial specialization for arrays (std::pair<const T*, const T*>).
161 template<typename T> struct StreamableTraits<std::pair<T*, T*>>
162 {
163 static constexpr StreamHelperCategory helper = StreamHelperCategorySequence;
164 static constexpr int minWireSize = 1;
165 static constexpr bool fixedLength = false;
166 };
167
168 //
169 // Specialization for built-in types (this is needed for sequence marshaling to figure out the minWireSize of each
170 // type).
171 //
172
173 template<> struct StreamableTraits<bool>
174 {
176 static constexpr int minWireSize = 1;
177 static constexpr bool fixedLength = true;
178 };
179
180 template<> struct StreamableTraits<std::byte>
181 {
183 static constexpr int minWireSize = 1;
184 static constexpr bool fixedLength = true;
185 };
186
187 template<> struct StreamableTraits<std::uint8_t>
188 {
190 static constexpr int minWireSize = 1;
191 static constexpr bool fixedLength = true;
192 };
193
194 template<> struct StreamableTraits<std::int16_t>
195 {
197 static constexpr int minWireSize = 2;
198 static constexpr bool fixedLength = true;
199 };
200
201 template<> struct StreamableTraits<std::int32_t>
202 {
204 static constexpr int minWireSize = 4;
205 static constexpr bool fixedLength = true;
206 };
207
208 template<> struct StreamableTraits<std::int64_t>
209 {
211 static constexpr int minWireSize = 8;
212 static constexpr bool fixedLength = true;
213 };
214 template<> struct StreamableTraits<float>
215 {
217 static constexpr int minWireSize = 4;
218 static constexpr bool fixedLength = true;
219 };
220
221 template<> struct StreamableTraits<double>
222 {
224 static constexpr int minWireSize = 8;
225 static constexpr bool fixedLength = true;
226 };
227
228 template<> struct StreamableTraits<std::string>
229 {
230 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
231 static constexpr int minWireSize = 1;
232 static constexpr bool fixedLength = false;
233 };
234
235 template<> struct StreamableTraits<std::string_view>
236 {
238 static constexpr int minWireSize = 1;
239 static constexpr bool fixedLength = false;
240 };
241
242 template<> struct StreamableTraits<std::wstring>
243 {
244 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
245 static constexpr int minWireSize = 1;
246 static constexpr bool fixedLength = false;
247 };
248
249 template<> struct StreamableTraits<std::wstring_view>
250 {
252 static constexpr int minWireSize = 1;
253 static constexpr bool fixedLength = false;
254 };
255
256 /// vector<bool> is a special type in C++: the streams handle it like a built-in type.
257 template<> struct StreamableTraits<std::vector<bool>>
258 {
259 static constexpr StreamHelperCategory helper = StreamHelperCategoryBuiltin;
260 static constexpr int minWireSize = 1;
261 static constexpr bool fixedLength = false;
262 };
263
264 //
265 // Partial specialization for proxies and classes.
266 //
267
268 template<typename T> struct StreamableTraits<std::optional<T>, std::enable_if_t<std::is_base_of_v<ObjectPrx, T>>>
269 {
270 static constexpr StreamHelperCategory helper = StreamHelperCategoryProxy;
271 static constexpr int minWireSize = 2;
272 static constexpr bool fixedLength = false;
273 };
274
275 template<typename T> struct StreamableTraits<std::shared_ptr<T>, std::enable_if_t<std::is_base_of_v<Value, T>>>
276 {
277 static constexpr StreamHelperCategory helper = StreamHelperCategoryClass;
278 static constexpr int minWireSize = 1;
279 static constexpr bool fixedLength = false;
280 };
281
282 template<typename T, StreamHelperCategory st> struct StreamHelper;
283
284 /// @endcond
285
286 /// @private
287 template<typename T, StreamHelperCategory st, bool fixedLength> struct StreamOptionalHelper;
288}
289
290#endif
The base class for all Ice proxies.
Definition Proxy.h:232
Abstract base class for all Ice 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
"Size encoding" using 1 to 5 bytes followed by data, e.g., string, fixed size struct,...
@ F8
Fixed 8-byte encoding.
@ Size
"Size encoding" using 1 to 5 bytes, e.g., enum, class identifier.
@ Class
Class instance. Not longer supported.
@ F1
Fixed 1-byte encoding.
@ FSize
Fixed size using 4 bytes followed by data, e.g., variable-size struct, container.
@ 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:59
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...