[Cppunit-cvs] cppunit2/include/cpput stringize.h,1.6,1.7
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-09-06 07:31:52
|
Update of /cvsroot/cppunit/cppunit2/include/cpput In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16321/include/cpput Modified Files: stringize.h Log Message: * fixed compilation without RTTI * revised stringize implementation to allow an additional customization point by overloading toString(). * added test and corrected bug in serialization/packets implementation. Index: stringize.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpput/stringize.h,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** stringize.h 4 Jul 2005 08:11:25 -0000 1.6 --- stringize.h 6 Sep 2005 07:31:42 -0000 1.7 *************** *** 7,12 **** --- 7,40 ---- # include <cpptl/stringtools.h> + /* How to: + * - define a conversion function for a string type: + * overload the function std::string getStdString( MyStringType ) + * + * namespace CppUT { + * inline std::string getStdString( const MyStringType &s ) { + * return s.str(); + * } } + * + */ + + + + // 1) Is it a string ? + // std::string convertToString( ptr ) => Yes, convert to string & add quotes. + // NoToStringConversion convertToString( .. ) + // stringize( impl::stringize( value, convertToString( ptr ) ) ); + // 2) Is there an overloaded toString() function for that type + // std::string toString( ptr ); => Yes, convert to string using the function + // NoToStringConversion toString( ... ); + // 3) Fall back DefaultStringizer. + + namespace CppUT { + /** A generic functor that can be used to convert a value into a string. + * It is used as default argument by function template that allow the user to + * pass a specific functor to convert data to string. + * \warning This class template should not be specialized. See stringize() for detail. + */ template<class ValueType> struct DefaultStringizer *************** *** 19,25 **** // ------------------- convertToString ------------------------------- ! // User should overload convertToString() to support their own string types. // ! // IMPORTANT: convertToString() must never be called with a 'by value' parameter. // Passing by value would result in undefined behavior for non string type parameter // 'eaten' by NotConvertibleToStdString convertToString( ... ). --- 47,55 ---- // ------------------- convertToString ------------------------------- ! // User should overload getStdString() to support their own string types. // ! // IMPORTANT: to handle compiler that do not support function template ordering ! // (CPPTL_NO_FUNCTION_TEMPLATE_ORDERING), such as vc++ 6.0. ! // getStdString() must never be called with a 'by value' parameter. // Passing by value would result in undefined behavior for non string type parameter // 'eaten' by NotConvertibleToStdString convertToString( ... ). *************** *** 31,59 **** // const StringType2 &str ) // convertToString will only be passed reference. // { ! // RegEx regex( CppUT::convertToString(pattern ) ); ! // CppUT::checkTrue( regex.matched( CppUT::convertToString( str ) ) ); // } struct NotConvertibleToStdString {}; ! inline std::string convertToString( const char *cstr ) { return std::string( cstr ); } ! inline std::string convertToString( const std::string &s ) { return s; } ! inline std::string convertToString( const CppTL::ConstString &s ) { return s.c_str(); } ! inline NotConvertibleToStdString convertToString( ... ) { return NotConvertibleToStdString(); } --- 61,103 ---- // const StringType2 &str ) // convertToString will only be passed reference. // { ! // RegEx regex( CppUT::convertToString(&pattern ) ); ! // CppUT::checkTrue( regex.matched( CppUT::convertToString( &str ) ) ); // } struct NotConvertibleToStdString {}; ! inline std::string getStdString( const char *cstr ) { return std::string( cstr ); } ! inline std::string getStdString( const std::string &s ) { return s; } ! inline std::string getStdString( const CppTL::ConstString &s ) { return s.c_str(); } ! #ifdef CPPTL_NO_FUNCTION_TEMPLATE_ORDERING ! inline NotConvertibleToStdString getStdString( ... ) { return NotConvertibleToStdString(); } + #else + template<class T> + inline NotConvertibleToStdString getStdString( const T & ) + { + return NotConvertibleToStdString(); + } + #endif + + template <class StringType> + inline std::string convertToString( const StringType &s ) + { + return getStdString( s ); // if you get a compilation error on this call, then getStdString() has not been overloaded for your string type. + } *************** *** 64,72 **** // std::string defaultStringize( const T &value ); namespace Impl { template<typename ValueType> ! std::string stringize( const ValueType &value, NotConvertibleToStdString ) { return defaultStringize( value ); --- 108,130 ---- // std::string defaultStringize( const T &value ); + struct NoToStringOverload {}; + + #ifdef CPPTL_NO_FUNCTION_TEMPLATE_ORDERING + inline NoToStringOverload toString( ... ) + { + return NoToStringOverload(); + } + #else + template<class T> + inline NoToStringOverload toString( const T & ) + { + return NoToStringOverload(); + } + #endif namespace Impl { template<typename ValueType> ! std::string toStringStringize( const ValueType &value, NoToStringOverload ) { return defaultStringize( value ); *************** *** 74,77 **** --- 132,147 ---- template<typename ValueType> + std::string toStringStringize( const ValueType &value, const std::string &str ) + { + return str; + } + + template<typename ValueType> + std::string stringize( const ValueType &value, NotConvertibleToStdString ) + { + return toStringStringize( value, toString( value ) ); + } + + template<typename ValueType> std::string stringize( const ValueType &value, const std::string &str ) { *************** *** 84,88 **** std::string stringize( const ValueType &value ) { ! return Impl::stringize( value, convertToString(value) ); } --- 154,158 ---- std::string stringize( const ValueType &value ) { ! return Impl::stringize( value, getStdString(&value) ); } *************** *** 100,108 **** namespace CppUT { ! template<class ValueType> ! struct StringizeTraits { - static std::string stringize( const ValueType &value ) - { # ifndef CPPTL_NO_SSTREAM std::ostringstream os; --- 170,176 ---- namespace CppUT { ! template<typename ValueType> ! std::string defaultStringize( const ValueType &value ) { # ifndef CPPTL_NO_SSTREAM std::ostringstream os; *************** *** 114,124 **** return std::string( os.str(), os.pcount() ); # endif - } - }; - - template<typename ValueType> - std::string defaultStringize( const ValueType &value ) - { - return StringizeTraits<ValueType>::stringize( value ); } --- 182,185 ---- |