/** * @file ActivMiddleware/ActivBase/StringConversion.h * * @brief Header file for the StringConversion */ #if (!defined ACTIV_BASE_STRING_CONVERSION_H) #define ACTIV_BASE_STRING_CONVERSION_H #include "ActivMiddleware/ActivBase/External.h" #include "ActivMiddleware/CxxYZ/type_traits.h" #include ACTIV_BOOST_INCLUDE(config.hpp) #include ACTIV_BOOST_INCLUDE(utility/string_view.hpp) #if (!defined ACTIV_BOOST_NO_CXX17_HDR_STRING_VIEW) #include #endif #if (defined __GLIBCXX__) #include #include #endif //! Activ namespace. namespace Activ { class IFieldType; class MessageValidator; template struct IsStringType : public MakeBoolConstant< #if (!defined ACTIV_BOOST_NO_CXX17_HDR_STRING_VIEW) CxxYZ::is_same::type>::value || #endif #if (defined __GLIBCXX__) CxxYZ::is_same<__gnu_cxx::__vstring, typename CxxYZ::decay::type>::value || #endif CxxYZ::is_same::type>::value || CxxYZ::is_same::type>::value || CxxYZ::is_same::type>::value> { }; // --------------------------------------------------------------------------------------------------------------------------------- template struct IsCharType : public MakeBoolConstant< CxxYZ::is_integral::value && (1 == sizeof(T))> { }; // --------------------------------------------------------------------------------------------------------------------------------- template String TypeToString(const T &value); // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- namespace Detail { /** * @brief Convert a value of an arbitrary non-container type to a string. * * @param value * * @return The value as a string. */ template inline typename CxxYZ::enable_if< !( has_type_iterator::value || has_member_function_ToString::value || has_member_function_ToString::value || has_member_function_ToString::value ), String>::type TypeToStringImpl(const T &value) { // Leave up to the user to figure out if they want the char or the integer appended. ACTIV_BOOST_STATIC_ASSERT((!CxxYZ::is_same::value)); try { return Boost::lexical_cast(value); } catch (const Boost::bad_lexical_cast &) { return String(""); } } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a value to a string where T has a ToString function. * * @param value * * @return The value as a string. */ template inline typename CxxYZ::enable_if< has_member_function_ToString::value || has_member_function_ToString::value || has_member_function_ToString::value , String>::type TypeToStringImpl(const T &value) { return value.ToString(); } // --------------------------------------------------------------------------------------------------------------------------------- inline String TypeToStringImpl(const int8_t &value) { // Otherwise value gets converted to an ascii char. return TypeToString(static_cast(value)); } // --------------------------------------------------------------------------------------------------------------------------------- inline String TypeToStringImpl(const uint8_t &value) { // Otherwise value gets converted to an ascii char. return TypeToString(static_cast(value)); } // --------------------------------------------------------------------------------------------------------------------------------- inline String TypeToStringImpl(const String &value) { // Simple case. return value; } // --------------------------------------------------------------------------------------------------------------------------------- inline String TypeToStringImpl(const char * const pStr) { return (ACTIV_UNLIKELY(ACTIV_NULLPTR == pStr)) ? String() : String(pStr); } // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_BASE_API String TypeToStringImpl(const double value); // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_BASE_API String TypeToStringImpl(const float value); // --------------------------------------------------------------------------------------------------------------------------------- #if (defined ACTIV_USING_ALTERNATIVE_STRING) inline String TypeToStringImpl(const std::string &value) { return String(value.data(), value.length()); } #endif // (defined ACTIV_USING_ALTERNATIVE_STRING) // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a std::pair to a string. * * @param value * * @return The pair as a string of the form "key=value". */ template String TypeToStringImpl(const std::pair &value) { String output; output.append(TypeToString(value.first)); output.append(1, '='); output.append(TypeToString(value.second)); return output; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a container to a string. * * @param values container with signed integral values * * @return The container as a string. */ template inline typename CxxYZ::enable_if<( has_type_iterator::value && !CxxYZ::is_unsigned::value ), String>::type TypeToStringImpl(const C &values) { String output; ACTIV_FOREACH(const typename C::value_type &value, values) { if (!output.empty()) output.append(1, ','); output.append(TypeToString(value)); } return output; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert an unsigned integral container to a string of the form "1,2-5,7,10". * * @param values container with unsigned integral values * * @return The container as a string. */ template inline typename CxxYZ::enable_if<( has_type_iterator::value && CxxYZ::is_unsigned::value ), String>::type TypeToStringImpl(const C &values) { typedef typename C::value_type IntegerType; typedef typename C::const_iterator ConstIterator; String output; if (!values.empty()) { ConstIterator iter = values.begin(); ConstIterator iterEnd = values.end(); do { if (!output.empty()) output.append(1, ','); output.append(TypeToString(*iter)); const IntegerType startValue = *iter; IntegerType endValue = startValue; while (iterEnd != ++iter) { if (*iter != (endValue + 1)) break; endValue = *iter; } if (startValue != endValue) { output.append(1, (endValue == (startValue + 1)) ? ',' : '-'); output.append(TypeToString(endValue)); } } while (iter != iterEnd); } return output; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a Boost::dynamic_bitset<> container to a string of the form "1,2-5,7,10" where the numbers * represent the set bit indexes. * * @param value * * @return The container as a string. */ template inline String TypeToStringImpl(const Boost::dynamic_bitset &value) { String output; if (!value.empty()) { size_t i = 0; do { if (value.test(i)) { if (!output.empty()) output.append(1, ','); output.append(TypeToString(i)); const size_t startValue = i; size_t endValue = startValue; while (++i < value.size()) { if (!value.test(i)) break; endValue = i; } if (startValue != endValue) { output.append(1, (endValue == (startValue + 1)) ? ',' : '-'); output.append(TypeToString(endValue)); } } else { ++i; } } while (i < value.size()); } return output; } } // namespace Detail // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert an arbitrarily typed value to a string. * * @param value * * @return The value as a string. */ template inline String TypeToString(const T &value) { return Activ::Detail::TypeToStringImpl(value); } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a char buffer to a non-integral, non-char type.. * * @param pStr the pointer to the start of the buffer to convert. * @param length the length of the buffer to convert. * @param target to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if< !IsIntegral::value && !has_member_function_FromString::value, bool>::type StringToType(const char * const pStr, const size_t length, T &target) { try { target = Boost::lexical_cast(pStr, length); } catch (Boost::bad_lexical_cast &) { return false; } return true; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a char buffer to an arbitrary non-container, non-unsigned and non-floating point type. * * @param pStr the pointer to the start of the buffer to convert. * @param length the length of the buffer to convert. * @param t to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if<( IsIntegral::value && !IsCharType::value ), bool>::type StringToType(const char * const pStr, const size_t length, T &t) { if (ACTIV_UNLIKELY((ACTIV_NULLPTR == pStr) || (0 == length))) return false; if (IsUnsigned::value && ACTIV_UNLIKELY(!::isdigit(pStr[0]))) return false; try { t = Boost::lexical_cast(pStr, length); } catch (Boost::bad_lexical_cast &) { return false; } return true; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a char buffer to a 1 bytes length integer. * * @param pStr the pointer to the start of the buffer to convert. * @param length the length of the buffer to convert. * @param target to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if< IsCharType::value, bool>::type StringToType(const char * const pStr, const size_t length, T &target) { typename LengthToIntegralType<2, CxxYZ::is_signed::value>::Type tmp; if (!StringToType(pStr, length, tmp)) return false; if (!InRangeOfIntegralType()(tmp)) return false; target = static_cast(tmp); return true; } // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_BASE_API bool StringToType(const char * const pStr, const size_t length, IFieldType &target); // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_BASE_API bool StringToType(const char * const pStr, const size_t length, double &target); // --------------------------------------------------------------------------------------------------------------------------------- ACTIV_BASE_API bool StringToType(const char * const pStr, const size_t length, float &target); // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a char buffer to an arbitrary type. * @brief Wrapper ensuring compatibility with existing call which worked thanks to implicit conversion to Activ::String * * @param pStr the pointer to the start of the buffer to convert. * @param t to receive the value. * * @return true if conversion was successful. */ template inline bool StringToType(const char * pStr, T &t) { return StringToType(pStr, strlen(pStr), t); } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Wrapper to set of methods converting a string-like type to an arbitrary non-container type * * @param str the string to convert. * @param t to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if<( !has_type_iterator::value && IsStringType::value ), bool>::type StringToType(const S &str, T &t) { return StringToType(str.data(), str.length(), t); } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a string-like type to a String. * * @param str the string-like object to convert. * @param target to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if< IsStringType::value , bool>::type StringToType(const S &str, String &target) { target = String(str.data(), str.length()); return true; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a string-like type to a container of an arbitrary type. * * @param str the string to convert. * @param target to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if<( IsStringType::value && has_type_iterator::value && !CxxYZ::is_unsigned::value ), bool>::type StringToType(const S &str, C &target) { std::vector stringList; Boost::algorithm::split(stringList, str, Boost::is_any_of(","), Boost::token_compress_on); ACTIV_FOREACH(const String &string, stringList) { typename C::value_type value; if (!StringToType(string, value)) return false; target.insert(target.end(), value); } return true; } // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Convert a string-like type of the form "1,2-5,7,10" to a container of an unsigned integrals. * * @param str the string to convert. * @param target to receive the value. * * @return true if conversion was successful. */ template inline typename CxxYZ::enable_if<( IsStringType::value && has_type_iterator::value && CxxYZ::is_unsigned::value ), bool>::type StringToType(const S &str, C &target) { typedef typename C::value_type TYPE; std::vector stringList; if (!StringToType(str, stringList)) return false; ACTIV_FOREACH(const String &token, stringList) { if (token.empty()) continue; // Could be a range of values (N-M). // N or M may be negative so skip the first char, the next occurence must be the range separator. const size_t rangeSeparatorPosition = token.find('-', 1); if (String::npos != rangeSeparatorPosition) { TYPE startValue; if (!StringToType(token.substr(0, rangeSeparatorPosition), startValue)) return false; TYPE endValue; if (!StringToType(token.substr(rangeSeparatorPosition + 1), endValue)) return false; // Convert a descending range to ascending. if (startValue <= endValue) { for (; startValue <= endValue; ++startValue) target.insert(target.end(), startValue); } else { for (; startValue >= endValue; --startValue) target.insert(target.end(), startValue); } } else { TYPE value; if (!StringToType(token, value)) return false; target.insert(target.end(), value); } } return true; } } // namespace Activ #endif // (defined ACTIV_BASE_STRING_CONVERSION_H)