Browse Source

Ensure all invoked types define a string converter

Michael Ragazzon 1 year ago
parent
commit
7335eed838
2 changed files with 34 additions and 0 deletions
  1. 1 0
      Include/RmlUi/Core/TypeConverter.h
  2. 33 0
      Include/RmlUi/Core/TypeConverter.inl

+ 1 - 0
Include/RmlUi/Core/TypeConverter.h

@@ -35,6 +35,7 @@
 #include "Types.h"
 #include "Types.h"
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <type_traits>
 
 
 namespace Rml {
 namespace Rml {
 
 

+ 33 - 0
Include/RmlUi/Core/TypeConverter.inl

@@ -385,6 +385,24 @@ public:
 	}
 	}
 };
 };
 
 
+template <>
+class TypeConverter<void*, String> {
+public:
+	static bool Convert(void* const& src, String& dest) { return FormatString(dest, 32, "%p", src) > 0; }
+};
+
+template <>
+class TypeConverter<ScriptInterface*, String> {
+public:
+	static bool Convert(ScriptInterface* const& src, String& dest) { return FormatString(dest, 32, "%p", static_cast<void*>(src)) > 0; }
+};
+
+template <>
+class TypeConverter<char, String> {
+public:
+	static bool Convert(const char& src, String& dest) { return FormatString(dest, 32, "%c", src) > 0; }
+};
+
 template <typename SourceType, typename InternalType, int count>
 template <typename SourceType, typename InternalType, int count>
 class TypeConverterVectorString {
 class TypeConverterVectorString {
 public:
 public:
@@ -423,6 +441,21 @@ VECTOR_STRING_CONVERTER(Vector4i, int, 4);
 VECTOR_STRING_CONVERTER(Vector4f, float, 4);
 VECTOR_STRING_CONVERTER(Vector4f, float, 4);
 VECTOR_STRING_CONVERTER(Colourf, float, 4);
 VECTOR_STRING_CONVERTER(Colourf, float, 4);
 VECTOR_STRING_CONVERTER(Colourb, byte, 4);
 VECTOR_STRING_CONVERTER(Colourb, byte, 4);
+
+template <typename SourceType>
+class TypeConverter<SourceType, String> {
+public:
+	template <typename...>
+	struct AlwaysFalse : std::integral_constant<bool, false> {};
+
+	static bool Convert(const SourceType& /*src*/, String& /*dest*/)
+	{
+		static_assert(AlwaysFalse<SourceType>{},
+			"The type converter was invoked on a type without a string converter, please define a converter from SourceType to String.");
+		return false;
+	}
+};
+
 #undef PASS_THROUGH
 #undef PASS_THROUGH
 #undef BASIC_CONVERTER
 #undef BASIC_CONVERTER
 #undef BASIC_CONVERTER_BOOL
 #undef BASIC_CONVERTER_BOOL