Menu

[94f52a]: / lexical_cast.hpp  Maximize  Restore  History

Download this file

69 lines (58 with data), 2.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.