diff --git a/cwNumericConvert.h b/cwNumericConvert.h index d110040..c1a1b52 100644 --- a/cwNumericConvert.h +++ b/cwNumericConvert.h @@ -136,12 +136,21 @@ namespace cw } return kOkRC; } - template< typename T > - void number_to_string( const T& v, const char* fmt, char* buf, int bufN ) - { snprintf(buf,bufN,fmt,v); } + int number_to_string( const T& v, char* buf, int bufN, const char* fmt=nullptr ) + { return snprintf(buf,bufN,fmt,v); } + template < > inline + int number_to_string( const int& v, char* buf, int bufN, const char* fmt ) { return snprintf(buf,bufN,fmt==nullptr ? "%i" : fmt, v); } + + template < > inline + int number_to_string( const unsigned& v, char* buf, int bufN, const char* fmt ) { return snprintf(buf,bufN,fmt==nullptr ? "%i" : fmt, v); } + + template < > inline + int number_to_string( const double& v, char* buf, int bufN, const char* fmt ) { return snprintf(buf,bufN,fmt==nullptr ? "%f" : fmt, v); } + + } #endif diff --git a/cwObject.cpp b/cwObject.cpp index 8b36822..974c9bc 100644 --- a/cwObject.cpp +++ b/cwObject.cpp @@ -57,18 +57,18 @@ namespace cw void _objTypeNullToString( object_t* o, char* buf, unsigned bufN ) { snprintf(buf,bufN,"%s","NULL"); } - void _objTypeCharToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.c,"%c",buf,bufN); } - void _objTypeInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i8,"%i",buf,bufN); } - void _objTypeUInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u8,"%i",buf,bufN); } - void _objTypeInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i16,"%i",buf,bufN); } - void _objTypeUInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u16,"%i",buf,bufN); } - void _objTypeInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i32,"%i",buf,bufN); } - void _objTypeUInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u32,"%i",buf,bufN); } - void _objTypeInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i64,"%i",buf,bufN); } - void _objTypeUInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u64,"%i",buf,bufN); } - void _objTypeBoolToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.b,"%i",buf,bufN); } - void _objTypeFloatToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.f,"%f",buf,bufN); } - void _objTypeDoubleToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.d,"%f",buf,bufN); } + void _objTypeCharToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.c,buf,bufN,"%c"); } + void _objTypeInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i8,buf,bufN,"%i"); } + void _objTypeUInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u8,buf,bufN,"%i"); } + void _objTypeInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i16,buf,bufN,"%i"); } + void _objTypeUInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u16,buf,bufN,"%i"); } + void _objTypeInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i32,buf,bufN,"%i"); } + void _objTypeUInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u32,buf,bufN,"%i"); } + void _objTypeInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i64,buf,bufN,"%i"); } + void _objTypeUInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u64,buf,bufN,"%i"); } + void _objTypeBoolToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.b,buf,bufN,"%i"); } + void _objTypeFloatToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.f,buf,bufN,"%f"); } + void _objTypeDoubleToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.d,buf,bufN,"%f"); } void _objTypeStringToString( object_t* o, char* buf, unsigned bufN ) { snprintf(buf,bufN,"%s",o->u.str); }