File: persist.cpp

package info (click to toggle)
ucommon 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,068 kB
  • sloc: cpp: 45,917; ansic: 714; sh: 226; makefile: 173
file content (292 lines) | stat: -rw-r--r-- 7,482 bytes parent folder | download | duplicates (3)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015-2020 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

#ifndef UCOMMON_SYSRUNTIME

#include <ucommon-config.h>
#include <commoncpp/config.h>

#include <commoncpp/export.h>
// local includes
#include <commoncpp/persist.h>

namespace ost {

const uint32_t NullObject = 0xffffffff;

PersistException::PersistException(const std::string& reason) :
_what(reason)
{
}

const std::string& PersistException::getString() const
{
    return _what;
}

PersistException::~PersistException()
{
}

const char* PersistObject::getPersistenceID() const
{
  return "PersistObject";
}

PersistObject::PersistObject()
{
  // Do nothing
}

PersistObject::~PersistObject()
{
  // Do nothing
}

bool PersistObject::write(PersistEngine& archive) const
{
  // Do nothing
  return true; // Successfully
}

bool PersistObject::read(PersistEngine& archive)
{
  // Do nothing
  return true; // Successfully
}

static TypeManager::StringFunctionMap* theInstantiationFunctions = 0;
static int refCount = 0;

TypeManager::StringFunctionMap& _internal_GetMap()
{
    return *theInstantiationFunctions;
}
void TypeManager::add(const char* name, NewPersistObjectFunction construction)
{
  if (refCount++ == 0) {
    theInstantiationFunctions = new StringFunctionMap;
  }
  assert(_internal_GetMap().find(std::string(name)) == _internal_GetMap().end());
  _internal_GetMap()[std::string(name)] = construction;
}

void TypeManager::remove(const char* name)
{
  assert(_internal_GetMap().find(std::string(name)) != _internal_GetMap().end());
  _internal_GetMap().erase(_internal_GetMap().find(std::string(name)));
  if (--refCount == 0) {
    delete theInstantiationFunctions;
    theInstantiationFunctions = 0;
  }
}

PersistObject* TypeManager::createInstanceOf(const char* name)
{
  assert(refCount);
  assert(_internal_GetMap().find(std::string(name)) != _internal_GetMap().end());
  return (_internal_GetMap()[std::string(name)])();
}

TypeManager::registration::registration(const char *name, NewPersistObjectFunction func) :
myName(name)
{
    TypeManager::add(name, func);
}

TypeManager::registration::~registration()
{
    TypeManager::remove(myName.c_str());
}

PersistEngine::PersistEngine(std::iostream& stream, EngineMode mode) :
myUnderlyingStream(stream), myOperationalMode(mode)
{
}

PersistEngine::~PersistEngine()
{
    if (myUnderlyingStream.good())
        myUnderlyingStream.sync();
}

void PersistEngine::writeBinary(const uint8_t* data, const uint32_t size)
{
  if(myOperationalMode != modeWrite)
    throw(PersistException("Cannot write to an input Engine"));
  myUnderlyingStream.write((const char *)data,size);
}


void PersistEngine::readBinary(uint8_t* data, uint32_t size)
{
  if(myOperationalMode != modeRead)
    throw(PersistException("Cannot read from an output Engine"));
  myUnderlyingStream.read((char *)data,size);
}

void PersistEngine::write(const PersistObject *object)
{
  // Pre-step, if object is NULL, then don't serialize it - serialize a
  // marker to say that it is null.
  // as ID's are uint32's, NullObject will do nicely for the task
  if (object == NULL) {
    uint32_t id = NullObject;
    write(id);
    return;
  }

  // First off - has this Object been serialized already?
  ArchiveMap::const_iterator itor = myArchiveMap.find(object);
  if (itor == myArchiveMap.end()) {
    // Unfortunately we need to serialize it - here we go ....
    uint32_t id = (uint32_t)myArchiveMap.size();
    myArchiveMap[object] = id; // bumps id automatically for next one
    write(id);
    ClassMap::const_iterator classItor = myClassMap.find(object->getPersistenceID());
    if (classItor == myClassMap.end()) {
      uint32_t classId = (uint32_t)myClassMap.size();
      myClassMap[object->getPersistenceID()] = classId;
      write(classId);
      write(static_cast<std::string>(object->getPersistenceID()));
        }
    else {
      write(classItor->second);
        }
    std::string majik;
    majik = "OBST";
    write(majik);
    object->write(*this);
    majik = "OBEN";
    write(majik);
  }
  else {
    // This object has been serialized, so just pop its ID out
    write(itor->second);
  }
}

void PersistEngine::read(PersistObject &object)
{
  uint32_t id = 0;
  read(id);
  if (id == NullObject)
    throw(PersistException("Object Id should not be NULL when un-persisting to a reference"));

  // Do we already have this object in memory?
  if (id < myArchiveVector.size()) {
    object = *(myArchiveVector[id]);
    return;
  }

  // Okay - read the identifier for the class in...
  // we won't need it later since this object is already allocated
  readClass();

  // Okay then - we can read data straight into this object
  readObject(&object);
}

void PersistEngine::read(PersistObject *&object)
{
  uint32_t id = 0;
  read(id);
  // Is the ID a NULL object?
  if (id == NullObject) {
    object = NULL;
    return;
  }

  // Do we already have this object in memory?
  if (id < myArchiveVector.size()) {
    object = myArchiveVector[id];
    return;
  }

  // Okay - read the identifier for the class in...
  std::string className = readClass();

  // is the pointer already initialized? if so then no need to reallocate
  if (object != NULL) {
    readObject(object);
    return;
  }

  // Create the object (of the relevant type)
  object = TypeManager::createInstanceOf(className.c_str());
  if (object) {
    // Okay then - we can make this object
    readObject(object);
  }
  else
    throw(PersistException(std::string("Unable to instantiate object of class ")+className));
}

void PersistEngine::readObject(PersistObject* object)
{
  // Okay then - we can make this object
  myArchiveVector.push_back(object);
  std::string majik;
  read(majik);
  if(majik != std::string("OBST"))
    throw( PersistException("Missing Start-of-Object marker"));
  object->read(*this);
  read(majik);
  if(majik != std::string("OBEN"))
    throw( PersistException("Missing End-of-Object marker"));
}

const std::string PersistEngine::readClass()
{
  // Okay - read the identifier for the class in...
  uint32_t classId = 0;
  read(classId);
  std::string className;
  if (classId < myClassVector.size()) {
    className = myClassVector[classId];
  }
  else {
    // Okay the class wasn't known yet - save its name
    read(className);
    myClassVector.push_back(className);
  }

  return className;
}

void PersistEngine::write(const std::string& str)
{
  uint32_t len = (uint32_t)str.length();
  write(len);
  writeBinary((uint8_t*)str.c_str(),len);
}

void PersistEngine::read(std::string& str)
{
  uint32_t len = 0;
  read(len);
  uint8_t *buffer = new uint8_t[len+1];
  readBinary(buffer,len);
  buffer[len] = 0;
  str = (char*)buffer;
  delete[] buffer;
}

} // namespace ucommon

#endif