From d10e6a5ca0905f0ef2836cd98aebfb48e7f1e8a3 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 20 Apr 2022 11:14:47 -0700 Subject: ArchiveBase/Archive renamed. More to come. --- src/stable/archival.cpp | 35 -------- src/stable/archival.h | 52 ----------- src/stable/archive.cpp | 200 ++++++++++++++++++++++++++++++++----------- src/stable/archive.h | 159 +++++++++++++--------------------- src/stable/archivebase.cpp | 197 ------------------------------------------ src/stable/archivebase.h | 80 ----------------- src/stable/archivebinary.cpp | 98 +++++++++++++++++++++ src/stable/archivebinary.h | 120 ++++++++++++++++++++++++++ src/stable/array.h | 6 +- src/stable/hash.h | 6 +- src/stable/list.h | 6 +- src/stable/string.cpp | 4 +- src/stable/string.h | 6 +- 13 files changed, 440 insertions(+), 529 deletions(-) delete mode 100644 src/stable/archival.cpp delete mode 100644 src/stable/archival.h delete mode 100644 src/stable/archivebase.cpp delete mode 100644 src/stable/archivebase.h create mode 100644 src/stable/archivebinary.cpp create mode 100644 src/stable/archivebinary.h (limited to 'src/stable') diff --git a/src/stable/archival.cpp b/src/stable/archival.cpp deleted file mode 100644 index 227e25d..0000000 --- a/src/stable/archival.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2007-2019 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/archival.h" - -Bu::Archival::Archival() -{ -} - -Bu::Archival::~Archival() -{ -} - -Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, const Bu::Archival &p) -{ - const_cast(p).archive( s ); - return s; -} - -Bu::ArchiveBase &Bu::operator<<(Bu::ArchiveBase &s, Bu::Archival &p) -{ - p.archive( s ); - return s; -} - -Bu::ArchiveBase &Bu::operator>>(Bu::ArchiveBase &s, Bu::Archival &p) -{ - p.archive( s ); - return s; -} - diff --git a/src/stable/archival.h b/src/stable/archival.h deleted file mode 100644 index f7122bc..0000000 --- a/src/stable/archival.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2007-2019 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARCHIVAL_H -#define BU_ARCHIVAL_H - -#include "bu/archivebase.h" - -namespace Bu -{ - /** - * The base class for any class you want to archive. Simply include this as - * a base class, implement the purely virtual archive function and you've - * got an easily archiveable class. - * - * Archival: "of or pertaining to archives or valuable records; contained - * in or comprising such archives or records." - */ - class Archival - { - public: - /** - * Does nothing, here for completeness. - */ - Archival(); - - /** - * Here to ensure the deconstructor is virtual. - */ - virtual ~Archival(); - - /** - * This is the main workhorse of the archive system, just override and - * you've got a archiveable class. A reference to the Archive - * used is passed in as your only parameter, query it to discover if - * you are loading or saving. - * @param ar A reference to the Archive object to use. - */ - virtual void archive( class ArchiveBase &ar )=0; - }; - - ArchiveBase &operator<<(ArchiveBase &, const class Bu::Archival &); - ArchiveBase &operator<<(ArchiveBase &, class Bu::Archival &); - ArchiveBase &operator>>(ArchiveBase &, class Bu::Archival &); - -} - -#endif diff --git a/src/stable/archive.cpp b/src/stable/archive.cpp index 69447fb..df9b1ff 100644 --- a/src/stable/archive.cpp +++ b/src/stable/archive.cpp @@ -6,15 +6,8 @@ */ #include "bu/archive.h" -#include "bu/stream.h" -#include "bu/archival.h" -#include "bu/sio.h" - -Bu::Archive::Archive( Stream &rStream, bool bLoading ) : - bLoading( bLoading ), - rStream( rStream ), - nNextID( 1 ) +Bu::Archive::Archive() { } @@ -22,78 +15,183 @@ Bu::Archive::~Archive() { } -void Bu::Archive::write( const void *pData, size_t nSize ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, bool p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned char p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed short p) +{ + ar.write( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned short p) { - if( nSize == 0 || pData == NULL ) - return; + ar.write( &p, sizeof(p) ); + return ar; +} - rStream.write( (const char *)pData, nSize ); +Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed int p) +{ + ar.write( &p, sizeof(p) ); + return ar; } -void Bu::Archive::read( void *pData, size_t nSize ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned int p) { - if( nSize == 0 || pData == NULL ) - return; + ar.write( &p, sizeof(p) ); + return ar; +} - if( (size_t)rStream.read( (char *)pData, nSize ) < nSize ) - throw Bu::ExceptionBase("Insufficient data to unarchive object."); +Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed long p) +{ + ar.write( &p, sizeof(p) ); + return ar; } -void Bu::Archive::close() +Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned long p) { - rStream.close(); + ar.write( &p, sizeof(p) ); + return ar; } -bool Bu::Archive::isLoading() +Bu::Archive &Bu::operator<<( Bu::Archive &ar, signed long long p) { - return bLoading; + ar.write( &p, sizeof(p) ); + return ar; } -uint32_t Bu::Archive::getID( const void *ptr ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, unsigned long long p) { - if( hPtrID.has( (ptrdiff_t)ptr ) ) - return hPtrID.get( (ptrdiff_t)ptr ); - hPtrID.insert( (ptrdiff_t)ptr, nNextID ); - return nNextID++; + ar.write( &p, sizeof(p) ); + return ar; } -void Bu::Archive::assocPtrID( void **ptr, uint32_t id ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, float p) { - if( hPtrID.has( id ) ) - { - *ptr = (void *)hPtrID.get( id ); - return; - } + ar.write( &p, sizeof(p) ); + return ar; +} - if( !hPtrDest.has( id ) ) - hPtrDest.insert( id, List() ); - - hPtrDest[id].getValue().append( ptr ); +Bu::Archive &Bu::operator<<( Bu::Archive &ar, double p) +{ + ar.write( &p, sizeof(p) ); + return ar; } -void Bu::Archive::readID( const void *ptr, uint32_t id ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, long double p) { - hPtrID.insert( id, (ptrdiff_t)ptr ); + ar.write( &p, sizeof(p) ); + return ar; +} - if( hPtrDest.has( id ) ) - { - Bu::List &l = hPtrDest.get( id ); - for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) - { - *(*i) = (void *)ptr; - } +Bu::Archive &Bu::operator>>( Bu::Archive &ar, bool &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} - hPtrDest.erase( id ); - } +Bu::Archive &Bu::operator>>( Bu::Archive &ar, char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned char &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed short &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned short &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed int &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned int &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, signed long long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, unsigned long long &p) +{ + ar.read( &p, sizeof(p) ); + return ar; +} + +Bu::Archive &Bu::operator>>( Bu::Archive &ar, float &p) +{ + ar.read( &p, sizeof(p) ); + return ar; } -void Bu::Archive::setProperty( const Bu::Blob &rKey, const Bu::Variant &rValue ) +Bu::Archive &Bu::operator>>( Bu::Archive &ar, double &p) { - hProps.insert( rKey, rValue ); + ar.read( &p, sizeof(p) ); + return ar; } -Bu::Variant Bu::Archive::getProperty( const Bu::Blob &rKey ) const +Bu::Archive &Bu::operator>>( Bu::Archive &ar, long double &p) { - return hProps.get( rKey ); + ar.read( &p, sizeof(p) ); + return ar; } diff --git a/src/stable/archive.h b/src/stable/archive.h index b6cc2a9..45d9af6 100644 --- a/src/stable/archive.h +++ b/src/stable/archive.h @@ -5,123 +5,82 @@ * terms of the license contained in the file LICENSE. */ -#ifndef BU_ARCHIVE_H -#define BU_ARCHIVE_H +#ifndef BU_ARCHIVE_BASE_H +#define BU_ARCHIVE_BASE_H #include -#include "bu/archivebase.h" -#include "bu/hash.h" -#include "bu/util.h" +#include + #include "bu/variant.h" #include "bu/blob.h" namespace Bu { - class Archival; - class Stream; - - /** - * Provides a framework for serialization of objects and primitives. The - * archive will handle any basic primitive, a few special types, like char * - * strings, as well as STL classes and anything that inherits from the - * Archival class. Each Archive operates on a Stream, so you can send the - * data using an Archive almost anywhere. - * - * In order to use an Archive to store something to a file, try something - * like: - *@code - * File sOut("output", "wb"); // This is a stream subclass - * Archive ar( sOut, Archive::save ); - * ar << myClass; - @endcode - * In this example myClass is any class that inherits from Archival. When - * the storage operator is called, the Archival::archive() function in the - * myClass object is called with a reference to the Archive. This can be - * handled in one of two ways: - *@code - * void MyClass::archive( Archive &ar ) - * { - * ar && sName && nAge && sJob; - * } - @endcode - * Here we don't worry about weather we're loading or saving by using the - * smart && operator. This allows us to write very consistent, very simple - * archive functions that really do a lot of work. If we wanted to do - * something different in the case of loading or saving we would do: - *@code - * void MyClass::archive( Archive &ar ) - * { - * if( ar.isLoading() ) - * { - * ar >> sName >> nAge >> sJob; - * } else - * { - * ar << sName << nAge << sJob; - * } - * } - @endcode - * Archive currently does not provide facility to make fully portable - * archives. For example, it will not convert between endianness for you, - * nor will it take into account differences between primitive sizes on - * different platforms. This, at the moment, is up to the user to ensure. - * One way of dealing with the latter problem is to make sure and use - * explicit primitive types from the stdint.h header, i.e. int32_t. - */ - class Archive : public ArchiveBase + class Archive { - private: - bool bLoading; public: - bool isLoading(); - + Archive(); + virtual ~Archive(); + enum { load = true, save = false }; - Archive( Stream &rStream, bool bLoading ); - virtual ~Archive(); - virtual void close(); - - virtual void write( const void *pData, size_t iSize ); - virtual void read( void *pData, size_t iSize ); - - /** - * For storage, get an ID for the pointer to the object you're going to - * write. - */ - uint32_t getID( const void *ptr ); - - /** - * For loading. Assosiates an empty pointer with an id. When you wind - * up loading an id reference to a pointer for an object that may or - * may not have loaded yet, call this with the id, if it has been loaded - * already, you'll immediately get a pointer, if not, it will write one - * for you when the time comes. - */ - void assocPtrID( void **ptr, uint32_t id ); - - /** - * For loading. Call this when you load an object that other things may - * have pointers to. It will assosiate every pointer that's been - * registered with assocPtrID to the pointer passed in, and id passed - * in. It will also set things up so future calls to assocPtrID will - * automatically succeed immediately. - */ - void readID( const void *ptr, uint32_t id ); + virtual void close()=0; + virtual void write( const void *pData, size_t iLength )=0; + virtual void read( void *pData, size_t iLength )=0; + virtual bool isLoading()=0; virtual void setProperty( const Bu::Blob &rKey, - const Bu::Variant &rValue ); - virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const; - - private: - Stream &rStream; - uint32_t nNextID; - Hash hPtrID; - Hash > hPtrDest; - Hash hProps; + const Bu::Variant &rValue )=0; + virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const=0; }; -} + + template Archive &operator&&( Archive &ar, T &dat ) + { + if( ar.isLoading() ) + { + return ar >> dat; + } + else + { + return ar << dat; + } + } + + Archive &operator<<( Archive &ar, bool p ); + Archive &operator<<( Archive &ar, char p ); + Archive &operator<<( Archive &ar, signed char p ); + Archive &operator<<( Archive &ar, unsigned char p ); + Archive &operator<<( Archive &ar, signed short p ); + Archive &operator<<( Archive &ar, unsigned short p ); + Archive &operator<<( Archive &ar, signed int p ); + Archive &operator<<( Archive &ar, unsigned int p ); + Archive &operator<<( Archive &ar, signed long p ); + Archive &operator<<( Archive &ar, unsigned long p ); + Archive &operator<<( Archive &ar, signed long long p ); + Archive &operator<<( Archive &ar, unsigned long long p ); + Archive &operator<<( Archive &ar, float p ); + Archive &operator<<( Archive &ar, double p ); + Archive &operator<<( Archive &ar, long double p ); + + Archive &operator>>( Archive &ar, bool &p ); + Archive &operator>>( Archive &ar, char &p ); + Archive &operator>>( Archive &ar, signed char &p ); + Archive &operator>>( Archive &ar, unsigned char &p ); + Archive &operator>>( Archive &ar, signed short &p ); + Archive &operator>>( Archive &ar, unsigned short &p ); + Archive &operator>>( Archive &ar, signed int &p ); + Archive &operator>>( Archive &ar, unsigned int &p ); + Archive &operator>>( Archive &ar, signed long &p ); + Archive &operator>>( Archive &ar, unsigned long &p ); + Archive &operator>>( Archive &ar, signed long long &p ); + Archive &operator>>( Archive &ar, unsigned long long &p ); + Archive &operator>>( Archive &ar, float &p ); + Archive &operator>>( Archive &ar, double &p ); + Archive &operator>>( Archive &ar, long double &p ); +}; #endif diff --git a/src/stable/archivebase.cpp b/src/stable/archivebase.cpp deleted file mode 100644 index d2d80a1..0000000 --- a/src/stable/archivebase.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2007-2019 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#include "bu/archivebase.h" - -Bu::ArchiveBase::ArchiveBase() -{ -} - -Bu::ArchiveBase::~ArchiveBase() -{ -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, bool p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned char p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed short p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned short p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed int p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned int p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, signed long long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, unsigned long long p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, float p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, double p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, long double p) -{ - ar.write( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, bool &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned char &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed short &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned short &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed int &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned int &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, signed long long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, unsigned long long &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, float &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, double &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, long double &p) -{ - ar.read( &p, sizeof(p) ); - return ar; -} - diff --git a/src/stable/archivebase.h b/src/stable/archivebase.h deleted file mode 100644 index d846c27..0000000 --- a/src/stable/archivebase.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2007-2019 Xagasoft, All rights reserved. - * - * This file is part of the libbu++ library and is released under the - * terms of the license contained in the file LICENSE. - */ - -#ifndef BU_ARCHIVE_BASE_H -#define BU_ARCHIVE_BASE_H - -#include -#include - -#include "bu/variant.h" -#include "bu/blob.h" - -namespace Bu -{ - class ArchiveBase - { - public: - ArchiveBase(); - virtual ~ArchiveBase(); - - virtual void close()=0; - virtual void write( const void *pData, size_t iLength )=0; - virtual void read( void *pData, size_t iLength )=0; - virtual bool isLoading()=0; - - virtual void setProperty( const Bu::Blob &rKey, - const Bu::Variant &rValue )=0; - virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const=0; - }; - - template ArchiveBase &operator&&( ArchiveBase &ar, T &dat ) - { - if( ar.isLoading() ) - { - return ar >> dat; - } - else - { - return ar << dat; - } - } - - ArchiveBase &operator<<( ArchiveBase &ar, bool p ); - ArchiveBase &operator<<( ArchiveBase &ar, char p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed char p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned char p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed short p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned short p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed int p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned int p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed long p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned long p ); - ArchiveBase &operator<<( ArchiveBase &ar, signed long long p ); - ArchiveBase &operator<<( ArchiveBase &ar, unsigned long long p ); - ArchiveBase &operator<<( ArchiveBase &ar, float p ); - ArchiveBase &operator<<( ArchiveBase &ar, double p ); - ArchiveBase &operator<<( ArchiveBase &ar, long double p ); - - ArchiveBase &operator>>( ArchiveBase &ar, bool &p ); - ArchiveBase &operator>>( ArchiveBase &ar, char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned char &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed short &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned short &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed int &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned int &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, signed long long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, unsigned long long &p ); - ArchiveBase &operator>>( ArchiveBase &ar, float &p ); - ArchiveBase &operator>>( ArchiveBase &ar, double &p ); - ArchiveBase &operator>>( ArchiveBase &ar, long double &p ); -}; - -#endif diff --git a/src/stable/archivebinary.cpp b/src/stable/archivebinary.cpp new file mode 100644 index 0000000..3f05947 --- /dev/null +++ b/src/stable/archivebinary.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/archivebinary.h" +#include "bu/stream.h" + +#include "bu/sio.h" + +Bu::ArchiveBinary::ArchiveBinary( Stream &rStream, bool bLoading ) : + bLoading( bLoading ), + rStream( rStream ), + nNextID( 1 ) +{ +} + +Bu::ArchiveBinary::~ArchiveBinary() +{ +} + +void Bu::ArchiveBinary::write( const void *pData, size_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.write( (const char *)pData, nSize ); +} + +void Bu::ArchiveBinary::read( void *pData, size_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + if( (size_t)rStream.read( (char *)pData, nSize ) < nSize ) + throw Bu::ExceptionBase("Insufficient data to unarchive object."); +} + +void Bu::ArchiveBinary::close() +{ + rStream.close(); +} + +bool Bu::ArchiveBinary::isLoading() +{ + return bLoading; +} + +uint32_t Bu::ArchiveBinary::getID( const void *ptr ) +{ + if( hPtrID.has( (ptrdiff_t)ptr ) ) + return hPtrID.get( (ptrdiff_t)ptr ); + hPtrID.insert( (ptrdiff_t)ptr, nNextID ); + return nNextID++; +} + +void Bu::ArchiveBinary::assocPtrID( void **ptr, uint32_t id ) +{ + if( hPtrID.has( id ) ) + { + *ptr = (void *)hPtrID.get( id ); + return; + } + + if( !hPtrDest.has( id ) ) + hPtrDest.insert( id, List() ); + + hPtrDest[id].getValue().append( ptr ); +} + +void Bu::ArchiveBinary::readID( const void *ptr, uint32_t id ) +{ + hPtrID.insert( id, (ptrdiff_t)ptr ); + + if( hPtrDest.has( id ) ) + { + Bu::List &l = hPtrDest.get( id ); + for( Bu::List::iterator i = l.begin(); i != l.end(); i++ ) + { + *(*i) = (void *)ptr; + } + + hPtrDest.erase( id ); + } +} + +void Bu::ArchiveBinary::setProperty( const Bu::Blob &rKey, const Bu::Variant &rValue ) +{ + hProps.insert( rKey, rValue ); +} + +Bu::Variant Bu::ArchiveBinary::getProperty( const Bu::Blob &rKey ) const +{ + return hProps.get( rKey ); +} + diff --git a/src/stable/archivebinary.h b/src/stable/archivebinary.h new file mode 100644 index 0000000..4c12e02 --- /dev/null +++ b/src/stable/archivebinary.h @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2007-2019 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef BU_ARCHIVE_BINARY_H +#define BU_ARCHIVE_BINARY_H + +#include +#include "bu/archive.h" +#include "bu/hash.h" +#include "bu/util.h" +#include "bu/variant.h" +#include "bu/blob.h" + +namespace Bu +{ + class Stream; + + /** + * Provides a framework for serialization of objects and primitives. The + * archive will handle any basic primitive, a few special types, like char * + * strings, as well as STL classes and anything that inherits from the + * Archival class. Each ArchiveBinary operates on a Stream, so you can send the + * data using an ArchiveBinary almost anywhere. + * + * In order to use an ArchiveBinary to store something to a file, try something + * like: + *@code + * File sOut("output", "wb"); // This is a stream subclass + * ArchiveBinary ar( sOut, ArchiveBinary::save ); + * ar << myClass; + @endcode + * In this example myClass is any class that inherits from Archival. When + * the storage operator is called, the Archival::archive() function in the + * myClass object is called with a reference to the ArchiveBinary. This can be + * handled in one of two ways: + *@code + * void MyClass::archive( ArchiveBinary &ar ) + * { + * ar && sName && nAge && sJob; + * } + @endcode + * Here we don't worry about weather we're loading or saving by using the + * smart && operator. This allows us to write very consistent, very simple + * archive functions that really do a lot of work. If we wanted to do + * something different in the case of loading or saving we would do: + *@code + * void MyClass::archive( ArchiveBinary &ar ) + * { + * if( ar.isLoading() ) + * { + * ar >> sName >> nAge >> sJob; + * } else + * { + * ar << sName << nAge << sJob; + * } + * } + @endcode + * ArchiveBinary currently does not provide facility to make fully portable + * archives. For example, it will not convert between endianness for you, + * nor will it take into account differences between primitive sizes on + * different platforms. This, at the moment, is up to the user to ensure. + * One way of dealing with the latter problem is to make sure and use + * explicit primitive types from the stdint.h header, i.e. int32_t. + */ + class ArchiveBinary : public Archive + { + private: + bool bLoading; + public: + bool isLoading(); + + ArchiveBinary( Stream &rStream, bool bLoading ); + virtual ~ArchiveBinary(); + virtual void close(); + + virtual void write( const void *pData, size_t iSize ); + virtual void read( void *pData, size_t iSize ); + + /** + * For storage, get an ID for the pointer to the object you're going to + * write. + */ + uint32_t getID( const void *ptr ); + + /** + * For loading. Assosiates an empty pointer with an id. When you wind + * up loading an id reference to a pointer for an object that may or + * may not have loaded yet, call this with the id, if it has been loaded + * already, you'll immediately get a pointer, if not, it will write one + * for you when the time comes. + */ + void assocPtrID( void **ptr, uint32_t id ); + + /** + * For loading. Call this when you load an object that other things may + * have pointers to. It will assosiate every pointer that's been + * registered with assocPtrID to the pointer passed in, and id passed + * in. It will also set things up so future calls to assocPtrID will + * automatically succeed immediately. + */ + void readID( const void *ptr, uint32_t id ); + + virtual void setProperty( const Bu::Blob &rKey, + const Bu::Variant &rValue ); + virtual Bu::Variant getProperty( const Bu::Blob &rKey ) const; + + private: + Stream &rStream; + uint32_t nNextID; + Hash hPtrID; + Hash > hPtrDest; + Hash hProps; + }; +} + +#endif diff --git a/src/stable/array.h b/src/stable/array.h index ca66213..6069832 100644 --- a/src/stable/array.h +++ b/src/stable/array.h @@ -10,7 +10,7 @@ #include #include "bu/exceptionbase.h" -#include "bu/archivebase.h" +#include "bu/archive.h" #include "bu/sharedcore.h" namespace Bu @@ -761,7 +761,7 @@ namespace Bu } template - ArchiveBase &operator<<( ArchiveBase &ar, + Archive &operator<<( Archive &ar, const Array &h ) { ar << h.getSize(); @@ -775,7 +775,7 @@ namespace Bu } template - ArchiveBase &operator>>(ArchiveBase &ar, Array &h ) + Archive &operator>>(Archive &ar, Array &h ) { h.clear(); long nSize; diff --git a/src/stable/hash.h b/src/stable/hash.h index 0c5bd9e..e515088 100644 --- a/src/stable/hash.h +++ b/src/stable/hash.h @@ -12,7 +12,7 @@ #include "bu/exceptionbase.h" #include "bu/list.h" #include "bu/util.h" -#include "bu/archivebase.h" +#include "bu/archive.h" #include "bu/sharedcore.h" namespace Bu @@ -1319,7 +1319,7 @@ namespace Bu template - ArchiveBase &operator<<( ArchiveBase &ar, const Hash &h ) + Archive &operator<<( Archive &ar, const Hash &h ) { long iSize = h.getSize(); ar << iSize; @@ -1334,7 +1334,7 @@ namespace Bu template - ArchiveBase &operator>>( ArchiveBase &ar, Hash &h ) + Archive &operator>>( Archive &ar, Hash &h ) { h.clear(); long nSize; diff --git a/src/stable/list.h b/src/stable/list.h index c1a5559..5ef7ce0 100644 --- a/src/stable/list.h +++ b/src/stable/list.h @@ -11,7 +11,7 @@ #include #include "bu/exceptionbase.h" #include "bu/sharedcore.h" -#include "bu/archivebase.h" +#include "bu/archive.h" #include "bu/heap.h" namespace Bu @@ -1042,7 +1042,7 @@ namespace Bu } template - ArchiveBase &operator<<( ArchiveBase &ar, const List &h ) + Archive &operator<<( Archive &ar, const List &h ) { ar << h.getSize(); for( typename List::const_iterator i = h.begin(); i != h.end(); i++ ) @@ -1054,7 +1054,7 @@ namespace Bu } template - ArchiveBase &operator>>( ArchiveBase &ar, List &h ) + Archive &operator>>( Archive &ar, List &h ) { h.clear(); long nSize; diff --git a/src/stable/string.cpp b/src/stable/string.cpp index ed4e58b..c36b768 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -1520,7 +1520,7 @@ Bu::String &Bu::operator<<( Bu::String &dst, const Bu::String &sIn ) return dst; } -Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Bu::String &s ) +Bu::Archive &Bu::operator<<( Bu::Archive &ar, const Bu::String &s ) { long n = s.getSize(); ar << n; @@ -1528,7 +1528,7 @@ Bu::ArchiveBase &Bu::operator<<( Bu::ArchiveBase &ar, const Bu::String &s ) return ar; } -Bu::ArchiveBase &Bu::operator>>( Bu::ArchiveBase &ar, Bu::String &s ) +Bu::Archive &Bu::operator>>( Bu::Archive &ar, Bu::String &s ) { long n; ar >> n; diff --git a/src/stable/string.h b/src/stable/string.h index c469580..b4768bb 100644 --- a/src/stable/string.h +++ b/src/stable/string.h @@ -14,7 +14,7 @@ #include "bu/util.h" #include "bu/sharedcore.h" #include "bu/exceptionbase.h" -#include "bu/archivebase.h" +#include "bu/archive.h" #include "bu/list.h" #include "bu/fmt.h" #include "bu/variant.h" @@ -1051,8 +1051,8 @@ namespace Bu return ret; } - ArchiveBase &operator<<( ArchiveBase &ar, const String &s ); - ArchiveBase &operator>>( ArchiveBase &ar, String &s ); + Archive &operator<<( Archive &ar, const String &s ); + Archive &operator>>( Archive &ar, String &s ); template uint32_t __calcHashCode( const T &k ); -- cgit v1.2.3