1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
7 /// \brief This file contains stream_size_of functions for basic types and *STREAMABLE macros.
8 /// stream_size_of functions return the size of the object's data that is written or
9 /// read from a stream.
12 #ifndef STRMSIZE_H_052FF16B2D8A608761BF10333D065073
13 #define STRMSIZE_H_052FF16B2D8A608761BF10333D065073
17 /// For partial specialization of stream_size_of for objects
18 template <typename T
> struct object_stream_size
{
19 inline size_t operator()(const T
& v
) const { return (v
.stream_size()); }
21 template <typename T
> struct integral_object_stream_size
{
22 inline size_t operator()(const T
& v
) const { return (sizeof(v
)); }
24 /// Returns the size of the given object. Overloads for standard types are available.
26 inline size_t stream_size_of (const T
& v
) {
27 typedef typename
tm::Select
<numeric_limits
<T
>::is_integral
,
28 integral_object_stream_size
<T
>, object_stream_size
<T
> >::Result stream_sizer_t
;
29 return (stream_sizer_t()(v
));
35 // Extra overloads in this macro are needed because it is the one used for
36 // marshalling pointers. Passing a pointer to stream_size_of creates a
37 // conversion ambiguity between converting to const pointer& and converting
38 // to bool; the compiler always chooses the bool conversion (because it
39 // requires 1 conversion instead of 2 for the other choice). There is little
40 // point in adding the overloads to other macros, since they are never used
43 /// Declares that T is to be written as is into binary streams.
44 #define INTEGRAL_STREAMABLE(T) \
46 inline istream& operator>> (istream& is, T& v) { is.iread(v); return (is); } \
47 inline ostream& operator<< (ostream& os, const T& v) { os.iwrite(v); return (os); } \
48 inline ostream& operator<< (ostream& os, T& v) { os.iwrite(v); return (os); } \
49 template <> inline size_t stream_size_of (const T& v) { return (sizeof(v)); } \
52 /// Declares that T contains read, write, and stream_size methods. This is no longer needed and is deprecated.
53 #define STD_STREAMABLE(T)
55 /// Declares \p T to be writable to text streams. This is no longer needed and is deprecated.
56 #define TEXT_STREAMABLE(T)
58 /// Declares that T is to be cast into TSUB for streaming.
59 #define CAST_STREAMABLE(T,TSUB) \
61 inline istream& operator>> (istream& is, T& v) { TSUB sv; is >> sv; v = (T)(sv); return (is); } \
62 inline ostream& operator<< (ostream& os, const T& v) { os << TSUB(v); return (os); } \
63 template <> inline size_t stream_size_of (const T& v) { return (stream_size_of (TSUB(v))); } \
66 /// Placed into a class it declares the methods required by STD_STREAMABLE. Syntactic sugar.
67 #define DECLARE_STD_STREAMABLE \
69 void read (istream& is); \
70 void write (ostream& os) const; \
71 size_t stream_size (void) const
73 /// Specifies that \p T is printed by using it as an index into \p Names string array.
74 #define LOOKUP_TEXT_STREAMABLE(T,Names,nNames) \
76 inline ostringstream& operator<< (ostringstream& os, const T& v) \
78 os << Names[min(uoff_t(v),uoff_t(nNames-1))]; \