diff --git a/PROPOSAL.md b/PROPOSAL.md new file mode 100644 index 0000000..bbdc424 --- /dev/null +++ b/PROPOSAL.md @@ -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; +``` diff --git a/README.md b/README.md index 989da2c..3f91625 100644 --- a/README.md +++ b/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(data.at("answer")); @@ -60,13 +59,20 @@ const auto vc = toml::get>(data.at("numbers")); const auto ls = toml::get>(data.at("numbers")); const auto dq = toml::get>(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>(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(data.at("integer")); // exception thrown! +const auto wrong2 = toml::get(data.at("integer")); // ditto +const auto wrong3 = toml::get(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 a; const auto t = data.at("something").type(); switch(t) { - case toml::value_t::Integer: i = toml::get(data.at("something")); - case toml::value_t::Float : d = toml::get(data.at("something")); - case toml::value_t::String : s = toml::get(data.at("something")); - case toml::value_t::Array : a = toml::get>(data.at("something")); + case toml::value_t::Integer: i = toml::get(data.at("something")); break; + case toml::value_t::Float : d = toml::get(data.at("something")); break; + case toml::value_t::String : s = toml::get(data.at("something")); break; + case toml::value_t::Array : a = toml::get>(data.at("something")); break; default : throw std::runtime_error("unexpected type : " + stringize(t)); } ```