LCCD  1.5.0
Xdr.hh
1 #ifndef Xdr_h
2 #define Xdr_h
3 
4 #include "lccd.h"
5 
6 #include <typeinfo>
7 
8 namespace lccd {
9 
19  class Xdr {
20 
21  public:
22 
26  static void copy( unsigned const char* from, unsigned char* dest,
27  const int size, const int count);
28 
29 
30 
34  template <class T>
35  static void tostream( std::string& s , T* t, unsigned count ) {
36 
37  int bytlen, padlen;
38  bytlen = count * sizeof( T ) ;
39  padlen = (bytlen + 3) & 0xfffffffc ;
40 
41 
42  unsigned index = s.size() ;
43 
44  s.resize( s.size() + padlen ) ;
45 
46  unsigned char* dest = reinterpret_cast< unsigned char* > ( & s[ index ] ) ;
47  unsigned char* from = reinterpret_cast< unsigned char* > ( t ) ;
48 
49 // std::cout << " tostream: 0x" << std::hex << *t << std::dec << std::endl ;
50 
51  Xdr::copy( from , dest , sizeof( T ) , count ) ;
52 
53  }
54 
60  template <class T>
61  static unsigned fromstream( const std::string& s , unsigned position, T* t, unsigned count ) {
62 
63  if( count == 0 )
64  return position ;
65 
66  unsigned bytlen, padlen;
67  bytlen = count * sizeof( T ) ;
68  padlen = (bytlen + 3) & 0xfffffffc ;
69 
70 
71  unsigned const char* from = reinterpret_cast< unsigned const char* > ( & s[ position ] ) ;
72  unsigned char* dest = reinterpret_cast< unsigned char* > ( t ) ;
73 
74 
75 
76  Xdr::copy( from , dest , sizeof( T ) , count ) ;
77 
78 
79 // std::cout << " fromstream: " ;
80 // for( unsigned int i=0; i < count; i++ )
81 // std::cout << std::hex << t[i] << ", " << std::dec ;
82 // std::cout << " - [" << position << "] - type:"
83 // << typeid( *t ).name()
84 // << std::endl ;
85 
86 
87  return position + padlen ;
88  }
89 
90 
91 
94 // template<>
95  static void tostream( std::string& s , std::string* strVal, unsigned /*count*/ ) {
96 
97  int strSize = (int) strVal->size() ;
98 
99  int bytlen, padlen;
100  bytlen = strSize + sizeof( int ) ;
101 
102  padlen = (bytlen + 3) & 0xfffffffc ;
103 
104  unsigned index = s.size() ;
105 
106  s.resize( index + padlen ) ;
107 
108 
109  // write the string's size first
110 
111  unsigned char* dest = reinterpret_cast< unsigned char* > ( & s[ index ] ) ;
112  unsigned char* from = reinterpret_cast< unsigned char* > ( & strSize ) ;
113 
114  Xdr::copy( from , dest , sizeof( int ) , 1 ) ;
115 
116 
117  dest = reinterpret_cast< unsigned char* > ( & s[ index + sizeof( int ) ] ) ;
118  from = reinterpret_cast< unsigned char* > ( & strVal->operator[](0) ) ;
119 
120  // std::cout << " tostream: - " << *strVal
121  // << " - lenght: " << strSize
122  // << std::endl ;
123 
124  Xdr::copy( from , dest , 1 , strSize ) ;
125 
126  }
127 
128 
132 // template <>
133  static unsigned fromstream( const std::string& s, unsigned index,
134  std::string* strVal, unsigned count ) {
135 
136  if( count == 0 )
137  return index ;
138 
139 
140  // read the string length first
141  int strSize ;
142  unsigned const char* from = reinterpret_cast< unsigned const char* > ( & s[ index ] ) ;
143  unsigned char* dest = reinterpret_cast< unsigned char* > ( & strSize ) ;
144 
145  Xdr::copy( from , dest , sizeof( int ) , 1 ) ;
146 
147  index += sizeof( int ) ;
148 
149 
150  unsigned bytlen, padlen;
151  bytlen = strSize ;
152  padlen = (bytlen + 3) & 0xfffffffc ;
153 
154 
155  strVal->resize( strSize ) ;
156 
157  from = reinterpret_cast< unsigned const char* > ( & s[ index ] ) ;
158  dest = reinterpret_cast< unsigned char* > ( & strVal->operator[](0) ) ;
159 
160  Xdr::copy( from , dest , 1 , strSize ) ;
161 
162  // std::cout << " fromstream: - " << *strVal
163  // << " - lenght: " << strSize
164  // << " - [" << index + padlen << "] "
165  // << std::endl ;
166 
167 
168  return index + padlen ;
169  }
170 
171 
172  }; // class Xdr
173 } //end namespace
174 
175 #endif // Xdr_h
176 
static void copy(unsigned const char *from, unsigned char *dest, const int size, const int count)
Copies the the data in XDR, i.e.
static unsigned fromstream(const std::string &s, unsigned index, std::string *strVal, unsigned count)
Specialization for strings - count is ignored and taken to be 1.
Definition: Xdr.hh:133
static unsigned fromstream(const std::string &s, unsigned position, T *t, unsigned count)
Read count objects of (simple) type t from the string/stream s at position.
Definition: Xdr.hh:61
static void tostream(std::string &s, T *t, unsigned count)
Writes count objects of (simple) type T to the string s in Xdr format.
Definition: Xdr.hh:35
static void tostream(std::string &s, std::string *strVal, unsigned)
Specialization for strings - count is ignored and taken to be 1.
Definition: Xdr.hh:95
Utility functions for DB streamer objects.
Definition: Xdr.hh:19