-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerializable.h
More file actions
56 lines (47 loc) · 1.15 KB
/
Serializable.h
File metadata and controls
56 lines (47 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Serializable.h
*
* Created on: Jan 18, 2016
* Author: sabeyruw
*/
#ifndef SERIALIZABLE_H_
#define SERIALIZABLE_H_
#include "ObjectInput.h"
#include "ObjectOutput.h"
#include "BufferInputOutputStream.h"
class Serializable
{
public:
virtual ~Serializable() {}
virtual void serialize(ObjectInput* in, ObjectOutput* out) =0;
virtual size_t readFromBuffer(unsigned char *buffer)
{
BufferInputOutputStream b(buffer);
ObjectInput in(&b);
serialize(&in, 0);
return b.bufferLocation;
}
virtual size_t writeToBuffer(unsigned char *buffer)
{
BufferInputOutputStream b(buffer);
ObjectOutput out(&b);
serialize(0, &out);
return b.bufferLocation;
}
protected:
void serializeBuffer(ObjectInput* in, ObjectOutput* out, unsigned char* p, const int& size)
{
if (in)
in->read(p, size);
if (out)
out->write(p, size);
}
template <typename T> void serializeObject(ObjectInput* in, ObjectOutput* out, const char*, T& p)
{
if (in)
in->read(p);
if (out)
out->write(p);
}
};
#endif /* SERIALIZABLE_H_ */