mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	Add stringification methods for CLR objects
This commit is contained in:
		 David Aue
					David Aue
				
			
				
					committed by
					
						 Martin Hořeňovský
						Martin Hořeňovský
					
				
			
			
				
	
			
			
			 Martin Hořeňovský
						Martin Hořeňovský
					
				
			
						parent
						
							319bddd5b8
						
					
				
				
					commit
					dfb83f20e9
				
			| @@ -75,6 +75,19 @@ namespace Catch { | ||||
|             return convertUnknownEnumToString( value ); | ||||
|         } | ||||
|  | ||||
|  | ||||
| #if defined(_MANAGED) | ||||
|         //! Convert a CLR string to a utf8 std::string | ||||
|         template<typename T> | ||||
|         std::string clrReferenceToString( T^ ref ) { | ||||
|             if (ref == nullptr) | ||||
|                 return std::string("null"); | ||||
|             auto bytes = System::Text::Encoding::UTF8->GetBytes(ref->ToString()); | ||||
|             cli::pin_ptr<System::Byte> p = &bytes[0]; | ||||
|             return std::string(reinterpret_cast<char const *>(p), bytes->Length); | ||||
|         } | ||||
| #endif | ||||
|  | ||||
|     } // namespace Detail | ||||
|  | ||||
|  | ||||
| @@ -112,6 +125,13 @@ namespace Catch { | ||||
|             return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<E>::type>(e)); | ||||
|         } | ||||
|  | ||||
| #if defined(_MANAGED) | ||||
|         template <typename T> | ||||
|         std::string stringify( T^ e ) { | ||||
|             return ::Catch::StringMaker<T^>::convert(e); | ||||
|         } | ||||
| #endif | ||||
|  | ||||
|     } // namespace Detail | ||||
|  | ||||
|     // Some predefined specializations | ||||
| @@ -245,6 +265,15 @@ namespace Catch { | ||||
|         } | ||||
|     }; | ||||
|  | ||||
| #if defined(_MANAGED) | ||||
|     template <typename T> | ||||
|     struct StringMaker<T^> { | ||||
|         static std::string convert( T^ ref ) { | ||||
|             return ::Catch::Detail::clrReferenceToString(ref); | ||||
|         } | ||||
|     }; | ||||
| #endif | ||||
|  | ||||
|     namespace Detail { | ||||
|         template<typename InputIterator> | ||||
|         std::string rangeToString(InputIterator first, InputIterator last) { | ||||
| @@ -374,6 +403,13 @@ namespace Catch { | ||||
|             !std::is_same<decltype(end(std::declval<T>())), not_this_one>::value; | ||||
|     }; | ||||
|  | ||||
| #if defined(_MANAGED) // Managed types are never ranges | ||||
|     template <typename T> | ||||
|     struct is_range<T^> { | ||||
|         static const bool value = false; | ||||
|     }; | ||||
| #endif | ||||
|  | ||||
|     template<typename Range> | ||||
|     std::string rangeToString( Range const& range ) { | ||||
|         return ::Catch::Detail::rangeToString( begin( range ), end( range ) ); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user