/usr/include/cc++/persist.h is in libcommoncpp2-dev 1.8.1-6.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | // Copyright (C) 1999-2005 Open Source Telecom Corporation.
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
//
// This exception applies only to the code released under the name GNU
// Common C++. If you copy code from other releases into a copy of GNU
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way. To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for GNU Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.
//
/**
* @file persist.h
* @short Persistence library classes.
**/
#ifndef CCXX_PERSIST_H_
#define CCXX_PERSIST_H_
#ifndef CCXX_CONFIG_H_
#include <cc++/config.h>
#endif
#ifndef CCXX_EXCEPTIONS_H_
#include <cc++/exception.h>
#endif
#ifndef CCXX_MISSING_H_
#include <cc++/missing.h>
#endif
#ifndef CCXX_STRING_H_
#include <cc++/string.h>
#endif
#ifdef HAVE_ZLIB_H
#ifndef NO_COMPRESSION
#include <zlib.h>
#endif
#else
#define NO_COMPRESSION
#endif
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#ifdef CCXX_NAMESPACES
namespace ost {
#define NS_PREFIX ost::
#else
#define NS_PREFIX
#endif
#ifdef CCXX_EXCEPTIONS
#ifdef COMMON_STD_EXCEPTION
class __EXPORT PersistException : public Exception
{
public:
PersistException(const String &what) : Exception(what) {};
};
#else
class __EXPORT PersistException
{
public:
PersistException(const String& reason);
inline const String& getString() const
{return Exception::getString();};
virtual ~PersistException() {} throw();
protected:
String _what;
};
#endif
#endif
// This typedef allows us to declare NewBaseObjectFunction now
typedef class BaseObject* (*NewBaseObjectFunction) (void);
/**
* This class manages the types for generation of the persistent objects.
* Its data structures are managed automatically by the system. They are
* implicitly filled by the constructors who declare classes to the system.
*
* @author Daniel Silverstone
* @short Type manager for persistence engine.
*/
class __EXPORT TypeManager
{
public:
/**
* This manages a registration to the typemanager - attempting to
* remove problems with the optimisers
*/
class Registration
{
public:
Registration(const char* name, NewBaseObjectFunction func);
virtual ~Registration();
private:
String myName;
};
/**
* This adds a new construction function to the type manager
*/
static void add(const char* name, NewBaseObjectFunction construction);
/**
* And this one removes a type from the managers lists
*/
static void remove(const char* name);
/**
* This function creates a new object of the required type and
* returns a pointer to it. NULL is returned if we couldn't find
* the type
*/
static BaseObject* createInstanceOf(const char* name);
typedef std::map<String,NewBaseObjectFunction> StringFunctionMap;
};
/*
* The following defines are used to declare and define the relevant code
* to allow a class to use the Persistence::Engine code.
*/
#define DECLARE_PERSISTENCE(ClassType) \
public: \
friend NS_PREFIX Engine& operator>>( NS_PREFIX Engine& ar, ClassType *&ob); \
friend NS_PREFIX Engine& operator<<( NS_PREFIX Engine& ar, ClassType const &ob); \
friend NS_PREFIX BaseObject *createNew##ClassType(); \
virtual const char* getPersistenceID() const; \
static NS_PREFIX TypeManager::Registration registrationFor##ClassType;
#define IMPLEMENT_PERSISTENCE(ClassType, FullyQualifiedName) \
NS_PREFIX BaseObject *createNew##ClassType() { return new ClassType; } \
const char* ClassType::getPersistenceID() const {return FullyQualifiedName;} \
NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType &ob) \
{ ar >> (NS_PREFIX BaseObject &) ob; return ar; } \
NS_PREFIX Engine& operator>>(NS_PREFIX Engine& ar, ClassType *&ob) \
{ ar >> (NS_PREFIX BaseObject *&) ob; return ar; } \
NS_PREFIX Engine& operator<<(NS_PREFIX Engine& ar, ClassType const &ob) \
{ ar << (NS_PREFIX BaseObject const *)&ob; return ar; } \
NS_PREFIX TypeManager::Registration \
ClassType::registrationFor##ClassType(FullyQualifiedName, \
createNew##ClassType);
class Engine;
/**
* BaseObject
*
* This object is the base for all Persistent data which is not
* natively serialised by the Persistence::Engine
*
* It registers itself with the Persistence::TypeManager
* using a global constructor function. A matching deregister call
* is made in a global destructor, to allow DLL's to use the
* Persistence::Engine in a main executable.
*
* Persistable objects must never maintain bad pointers. If a pointer
* doesn't point to something valid, it must be NULL. This is so
* the persistence engine knows whether to allocate memory for an object
* or whether the memory has been pre-allocated.
*
* @author Daniel Silverstone
* @short Base class for classes that will be persistent.
*/
class __EXPORT BaseObject
{
public:
/**
* This constructor is used in serialisation processes.
* It is called in CreateNewInstance in order to create
* an instance of the class to have Read() called on it.
*/
BaseObject();
/**
* Default destructor
*/
virtual ~BaseObject();
/**
* This returns the ID of the persistent object (Its type)
*/
virtual const char* getPersistenceID() const;
/**
* This method is used to write to the Persistence::Engine
* It is not equivalent to the << operator as it writes only the data
* and not the object type etc.
*/
virtual bool write(Engine& archive) const;
/**
* This method is used to read from a Persistence::Engine
* It is not equivalent to the >> operator as it does no
* typesafety or anything.
*/
virtual bool read(Engine& archive);
};
/**
* Engine
*
* This class constructs on a standard C++ STL stream and then
* operates in the mode specified. The stream passed into the
* constructor must be a binary mode to function properly.
*
* @author Daniel Silverstone
* @short stream serialization of persistent classes.
*/
class __EXPORT Engine
{
public:
/**
* These are the modes the Persistence::Engine can work in
*/
enum EngineMode {
modeRead,
modeWrite
};
/**
* Constructs a Persistence::Engine with the specified stream in
* the given mode. The stream must be initialised properly prior
* to this call or problems will ensue. If built using zlib compress
* can be used to enable compression
*/
Engine(std::iostream& stream, EngineMode mode, bool compress=true) THROWS (PersistException);
/**
* This Flushes the buffers and closes the Persistence::Engine
* this must happen before the underlying stream is shut down
*/
void sync();
/**
* This says there are more objects to deserialize
*/
bool more();
virtual ~Engine();
// Write operations
/**
* writes a BaseObject from a reference.
*/
void write(const BaseObject &object) THROWS (PersistException)
{ write(&object); };
/**
* writes a BaseObject from a pointer.
*/
void write(const BaseObject *object) THROWS (PersistException);
// writes supported primitive types
// shortcut, to make the following more readable
#define CCXX_ENGINEWRITE_REF(valref) writeBinary((const uint8*)&valref,sizeof(valref))
void write(int8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(uint8 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(int16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(uint16 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(int32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(uint32 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
#ifdef HAVE_64_BITS
void write(int64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(uint64 i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
#endif
void write(float i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
void write(double i) THROWS (PersistException) { CCXX_ENGINEWRITE_REF(i); }
#undef CCXX_ENGINEWRITE_REF
void write(const String& str) THROWS (PersistException);
void write(const std::string& str) THROWS (PersistException);
// Every write operation boils down to one or more of these
void writeBinary(const uint8* data, const uint32 size) THROWS (PersistException);
// Read Operations
/**
* reads a BaseObject into a reference overwriting the object.
*/
void read(BaseObject &object) THROWS (PersistException);
/**
* reads a BaseObject into a pointer allocating memory for the object if necessary.
*/
void read(BaseObject *&object) THROWS (PersistException);
// reads supported primitive types
// shortcut, to make the following more readable
#define CCXX_ENGINEREAD_REF(valref) readBinary((uint8*)&valref,sizeof(valref))
void read(int8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(uint8& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(int16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(uint16& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(int32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(uint32& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
#ifdef HAVE_64_BITS
void read(int64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(uint64& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
#endif
void read(float& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
void read(double& i) THROWS (PersistException) { CCXX_ENGINEREAD_REF(i); }
#undef CCXX_ENGINEREAD_REF
void read(String& str) THROWS (PersistException);
void read(std::string& str) THROWS (PersistException);
// Every read operation boild down to one or more of these
void readBinary(uint8* data, uint32 size) THROWS (PersistException);
private:
/**
* reads the actual object data into a pre-instantiated object pointer
* by calling the read function of the derived class.
*/
void readObject(BaseObject* object) THROWS (PersistException);
/**
* reads in a class name, and caches it into the ClassMap.
*/
const String readClass() THROWS (PersistException);
/**
* The underlying stream
*/
std::iostream& myUnderlyingStream;
/**
* The mode of the engine. read or write
*/
EngineMode myOperationalMode;
/**
* Typedefs for the Persistence::BaseObject support
*/
typedef std::vector<BaseObject*> ArchiveVector;
typedef std::map<BaseObject const*, int32> ArchiveMap;
typedef std::vector<String> ClassVector;
typedef std::map<String, int32> ClassMap;
ArchiveVector myArchiveVector;
ArchiveMap myArchiveMap;
ClassVector myClassVector;
ClassMap myClassMap;
// Compression support
bool use_compression; // valid onlry if NO_COMPRESSION is false
#ifndef NO_COMPRESSION
z_stream myZStream;
uint8* myCompressedDataBuffer;
uint8* myUncompressedDataBuffer;
uint8* myLastUncompressedDataRead;
#endif
};
// Standard >> and << stream operators for BaseObject
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, BaseObject &ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, BaseObject *&ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, BaseObject const &ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, BaseObject const *ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, int8& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, int8 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, uint8& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, uint8 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, int16& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, int16 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, uint16& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, uint16 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, int32& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, int32 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, uint32& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, uint32 ob) THROWS (PersistException);
#ifdef HAVE_64_BITS
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, int64& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, int64 ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, uint64& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, uint64 ob) THROWS (PersistException);
#endif
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, float& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, float ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, double& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, double ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, String& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, String ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, std::string& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, std::string ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator >>( Engine& ar, bool& ob) THROWS (PersistException);
/** @relates Engine */
__EXPORT Engine& operator <<( Engine& ar, bool ob) THROWS (PersistException);
/**
* The following are templated classes
*/
/**
* @relates Engine
* serialize a vector of some serializable content to
* the engine
*/
template<class T>
Engine& operator <<( Engine& ar, typename std::vector<T> const& ob) THROWS (PersistException)
{
ar << (uint32)ob.size();
for(unsigned int i=0; i < ob.size(); ++i)
ar << ob[i];
return ar;
}
/**
* @relates Engine
* deserialize a vector of deserializable content from
* an engine.
*/
template<class T>
Engine& operator >>( Engine& ar, typename std::vector<T>& ob) THROWS (PersistException)
{
ob.clear();
uint32 siz;
ar >> siz;
ob.resize(siz);
for(uint32 i=0; i < siz; ++i)
ar >> ob[i];
return ar;
}
/**
* @relates Engine
* serialize a deque of some serializable content to
* the engine
*/
template<class T>
Engine& operator <<( Engine& ar, typename std::deque<T> const& ob) THROWS (PersistException)
{
ar << (uint32)ob.size();
for(typename std::deque<T>::const_iterator it=ob.begin(); it != ob.end(); ++it)
ar << *it;
return ar;
}
/**
* @relates Engine
* deserialize a deque of deserializable content from
* an engine.
*/
template<class T>
Engine& operator >>( Engine& ar, typename std::deque<T>& ob) THROWS (PersistException)
{
ob.clear();
uint32 siz;
ar >> siz;
//ob.resize(siz);
for(uint32 i=0; i < siz; ++i) {
T node;
ar >> node;
ob.push_back(node);
//ar >> ob[i];
}
return ar;
}
/**
* @relates Engine
* serialize a map with keys/values which both are serializeable
* to an engine.
*/
template<class Key, class Value>
Engine& operator <<( Engine& ar, typename std::map<Key,Value> const & ob) THROWS (PersistException)
{
ar << (uint32)ob.size();
for(typename std::map<Key,Value>::const_iterator it = ob.begin();it != ob.end();++it)
ar << it->first << it->second;
return ar;
}
/**
* @relates Engine
* deserialize a map with keys/values which both are serializeable
* from an engine.
*/
template<class Key, class Value>
Engine& operator >>( Engine& ar, typename std::map<Key,Value>& ob) THROWS (PersistException)
{
ob.clear();
uint32 siz;
ar >> siz;
for(uint32 i=0; i < siz; ++i) {
Key a;
ar >> a;
ar >> ob[a];
}
return ar;
}
/**
* @relates Engine
* serialize a pair of some serializable content to the engine.
*/
template<class x, class y>
Engine& operator <<( Engine& ar, std::pair<x,y> &ob) THROWS (PersistException)
{
ar << ob.first << ob.second;
return ar;
}
/**
* @relates Engine
* deserialize a pair of some serializable content to the engine.
*/
template<class x, class y>
Engine& operator >>(Engine& ar, std::pair<x, y> &ob) THROWS (PersistException)
{
ar >> ob.first >> ob.second;
return ar;
}
#ifdef CCXX_NAMESPACES
}
#endif
#endif
/** EMACS **
* Local variables:
* mode: c++
* c-basic-offset: 4
* End:
*/
|