Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++

ESS: Extremely Simple Serialization for C++

Rate me:
Please Sign up or sign in to vote.
4.94/5 (14 votes)
26 Nov 2012BSD15 min read 86.9K   1.7K   68  
An article on persistent C++ objects. Includes several console mode test apps and an MFC GUI demo.
/*

	ESS Extremely Simple Serialization for C++

	http://www.novadsp.com/ess.htm

	Copyright (c) Jerry Evans, 2008-2009
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, 
	are permitted provided that the following conditions are met:

	* Redistributions of source code must retain the above copyright notice, 
	this list of conditions and the following disclaimer.

	* Redistributions in binary form must reproduce the above copyright notice, 
	this list of conditions and the following disclaimer in the documentation 
	and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 
	BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
	GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
	POSSIBILITY OF SUCH DAMAGE.

*/

//-----------------------------------------------------------------------------
// for CoCreateGuid
#ifdef _WIN32
#include <ObjBase.h>
#pragma comment( lib, "ole32.lib")
#endif

//-----------------------------------------------------------------------------
// for creating output files
#include <ostream>
#include <fstream>

#if _MSC_VER < 1400 
#pragma warning(disable:4702)	// unreachable code in xtree - VS2003 only
#endif

#include <debugout.h>

// main RTTI
#include "ess_rtti.h"
// main ESS include file
#include "ess_stream.h"
// for storing to XML
#include "ess_xml.h"
// for storing to binary
#include "ess_binary.h"


//-----------------------------------------------------------------------------
// generate a new GUID - sorry! Windows specific.
inline const GUID generate(GUID& guid)
{
#ifdef _WIN32
	CoCreateGuid(&guid);
#endif
	return guid;
}

//-----------------------------------------------------------------------------
// example hierarchy implemented using ESS macros
class C0
{
protected:
	C0()	{}
public:
	ESS_ROOT(C0)
	ESS_RTTI(C0,C0)
};

class C1 : public C0
{
public:
	ESS_RTTI(C1,C0)
};

class C2 : public C1
{
public:
	ESS_RTTI(C2,C0)
};

//-----------------------------------------------------------------------------
// tests - all inline code
#if 1
#include "test_intrinsics.h"
#include "test_derivation.h"
#include "test_registration.h"
#include "test_not_registered.h"
#include "test_binary.h"
#endif

//-----------------------------------------------------------------------------
// Simplest possible set of tests for registration and dynamic polymorphism
inline bool test()
{
	//
	bool ret = false;
	try
	{
		// typed registry with a CInfo as root
		ess::registry_manager registry;
		// register the 3 classes of interest
		registry 
			<< ess::class_registrar<C0,C0>("C0")
			<< ess::class_registrar<C1,C0>("C1")
			<< ess::class_registrar<C2,C0>("C2");

		// test using registry accessor
		ess::class_registry<C0>* pr = C0::get_registry();
		C0* p0 = pr->Create("C0");
		C0* p1 = pr->Create("C1");
		C0* p2 = pr->Create("C2");
		std::string n0 = p0->get_name();
		std::string n1 = p1->get_name();
		std::string n2 = p2->get_name();
		delete p0;
		delete p1;
		delete p2;

		p0 = ess::instance_from_name<C0>("C0");
		p1 = ess::instance_from_name<C0>("C1");
		p2 = ess::instance_from_name<C2>("C2");
		std::string nn0 = p0->get_name();
		std::string nn1 = p1->get_name();
		std::string nn2 = p2->get_name();
		delete p0;
		delete p1;
		delete p2;
		
		// error ... incomplete class
		C2* pWhat = ess::instance_from_name<C2>("C0");
		//
		std::string nwhat = pWhat->get_name();
		//
		delete pWhat;
		
		//
		ret = true;
	}
	catch (std::exception ex)
	{
		std::cout << ex.what() << std::endl;
	}
	catch (...)
	{
		std::cout << "Unknown exception!" << std::endl;
	}
	// true if we made it to the end of the test
	return ret;
}


//-----------------------------------------------------------------------------
int main(int /*argc*/,char* /*argv[]*/)
{
	test();
	// set the final flag to true - each unit test AND's its own result
	bool b = true;
	// test registration
	bool ret = test_registration();
	std::cout << "ESS test_registration(): " << (ret ? "passed" : "failed") << std::endl;
	b &= ret;
	// typed
	ret = test_typed_registration();
	std::cout << "ESS test_typed_registration(): " << (ret ? "passed" : "failed") << std::endl;
	b &= ret;
	
	// test storing all intrinsics
	ret = native_test();
	std::cout << "ESS native test(): " << (ret ? "passed" : "failed") << std::endl;
	b &= ret;
	// test derived pointers and polymorphic containers
	ret = pointer_test();
	std::cout << "ESS pointer_test(): " << ((ret) ? "passed" : "failed") << std::endl;
	b &= ret;
	// test binary storage
	ret = binary_test();
	std::cout << "ESS binary_test() " << (ret ? "passed" : "failed") << std::endl;
	b &= ret;
	//
	ret = test_not_registered();
	std::cout << "ESS test_not_registered() " << (ret ? "passed" : "failed") << std::endl;
	b &= ret;

	// return 0 if all passed.
	return (b ? 0 : -1);
}
//-----------------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Architect
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions