#ifndef CHILON_LEXICAL_CAST_HPP
#define CHILON_LEXICAL_CAST_HPP
#ifndef CHILON_DEFAULT_CAST_OPTIONS_HPP
#define CHILON_DEFAULT_CAST_OPTIONS 0
#endif
#include <exception>
/**
* @file
*
* lexical_cast helps convert one type to a different type containing data
* with the same lexical meaning.
*/
namespace chilon {
/// lexical cast failure exception.
struct bad_lexical_cast : std::exception {};
/// These cast options can be passed in via the optional second parameter of
/// chilon::lexical_cast in order to alter the cast procedure.
enum cast_options {
CHILON_CAST_NO_PLUS = 1, ///< Don't allow + before positive numbers.
CHILON_CAST_NO_NEGATIVE = 2, ///< Only accept positive numbers for signed types.
CHILON_CAST_ROUND = 4, ///< In integer conversion allow and round floating point strings.
CHILON_CAST_FLOOR = 8, ///< In integer conversion allow and ceil floating point strings.
CHILON_CAST_CEILING = 16, ///< In integer conversion allow and floor floating point strings.
CHILON_CAST_NO_UPPER_CASE = 32, ///< Don't accept A-F for hexadecimal numbers or E for exponents.
CAST_NO_LOWER_CASE = 64, ///< Don't accept a-f for hexadecimal numbers or e for exponents.
};
}
#include <chilon/detail/lexical_cast.hpp>
namespace chilon {
// the inline optimises g++ (4.2.3 at least)
/**
* Perform a lexical_cast similar to boost::lexical_cast, with several enhancements.
*
* @detailed lexical_cast is similar to boost::lexical_cast but converts the
* types directly in C++ for optimal speed rather than redirecting through
* a stringstream type. This form of lexical_cast also has other
* improvements, for example a type can be lexically transformed to the
* same type without causing a compiler error, allowing this function to
* be used generically. A second optional template argument can be used
* to pass in options which alter the behaviour of the cast.
* @tparam Target The target type to cast to.
* @tparam options cast_options to affect the casting procedure.
* @see cast_options
* @tparam Source Source type, automatically determined by the compiler.
* @param src Input type to lexically cast based on first template argument.
*/
template <class Target, int options, class Source>
inline auto lexical_cast(Source&& src)
CHILON_RETURN(detail::lexical_cast<Target, Source, options>::exec(
std::forward<Source>(src)))
/// As previous lexical_cast but use default options
template <class Target, class Source>
inline auto lexical_cast(Source&& src)
CHILON_RETURN(detail::lexical_cast<
Target, Source, CHILON_DEFAULT_CAST_OPTIONS>::exec(
std::forward<Source>(src)))
}
#endif