forked from trachten/cpisync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataObject.h
173 lines (143 loc) · 5.07 KB
/
DataObject.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* This code is part of the CPISync project developed at Boston University. Please see the README for use and references. */
#ifndef DATA_OBJECT_H
#define DATA_OBJECT_H
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <map>
#include <list>
#include <CPISync/Aux/UID.h>
#include <CPISync/Aux/Auxiliary.h>
// namespaces
using std::string;
using std::ostream;
/**
* DataObject.h -- a wrapper for a generic set element to be synchronized
*/
class DataObject : public UID {
public:
// Constructors
/**
* Construct an empty data object.
*/
DataObject();
/**
* Constructs a data object that contains the given byte vector (not a copy!).
* @param data An vector of bytes containing the data of the object.
*/
explicit DataObject(const ZZ &datum);
/**
* Constructs a data object that contains the given string (in an encoded format).
* @param data The string to place in the DataObject.
*/
explicit DataObject(const string& str);
/**
* Constructs a data object that contains a given pair {number, list of elements}
* @param index a number in a templated type
* @param elems a list containing pointers to data object
**/
template <class T>
explicit DataObject(const T index, const list<shared_ptr<DataObject>> elems)
{
string str = base64_encode(toStr<T>(index).c_str(), toStr<T>(index).length()) + " ";
for (auto itr : elems)
str += base64_encode(itr->to_string().c_str(), itr->to_string().length()) + " ";
str = base64_encode(str.c_str(),str.length());
myBuffer = pack(str);
}
/**
* Constructs a data object that contains a given set
* @param mySet The set to place in the DataObject
**/
explicit DataObject(const multiset<shared_ptr<DataObject>> mySet);
/**
* Constructs a data object that contains the given object of type T, which must
* be translatable to a string with the global toStr function. In effect, this constructor
* simply converts the object to a string and then does the same as the string constructor.
* @param item The item to place in the DataObject.
*/
template<typename T>
explicit DataObject(const T item) {
myBuffer = pack(toStr(item));
}
// INFORMATION METHODS
// MANIPULATION METHODS
/**
* @return A ZZ encoding of the string
*/
ZZ to_ZZ() const;
/**
* @return The string version of a copy of the contents this data object.
* The string could have null bytes and non-printable characters.
*/
string to_string() const;
/**
* @return a multiset containing all the data object in the set
**/
multiset<shared_ptr<DataObject>> to_Set() const;
/**
* @param ind no specific usage but to indicate class type T for the first
* element in the element pair
* @return a pair {long ,list<shared_ptr<DataObject>>}
**/
template <class T>
pair<T, list<shared_ptr<DataObject>>> to_pair(){
string str = unpack(myBuffer);
auto splt = split(base64_decode(str), " ");
T out = strTo<T>(base64_decode(splt[0]));
list<shared_ptr<DataObject>> outList;
for (int ii = 1; ii < splt.size(); ii++)
outList.push_back(make_shared<DataObject>(base64_decode(splt[ii])));
return {out, outList};
}
/**
* @param len Stores the length of the char array (upon return)
* @return A char array version of the contents of this data object.
* The string could have null bytes and non-printable characters.
*/
const char *to_char_array(long &len) const;
/**
* @return A string of printable characters representing the object, essentially
* a base64 encoding of the internal contents of the DataObject.
*/
string print() const;
/**
* Allows stream printing of the object
*/
friend ostream& operator<<(ostream& out, const DataObject &datum);
// comparisons
bool operator < (const DataObject& second) const{
return this->myBuffer < second.to_ZZ();
}
bool operator== (const DataObject& second) const {
return this->myBuffer == second.to_ZZ();
}
// static variables
static bool RepIsInt; /** if true, then DataObject string inputs are interpreted as
* arbitrarily-sized integers. Otherwise, DataObject string inputs
* are interpreted as byte sequences.
*/
void setTimeStamp(clock_t ts);
clock_t getTimeStamp();
protected:
ZZ myBuffer; /** The buffer for the data object container itself. */
clock_t timestamp;
/**
* Unpacks a ZZ into a string
* @param num
* @return
*/
static string unpack(const ZZ& num);
private:
/**
* Packs a string into a ZZ in a memory-efficient manner
* @param theStr The string to pack
* @return a ZZ representing the string
*/
static ZZ pack(const string& theStr);
};
#endif