From 38ff8576f2fd4efeed98cadbc6bc1a48e05582e6 Mon Sep 17 00:00:00 2001 From: David Wagner Date: Tue, 23 Feb 2016 19:14:00 +0100 Subject: [PATCH] Integer template specializations: use builtin types instead of stdint Template resolution is tied to name mangling. Since we can't know for sure which builtin type is used for each of int64_t, uint32_t, etc. we can't rely on these typedefs. Builtin types, however, are reliable. As an exemple: on some ABIs, size_t can be a long long but uint64_t a mere long which means that for this particular platform, a call with a size_t argument will not match a template instantiation with a uint64_t. Signed-off-by: David Wagner --- utility/convert.hpp | 49 ++++++++++++++++++++---------------- xmlserializer/XmlElement.cpp | 26 +++++++++++++------ 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/utility/convert.hpp b/utility/convert.hpp index a45c4a718..fcf8e03a8 100644 --- a/utility/convert.hpp +++ b/utility/convert.hpp @@ -52,35 +52,43 @@ struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type { }; template <> -struct ConvertionAllowed : std::true_type +struct ConvertionAllowed : std::true_type +{ +}; +template <> +struct ConvertionAllowed : std::true_type +{ +}; +template <> +struct ConvertionAllowed : std::true_type { }; template <> @@ -98,11 +106,11 @@ struct ConvertionAllowedVia : std::false_type { }; template <> -struct ConvertionAllowedVia : std::true_type +struct ConvertionAllowedVia : std::true_type { }; template <> -struct ConvertionAllowedVia : std::true_type +struct ConvertionAllowedVia : std::true_type { }; @@ -191,16 +199,16 @@ static inline bool convertTo(const std::string &str, T &result) return details::convertTo(str, result); } -/** Specialization for uint8_t of convertTo template function. +/** Specialization for unsigned char of convertTo template function. * * This function follows the same paradigm than it's generic version. * - * The generic version was converting int8 as it was a character - * (uint8_t is an alias to unsigned char on most compiler). + * The generic version was converting char as it was a character + * (unsigned char is an alias to unsigned char on most compiler). * Thus converting "1" would return 49 ie '1'. * As convertTo is thought as an _numerical_ convertion tool * (contrary to boost::lexical_cast for example), - * forbid considering the input as a character and consider uint8_t + * forbid considering the input as a character and consider unsigned char * (aka unsigned char) as a number exclusively. * * @param[in] str the string to parse. @@ -209,21 +217,20 @@ static inline bool convertTo(const std::string &str, T &result) * @return true if conversion was successful, false otherwise. */ template <> -inline bool convertTo(const std::string &str, uint8_t &result) +inline bool convertTo(const std::string &str, unsigned char &result) { - return details::convertToVia(str, result); + return details::convertToVia(str, result); } -/** Specialization for int8_t of convertTo template function. +/** Specialization for signed char of convertTo template function. * - * @see convertTo + * @see convertTo */ template <> -inline bool convertTo(const std::string &str, int8_t &result) +inline bool convertTo(const std::string &str, signed char &result) { - return details::convertToVia(str, result); + return details::convertToVia(str, result); } - /** * Specialization for float of convertTo template function. * diff --git a/xmlserializer/XmlElement.cpp b/xmlserializer/XmlElement.cpp index c54d97e70..902d0d966 100644 --- a/xmlserializer/XmlElement.cpp +++ b/xmlserializer/XmlElement.cpp @@ -265,17 +265,27 @@ bool CXmlElement::CChildIterator::next(CXmlElement &xmlChildElement) } template bool CXmlElement::getAttribute(const std::string &name, std::string &value) const; -template bool CXmlElement::getAttribute(const std::string &name, uint16_t &value) const; -template bool CXmlElement::getAttribute(const std::string &name, uint32_t &value) const; -template bool CXmlElement::getAttribute(const std::string &name, int32_t &value) const; -template bool CXmlElement::getAttribute(const std::string &name, uint64_t &value) const; template bool CXmlElement::getAttribute(const std::string &name, bool &value) const; -template bool CXmlElement::getAttribute(const std::string &name, double &value) const; +template bool CXmlElement::getAttribute(const std::string &name, short &value) const; +template bool CXmlElement::getAttribute(const std::string &name, unsigned short &value) const; +template bool CXmlElement::getAttribute(const std::string &name, int &value) const; +template bool CXmlElement::getAttribute(const std::string &name, unsigned int &value) const; +template bool CXmlElement::getAttribute(const std::string &name, long &value) const; +template bool CXmlElement::getAttribute(const std::string &name, unsigned long &value) const; +template bool CXmlElement::getAttribute(const std::string &name, long long &value) const; +template bool CXmlElement::getAttribute(const std::string &name, unsigned long long &value) const; template bool CXmlElement::getAttribute(const std::string &name, float &value) const; +template bool CXmlElement::getAttribute(const std::string &name, double &value) const; template void CXmlElement::setAttribute(const std::string &name, const std::string &value); template void CXmlElement::setAttribute(const std::string &name, const bool &value); -template void CXmlElement::setAttribute(const std::string &name, const int32_t &value); -template void CXmlElement::setAttribute(const std::string &name, const uint32_t &value); -template void CXmlElement::setAttribute(const std::string &name, const uint64_t &value); +template void CXmlElement::setAttribute(const std::string &name, const short &value); +template void CXmlElement::setAttribute(const std::string &name, const unsigned short &value); +template void CXmlElement::setAttribute(const std::string &name, const int &value); +template void CXmlElement::setAttribute(const std::string &name, const unsigned int &value); +template void CXmlElement::setAttribute(const std::string &name, const long &value); +template void CXmlElement::setAttribute(const std::string &name, const unsigned long &value); +template void CXmlElement::setAttribute(const std::string &name, const long long &value); +template void CXmlElement::setAttribute(const std::string &name, const unsigned long long &value); template void CXmlElement::setAttribute(const std::string &name, const float &value); +template void CXmlElement::setAttribute(const std::string &name, const double &value);