#ifndef TOML11_TYPES_HPP #define TOML11_TYPES_HPP #include "comments.hpp" #include "compat.hpp" #include "error_info.hpp" #include "format.hpp" #include "ordered_map.hpp" #include "value.hpp" #include #include #include #include #include #include #include #include namespace toml { // forward decl template class basic_value; // when you use a special integer type as toml::value::integer_type, parse must // be able to read it. So, type_config has static member functions that read the // integer_type as {dec, hex, oct, bin}-integer. But, in most cases, operator<< // is enough. To make config easy, we provide the default read functions. // // Before this functions is called, syntax is checked and prefix(`0x` etc) and // spacer(`_`) are removed. template result read_dec_int(const std::string& str, const source_location src) { constexpr auto max_digits = std::numeric_limits::digits; assert( ! str.empty()); T val{0}; std::istringstream iss(str); iss >> val; if(iss.fail()) { return err(make_error_info("toml::parse_dec_integer: " "too large integer: current max digits = 2^" + std::to_string(max_digits), std::move(src), "must be < 2^" + std::to_string(max_digits))); } return ok(val); } template result read_hex_int(const std::string& str, const source_location src) { constexpr auto max_digits = std::numeric_limits::digits; assert( ! str.empty()); T val{0}; std::istringstream iss(str); iss >> std::hex >> val; if(iss.fail()) { return err(make_error_info("toml::parse_hex_integer: " "too large integer: current max value = 2^" + std::to_string(max_digits), std::move(src), "must be < 2^" + std::to_string(max_digits))); } return ok(val); } template result read_oct_int(const std::string& str, const source_location src) { constexpr auto max_digits = std::numeric_limits::digits; assert( ! str.empty()); T val{0}; std::istringstream iss(str); iss >> std::oct >> val; if(iss.fail()) { return err(make_error_info("toml::parse_oct_integer: " "too large integer: current max value = 2^" + std::to_string(max_digits), std::move(src), "must be < 2^" + std::to_string(max_digits))); } return ok(val); } template result read_bin_int(const std::string& str, const source_location src) { constexpr auto is_bounded = std::numeric_limits::is_bounded; constexpr auto max_digits = std::numeric_limits::digits; const auto max_value = (std::numeric_limits::max)(); T val{0}; T base{1}; for(auto i = str.rbegin(); i != str.rend(); ++i) { const auto c = *i; if(c == '1') { val += base; // prevent `base` from overflow if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) { base = 0; } else { base *= 2; } } else { assert(c == '0'); if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) { base = 0; } else { base *= 2; } } } if(base == 0) { return err(make_error_info("toml::parse_bin_integer: " "too large integer: current max value = 2^" + std::to_string(max_digits), std::move(src), "must be < 2^" + std::to_string(max_digits))); } return ok(val); } template result read_int(const std::string& str, const source_location src, const std::uint8_t base) { assert(base == 10 || base == 16 || base == 8 || base == 2); switch(base) { case 2: { return read_bin_int(str, src); } case 8: { return read_oct_int(str, src); } case 16: { return read_hex_int(str, src); } default: { assert(base == 10); return read_dec_int(str, src); } } } inline result read_hex_float(const std::string& str, const source_location src, float val) { #if defined(_MSC_VER) && ! defined(__clang__) const auto res = ::sscanf_s(str.c_str(), "%a", std::addressof(val)); #else const auto res = std::sscanf(str.c_str(), "%a", std::addressof(val)); #endif if(res != 1) { return err(make_error_info("toml::parse_floating: " "failed to read hexadecimal floating point value ", std::move(src), "here")); } return ok(val); } inline result read_hex_float(const std::string& str, const source_location src, double val) { #if defined(_MSC_VER) && ! defined(__clang__) const auto res = ::sscanf_s(str.c_str(), "%la", std::addressof(val)); #else const auto res = std::sscanf(str.c_str(), "%la", std::addressof(val)); #endif if(res != 1) { return err(make_error_info("toml::parse_floating: " "failed to read hexadecimal floating point value ", std::move(src), "here")); } return ok(val); } template cxx::enable_if_t, double>>, cxx::negation, float>> >::value, result> read_hex_float(const std::string&, const source_location src, T) { return err(make_error_info("toml::parse_floating: failed to read " "floating point value because of unknown type in type_config", std::move(src), "here")); } template result read_dec_float(const std::string& str, const source_location src) { T val; std::istringstream iss(str); iss >> val; if(iss.fail()) { return err(make_error_info("toml::parse_floating: " "failed to read floating point value from stream", std::move(src), "here")); } return ok(val); } template result read_float(const std::string& str, const source_location src, const bool is_hex) { if(is_hex) { return read_hex_float(str, src, T{}); } else { return read_dec_float(str, src); } } struct type_config { using comment_type = preserve_comments; using boolean_type = bool; using integer_type = std::int64_t; using floating_type = double; using string_type = std::string; template using array_type = std::vector; template using table_type = std::unordered_map; static result parse_int(const std::string& str, const source_location src, const std::uint8_t base) { return read_int(str, src, base); } static result parse_float(const std::string& str, const source_location src, const bool is_hex) { return read_float(str, src, is_hex); } }; using value = basic_value; using table = typename value::table_type; using array = typename value::array_type; struct ordered_type_config { using comment_type = preserve_comments; using boolean_type = bool; using integer_type = std::int64_t; using floating_type = double; using string_type = std::string; template using array_type = std::vector; template using table_type = ordered_map; static result parse_int(const std::string& str, const source_location src, const std::uint8_t base) { return read_int(str, src, base); } static result parse_float(const std::string& str, const source_location src, const bool is_hex) { return read_float(str, src, is_hex); } }; using ordered_value = basic_value; using ordered_table = typename ordered_value::table_type; using ordered_array = typename ordered_value::array_type; // ---------------------------------------------------------------------------- // meta functions for internal use namespace detail { // ---------------------------------------------------------------------------- // check if type T has all the needed member types template struct has_comment_type: std::false_type{}; template struct has_comment_type>: std::true_type{}; template struct has_integer_type: std::false_type{}; template struct has_integer_type>: std::true_type{}; template struct has_floating_type: std::false_type{}; template struct has_floating_type>: std::true_type{}; template struct has_string_type: std::false_type{}; template struct has_string_type>: std::true_type{}; template struct has_array_type: std::false_type{}; template struct has_array_type>>: std::true_type{}; template struct has_table_type: std::false_type{}; template struct has_table_type>>: std::true_type{}; template struct has_parse_int: std::false_type{}; template struct has_parse_int().parse_int( std::declval(), std::declval<::toml::source_location const&>(), std::declval() ))>>: std::true_type{}; template struct has_parse_float: std::false_type{}; template struct has_parse_float().parse_float( std::declval(), std::declval<::toml::source_location const&>(), std::declval() ))>>: std::true_type{}; template using is_type_config = cxx::conjunction< has_comment_type, has_integer_type, has_floating_type, has_string_type, has_array_type, has_table_type, has_parse_int, has_parse_float >; } // namespace detail } // namespace toml #if defined(TOML11_COMPILE_SOURCES) namespace toml { extern template class basic_value; extern template class basic_value; } // toml #endif // TOML11_COMPILE_SOURCES #endif // TOML11_TYPES_HPP