mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
feat: add examples
This commit is contained in:
1
examples/boost_container/.gitignore
vendored
Normal file
1
examples/boost_container/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
container
|
8
examples/boost_container/CMakeLists.txt
Normal file
8
examples/boost_container/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
find_package(Boost 1.81.0)
|
||||
|
||||
if(Boost_FOUND)
|
||||
add_executable(container container.cpp)
|
||||
target_link_libraries(container PRIVATE toml11::toml11 Boost::boost)
|
||||
set_target_properties(container PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
endif()
|
13
examples/boost_container/README.md
Normal file
13
examples/boost_container/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# container
|
||||
|
||||
Use `boost::container::vector` and `boost::unordered_flat_map` as `array_type` and `table_type`.
|
||||
|
||||
## build
|
||||
|
||||
Install [boost](https://boost.org), especially after 1.81.0 that supports `unordered_flat_map`.
|
||||
|
||||
Then, build toml11 with `-DTOML11_BUILD_EXAMPLES=ON`
|
||||
|
||||
```cpp
|
||||
$ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON
|
||||
```
|
47
examples/boost_container/container.cpp
Normal file
47
examples/boost_container/container.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include <boost/unordered/unordered_flat_map.hpp>
|
||||
|
||||
#include <toml.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
struct boost_config
|
||||
{
|
||||
using comment_type = toml::preserve_comments;
|
||||
|
||||
using boolean_type = bool;
|
||||
using integer_type = std::int64_t;
|
||||
using floating_type = double;
|
||||
using string_type = std::string;
|
||||
|
||||
template<typename T>
|
||||
using array_type = boost::container::small_vector<T, 8>;
|
||||
template<typename K, typename T>
|
||||
using table_type = boost::unordered_flat_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(int argc, char** argv)
|
||||
{
|
||||
if(argc != 2)
|
||||
{
|
||||
std::cerr << "usage: ./container <input.toml>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const auto input = toml::parse_str<boost_config>(argv[1]);
|
||||
|
||||
std::cout << toml::format(input) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user