1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
//
|
6 |
|
|
/// \file strmsize.h
|
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.
|
10 |
|
|
|
11 |
|
|
#ifndef STRMSIZE_H_052FF16B2D8A608761BF10333D065073
|
12 |
|
|
#define STRMSIZE_H_052FF16B2D8A608761BF10333D065073
|
13 |
|
|
|
14 |
|
|
namespace ustl {
|
15 |
|
|
|
16 |
|
|
/// For partial specialization of stream_size_of for objects
|
17 |
|
|
template <typename T> struct object_stream_size {
|
18 |
|
|
inline streamsize operator()(const T& v) const { return (v.stream_size()); }
|
19 |
|
|
};
|
20 |
|
|
template <typename T> struct integral_object_stream_size {
|
21 |
|
|
inline streamsize operator()(const T& v) const { return (sizeof(v)); }
|
22 |
|
|
};
|
23 |
|
|
/// Returns the size of the given object. Overloads for standard types are available.
|
24 |
|
|
template <typename T>
|
25 |
|
|
inline streamsize stream_size_of (const T& v) {
|
26 |
|
|
typedef typename tm::Select <numeric_limits<T>::is_integral,
|
27 |
|
|
integral_object_stream_size<T>, object_stream_size<T> >::Result stream_sizer_t;
|
28 |
|
|
return (stream_sizer_t()(v));
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
} // namespace ustl
|
32 |
|
|
|
33 |
|
|
//
|
34 |
|
|
// Extra overloads in this macro are needed because it is the one used for
|
35 |
|
|
// marshalling pointers. Passing a pointer to stream_size_of creates a
|
36 |
|
|
// conversion ambiguity between converting to const pointer& and converting
|
37 |
|
|
// to bool; the compiler always chooses the bool conversion (because it
|
38 |
|
|
// requires 1 conversion instead of 2 for the other choice). There is little
|
39 |
|
|
// point in adding the overloads to other macros, since they are never used
|
40 |
|
|
// for pointers.
|
41 |
|
|
//
|
42 |
|
|
/// Declares that T is to be written as is into binary streams.
|
43 |
|
|
#define INTEGRAL_STREAMABLE(T) \
|
44 |
|
|
namespace ustl { \
|
45 |
|
|
inline istream& operator>> (istream& is, T& v) { is.iread(v); return (is); } \
|
46 |
|
|
inline ostream& operator<< (ostream& os, const T& v) { os.iwrite(v); return (os); } \
|
47 |
|
|
inline ostream& operator<< (ostream& os, T& v) { os.iwrite(v); return (os); } \
|
48 |
|
|
template <> inline streamsize stream_size_of (const T& v) { return (sizeof(v)); } \
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
/// Declares that T contains read, write, and stream_size methods. This is no longer needed and is deprecated.
|
52 |
|
|
#define STD_STREAMABLE(T)
|
53 |
|
|
|
54 |
|
|
/// Declares \p T to be writable to text streams. This is no longer needed and is deprecated.
|
55 |
|
|
#define TEXT_STREAMABLE(T)
|
56 |
|
|
|
57 |
|
|
/// Declares that T is to be cast into TSUB for streaming.
|
58 |
|
|
#define CAST_STREAMABLE(T,TSUB) \
|
59 |
|
|
namespace ustl { \
|
60 |
|
|
inline istream& operator>> (istream& is, T& v) { TSUB sv; is >> sv; v = (T)(sv); return (is); } \
|
61 |
|
|
inline ostream& operator<< (ostream& os, const T& v) { os << TSUB(v); return (os); } \
|
62 |
|
|
template <> inline streamsize stream_size_of (const T& v) { return (stream_size_of (TSUB(v))); } \
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/// Placed into a class it declares the methods required by STD_STREAMABLE. Syntactic sugar.
|
66 |
|
|
#define DECLARE_STD_STREAMABLE \
|
67 |
|
|
public: \
|
68 |
|
|
void read (istream& is); \
|
69 |
|
|
void write (ostream& os) const; \
|
70 |
|
|
streamsize stream_size (void) const
|
71 |
|
|
|
72 |
|
|
/// Specifies that \p T is printed by using it as an index into \p Names string array.
|
73 |
|
|
#define LOOKUP_TEXT_STREAMABLE(T,Names,nNames) \
|
74 |
|
|
namespace ustl { \
|
75 |
|
|
inline ostringstream& operator<< (ostringstream& os, const T& v) \
|
76 |
|
|
{ \
|
77 |
|
|
os << Names[min(uoff_t(v),uoff_t(nNames-1))]; \
|
78 |
|
|
return (os); \
|
79 |
|
|
} \
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
#endif
|