Skip to content

Instantly share code, notes, and snippets.

@poseidon4o
Created June 2, 2014 14:09
Show Gist options
  • Save poseidon4o/289b79b105285f503942 to your computer and use it in GitHub Desktop.
Save poseidon4o/289b79b105285f503942 to your computer and use it in GitHub Desktop.
Simple serialization example
#include <iostream>
#include <cstring>
using namespace std;
// add Copy ctors // operator =
void stupid_memcopy(char * dest, char * source, int size) {
while(size--) {
*dest = *source;
}
}
class serializable {
public:
virtual char * serialize() const = 0;
virtual int serialized_len() const = 0;
};
struct coord {
int x,y;
};
class snake_cls : public serializable {
protected:
coord pos;
char symbol;
public:
snake_cls(int x, int y, char symbol) {
pos.x = x;
pos.y = y;
this->symbol = symbol;
}
virtual int serialize_len() const {
return sizeof(coord) + sizeof(char);
}
virtual char * serialize() const {
char * data = new char[sizeof(snake_cls)];
stupid_memcopy(data, (char*)&this->pos, sizeof(coord));
stupid_memcopy(data + sizeof(coord), (char*)&this->symbol, sizeof(char));
return data;
}
};
class horse_cls: public serializable {
protected:
char * name;
public:
horse_cls(const char * name) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
}
virtual ~horse_cls() {
delete[] this->name;
}
virtual int serialize_len() const {
return sizeof(int) + strlen(name);
}
virtual char * serialize() const {
int len = strlen(name);
char * data = new char[sizeof(int) + len];
stupid_memcopy(data, (char*)&len, sizeof(int));
stupid_memcopy(data + sizeof(int), name, len);
return data;
}
};
class lamia_cls: public snake_cls, public horse_cls {
char * snake_part_name;
public:
lamia_cls(const char * spn, char * horse_name, int x, int y, char symbol)
:snake_cls(x, y, symbol), horse_cls(horse_name) {
snake_part_name = new char[strlen(spn) + 1];
strcpy(snake_part_name, spn);
}
const char * get_name() {
char * whole_name = new char[strlen(name) + strlen(snake_part_name) + 1];
strcpy(whole_name, snake_part_name);
strcat(whole_name, name);
return whole_name;
}
virtual ~lamia_cls() {
delete[] snake_part_name;
}
virtual char * serialize() const {
char * snake = snake_cls::serialize();
int snake_len = snake_cls::serialized_len();
char * horse = horse_cls::serialize();
int horse_len = horse_cls::serialized_len();
int snake_name_len = strlen(snake_part_name);
char * data = new char[snake_len + horse_len + sizeof(int) + snake_name_len];
#define append(dest, src, size, off) \
stupid_memcopy(dest + off, (char*)src, size); \
off += size;
int offset = 0;
append(data, snake, snake_len, offset);
append(data, horse, horse_len, offset);
append(data, &snake_name_len, sizeof(int), offset);
append(data, snake_part_name, snake_name_len, offset);
delete[] horse;
delete[] snake;
return data;
}
virtual int serialized_len() const {
return snake_cls::serialized_len() + horse_cls::serialized_len() + sizeof(int) + strlen(snake_part_name);
}
static lamia_cls deserialize(char * data) {
#define read(dest, src, size, off) \
stupid_memcopy((char*) dest, src + off, size); \
off += size;
//stupid_memcopy((char*)&pos, data, sizeof(coord));
//offset += sizeof(coord);
coord pos;
char symbol;
int offset = 0;
read(&pos, data, sizeof(coord), offset)
read(&symbol, data, sizeof(symbol), offset)
int horse_name_len;
read(&horse_name_len, data, sizeof(int), offset);
char * horse_name = new char[horse_name_len + 1];
read(horse_name, data, horse_name_len, offset)
int snake_name_len;
read(&snake_name_len, data, sizeof(int), offset)
char * snake_part_name = new char[snake_name_len + 1];
read(snake_part_name, data, snake_name_len, offset)
lamia_cls lamia(snake_part_name, horse_name, pos.x, pos.y, symbol);
delete[] horse_name;
delete[] snake_part_name;
return lamia;
}
};
int main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment