mirror of
https://github.com/ToruNiina/toml11.git
synced 2024-11-09 22:30:07 +00:00
update readme and add proposal
This commit is contained in:
parent
36199498b4
commit
be9ea157a7
59
PROPOSAL.md
Normal file
59
PROPOSAL.md
Normal file
@ -0,0 +1,59 @@
|
||||
|
||||
### encoding user's data
|
||||
|
||||
You can encode your data to toml format.
|
||||
|
||||
```cpp
|
||||
const toml::value integer(1);
|
||||
const toml::value array{3.1, 3.14, 3.141, 3.1415};
|
||||
const toml::value table{{"answer", 42}, {"pi", 3.14}, {"string", "foobar"}};
|
||||
|
||||
std::cout << toml::format("integer", integer) << std::endl;
|
||||
std::cout << toml::format("array", array) << std::endl;
|
||||
std::cout << toml::format("table", table) << std::endl;
|
||||
```
|
||||
|
||||
this program will output as below.
|
||||
|
||||
```toml
|
||||
integer = 1
|
||||
array = [3.1, 3.14, 3.141, 3.1415]
|
||||
[table]
|
||||
answer = 42
|
||||
pi = 3.14
|
||||
string = "foobar"
|
||||
```
|
||||
|
||||
Without key name, you can make string formatted as toml.
|
||||
|
||||
```cpp
|
||||
const std::string integer_ = toml::format(integer); // "1"
|
||||
const std::string array_ = toml::format(array); // "[3.1, 3.14, 3.141, 3.1415]"
|
||||
const std::string table_ = toml::format(table); // "answer = 42\npi=3.14\nstring=foobar"
|
||||
```
|
||||
|
||||
### inlinize
|
||||
|
||||
You can make `toml::Table` inline.
|
||||
|
||||
```cpp
|
||||
const toml::value table{{"answer", 42}, {"pi", 3.14}, {"string", "foobar"}};
|
||||
// if the inline-table format length is less than 80, the table will be inlined
|
||||
std::cout << toml::format("table", table, toml::make_inline(80)) << std::endl;
|
||||
// In any case, the table will be inlined.
|
||||
std::cout << toml::format("table", table, toml::forceinline) << std::endl;
|
||||
```
|
||||
|
||||
```toml
|
||||
table = {answer = 42, pi = 3.14, string = "foobar"}
|
||||
```
|
||||
|
||||
And there are some stream manipulators for toml format.
|
||||
|
||||
```cpp
|
||||
const toml::value table{{"answer", 42}, {"pi", 3.14}, {"string", "foobar"}};
|
||||
// if the inline-table format length is less than 80, the table will be inlined
|
||||
std::cout << toml::make_inline(80) << table << std::endl;
|
||||
// In any case, the table will be inlined.
|
||||
std::cout << toml::forceinline << table << std::endl;
|
||||
```
|
20
README.md
20
README.md
@ -42,8 +42,7 @@ If there are syntax error in the toml file,
|
||||
|
||||
#### toml::get()
|
||||
|
||||
Then you can obtain the various value from the `data` using `toml::get` function
|
||||
no matter what the value type is.
|
||||
Then you can obtain the various value from the `data` using `toml::get` function.
|
||||
|
||||
``` cpp
|
||||
const auto answer = toml::get<std::int64_t>(data.at("answer"));
|
||||
@ -60,13 +59,20 @@ const auto vc = toml::get<std::vector<int>>(data.at("numbers"));
|
||||
const auto ls = toml::get<std::list<int>>(data.at("numbers"));
|
||||
const auto dq = toml::get<std::deque<int>>(data.at("numbers"));
|
||||
// if size of data.at("numbers") is larger than 3, it will throw toml::type_error.
|
||||
// because std::array is not resizable.
|
||||
const auto ar = toml::get<std::array<int, 3>>(data.at("numbers"));
|
||||
```
|
||||
|
||||
If the type you passed as a template parameter is incorrect,
|
||||
it will throw `toml::type_error`.
|
||||
|
||||
#### value\_t and toml::value::type()
|
||||
``` cpp
|
||||
const auto wrong1 = toml::get<bool>(data.at("integer")); // exception thrown!
|
||||
const auto wrong2 = toml::get<float>(data.at("integer")); // ditto
|
||||
const auto wrong3 = toml::get<toml::Datetime>(data.at("array")); // ditto
|
||||
```
|
||||
|
||||
#### toml::value\_t
|
||||
|
||||
When you don't know the exact type of toml-value, you can get `enum` type from
|
||||
`toml::value`.
|
||||
@ -79,10 +85,10 @@ std::vector<int> a;
|
||||
const auto t = data.at("something").type();
|
||||
switch(t)
|
||||
{
|
||||
case toml::value_t::Integer: i = toml::get<int>(data.at("something"));
|
||||
case toml::value_t::Float : d = toml::get<double>(data.at("something"));
|
||||
case toml::value_t::String : s = toml::get<std::string>(data.at("something"));
|
||||
case toml::value_t::Array : a = toml::get<std::vector<int>>(data.at("something"));
|
||||
case toml::value_t::Integer: i = toml::get<int>(data.at("something")); break;
|
||||
case toml::value_t::Float : d = toml::get<double>(data.at("something")); break;
|
||||
case toml::value_t::String : s = toml::get<std::string>(data.at("something")); break;
|
||||
case toml::value_t::Array : a = toml::get<std::vector<int>>(data.at("something")); break;
|
||||
default : throw std::runtime_error("unexpected type : " + stringize(t));
|
||||
}
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user