add config support

This commit is contained in:
2026-07-30 13:18:41 +02:00
parent 215ed9705e
commit dc1aa5e5b6
124 changed files with 7402 additions and 8 deletions
+73
View File
@@ -0,0 +1,73 @@
# C++20 Accessors in tomlc17
The library includes C++ class and routines in the `toml` namespace. These constructs provide:
- **Safe resource management** - Result class respects RAII rules.
- **Direct subtable access** Retrieves values from nested subtables directly.
- **Safe value access** Uses C++ `std::optional` for error handling, throwing exceptions on invalid access.
- **Time and date support** Represents timestamps as `std::chrono` objects for precise time handling.
- **Array conversion** Converts datum arrays into integer or string vectors for easy manipulation.
## Usage
Here is a simple example:
```c++
/*
* Parse the config file simple.toml:
*
* [server]
* host = "www.example.com"
* port = [8080, 8181, 8282]
*
*/
#include "../src/tomlcpp.hpp"
#include <iostream>
#include <vector>
using std::cout;
static void error(const char *msg) {
fprintf(stderr, "ERROR: %s\n", msg);
exit(1);
}
int main() {
// Parse the toml file
toml::Result result = toml::parse_file_ex("simple.toml");
// Check for parse error
if (!result.ok()) {
error(result.errmsg());
}
// Extract values
std::string host;
std::vector<int64_t> port;
try {
// use the Datum::get() method
host = result.get({"server", "host"})->as_str().value();
} catch (const std::bad_optional_access &ex) {
error("missing or invalid 'server.host' property in config");
}
try {
// use the Datum::seek() method
port = result.seek("server.port")->as_intvec().value();
} catch (const std::bad_optional_access &ex) {
error("missing or invalid 'server.port' property in config");
}
// Print values
cout << "server.host = " << host << "\n";
cout << "server.port = [";
for (size_t i = 0; i < port.size(); i++) {
cout << (i ? ", " : "") << port[i];
}
cout << "]\n";
// Done! No need to call toml_free(). Result::~Result() will handle destruction.
return 0;
}
```