AlpsEncoded.h
Go to the documentation of this file.
1 #ifndef AlpsEncoded_h
2 #define AlpsEncoded_h
3 
4 #include <cstring>
5 #include <string>
6 #include <memory>
7 #include <vector>
8 
9 #include "CoinError.hpp"
10 
11 #include "Alps.h"
12 
13 // AlpsEncoded is modified from BCP_buffer and CoinEncoded
14 
15 //#############################################################################
23 //#############################################################################
24 
25 class AlpsEncoded {
26 
27  private:
28 
30  AlpsEncoded(const AlpsEncoded&);
34 
35  private:
36 
38  size_t pos_;
39 
41  size_t maxSize_;
42 
47  //std::string type_;
48 
49  int type_;
50 
52  int size_;
53 
55  // const char* representation_; //why const ??? XY
57 
58  public:
59 
62 
66  :
67  pos_(0),
68  maxSize_(0x4000/*16K*/),
69  type_(0),
70  size_(0),
71  representation_(new char[maxSize_])
72  {}
73 
75  AlpsEncoded(int t)
76  :
77  pos_(0),
78  maxSize_(0),
79  type_(t),
80  size_(0),
81  representation_(0)
82  {}
83 
85  AlpsEncoded(int t, int s, char*& r)
86  :
87  pos_(0),
88  maxSize_(s + 4),
89  type_(t),
90  size_(s),
91  representation_(r)
92  { r = NULL; }
93 
96  if (representation_) {
97  delete [] representation_;
98  representation_ = 0;
99  }
100  }
104  int type() const { return type_; }
106  int size() const { return size_; }
107  const char* representation() const { return representation_; }
109 
110  inline void setPosition(const int pos) {
111  if (pos < 0 || pos >= size()) {
112  // const char msg [100] = "Incorrest position setting.";
113  //throw AlpsException(__FILE__, __LINE__, msg);
114  throw CoinError("Incorrest position setting.", "setPosition",
115  "AlpsEncoded");
116  }
117  pos_ = pos;
118  }
119 
120  inline void setRepresentation(char*& buf) {
121  //> 0x4000/*16K*/ ? (strlen(buf)+1) : 0x4000;
122  maxSize_ = strlen(buf) + 1;
123  representation_ = buf;
124  buf = 0;
125  }
126 
130  inline void make_fit(const int addSize){
131  assert(addSize > 0);
132  size_t addSize1 = static_cast<size_t>(addSize);
133 
134  if (maxSize_ < size_ + addSize1){
135  maxSize_ = 4 * (size_ + addSize1 + 0x1000/*4K*/);
136  char* newRep = new char[maxSize_];
137  if (size_)
138  memcpy(newRep, representation_, size_);
139  delete[] representation_;
140  representation_ = newRep;
141  }
142  }
143 
146  inline void clear(){
147  size_ = 0;
148  pos_ = 0;
149  type_ = 0;
150  if (representation_ != 0) {
151  delete representation_;
152  representation_ = 0;
153  }
154  }
155 
156  //------------------------------------------------------
157  // Following functiosn are used in parallel code only.
158  //------------------------------------------------------
159 
163  template <class T> AlpsEncoded& writeRep(const T& value) {
164  make_fit( sizeof(T) );
165  memcpy(representation_ + size_, &value, sizeof(T));
166  size_ += static_cast<int>(sizeof(T));
167  return *this;
168  }
169 
173  template <class T> AlpsEncoded& readRep(T& value){
174 #ifdef PARANOID
175  if (pos_ + sizeof(T) > size_) {
176  throw CoinError("Reading over the end of buffer.",
177  "readRep(const T& value)", "AlpsEncoded");
178  }
179 #endif
180  memcpy(&value, representation_ + pos_, sizeof(T));
181  pos_ += sizeof(T);
182  return *this;
183  }
184 
185 
189  template <class T> AlpsEncoded& writeRep(const T* const values,
190  const int length){
191  make_fit( static_cast<int>(sizeof(int)) +
192  static_cast<int>(sizeof(T)) * length );
193  memcpy(representation_ + size_, &length, sizeof(int));
194  size_ += static_cast<int>(sizeof(int));
195  if (length > 0){
196  memcpy(representation_ + size_, values,
197  static_cast<int>(sizeof(T)) * length);
198  size_ += static_cast<int>(sizeof(T)) * length;
199  }
200  return *this;
201  }
202 
216  template <class T> AlpsEncoded& readRep(T*& values,
217  int& length,
218  bool needAllocateMemory = true)
219  {
220 
221  if (needAllocateMemory) {
222  // Need allocate memeory for arrary "values".
223 
224 #ifdef PARANOID
225  if (pos_ + sizeof(int) > size_) {
226  throw CoinError("Reading over the end of buffer.",
227  "readRep(T*& values, int& length,...",
228  "AlpsEncoded");
229  }
230 #endif
231  memcpy(&length, representation_ + pos_, sizeof(int));
232  pos_ += sizeof(int);
233  if (length > 0){
234 #ifdef PARANOID
235  if (pos_ + sizeof(T)*length > size_) {
236  throw CoinError("Reading over the end of buffer.",
237  "readRep(T*& values, int& length,...",
238  "AlpsEncoded");
239  }
240 #endif
241  values = new T[length];
242  memcpy(values, representation_ + pos_, sizeof(T)*length);
243  pos_ += sizeof(T) * length;
244  }
245 
246  }
247  else { /* values has been allocated memory. */
248 
249  int l;
250 #ifdef PARANOID
251  if (pos_ + sizeof(int) > size_) {
252  throw CoinError("Reading over the end of buffer.",
253  "readRep(T*& values, int& length,...",
254  "AlpsEncoded");
255  }
256 #endif
257  memcpy(&l, representation_ + pos_, sizeof(int));
258  pos_ += sizeof(int);
259  if (l != length) {
260  throw CoinError("Reading over the end of buffer.",
261  "readRep(T*& values, int& length,...",
262  "AlpsEncoded");
263  }
264  if (length > 0){
265 #ifdef PARANOID
266  if (pos_ + sizeof(T)*length > size_) {
267  throw CoinError("Reading over the end of buffer.",
268  "readRep(T*& values, int& length,...",
269  "AlpsEncoded");
270  }
271 #endif
272  memcpy(values, representation_ + pos_, sizeof(T)*length);
273  pos_ += sizeof(T) * length;
274  }
275  }
276 
277  return *this;
278  }
279 
281  AlpsEncoded& writeRep(std::string& value){
282  // must define here, 'cos in *_message.C we have only templated members
283  const int len = static_cast<const int> (value.length());
284  make_fit( static_cast<int>(sizeof(int)) + len );
285  memcpy(representation_ + size_, &len, static_cast<int>(sizeof(int)));
286  size_ += static_cast<int>(sizeof(int));
287  if (len > 0){
288  memcpy(representation_ + size_, value.c_str(), len);
289  size_ += len;
290  }
291  return *this;
292  }
293 
295  AlpsEncoded& readRep(std::string& value){
296  int len;
297  readRep(len);
298  value.assign(representation_ + pos_, len);
299  pos_ += len;
300  return *this;
301  }
302 
304  template <class T> AlpsEncoded& writeRep(const std::vector<T>& vec) {
305  int objnum = vec.size();
306  int new_bytes = objnum * sizeof(T);
307  make_fit( sizeof(int) + new_bytes );
308  memcpy(representation_ + size_, &objnum, sizeof(int));
309  size_ += sizeof(int);
310  if (objnum > 0){
311  memcpy(representation_ + size_, &vec[0], new_bytes);
312  size_ += new_bytes;
313  }
314  return *this;
315  }
316 
318  template <class T> AlpsEncoded& readRep(std::vector<T>& vec) {
319  int objnum;
320 #ifdef PARANOID
321  if (pos_ + sizeof(int) > size_)
322  throw CoinError("Reading over the end of buffer.",
323  "AlpsEncoded", "readRep(std::vector<T>& vec");
324 #endif
325  memcpy(&objnum, representation_ + pos_, sizeof(int));
326  pos_ += sizeof(int);
327  vec.clear();
328  if (objnum > 0){
329 #ifdef PARANOID
330  if (pos_ + sizeof(T)*objnum > size_)
331  throw CoinError("Reading over the end of buffer.",
332  "AlpsEncoded", "readRep(std::vector<T>& vec");
333 #endif
334  vec.insert(vec.end(), objnum, T());
335  memcpy(&vec[0], representation_ + pos_, objnum * sizeof(T));
336  pos_ += objnum * sizeof(T);
337  }
338  return *this;
339  }
342 };
343 
344 #endif
int size() const
Definition: AlpsEncoded.h:106
AlpsEncoded & readRep(T &value)
Read a single object of type T from repsentation_ .
Definition: AlpsEncoded.h:173
AlpsEncoded & writeRep(const T &value)
Write a single object of type T in repsentation_ .
Definition: AlpsEncoded.h:163
AlpsEncoded(int t)
Useful constructor.
Definition: AlpsEncoded.h:75
AlpsEncoded(int t, int s, char *&r)
Useful constructor.
Definition: AlpsEncoded.h:85
const char * representation() const
Definition: AlpsEncoded.h:107
AlpsEncoded & readRep(T *&values, int &length, bool needAllocateMemory=true)
Read an array of objects of type T from repsentation_, where T must be a built-in type (ar at least ...
Definition: AlpsEncoded.h:216
AlpsEncoded()
The default constructor creates a buffer of size 16 Kbytes with no message in it. ...
Definition: AlpsEncoded.h:65
This data structure is to contain the packed form of an encodable knowledge.
Definition: AlpsEncoded.h:25
AlpsEncoded & writeRep(const std::vector< T > &vec)
Write a std::vector into repsentation_ .
Definition: AlpsEncoded.h:304
~AlpsEncoded()
Destructor.
Definition: AlpsEncoded.h:95
AlpsEncoded & writeRep(const T *const values, const int length)
Write a C style array of objects of type T in repsentation_.
Definition: AlpsEncoded.h:189
void make_fit(const int addSize)
Reallocate the size of encoded if necessary so that at least addsize_ number of additional bytes will...
Definition: AlpsEncoded.h:130
char * representation_
The encoded/compressed representation of the object.
Definition: AlpsEncoded.h:56
AlpsEncoded & operator=(const AlpsEncoded &)
Disable copy constructor and assignment operator.
int type() const
Definition: AlpsEncoded.h:105
AlpsEncoded & readRep(std::string &value)
Read a std::string from repsentation_ .
Definition: AlpsEncoded.h:295
size_t maxSize_
The amount of memory allocated for the representation.
Definition: AlpsEncoded.h:41
int type_
Represent the type of the object.
Definition: AlpsEncoded.h:49
Error Class thrown by an exception.
Definition: CoinError.hpp:42
size_t pos_
The next read/write position in the representation.
Definition: AlpsEncoded.h:38
void clear()
Completely clear the encoded.
Definition: AlpsEncoded.h:146
void setRepresentation(char *&buf)
Definition: AlpsEncoded.h:120
AlpsEncoded & writeRep(std::string &value)
Read a std::string in repsentation_ .
Definition: AlpsEncoded.h:281
void setPosition(const int pos)
Definition: AlpsEncoded.h:110
int size_
The size of the packed representation.
Definition: AlpsEncoded.h:52
AlpsEncoded & readRep(std::vector< T > &vec)
Read a std::vector from repsentation_ .
Definition: AlpsEncoded.h:318