From 81a122e66a08518083037f9393d0d1049b15f554 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 8 May 2012 08:10:49 +0100 Subject: [PATCH] Factored toString overloads into their own file --- include/internal/catch_capture.hpp | 102 +-------------- include/internal/catch_tostring.hpp | 121 ++++++++++++++++++ .../CatchSelfTest.xcodeproj/project.pbxproj | 4 +- 3 files changed, 125 insertions(+), 102 deletions(-) create mode 100644 include/internal/catch_tostring.hpp diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp index d3c03c73..fa5f8633 100644 --- a/include/internal/catch_capture.hpp +++ b/include/internal/catch_capture.hpp @@ -12,6 +12,7 @@ #ifndef TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED #define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED +#include "catch_tostring.hpp" #include "catch_resultinfo.hpp" #include "catch_result_type.h" #include "catch_interfaces_capture.h" @@ -24,107 +25,6 @@ namespace Catch { -namespace Detail -{ - struct NonStreamable { - template NonStreamable( const T& ){} - }; - - // If the type does not have its own << overload for ostream then - // this one will be used instead - inline std::ostream& operator << ( std::ostream& ss, NonStreamable ){ - return ss << "{?}"; - } - - template - inline std::string makeString( const T& value ) { - std::ostringstream oss; - oss << value; - return oss.str(); - } - - template - inline std::string makeString( T* p ) { - if( !p ) - return INTERNAL_CATCH_STRINGIFY( NULL ); - std::ostringstream oss; - oss << p; - return oss.str(); - } - - template - inline std::string makeString( const T* p ) { - if( !p ) - return INTERNAL_CATCH_STRINGIFY( NULL ); - std::ostringstream oss; - oss << p; - return oss.str(); - } - -}// end namespace Detail - -template -std::string toString( const T& value ) { - return Detail::makeString( value ); -} - -// Shortcut overloads - -inline std::string toString( const std::string& value ) { - return "\"" + value + "\""; -} - -inline std::string toString( const std::wstring& value ) { - std::ostringstream oss; - oss << "\""; - for(size_t i = 0; i < value.size(); ++i ) - oss << static_cast( value[i] <= 0xff ? value[i] : '?'); - oss << "\""; - return oss.str(); -} - -inline std::string toString( const char* const value ) { - return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); -} - -inline std::string toString( char* const value ) { - return Catch::toString( static_cast( value ) ); -} - -inline std::string toString( int value ) { - std::ostringstream oss; - oss << value; - return oss.str(); -} - -inline std::string toString( unsigned int value ) { - std::ostringstream oss; - if( value > 8192 ) - oss << "0x" << std::hex << value; - else - oss << value; - return oss.str(); -} - -inline std::string toString( unsigned long value ) { - std::ostringstream oss; - if( value > 8192 ) - oss << "0x" << std::hex << value; - else - oss << value; - return oss.str(); -} - -inline std::string toString( const double value ) { - std::ostringstream oss; - oss << value; - return oss.str(); -} - -inline std::string toString( bool value ) { - return value ? "true" : "false"; -} - struct TestFailureException{}; struct DummyExceptionType_DontUse{}; diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp new file mode 100644 index 00000000..f6ee6c36 --- /dev/null +++ b/include/internal/catch_tostring.hpp @@ -0,0 +1,121 @@ +/* + * Created by Phil on 8/5/2012. + * Copyright 2012 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED +#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED + +#include "catch_common.h" +#include + +namespace Catch +{ +namespace Detail +{ + struct NonStreamable { + template NonStreamable( const T& ){} + }; + + // If the type does not have its own << overload for ostream then + // this one will be used instead + inline std::ostream& operator << ( std::ostream& ss, NonStreamable ){ + return ss << "{?}"; + } + + template + inline std::string makeString( const T& value ) { + std::ostringstream oss; + oss << value; + return oss.str(); + } + + template + inline std::string makeString( T* p ) { + if( !p ) + return INTERNAL_CATCH_STRINGIFY( NULL ); + std::ostringstream oss; + oss << p; + return oss.str(); + } + + template + inline std::string makeString( const T* p ) { + if( !p ) + return INTERNAL_CATCH_STRINGIFY( NULL ); + std::ostringstream oss; + oss << p; + return oss.str(); + } + +} // end namespace Detail + +/// \brief converts any type to a string +/// +/// The default template forwards on to ostringstream - except when an +/// ostringstream overload does not exist - in which case it attempts to detect +/// that and writes {?}. +/// Overload (not specialise) this template for custom typs that you don't want +/// to provide an ostream overload for. +template +std::string toString( const T& value ) { + return Detail::makeString( value ); +} + +// Built in overloads + +inline std::string toString( const std::string& value ) { + return "\"" + value + "\""; +} + +inline std::string toString( const std::wstring& value ) { + std::ostringstream oss; + oss << "\""; + for(size_t i = 0; i < value.size(); ++i ) + oss << static_cast( value[i] <= 0xff ? value[i] : '?'); + oss << "\""; + return oss.str(); +} + +inline std::string toString( const char* const value ) { + return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); +} + +inline std::string toString( char* const value ) { + return Catch::toString( static_cast( value ) ); +} + +inline std::string toString( int value ) { + std::ostringstream oss; + oss << value; + return oss.str(); +} + +inline std::string toString( unsigned long value ) { + std::ostringstream oss; + if( value > 8192 ) + oss << "0x" << std::hex << value; + else + oss << value; + return oss.str(); +} + +inline std::string toString( unsigned int value ) { + return toString( (unsigned long)value ); +} + +inline std::string toString( const double value ) { + std::ostringstream oss; + oss << value; + return oss.str(); +} + +inline std::string toString( bool value ) { + return value ? "true" : "false"; +} + +} // end namespace Catch + +#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED diff --git a/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj b/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj index 4df06ec3..92672ce3 100644 --- a/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj +++ b/projects/XCode4/CatchSelfTest/CatchSelfTest.xcodeproj/project.pbxproj @@ -85,11 +85,12 @@ 4A6D0C67149B3E3D00DB3EAA /* catch_reporter_junit.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_junit.hpp; sourceTree = ""; }; 4A6D0C68149B3E3D00DB3EAA /* catch_reporter_xml.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_xml.hpp; sourceTree = ""; }; 4A7ADB4314F631E10094FE10 /* catch_totals.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_totals.hpp; sourceTree = ""; }; + 4A9D84B11558FC0400FBB209 /* catch_tostring.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_tostring.hpp; sourceTree = ""; }; 4AB1C73514F97BDA00F31DF7 /* catch_console_colour_impl.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_console_colour_impl.hpp; sourceTree = ""; }; 4AB1C73714F97C1300F31DF7 /* catch_console_colour.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_console_colour.hpp; sourceTree = ""; }; 4AB77CB51551AEA200857BF0 /* catch_ptr.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_ptr.hpp; sourceTree = ""; }; 4AB77CB71553B72B00857BF0 /* catch_section_info.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_section_info.hpp; sourceTree = ""; }; - 4AB77CB81553BB3800857BF0 /* catch_running_test.hpp */ = {isa = PBXFileReference; path = catch_running_test.hpp; sourceTree = ""; }; + 4AB77CB81553BB3800857BF0 /* catch_running_test.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_running_test.hpp; sourceTree = ""; }; 4AE1840A14EE4F230066340D /* catch_self_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = catch_self_test.cpp; path = ../../../SelfTest/catch_self_test.cpp; sourceTree = ""; }; /* End PBXFileReference section */ @@ -201,6 +202,7 @@ 4AB77CB51551AEA200857BF0 /* catch_ptr.hpp */, 4AB77CB71553B72B00857BF0 /* catch_section_info.hpp */, 4AB77CB81553BB3800857BF0 /* catch_running_test.hpp */, + 4A9D84B11558FC0400FBB209 /* catch_tostring.hpp */, ); name = internal; path = ../../../../include/internal;