mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 17:21:43 +08:00
feat: add examples
This commit is contained in:
1
examples/boost_multiprecision/.gitignore
vendored
Normal file
1
examples/boost_multiprecision/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
multiprecision
|
8
examples/boost_multiprecision/CMakeLists.txt
Normal file
8
examples/boost_multiprecision/CMakeLists.txt
Normal 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()
|
13
examples/boost_multiprecision/README.md
Normal file
13
examples/boost_multiprecision/README.md
Normal 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
|
||||
```
|
55
examples/boost_multiprecision/multiprecision.cpp
Normal file
55
examples/boost_multiprecision/multiprecision.cpp
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user