From 63cc5adefd0e9f00b0ee0592cddb45a2ea1533c7 Mon Sep 17 00:00:00 2001 From: jbeder Date: Tue, 10 Nov 2009 21:23:52 +0000 Subject: [PATCH] Replaced conversion macros with SFINAE --- CMakeLists.txt | 2 +- include/conversion.h | 50 +++++++++++++++++++++++++++----------------- test/parsertests.cpp | 30 ++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ab7746..9f21473 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ if(IPHONE) endif(IPHONE) if(CMAKE_COMPILER_IS_GNUCC) - set(CMAKE_CXX_FLAGS "-O2 -Wall -pedantic -Wextra ${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS "-O2 -Wall -Wextra -pedantic -Wno-long-long ${CMAKE_CXX_FLAGS}") endif(CMAKE_COMPILER_IS_GNUCC) set(YAML_CPP_VERSION_MAJOR "0") diff --git a/include/conversion.h b/include/conversion.h index a1aadce..4cb0803 100644 --- a/include/conversion.h +++ b/include/conversion.h @@ -18,26 +18,38 @@ namespace YAML bool Convert(const std::string& input, bool& output); bool Convert(const std::string& input, _Null& output); -#define YAML_MAKE_STREAM_CONVERT(type) \ - inline bool Convert(const std::string& input, type& output) { \ - std::stringstream stream(input); \ - stream.unsetf(std::ios::dec); \ - return stream >> output; \ + template + struct is_numeric { enum { value = false }; }; + + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + template <> struct is_numeric { enum { value = true }; }; + + + template + struct enable_if; + + template + struct enable_if { + typedef R type; + }; + + template + inline bool Convert(const std::string& input, T& output, typename enable_if, T>::type * = 0) { + std::stringstream stream(input); + stream.unsetf(std::ios::dec); + return stream >> output; } - - YAML_MAKE_STREAM_CONVERT(char) - YAML_MAKE_STREAM_CONVERT(unsigned char) - YAML_MAKE_STREAM_CONVERT(int) - YAML_MAKE_STREAM_CONVERT(unsigned int) - YAML_MAKE_STREAM_CONVERT(short) - YAML_MAKE_STREAM_CONVERT(unsigned short) - YAML_MAKE_STREAM_CONVERT(long) - YAML_MAKE_STREAM_CONVERT(unsigned long) - YAML_MAKE_STREAM_CONVERT(float) - YAML_MAKE_STREAM_CONVERT(double) - YAML_MAKE_STREAM_CONVERT(long double) - -#undef YAML_MAKE_STREAM_CONVERT } #endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/test/parsertests.cpp b/test/parsertests.cpp index 4c159bc..6bf8642 100644 --- a/test/parsertests.cpp +++ b/test/parsertests.cpp @@ -118,7 +118,7 @@ namespace Test inputScalar = "http://example.com/foo#bar"; desiredOutput = "http://example.com/foo#bar"; } - + bool SimpleSeq() { std::string input = @@ -620,7 +620,6 @@ namespace Test std::stringstream stream(input); YAML::Parser parser(stream); YAML::Node doc; - std::string output; parser.GetNextDocument(doc); if(doc.size() != 2) @@ -632,6 +631,32 @@ namespace Test return true; } + + bool Bases() + { + std::string input = + "- 15\n" + "- 0x10\n" + "- 030\n" + "- 0xffffffff\n"; + + std::stringstream stream(input); + YAML::Parser parser(stream); + YAML::Node doc; + + parser.GetNextDocument(doc); + if(doc.size() != 4) + return false; + if(doc[0] != 15) + return false; + if(doc[1] != 0x10) + return false; + if(doc[2] != 030) + return false; + if(doc[3] != 0xffffffff) + return false; + return true; + } } namespace { @@ -892,6 +917,7 @@ namespace Test RunParserTest(&Parser::ExplicitEndDoc, "explicit end doc", passed, total); RunParserTest(&Parser::MultipleDocsWithSomeExplicitIndicators, "multiple docs with some explicit indicators", passed, total); RunParserTest(&Parser::BlockKeyWithNullValue, "block key with null value", passed, total); + RunParserTest(&Parser::Bases, "bases", passed, total); RunEncodingTest(&EncodeToUtf8, false, "UTF-8, no BOM", passed, total); RunEncodingTest(&EncodeToUtf8, true, "UTF-8 with BOM", passed, total);