Click here to Skip to main content
15,892,059 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 87.2K   1.7K   68  
An article on persistent C++ objects. Includes several console mode test apps and an MFC GUI demo.
/*

	Jerry 11-JUN-96

	Quickie debug trace class 

	This technique is thread safe as there are no shared buffers.

*/

#ifndef DEBUGOUT_H
#define DEBUGOUT_H

#include <windows.h>
#include <stdarg.h>
#include <string>


//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

class CSysError
{
	public:

	CSysError(DWORD dwError)
	{
		LPVOID lpMsgBuf;

		DWORD dw = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
						NULL,
						dwError,
						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
						(LPTSTR) &lpMsgBuf,
						0,
						NULL);
		if (dw)
		{
			m_cs = (const char*) lpMsgBuf;
		}

		// Free the buffer.
		LocalFree(lpMsgBuf);
	}

	const char* data()
	{ return m_cs.data(); }

	protected:

	std::string m_cs;
};

//----------------------------------------------------------------------------
//
//	Example:
//
//	DBMSG(CMsg("A fatal error has occurred. File %s code %d",pszName,nError))
//
//
//----------------------------------------------------------------------------

#if defined(USE_CLR)
#pragma managed(push, off)
#endif

#ifdef _DEBUG

class CMsg
{
	//
	enum { _MAX_CHARS = 8 * 1024, _OFFSET = 32 };
	// buffer for strings
	char m_szText[_MAX_CHARS];

	public:

	// standard hard-coded format string
	CMsg(const char* pszFormat,...)
	{
		va_list ap;
		va_start(ap,pszFormat);
		// to ensure we do not get any buffer overruns ...
#if _MSC_VER >= 1400
		_vsnprintf_s(&m_szText[0],sizeof(m_szText),_TRUNCATE,pszFormat,ap);
#else
		_vsnprintf(&m_szText[0],sizeof(m_szText),pszFormat,ap);
#endif		
		va_end(ap);
	}
	
	// conversion operator for use by static
	// OutputDebugMessage
	const char* c_str() const
	{ 
		return m_szText; 
	}

	// central handler marshalls strings and writes
	// the string to the debug console, log file etc.
	void OutputDebugMessage(const char* pszFile, int nLine,const CMsg& msg)
	{
		const int _MAX_DIGITS = 32;
		char buffer[_MAX_DIGITS];
		const char* pszName = strrchr(pszFile,'\\');
		if (pszName)
		{
			pszName++;
		}
		else
		{
			pszName = pszFile;
		}
		//
		std::string str(pszName);
		str += "(";
#if _MSC_VER >= 1400
		_itoa_s(nLine,buffer,_MAX_DIGITS,10);
		str += buffer;
#else
		str += _itoa(nLine,buffer,10);
#endif		
		str += ") : ";
		OutputDebugString(str.c_str());
		OutputDebugString(msg.c_str());
		OutputDebugString("\n");
	}

};

//----------------------------------------------------------------------------
//
//	Macros to wrap calls to the real handler. This flavour
//	automatically adds file and line info to the debug string
//
//----------------------------------------------------------------------------

#define DBMSG(msg) msg.OutputDebugMessage(__FILE__,__LINE__,msg)

#else

class CMsg
{
	public:

	// standard hard-coded format string
	CMsg(const char*,...) {}
};

#define DBMSG(msg) 

#endif

#if defined(USE_CLR)
#pragma managed(pop)
#endif

class CIntervalTimer
{
	public:

	CIntervalTimer()
	{ m_Time = GetTickCount(); }

	~CIntervalTimer()
	{ DBMSG(CMsg("Interval is %ld ",GetTickCount() - m_Time)); }

	protected:

	DWORD m_Time;
};


#endif

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