35 #define _BAS_SIZE_BYTES_ 2 // Number of bytes used to store var size and array size in the payload, 36 #define _BAS_ARRAY_SIZE_ 2 // MAX is your OS size_t byte size (4 is max recommended as it will be compatible in most cases) 37 #define _BAS_CHECKSUM_SIZE_ 4 // If this lib is used for networking, make sure that these 3 numbers are the same on both ends 69 constructFromPayload(data);
79 , _isChecksumRemoved(other._isChecksumRemoved)
100 helper.pushData(*
this, data);
112 template <
typename T>
113 inline void pushData(
const T* data,
size_t array_size)
115 const char* data_ = (
const char*)data;
116 size_t size =
sizeof(T);
118 pushSizes(
size, array_size);
120 pushRawData(
size, array_size, data_);
130 template <
typename T>
134 return helper.popData(*
this);
143 template <
typename T>
147 size_t array_size = 0;
150 std::tie(
size, array_size) = getSizes();
152 var =
new T[array_size];
154 copyMem(
size, array_size, var);
198 _isChecksumRemoved =
false;
208 _isChecksumRemoved = other._isChecksumRemoved;
222 constructFromPayload(data);
234 if (_isChecksumRemoved)
236 _isChecksumRemoved =
true;
238 _data.erase(_data.begin());
249 if (!_isChecksumRemoved)
252 _data.insert(_data.begin(), 0);
257 inline void prepareChecksum(
void)
263 inline void constructFromPayload(
const char* data)
267 size |= (data[i] << (i * 8));
272 inline void checksumUpdate(
void)
274 size_t size = _data.size();
276 if (_isChecksumRemoved)
280 _data[i] = (
size >> (i * 8));
284 inline void pushRawData(
size_t size,
size_t array_size,
const char* data)
286 for (
size_t j = 0; j < array_size; j++) {
287 for (
size_t i = 0; i <
size; i++) {
288 _data.push_back(data[j *
size + i]);
293 inline void pushSizes(
size_t size,
size_t array_size)
296 _data.push_back((
size >> (i * 8)) & 0xFF);
299 _data.push_back((array_size >> (i * 8)) & 0xFF);
302 inline std::tuple<int, int> getSizes(
void)
304 size_t size = 0, array_size = 0;
306 if (!_isChecksumRemoved) {
311 size |= (_data.front() << (i * 8));
312 _data.erase(_data.begin());
316 array_size |= (_data.front() << (i * 8));
317 _data.erase(_data.begin());
319 return std::make_tuple(
size, array_size);
322 template <
typename T>
323 inline void copyMem(
size_t size,
size_t array_size, T* var)
325 for (
size_t j = 0; j < array_size; j++) {
326 std::memcpy(var + j, _data.data(),
size);
327 for (
size_t i = 0; i <
size; i++) {
328 _data.erase(_data.begin());
333 std::vector<char> _data;
334 bool _isChecksumRemoved =
false;
336 template <
typename T>
338 friend class Helper<std::string>;
385 makeUnserialization(obj);
398 makeSerialization(obj);
411 template <
typename T>
416 PoppedArray() =
default;
418 inline PoppedArray(
size_t size, T* ptr)
450 std::shared_ptr<T> _ptr;
459 template <
typename T>
464 const char* data = (
const char*)&data_;
465 size_t size =
sizeof(T);
466 size_t array_size = 1;
468 obj.pushSizes(
size, array_size);
470 obj.pushRawData(
size, array_size, data);
472 obj.checksumUpdate();
478 size_t array_size = 0;
481 std::tie(
size, array_size) = obj.getSizes();
483 obj.copyMem(
size, array_size, &var);
491 class Helper<std::string> {
495 const char* data = data_.c_str();
496 size_t size =
sizeof(char);
497 size_t array_size = data_.size() + 1;
499 obj.pushSizes(
size, array_size);
501 obj.pushRawData(
size, array_size, data);
503 obj.checksumUpdate();
509 size_t array_size = 0;
513 std::tie(
size, array_size) = obj.getSizes();
515 var =
new char[array_size];
517 obj.copyMem(
size, array_size, var);
528 template <
typename T>
529 class Helper<std::
vector<T>> {
533 const char* data = (
char*)data_.data();
534 size_t size =
sizeof(T);
535 size_t array_size = data_.size();
537 obj.pushSizes(
size, array_size);
539 obj.pushRawData(
size, array_size, data);
541 obj.checksumUpdate();
547 size_t array_size = 0;
551 std::tie(
size, array_size) = obj.getSizes();
553 var =
new T[array_size];
555 obj.copyMem(
size, array_size, var);
557 vector.assign(var, var + array_size);
571 const char* data = (
char*)data_.payload();
572 size_t size =
sizeof(char);
573 size_t array_size = data_.size();
575 obj.pushSizes(
size, array_size);
577 obj.pushRawData(
size, array_size, data);
579 obj.checksumUpdate();
585 size_t array_size = 0;
589 std::tie(
size, array_size) = obj.getSizes();
591 var =
new char[array_size];
593 obj.copyMem(
size, array_size, var);
void removeChecksum(void)
Remove Checksum at the beggining of payload.
Definition: bas.hpp:232
SerializedObject serialize(void)
Serialize the class.
Definition: bas.hpp:395
T * get(void)
Returns the pointer to the array popped by popDataArray<>().
Definition: bas.hpp:443
SerializedObject & operator=(const SerializedObject &other)
Assign values to the SerializedObject.
Definition: bas.hpp:205
SerializedObject()
Definition: bas.hpp:56
size_t size(void) const
Returns the size of the payload in Bytes.
Definition: bas.hpp:187
SerializedObject(const char *data)
Construct object from payload.
Definition: bas.hpp:67
#define _BAS_ARRAY_SIZE_
Definition: bas.hpp:36
void clear()
Clear the SerializedObject and resets it to its default values.
Definition: bas.hpp:195
~SerializedObject()=default
Default destructor.
The PoppedArray is the return value of SerializedObject::popDataArray<>().
Definition: bas.hpp:47
The Serializable class, when herited from, allows you to serialize your own classes.
Definition: bas.hpp:347
const char * payload(void) const
Returns a pointer to the payload of the object.
Definition: bas.hpp:165
void addChecksum(void)
Add Checksum at the beggining of payload.
Definition: bas.hpp:247
#define _BAS_CHECKSUM_SIZE_
Definition: bas.hpp:37
void unserialize(SerializedObject obj)
Reconstruct object from a SerializedObject.
Definition: bas.hpp:383
void pushData(const T *data, size_t array_size)
Pushes an array of data into the object payload.
Definition: bas.hpp:113
size_t size(void)
Returns the size of the array popped by popDataArray<>().
Definition: bas.hpp:431
T popData(void)
Pop the next data in the payload.
Definition: bas.hpp:131
SerializedObject(const SerializedObject &other)
Copy-construct object from another SerializedObject.
Definition: bas.hpp:77
std::vector< char > & vector(void)
Returns a reference to the std::vector containing the payload.
Definition: bas.hpp:176
void pushData(const T &data)
Pushes data into the payload.
Definition: bas.hpp:97
The SerializedObject contains and manages the payload of your serializations.
Definition: bas.hpp:54
SerializedObject & operator=(const char *data)
Assign a copy of data to the payload.
Definition: bas.hpp:220
PoppedArray< T > popDataArray(void)
Pop the next array in the payload and returns a PoppedArray.
Definition: bas.hpp:144
#define _BAS_SIZE_BYTES_
Definition: bas.hpp:35