Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C++

Extremely Efficient Type-safe printf Library

Rate me:
Please Sign up or sign in to vote.
4.85/5 (22 votes)
17 Aug 2011CPOL17 min read 100.4K   595   38  
Introduces a fast and type-safe parameter rendering library (type-safe printf)
// Type-safe C++0x printf library
// Copyright (c) 2011 HHD Software Ltd. http://www.hhdsoftware.com
// Written by Alexander Bessonov

// Distributed under the terms of The Code Project Open License (http://www.codeproject.com)

#pragma once

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4201) // nonstandard extension used
#endif

namespace ts_printf
{
	namespace _details
	{
		struct BaseFormatter {};
		struct BasePolicy {};

		// Format flags
		enum
		{
			FF_NORMAL				=0x00,
			FF_ELLIPSIS				=0x01,	// add … when clipping
			FF_ALWAYSPLUS		=0x02,	// always add + to positive numbers
			FF_LOWHEX				=0x04,	// use lowercase
			FF_FORCECHAR		=0x08,	// display integer as character
			FF_THOUSANDS		=0x10,	// display thousand separators
			FF_TIMEFORMAT		=0x20,	// time format has been specified
			FF_NUMBERPREFIX	=0x40,	// "0x" or "0X" must be rendered before hexadecimal number
			
			FF_FORMATSTRING	=0x80,	// a reference to an original format string
		};

		enum
		{
			AlignLeft,
			AlignCenter,
			AlignRight,
		};

		template<class CharT>
		struct FormatDesc
		{
			unsigned long flags;				// see FF_* constants. Only BYTE is currently used, but DWORD is chosen for better structure alignment
			union
			{
				struct
				{
					const CharT *tmfBegin;	// the beginning of the time format string
					const CharT *tmfEnd;		// the end of the time format string

					unsigned short parameter;	// index of the parameter
					unsigned short align;
					unsigned short minwidth;
					unsigned short maxwidth;
					unsigned short precision;		// used for floating-point only
					CharT pad;			// character used for padding
					char base;				// used for integer only
				};
				struct
				{
					const CharT *begin;
					const CharT *end;
					bool bHasEscapedChars;
				};
			};

			FormatDesc() throw() 
				: align(AlignLeft), minwidth(0), maxwidth(0), precision(0), base(10), flags(FF_NORMAL), parameter(0), pad((CharT) ' ')
			{
			}

			template<class Range>
			FormatDesc(const Range &range,bool bHasEscapedChars_) throw()
				: flags(FF_FORMATSTRING), begin(std::addressof(*boost::begin(range))), end(begin+boost::size(range)), bHasEscapedChars(bHasEscapedChars_)
			{
			}
		};
	};
};

#if defined(_MSC_VER)
#pragma warning(pop)
#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 Code Project Open License (CPOL)


Written By
Software Developer (Senior) HHD Software Ltd.
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions