#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_STORAGE_HELPERS_LEXICAL_H #define NUCLEX_STORAGE_HELPERS_LEXICAL_H #include "Nuclex/Storage/Config.h" #include #include //#error I want to remove this namespace Nuclex { namespace Storage { namespace Helpers { // ------------------------------------------------------------------------------------------- // /// Lexically casts between a string and non-string data type /// Type into which the value will be converted /// Type that will be converted /// Value that will be converted /// The value converted to the specified type template inline TTarget lexical_cast(const TSource &from) { std::stringstream stringStream; stringStream << from; TTarget to; stringStream >> to; if(stringStream.fail() || stringStream.bad()) { std::string message("Could not convert from \""); stringStream >> message; message.append("\" ("); message.append(typeid(TSource).name()); message.append(") to ("); message.append(typeid(TTarget).name()); message.append(")"); throw std::invalid_argument(message.c_str()); } return to; } // ------------------------------------------------------------------------------------------- // /// Converts a floating point value into a string /// Floating point value that will be converted /// A string containing the printed floating point value template<> std::string lexical_cast<>(const float &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> float lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a double precision floating point value into a string /// Double precision Floating point value that will be converted /// A string containing the printed double precision floating point value template<> std::string lexical_cast<>(const double &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> double lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // #if defined(HAVE_ITOA) /// Converts an integer value into a string /// Integer value that will be converted /// A string containing the printed integer value template<> std::string lexical_cast<>(const int &from); #endif // ------------------------------------------------------------------------------------------- // /// Converts a string into an integer value /// String that will be converted /// The integer value parsed from the specified string template<> int lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // #if defined(HAVE_ULTOA) /// Converts an unsigned long value into a string /// Unsigned long value that will be converted /// A string containing the printed unsigned long value template<> std::string lexical_cast<>(const unsigned long &from); #endif // ------------------------------------------------------------------------------------------- // /// Converts a boolean value into a string /// Boolean value that will be converted /// A string containing the printed boolean value template<> std::string lexical_cast<>(const bool &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a boolean value /// String that will be converted /// The boolean value parsed from the specified string template<> bool lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Storage::Helpers #endif // NUCLEX_STORAGE_HELPERS_LEXICAL_H