Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xdr.h
Go to the documentation of this file.
1 /*! \file xstream/xdr.h
2  *
3  * \brief C++ iostream like interface to read and write xdr streams
4  *
5  * \arg endian safe
6  * \arg needs floats and doubles stores in IEEE format in hardware
7  * (most architecture work this way, \c SPARC is the only exception I know)
8  *
9  */
10 
11 
12 #ifndef __XSTREAM_XDR
13 #define __XSTREAM_XDR
14 
15 #include <xstream/config.h>
16 
17 #include <vector>
18 #include <string>
19 
20 //for pair definition
21 #include <utility>
22 
23 #include <streambuf>
24 #include <iostream>
25 #include <istream>
26 #include <ostream>
27 
28 namespace{
29 
30 #if HAVE_INTTYPES_H
31 # include<inttypes.h>
32 #else
33 # if HAVE_STDINT_H
34 # include <stdint.h>
35 # endif
36 # error "I need inttypes.h or stdint.h to exist"
37 #endif
38 
39 }
40 
41 /*!
42  * \brief Library top namespace
43  */
44 namespace xstream{
45 /*!
46  * \brief xdr related objects
47  */
48 namespace xdr{
49 
50 using std::string;
51 using std::streambuf;
52 using std::pair;
53 using std::vector;
54 
55 /*!
56  * \brief Output xdr stream class
57  *
58  * Just a thin wrapper for streambuf to serialize all the datatypes
59  *
60  */
61 
62 class ostream
63 {
64  private:
65  streambuf *_sb;
66  public:
67  /*!
68  * \brief construct using a streambuf
69  *
70  */
71  ostream(streambuf* sb):_sb(sb){};
72 
73  /*!
74  * \brief construct using an ostream
75  *
76  */
77 
78  ostream(const std::ostream &os):_sb(os.rdbuf()){};
79 
80  ostream& operator<<(int32_t v);
81  //ostream& operator<<(int v);
82  ostream& operator<<(uint32_t v);
83  //ostream& operator<<(unsigned int v);
84  ostream& operator<<(int64_t v);
85  //ostream& operator<<(const long int v);
86  ostream& operator<<(uint64_t v);
87  //ostream& operator<<(unsigned long v);
88  ostream& operator<<(float v);
89  ostream& operator<<(double v);
90  ostream& operator<<(const string &v);
91 
92  /*!
93  * \brief Serializes STL pair containers to xdr
94  *
95  */
96  template <typename A, typename B>
97  ostream& operator<<(const pair<A,B> &p){
98  (*this)<<(p.first);
99  (*this)<<(p.second);
100  return *this;
101  }
102 
103  /*!
104  * \brief Serializes STL vector containers to xdr
105  *
106  */
107  template <typename T>
108  ostream& operator<<( const vector<T> &t){
109 
110  uint32_t sz = static_cast<uint32_t>( t.size() );
111  (*this)<<sz;
112  typename vector<T>::const_iterator cit = t.begin();
113  const typename vector<T>::const_iterator cend = t.end();
114  while(cit!=cend){
115  (*this)<<*(cit++);
116  }
117  return (*this);
118  }
119 };
120 
121 /*!
122  * \brief Input xdr stream class
123  *
124  * Just a thin wrapper for streambuf to deserialize all the datatypes
125  *
126  */
127 class istream
128 {
129  private:
130  streambuf *_sb;
131 
132  public:
133  /*!
134  * \brief construct using a streambuf
135  *
136  */
137  istream(streambuf* sb):_sb(sb){};
138 
139  /*!
140  * \brief construct using an istream
141  *
142  */
143  istream(const std::istream &os):_sb(os.rdbuf()){};
144 
145  istream& operator>>(int32_t &v);
146  //istream& operator>>(int &v);
147  istream& operator>>(uint32_t &v);
148  //istream& operator>>(unsigned int &v);
149  istream& operator>>(int64_t &v);
150  //istream& operator>>(long int &v);
151  istream& operator>>(uint64_t &v);
152  //istream& operator>>(unsigned long int &v);
153  istream& operator>>(float &v);
154  istream& operator>>(double &v);
155  istream& operator>>(string &v);
156 
157  /*!
158  * \brief Deserializes STL pair containers from xdr
159  *
160  */
161  template <typename A, typename B>
162  istream& operator>>(pair<A,B> &p){
163  (*this)>>(p.first);
164  (*this)>>(p.second);
165  return *this;
166  }
167 
168  /*!
169  * \brief Deserializes STL vector containers from xdr
170  *
171  */
172  template <typename T>
173  istream& operator>>( vector<T> &t){
174  uint32_t sz;
175  T val;
176 
177  //maybe I could allocate all the necessary entries here
178 
179  (*this)>>sz;
180 
181  std::clog<<"VECTOR SIZE: "<<sz<<std::endl;
182 
183  while(sz--!=0){
184  (*this)>>val;
185  std::clog<<"read: "<<val<<std::endl;
186  t.push_back(val);
187  }
188  return (*this);
189  }
190 };
191 
192 /*!
193  * \example xdr_in.cpp
194  *
195  * shows how to use the xstream::xdr::istream to restore a structure from \c XDR data
196  *
197  * reads from standard input
198  *
199  */
200 
201 /*!
202  * \example xdr_out.cpp
203  *
204  * shows how to use the xstream::xdr::ostream to serialize a structure to \c XDR data
205  *
206  * writes to standard output
207  *
208  */
209 
210 }//namespace xdr
211 }//namespace xstream
212 
213 #endif
char string[256]
Output xdr stream class.
Definition: xdr.h:62
streambuf * _sb
Definition: xdr.h:130
streambuf * _sb
Definition: xdr.h:65
ostream(streambuf *sb)
construct using a streambuf
Definition: xdr.h:71
istream(streambuf *sb)
construct using a streambuf
Definition: xdr.h:137
istream & operator>>(pair< A, B > &p)
Deserializes STL pair containers from xdr.
Definition: xdr.h:162
istream & operator>>(int32_t &v)
Definition: xdr.cpp:163
Input xdr stream class.
Definition: xdr.h:127
ostream(const std::ostream &os)
construct using an ostream
Definition: xdr.h:78
istream & operator>>(vector< T > &t)
Deserializes STL vector containers from xdr.
Definition: xdr.h:173
istream(const std::istream &os)
construct using an istream
Definition: xdr.h:143
ostream & operator<<(int32_t v)
Definition: xdr.cpp:49