56 char* representation_;
71 representation_(new char[maxSize_])
96 if (representation_) {
97 delete [] representation_;
105 int type()
const {
return type_; }
106 int size()
const {
return size_; }
111 if (pos < 0 || pos >=
size()) {
114 throw CoinError(
"Incorrest position setting.",
"setPosition",
122 maxSize_ = strlen(buf) + 1;
123 representation_ = buf;
132 size_t addSize1 =
static_cast<size_t>(addSize);
134 if (maxSize_ < size_ + addSize1){
135 maxSize_ = 4 * (size_ + addSize1 + 0x1000);
136 char* newRep =
new char[maxSize_];
138 memcpy(newRep, representation_, size_);
139 delete[] representation_;
140 representation_ = newRep;
150 if (representation_ != 0) {
151 delete representation_;
165 memcpy(representation_ + size_, &value,
sizeof(T));
166 size_ +=
static_cast<int>(
sizeof(T));
175 if (pos_ +
sizeof(T) > size_) {
176 throw CoinError(
"Reading over the end of buffer.",
177 "readRep(const T& value)",
"AlpsEncoded");
180 memcpy(&value, representation_ + pos_,
sizeof(T));
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));
196 memcpy(representation_ + size_, values,
197 static_cast<int>(
sizeof(T)) * length);
198 size_ +=
static_cast<int>(
sizeof(T)) * length;
218 bool needAllocateMemory =
true)
221 if (needAllocateMemory) {
225 if (pos_ +
sizeof(
int) > size_) {
226 throw CoinError(
"Reading over the end of buffer.",
227 "readRep(T*& values, int& length,...",
231 memcpy(&length, representation_ + pos_,
sizeof(
int));
235 if (pos_ +
sizeof(T)*length > size_) {
236 throw CoinError(
"Reading over the end of buffer.",
237 "readRep(T*& values, int& length,...",
241 values =
new T[length];
242 memcpy(values, representation_ + pos_,
sizeof(T)*length);
243 pos_ +=
sizeof(T) * length;
251 if (pos_ +
sizeof(
int) > size_) {
252 throw CoinError(
"Reading over the end of buffer.",
253 "readRep(T*& values, int& length,...",
257 memcpy(&l, representation_ + pos_,
sizeof(
int));
260 throw CoinError(
"Reading over the end of buffer.",
261 "readRep(T*& values, int& length,...",
266 if (pos_ +
sizeof(T)*length > size_) {
267 throw CoinError(
"Reading over the end of buffer.",
268 "readRep(T*& values, int& length,...",
272 memcpy(values, representation_ + pos_,
sizeof(T)*length);
273 pos_ +=
sizeof(T) * length;
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));
288 memcpy(representation_ + size_, value.c_str(), len);
298 value.assign(representation_ + pos_, len);
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);
311 memcpy(representation_ + size_, &vec[0], new_bytes);
321 if (pos_ +
sizeof(
int) > size_)
322 throw CoinError(
"Reading over the end of buffer.",
323 "AlpsEncoded",
"readRep(std::vector<T>& vec");
325 memcpy(&objnum, representation_ + pos_,
sizeof(
int));
330 if (pos_ +
sizeof(T)*objnum > size_)
331 throw CoinError(
"Reading over the end of buffer.",
332 "AlpsEncoded",
"readRep(std::vector<T>& vec");
334 vec.insert(vec.end(), objnum, T());
335 memcpy(&vec[0], representation_ + pos_, objnum *
sizeof(T));
336 pos_ += objnum *
sizeof(T);
This data structure is to contain the packed form of an encodable knowledge.
AlpsEncoded & writeRep(const std::vector< T > &vec)
Write a std::vector into repsentation_ .
AlpsEncoded & writeRep(std::string &value)
Read a std::string in repsentation_ .
const char * representation() const
void setPosition(const int pos)
AlpsEncoded(int t)
Useful constructor.
AlpsEncoded & writeRep(const T *const values, const int length)
Write a C style array of objects of type T in repsentation_.
AlpsEncoded & readRep(T &value)
Read a single object of type T from repsentation_ .
AlpsEncoded()
The default constructor creates a buffer of size 16 Kbytes with no message in it.
void clear()
Completely clear the encoded.
~AlpsEncoded()
Destructor.
AlpsEncoded & readRep(std::vector< T > &vec)
Read a std::vector from repsentation_ .
void setRepresentation(char *&buf)
AlpsEncoded & readRep(std::string &value)
Read a std::string from repsentation_ .
void make_fit(const int addSize)
Reallocate the size of encoded if necessary so that at least addsize_ number of additional bytes will...
AlpsEncoded(int t, int s, char *&r)
Useful constructor.
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 ...
AlpsEncoded & writeRep(const T &value)
Write a single object of type T in repsentation_ .