5 #ifndef ICE_STREAM_HELPERS_H
6 #define ICE_STREAM_HELPERS_H
10 #ifndef ICE_CPP11_MAPPING
23 typedef int StreamHelperCategory;
26 const StreamHelperCategory StreamHelperCategoryUnknown = 0;
28 const StreamHelperCategory StreamHelperCategoryBuiltin = 1;
30 const StreamHelperCategory StreamHelperCategoryStruct = 2;
32 const StreamHelperCategory StreamHelperCategoryStructClass = 3;
34 const StreamHelperCategory StreamHelperCategoryEnum = 4;
36 const StreamHelperCategory StreamHelperCategorySequence = 5;
38 const StreamHelperCategory StreamHelperCategoryDictionary = 6;
40 const StreamHelperCategory StreamHelperCategoryProxy = 7;
42 const StreamHelperCategory StreamHelperCategoryClass = 8;
44 const StreamHelperCategory StreamHelperCategoryUserException = 9;
54 #ifdef ICE_CPP11_MAPPING
55 enum class OptionalFormat : unsigned char
89 OptionalFormatSize = 4,
94 OptionalFormatVSize = 5,
96 OptionalFormatFSize = 6,
98 OptionalFormatClass = 7
111 static char test(
typename C::iterator*);
114 static long test(...);
116 static const bool value =
sizeof(test<T>(0)) ==
sizeof(
char);
128 static char test(
typename C::mapped_type*);
131 static long test(...);
133 static const bool value = IsContainer<T>::value &&
sizeof(test<T>(0)) ==
sizeof(
char);
136 #ifdef ICE_CPP11_MAPPING
142 template<
typename T,
typename Enabler =
void>
143 struct StreamableTraits
145 static const StreamHelperCategory helper = StreamHelperCategoryUnknown;
166 struct StreamableTraits<T, typename ::std::enable_if<IsMap<T>::value || IsContainer<T>::value>::type>
168 static const StreamHelperCategory helper = IsMap<T>::value ? StreamHelperCategoryDictionary : StreamHelperCategorySequence;
169 static const int minWireSize = 1;
170 static const bool fixedLength =
false;
178 struct StreamableTraits<T, typename ::std::enable_if<::std::is_base_of<::Ice::UserException, T>::value>::type>
180 static const StreamHelperCategory helper = StreamHelperCategoryUserException;
193 struct StreamableTraits<std::pair<T*, T*>>
195 static const StreamHelperCategory helper = StreamHelperCategorySequence;
196 static const int minWireSize = 1;
197 static const bool fixedLength =
false;
207 template<
typename T,
typename Enabler =
void>
208 struct StreamableTraits
210 static const StreamHelperCategory helper = IsMap<T>::value ? StreamHelperCategoryDictionary :
211 (IsContainer<T>::value ? StreamHelperCategorySequence : StreamHelperCategoryUnknown);
218 static const int minWireSize = 1;
224 static const bool fixedLength =
false;
232 struct StreamableTraits<UserException>
234 static const StreamHelperCategory helper = StreamHelperCategoryUserException;
247 template<
typename T,
typename U>
248 struct StreamableTraits< ::std::pair<T, U> >
250 static const StreamHelperCategory helper = StreamHelperCategorySequence;
251 static const int minWireSize = 1;
252 static const bool fixedLength =
false;
262 struct StreamableTraits<bool>
264 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
265 static const int minWireSize = 1;
266 static const bool fixedLength =
true;
275 struct StreamableTraits<
Byte>
277 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
278 static const int minWireSize = 1;
279 static const bool fixedLength =
true;
288 struct StreamableTraits<
Short>
290 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
291 static const int minWireSize = 2;
292 static const bool fixedLength =
true;
301 struct StreamableTraits<
Int>
303 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
304 static const int minWireSize = 4;
305 static const bool fixedLength =
true;
314 struct StreamableTraits<
Long>
316 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
317 static const int minWireSize = 8;
318 static const bool fixedLength =
true;
327 struct StreamableTraits<
Float>
329 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
330 static const int minWireSize = 4;
331 static const bool fixedLength =
true;
340 struct StreamableTraits<
Double>
342 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
343 static const int minWireSize = 8;
344 static const bool fixedLength =
true;
353 struct StreamableTraits< ::std::string>
355 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
356 static const int minWireSize = 1;
357 static const bool fixedLength =
false;
366 struct StreamableTraits< ::std::wstring>
368 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
369 static const int minWireSize = 1;
370 static const bool fixedLength =
false;
378 struct StreamableTraits< ::std::vector<bool> >
380 static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
381 static const int minWireSize = 1;
382 static const bool fixedLength =
false;
389 #ifdef ICE_CPP11_MAPPING
391 struct StreamableTraits<::std::shared_ptr<T>, typename ::std::enable_if<::std::is_base_of<::Ice::ObjectPrx, T>::value>::type>
393 static const StreamHelperCategory helper = StreamHelperCategoryProxy;
394 static const int minWireSize = 2;
395 static const bool fixedLength =
false;
399 struct StreamableTraits< ::IceInternal::ProxyHandle<T> >
401 static const StreamHelperCategory helper = StreamHelperCategoryProxy;
402 static const int minWireSize = 2;
403 static const bool fixedLength =
false;
411 #ifdef ICE_CPP11_MAPPING
413 struct StreamableTraits<::std::shared_ptr<T>, typename ::std::enable_if<::std::is_base_of<::Ice::Value, T>::value>::type>
415 static const StreamHelperCategory helper = StreamHelperCategoryClass;
416 static const int minWireSize = 1;
417 static const bool fixedLength =
false;
421 struct StreamableTraits< ::IceInternal::Handle<T> >
423 static const StreamHelperCategory helper = StreamHelperCategoryClass;
424 static const int minWireSize = 1;
425 static const bool fixedLength =
false;
434 template<
typename T, StreamHelperCategory st>
442 struct StreamHelper<T, StreamHelperCategoryBuiltin>
444 template<
class S>
static inline void
445 write(S* stream,
const T& v)
450 template<
class S>
static inline void
451 read(S* stream, T& v)
466 template<
typename T,
typename S>
469 #ifdef ICE_CPP11_MAPPING
470 static inline void write(S* stream,
const T& v)
472 stream->writeAll(v.ice_tuple());
475 static inline void write(S*,
const T&)
486 template<
typename T,
typename S>
489 static inline void read(S*, T&)
500 struct StreamHelper<T, StreamHelperCategoryStruct>
502 template<
class S>
static inline void
503 write(S* stream,
const T& v)
505 StreamWriter<T, S>::write(stream, v);
508 template<
class S>
static inline void
509 read(S* stream, T& v)
511 StreamReader<T, S>::read(stream, v);
520 struct StreamHelper<T, StreamHelperCategoryStructClass>
522 template<
class S>
static inline void
523 write(S* stream,
const T& v)
525 StreamWriter<T, S>::write(stream, v);
528 template<
class S>
static inline void
529 read(S* stream, T& v)
531 v =
new typename T::element_type;
532 StreamReader<T, S>::read(stream, v);
541 struct StreamHelper<T, StreamHelperCategoryEnum>
543 template<
class S>
static inline void
544 write(S* stream,
const T& v)
546 if(
static_cast<Int>(v) < StreamableTraits<T>::minValue ||
static_cast<Int>(v) > StreamableTraits<T>::maxValue)
548 IceInternal::Ex::throwMarshalException(__FILE__, __LINE__,
"enumerator out of range");
550 stream->writeEnum(
static_cast<Int>(v), StreamableTraits<T>::maxValue);
553 template<
class S>
static inline void
554 read(S* stream, T& v)
556 Int value = stream->readEnum(StreamableTraits<T>::maxValue);
557 if(value < StreamableTraits<T>::minValue || value > StreamableTraits<T>::maxValue)
559 IceInternal::Ex::throwMarshalException(__FILE__, __LINE__,
"enumerator out of range");
561 v =
static_cast<T
>(value);
570 struct StreamHelper<T, StreamHelperCategorySequence>
572 template<
class S>
static inline void
573 write(S* stream,
const T& v)
575 stream->writeSize(
static_cast<Int>(v.size()));
576 for(
typename T::const_iterator p = v.begin(); p != v.end(); ++p)
582 template<
class S>
static inline void
583 read(S* stream, T& v)
585 Int sz = stream->readAndCheckSeqSize(StreamableTraits<typename T::value_type>::minWireSize);
586 T(
static_cast<size_t>(sz)).swap(v);
587 for(
typename T::iterator p = v.begin(); p != v.end(); ++p)
599 struct StreamHelper<std::pair<const T*, const T*>, StreamHelperCategorySequence>
601 template<
class S>
static inline void
602 write(S* stream,
const std::pair<const T*, const T*>& v)
604 stream->write(v.first, v.second);
607 template<
class S>
static inline void
608 read(S* stream, std::pair<const T*, const T*>& v)
614 #ifndef ICE_CPP11_MAPPING
621 struct StreamHelper<std::pair<T, T>, StreamHelperCategorySequence>
623 template<
class S>
static inline void
624 write(S* stream,
const std::pair<T, T>& v)
626 stream->writeSize(
static_cast<Int>(IceUtilInternal::distance(v.first, v.second)));
627 for(T p = v.first; p != v.second; ++p)
633 template<
class S>
static inline void
634 read(S* stream, std::pair<T, T>& v)
645 struct StreamHelper<std::pair< ::std::vector<bool>::const_iterator,
646 ::std::vector<bool>::const_iterator>, StreamHelperCategorySequence>
648 template<
class S>
static inline void
649 write(S* stream,
const std::pair< ::std::vector<bool>::const_iterator,
650 ::std::vector<bool>::const_iterator>& v)
652 stream->writeSize(
static_cast<Int>(IceUtilInternal::distance(v.first, v.second)));
653 for(::std::vector<bool>::const_iterator p = v.first; p != v.second; ++p)
655 stream->write(
static_cast<bool>(*p));
665 struct StreamHelper<std::pair<IceUtil::ScopedArray<T>, std::pair<const T*, const T*> >, StreamHelperCategorySequence>
667 template<
class S>
static inline void
668 read(S* stream, std::pair<IceUtil::ScopedArray<T>, std::pair<const T*, const T*> >& v)
670 stream->read(v.second, v.first);
682 struct StreamHelper<T, StreamHelperCategoryDictionary>
684 template<
class S>
static inline void
685 write(S* stream,
const T& v)
687 stream->writeSize(
static_cast<Int>(v.size()));
688 for(
typename T::const_iterator p = v.begin(); p != v.end(); ++p)
690 stream->write(p->first);
691 stream->write(p->second);
695 template<
class S>
static inline void
696 read(S* stream, T& v)
698 Int sz = stream->readSize();
702 typename T::value_type p;
703 stream->read(
const_cast<typename T::key_type&
>(p.first));
704 typename T::iterator i = v.insert(v.end(), p);
705 stream->read(i->second);
715 struct StreamHelper<T, StreamHelperCategoryUserException>
717 template<
class S>
static inline void
718 write(S* stream,
const T& v)
720 stream->writeException(v);
731 struct StreamHelper<T, StreamHelperCategoryProxy>
733 template<
class S>
static inline void
734 write(S* stream,
const T& v)
739 template<
class S>
static inline void
740 read(S* stream, T& v)
751 struct StreamHelper<T, StreamHelperCategoryClass>
753 template<
class S>
static inline void
754 write(S* stream,
const T& v)
759 template<
class S>
static inline void
760 read(S* stream, T& v)
780 template<StreamHelperCategory st,
int minWireSize,
bool fixedLength>
781 struct GetOptionalFormat;
788 struct GetOptionalFormat<StreamHelperCategoryBuiltin, 1, true>
790 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, F1);
798 struct GetOptionalFormat<StreamHelperCategoryBuiltin, 2, true>
800 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, F2);
808 struct GetOptionalFormat<StreamHelperCategoryBuiltin, 4, true>
810 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, F4);
818 struct GetOptionalFormat<StreamHelperCategoryBuiltin, 8, true>
820 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, F8);
828 struct GetOptionalFormat<StreamHelperCategoryBuiltin, 1, false>
830 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, VSize);
838 struct GetOptionalFormat<StreamHelperCategoryClass, 1, false>
840 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, Class);
847 template<
int minWireSize>
848 struct GetOptionalFormat<StreamHelperCategoryEnum, minWireSize, false>
850 static const OptionalFormat value =
ICE_SCOPED_ENUM(OptionalFormat, Size);
857 template<
typename T, StreamHelperCategory st,
bool fixedLength>
858 struct StreamOptionalHelper
860 typedef StreamableTraits<T> Traits;
867 static const OptionalFormat optionalFormat = GetOptionalFormat<st, Traits::minWireSize, fixedLength>::value;
869 template<
class S>
static inline void
870 write(S* stream,
const T& v)
875 template<
class S>
static inline void
876 read(S* stream, T& v)
887 struct StreamOptionalHelper<T, StreamHelperCategoryStruct, true>
889 static const OptionalFormat optionalFormat =
ICE_SCOPED_ENUM(OptionalFormat, VSize);
891 template<
class S>
static inline void
892 write(S* stream,
const T& v)
894 stream->writeSize(StreamableTraits<T>::minWireSize);
898 template<
class S>
static inline void
899 read(S* stream, T& v)
911 struct StreamOptionalHelper<T, StreamHelperCategoryStruct, false>
913 static const OptionalFormat optionalFormat =
ICE_SCOPED_ENUM(OptionalFormat, FSize);
915 template<
class S>
static inline void
916 write(S* stream,
const T& v)
918 typename S::size_type pos = stream->startSize();
920 stream->endSize(pos);
923 template<
class S>
static inline void
924 read(S* stream, T& v)
935 template<
typename T,
bool fixedLength>
936 struct StreamOptionalHelper<T, StreamHelperCategoryStructClass, fixedLength> : StreamOptionalHelper<T, StreamHelperCategoryStruct, fixedLength>
945 struct StreamOptionalHelper<T, StreamHelperCategoryProxy, false> : StreamOptionalHelper<T, StreamHelperCategoryStruct, false>
953 template<
typename T,
bool fixedLength,
int sz>
954 struct StreamOptionalContainerHelper;
963 template<
typename T,
int sz>
964 struct StreamOptionalContainerHelper<T, false, sz>
966 static const OptionalFormat optionalFormat =
ICE_SCOPED_ENUM(OptionalFormat, FSize);
968 template<
class S>
static inline void
969 write(S* stream,
const T& v,
Int)
971 StreamOptionalHelper<T, StreamHelperCategoryStruct, false>::write(stream, v);
974 template<
class S>
static inline void
975 read(S* stream, T& v)
977 StreamOptionalHelper<T, StreamHelperCategoryStruct, false>::read(stream, v);
987 template<
typename T,
int sz>
988 struct StreamOptionalContainerHelper<T, true, sz>
990 static const OptionalFormat optionalFormat =
ICE_SCOPED_ENUM(OptionalFormat, VSize);
992 template<
class S>
static inline void
993 write(S* stream,
const T& v,
Int n)
1000 stream->writeSize(sz * n + (n < 255 ? 1 : 5));
1004 template<
class S>
static inline void
1005 read(S* stream, T& v)
1019 template<
typename T>
1020 struct StreamOptionalContainerHelper<T, true, 1>
1022 static const OptionalFormat optionalFormat =
ICE_SCOPED_ENUM(OptionalFormat, VSize);
1024 template<
class S>
static inline void
1025 write(S* stream,
const T& v,
Int)
1030 template<
class S>
static inline void
1031 read(S* stream, T& v)
1042 template<
typename T>
1043 struct StreamOptionalHelper<T, StreamHelperCategorySequence, false>
1045 typedef typename T::value_type E;
1046 static const int size = StreamableTraits<E>::minWireSize;
1047 static const bool fixedLength = StreamableTraits<E>::fixedLength;
1051 static const OptionalFormat optionalFormat = StreamOptionalContainerHelper<T, fixedLength, size>::optionalFormat;
1053 template<
class S>
static inline void
1054 write(S* stream,
const T& v)
1056 StreamOptionalContainerHelper<T, fixedLength, size>::write(stream, v,
static_cast<Int>(v.size()));
1059 template<
class S>
static inline void
1060 read(S* stream, T& v)
1062 StreamOptionalContainerHelper<T, fixedLength, size>::read(stream, v);
1071 template<
typename T>
1072 struct StreamOptionalHelper<std::pair<const T*, const T*>, StreamHelperCategorySequence, false>
1074 typedef std::pair<const T*, const T*> P;
1075 static const int size = StreamableTraits<T>::minWireSize;
1076 static const bool fixedLength = StreamableTraits<T>::fixedLength;
1080 static const OptionalFormat optionalFormat = StreamOptionalContainerHelper<P, fixedLength, size>::optionalFormat;
1082 template<
class S>
static inline void
1083 write(S* stream,
const P& v)
1085 Int n =
static_cast<Int>(v.second - v.first);
1086 StreamOptionalContainerHelper<P, fixedLength, size>::write(stream, v, n);
1089 template<
class S>
static inline void
1090 read(S* stream, P& v)
1092 StreamOptionalContainerHelper<P, fixedLength, size>::read(stream, v);
1096 #ifndef ICE_CPP11_MAPPING
1103 template<
typename T>
1104 struct StreamOptionalHelper<std::pair<T, T>, StreamHelperCategorySequence, false>
1106 typedef std::pair<T, T> P;
1107 static const int size = StreamableTraits<typename T::value_type>::minWireSize;
1108 static const bool fixedLength = StreamableTraits<typename T::value_type>::fixedLength;
1112 static const OptionalFormat optionalFormat = StreamOptionalContainerHelper<P, fixedLength, size>::optionalFormat;
1114 template<
class S>
static inline void
1115 write(S* stream,
const P& v)
1117 Int n =
static_cast<Int>(v.second - v.first);
1118 StreamOptionalContainerHelper<P, fixedLength, size>::write(stream, v, n);
1121 template<
class S>
static inline void
1122 read(S* stream, P& v)
1124 StreamOptionalContainerHelper<P, fixedLength, size>::read(stream, v);
1133 template<
typename T>
1134 struct StreamOptionalHelper<std::pair<IceUtil::ScopedArray<T>, std::pair<const T*, const T*> >,
1135 StreamHelperCategorySequence, false>
1137 typedef std::pair<IceUtil::ScopedArray<T>, std::pair<const T*, const T*> > P;
1138 static const int size = StreamableTraits<T>::minWireSize;
1139 static const bool fixedLength = StreamableTraits<T>::fixedLength;
1143 static const OptionalFormat optionalFormat = StreamOptionalContainerHelper<P, fixedLength, size>::optionalFormat;
1145 template<
class S>
static inline void
1146 read(S* stream, P& v)
1148 StreamOptionalContainerHelper<P, fixedLength, size>::read(stream, v);
1160 template<
typename T>
1161 struct StreamOptionalHelper<T, StreamHelperCategoryDictionary, false>
1163 typedef typename T::key_type K;
1164 typedef typename T::mapped_type V;
1166 static const int size = StreamableTraits<K>::minWireSize + StreamableTraits<V>::minWireSize;
1167 static const bool fixedLength = StreamableTraits<K>::fixedLength && StreamableTraits<V>::fixedLength;
1171 static const OptionalFormat optionalFormat = StreamOptionalContainerHelper<T, fixedLength, size>::optionalFormat;
1173 template<
class S>
static inline void
1174 write(S* stream,
const T& v)
1176 StreamOptionalContainerHelper<T, fixedLength, size>::write(stream, v,
static_cast<Int>(v.size()));
1179 template<
class S>
static inline void
1180 read(S* stream, T& v)
1182 StreamOptionalContainerHelper<T, fixedLength, size>::read(stream, v);