diff options
| author | Phil Nash | 2014-01-07 17:25:27 +0000 |
|---|---|---|
| committer | Phil Nash | 2014-01-07 17:25:27 +0000 |
| commit | e091018514bc7d63e672b360aff0df1340f5ce13 (patch) | |
| tree | 89f40875f187634c4c01142f07e314c2909127c1 /include | |
| parent | 440a47011fc5052d15f82a82f8704fc5320f67f7 (diff) | |
| download | catch3-e091018514bc7d63e672b360aff0df1340f5ce13.tar.gz | |
Fixes toString() for function pointers and added support for member function pointers.
- thanks to Malcolm Noyes for supplying sample code on which the solution here was based
Diffstat (limited to 'include')
| -rw-r--r-- | include/internal/catch_tostring.hpp | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index ae78e1f9..b463e4ff 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -79,6 +79,25 @@ namespace Detail { } }; + // For display purposes only. + // Does not consider endian-ness + template<typename T> + std::string rawMemoryToString( T value ) { + union + { + T value; + unsigned char bytes[sizeof(T)]; + } valueAsBuffer; + + valueAsBuffer.value = value; + + std::ostringstream oss; + oss << "0x"; + for( unsigned char* cp = valueAsBuffer.bytes; cp < valueAsBuffer.bytes+sizeof(T); ++cp ) + oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)*cp; + return oss.str(); + } + } // end namespace Detail template<typename T> @@ -94,9 +113,18 @@ struct StringMaker<T*> { static std::string convert( U* p ) { if( !p ) return INTERNAL_CATCH_STRINGIFY( NULL ); - std::ostringstream oss; - oss << "0x" << std::hex << reinterpret_cast<unsigned long>( p ); - return oss.str(); + else + return Detail::rawMemoryToString( p ); + } +}; + +template<typename R, typename C> +struct StringMaker<R C::*> { + static std::string convert( R C::* p ) { + if( !p ) + return INTERNAL_CATCH_STRINGIFY( NULL ); + else + return Detail::rawMemoryToString( p ); } }; |