StringPrintf: A Typesafe printf Family for C++
By Stefan Woerthmuller, August 01, 2005
You can use StringPrintf, a typesafe version of the printf function family, with std::string and C strings.
August, 2005: StringPrintf: A Typesafe printf Family for C++
Listing 2
class CPrintfArg
{
public:
typedef enum
{
TYP_INT, TYP_UINT, TYP_FLOAT, TYP_CHAR, TYP_C_STRING,
TYP_STD_STRING, TYP_C_STRING_NONCONST, TYP_STD_STRING_NONCONST,
TYP_VOIDP,
}DATA_TYPES;
typedef enum
{
DELETE_PARM,
NO_DELETE
}DELETE_MODE;
CPrintfArg(const int x) : mTyp(TYP_INT) , mDeleteMode(NO_DELETE)
{
v.miVal = x;
};
CPrintfArg(const unsigned int x) : mTyp(TYP_UINT), mDeleteMode(NO_DELETE)
{
v.muVal = x;
};
CPrintfArg(const void *x) : mTyp(TYP_UINT), mDeleteMode(NO_DELETE)
{
v.muVal = (unsigned int)x;
};
CPrintfArg(const long x) : mTyp(TYP_INT) , mDeleteMode(NO_DELETE)
{
v.miVal = x;
};
CPrintfArg(const unsigned long x) : mTyp(TYP_UINT), mDeleteMode(NO_DELETE)
{
v.muVal = x;
};
CPrintfArg(const float x) : mTyp(TYP_FLOAT) , mDeleteMode(NO_DELETE)
{
v.mfVal = (double)x;
};
CPrintfArg(const double x) : mTyp(TYP_FLOAT) , mDeleteMode(NO_DELETE)
{
v.mfVal = x;
};
CPrintfArg(const char c) : mTyp(TYP_CHAR) , mDeleteMode(NO_DELETE)
{
v.mChar = c;
};
CPrintfArg(const char *p) : mTyp(TYP_C_STRING) , mDeleteMode(NO_DELETE)
{
v.mConstCharp = p;
};
CPrintfArg(const unsigned char *p) : mTyp(TYP_C_STRING),
mDeleteMode(NO_DELETE)
{
v.mConstCharp = (const char*)p;
};
CPrintfArg(const std::string &s) : mTyp(TYP_STD_STRING),
mDeleteMode(NO_DELETE)
{
v.mConstString = &s;
};
// Non cost constructors for objects to delete on destruction
CPrintfArg(char *p, DELETE_MODE mode = NO_DELETE) :
mTyp(TYP_C_STRING_NONCONST) , mDeleteMode(mode)
{
v.mCharp = p;
};
CPrintfArg(unsigned char *p, DELETE_MODE mode = NO_DELETE) :
mTyp(TYP_C_STRING_NONCONST) , mDeleteMode(mode)
{
v.mCharp = (char*)p;
};
CPrintfArg(std::string &s, DELETE_MODE mode = NO_DELETE) :
mTyp(TYP_STD_STRING_NONCONST), mDeleteMode(mode)
{
v.mString = &s;
};
~CPrintfArg();
const std::string Out(const std::string &fmt) const ;
private:
DATA_TYPES mTyp;
DELETE_MODE mDeleteMode;
union
{
long miVal;
unsigned long muVal;
double mfVal;
char mChar;
char *mCharp;
std::string *mString;
const char *mConstCharp;
const std::string *mConstString;
}v;
};