| 1 |
#ifndef CHILON_LEXICAL_CAST_HPP |
| 2 |
#define CHILON_LEXICAL_CAST_HPP |
| 3 |
|
| 4 |
#ifndef CHILON_DEFAULT_CAST_OPTIONS_HPP |
| 5 |
#define CHILON_DEFAULT_CAST_OPTIONS 0 |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <exception> |
| 9 |
|
| 10 |
/** |
| 11 |
* @file |
| 12 |
* |
| 13 |
* lexical_cast helps convert one type to a different type containing data |
| 14 |
* with the same lexical meaning. |
| 15 |
*/ |
| 16 |
|
| 17 |
namespace chilon { |
| 18 |
/// lexical cast failure exception. |
| 19 |
struct bad_lexical_cast : std::exception {}; |
| 20 |
|
| 21 |
/// These cast options can be passed in via the optional second parameter of |
| 22 |
/// chilon::lexical_cast in order to alter the cast procedure. |
| 23 |
enum cast_options { |
| 24 |
CHILON_CAST_NO_PLUS = 1, ///< Don't allow + before positive numbers. |
| 25 |
CHILON_CAST_NO_NEGATIVE = 2, ///< Only accept positive numbers for signed types. |
| 26 |
CHILON_CAST_ROUND = 4, ///< In integer conversion allow and round floating point strings. |
| 27 |
CHILON_CAST_FLOOR = 8, ///< In integer conversion allow and ceil floating point strings. |
| 28 |
CHILON_CAST_CEILING = 16, ///< In integer conversion allow and floor floating point strings. |
| 29 |
CHILON_CAST_NO_UPPER_CASE = 32, ///< Don't accept A-F for hexadecimal numbers or E for exponents. |
| 30 |
CAST_NO_LOWER_CASE = 64, ///< Don't accept a-f for hexadecimal numbers or e for exponents. |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
#include <chilon/detail/lexical_cast.hpp> |
| 35 |
|
| 36 |
namespace chilon { |
| 37 |
|
| 38 |
// the inline optimises g++ (4.2.3 at least) |
| 39 |
/** |
| 40 |
* Perform a lexical_cast similar to boost::lexical_cast, with several enhancements. |
| 41 |
* |
| 42 |
* @detailed lexical_cast is similar to boost::lexical_cast but converts the |
| 43 |
* types directly in C++ for optimal speed rather than redirecting through |
| 44 |
* a stringstream type. This form of lexical_cast also has other |
| 45 |
* improvements, for example a type can be lexically transformed to the |
| 46 |
* same type without causing a compiler error, allowing this function to |
| 47 |
* be used generically. A second optional template argument can be used |
| 48 |
* to pass in options which alter the behaviour of the cast. |
| 49 |
* @tparam Target The target type to cast to. |
| 50 |
* @tparam options cast_options to affect the casting procedure. |
| 51 |
* @see cast_options |
| 52 |
* @tparam Source Source type, automatically determined by the compiler. |
| 53 |
* @param src Input type to lexically cast based on first template argument. |
| 54 |
*/ |
| 55 |
template <class Target, int options, class Source> |
| 56 |
inline auto lexical_cast(Source&& src) |
| 57 |
CHILON_RETURN(detail::lexical_cast<Target, Source, options>::exec( |
| 58 |
std::forward<Source>(src))) |
| 59 |
|
| 60 |
/// As previous lexical_cast but use default options |
| 61 |
template <class Target, class Source> |
| 62 |
inline auto lexical_cast(Source&& src) |
| 63 |
CHILON_RETURN(detail::lexical_cast< |
| 64 |
Target, Source, CHILON_DEFAULT_CAST_OPTIONS>::exec( |
| 65 |
std::forward<Source>(src))) |
| 66 |
|
| 67 |
} |
| 68 |
#endif |