feat: add examples

This commit is contained in:
ToruNiina
2024-06-15 19:23:05 +09:00
parent 7789b4e8be
commit da2a85b500
28 changed files with 954 additions and 0 deletions

View File

@@ -0,0 +1 @@
multiprecision

View File

@@ -0,0 +1,8 @@
find_package(Boost 1.67.0)
if(Boost_FOUND)
add_executable(multiprecision multiprecision.cpp)
target_link_libraries(multiprecision PRIVATE toml11::toml11 Boost::boost)
set_target_properties(multiprecision PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
endif()

View File

@@ -0,0 +1,13 @@
# multiprecision
Use `boost::multiprecision` as `integer_type` and `floating_type`.
## build
Install [boost](https://boost.org).
Then, build toml11 with `-DTOML11_BUILD_EXAMPLES=ON`
```cpp
$ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON
```

View File

@@ -0,0 +1,55 @@
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <toml.hpp>
struct large_num_config
{
using comment_type = toml::preserve_comments;
using boolean_type = bool;
using integer_type = boost::multiprecision::cpp_int;
using floating_type = boost::multiprecision::cpp_bin_float_oct;
using string_type = std::string;
template<typename T>
using array_type = std::vector<T>;
template<typename K, typename T>
using table_type = std::unordered_map<K, T>;
static toml::result<integer_type, toml::error_info>
parse_int(const std::string& str, const toml::source_location src, const std::uint8_t base)
{
return toml::read_int<integer_type>(str, src, base);
}
static toml::result<floating_type, toml::error_info>
parse_float(const std::string& str, const toml::source_location src, const bool is_hex)
{
return toml::read_float<floating_type>(str, src, is_hex);
}
};
int main()
{
const std::string input_str(R"(
large_int_dec = 10_000_000_000_000_000_000
large_int_hex = 0x0001_0000_0000_0000_0000
large_float_pi = 3.1415926535897932384626433832795028842
)");
const auto input = toml::parse_str<large_num_config>(input_str);
std::cout << "int64_t max = " << (std::numeric_limits<std::int64_t>::max)() << std::endl;
std::cout << "large_int_dec = " << input.at("large_int_dec" ).as_integer() << std::endl;
std::cout << "large_int_hex = " << input.at("large_int_hex" ).as_integer() << std::endl;
std::cout << "large_float_pi = "
<< std::setprecision(std::numeric_limits<boost::multiprecision::cpp_bin_float_oct>::max_digits10 - 1)
<< input.at("large_float_pi").as_floating() << std::endl;
std::cout << "=================" << std::endl;
std::cout << toml::format(input) << std::endl;
return 0;
}