GithubHelp home page GithubHelp logo

toruniina / toml11 Goto Github PK

View Code? Open in Web Editor NEW
932.0 21.0 140.0 1.3 MB

TOML for Modern C++

License: MIT License

C++ 98.23% CMake 1.73% Dockerfile 0.02% Shell 0.02%
toml c-plus-plus-11 c-plus-plus parser serializer c-plus-plus-14 c-plus-plus-17 header-only toml-parser c-plus-plus-20

toml11's Introduction

toml11

Build Status on GitHub Actions Build status on Appveyor Build status on CircleCI Version License DOI

toml11 is a C++11 (or later) header-only toml parser/encoder depending only on C++ standard library.

  • It is compatible to the latest version of TOML v1.0.0.
  • It is one of the most TOML standard compliant libraries, tested with the language agnostic test suite for TOML parsers by BurntSushi.
  • It shows highly informative error messages. You can see the error messages about invalid files at CircleCI.
  • It has configurable container. You can use any random-access containers and key-value maps as backend containers.
  • It optionally preserves comments without any overhead.
  • It has configurable serializer that supports comments, inline tables, literal strings and multiline strings.
  • It supports user-defined type conversion from/into toml values.
  • It correctly handles UTF-8 sequences, with or without BOM, both on posix and Windows.

Example

#include <toml.hpp>
#include <iostream>

int main()
{
    // ```toml
    // title = "an example toml file"
    // nums  = [3, 1, 4, 1, 5]
    // ```
    auto data = toml::parse("example.toml");

    // find a value with the specified type from a table
    std::string title = toml::find<std::string>(data, "title");

    // convert the whole array into any container automatically
    std::vector<int> nums = toml::find<std::vector<int>>(data, "nums");

    // access with STL-like manner
    if(!data.contains("foo"))
    {
        data["foo"] = "bar";
    }

    // pass a fallback
    std::string name = toml::find_or<std::string>(data, "name", "not found");

    // width-dependent formatting
    std::cout << std::setw(80) << data << std::endl;

    return 0;
}

Table of Contents

Integration

Just include the file after adding it to the include path.

#include <toml.hpp> // that's all! now you can use it.
#include <iostream>

int main()
{
    const auto data  = toml::parse("example.toml");
    const auto title = toml::find<std::string>(data, "title");
    std::cout << "the title is " << title << std::endl;
    return 0;
}

The convenient way is to add this repository as a git-submodule or to install it in your system by CMake.

Note for MSVC: We recommend to set /Zc:__cplusplus to detect C++ version correctly.

Example installation

For local installation build & use the provided install target

git clone https://github.com/ToruNiina/toml11.git
mkdir -p toml11/build
cd toml11/build
cmake .. -DCMAKE_CXX_STANDARD=11
sudo make install

In case you want to create a .deb you can use checkinstall.

sudo checkinstall
[[ .. skipping for clarity ]]
**********************************************************************

 Done. The new package has been installed and saved to

 /home/user/toml11/build/build_20230728-1_amd64.deb

 You can remove it from your system anytime using:

      dpkg -r build

**********************************************************************

You should get a package that you can install with dpkg -i <myfile>.deb and remove with dpkg -r <myfile>.deb

Decoding a toml file

To parse a toml file, the only thing you have to do is to pass a filename to the toml::parse function.

const std::string fname("sample.toml");
const toml::value data = toml::parse(fname);

As required by the TOML specification, the top-level value is always a table. You can find a value inside it, cast it into a table explicitly, and insert it as a value into other toml::value.

If it encounters an error while opening a file, it will throw std::runtime_error.

You can also pass a std::istream to the toml::parse function. To show a filename in an error message, however, it is recommended to pass the filename with the stream.

std::ifstream ifs("sample.toml", std::ios_base::binary);
assert(ifs.good());
const auto data = toml::parse(ifs, /*optional -> */ "sample.toml");

Note: When you are on Windows, open a file in binary mode. If a file is opened in text-mode, CRLF ("\r\n") will automatically be converted to LF ("\n") and this causes inconsistency between file size and the contents that would be read. This causes weird error.

In the case of syntax error

If there is a syntax error in a toml file, toml::parse will throw toml::syntax_error that inherits std::exception.

toml11 has clean and informative error messages inspired by Rust and it looks like the following.

terminate called after throwing an instance of 'toml::syntax_error'
  what():  [error] toml::parse_table: invalid line format # error description
 --> example.toml                                         # file name
 3 | a = 42 = true                                        # line num and content
   |        ^------ expected newline, but got '='.        # error reason

If you (mistakenly) duplicate tables and got an error, it is helpful to see where they are. toml11 shows both at the same time like the following.

terminate called after throwing an instance of 'toml::syntax_error'
  what():  [error] toml::insert_value: table ("table") already exists.
 --> duplicate-table.toml
 1 | [table]
   | ~~~~~~~ table already exists here
 ...
 3 | [table]
   | ~~~~~~~ table defined twice

When toml11 encounters a malformed value, it tries to detect what type it is. Then it shows hints to fix the format. An error message while reading one of the malformed files in the language agnostic test suite. is shown below.

what(): [error] bad time: should be HH:MM:SS.subsec
 --> ./datetime-malformed-no-secs.toml
 1 | no-secs = 1987-07-05T17:45Z
   |                     ^------- HH:MM:SS.subsec
   |
Hint: pass: 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999
Hint: fail: 1979-05-27T7:32:00, 1979-05-27 17:32

You can find other examples in a job named output_result on CircleCI.

Since the error message generation is generally a difficult task, the current status is not ideal. If you encounter a weird error message, please let us know and contribute to improve the quality!

Invalid UTF-8 codepoints

It throws syntax_error if a value of an escape sequence representing unicode character is not a valid UTF-8 codepoint.

  what():  [error] toml::read_utf8_codepoint: input codepoint is too large.
 --> utf8.toml
 1 | exceeds_unicode = "\U0011FFFF example"
   |                              ^--------- should be in [0x00..0x10FFFF]

Finding a toml value

After parsing successfully, you can obtain the values from the result of toml::parse using toml::find function.

# sample.toml
answer  = 42
pi      = 3.14
numbers = [1,2,3]
time    = 1979-05-27T07:32:00Z
const auto data      = toml::parse("sample.toml");
const auto answer    = toml::find<std::int64_t    >(data, "answer");
const auto pi        = toml::find<double          >(data, "pi");
const auto numbers   = toml::find<std::vector<int>>(data, "numbers");
const auto timepoint = toml::find<std::chrono::system_clock::time_point>(data, "time");

By default, toml::find returns a toml::value.

const toml::value& answer = toml::find(data, "answer");

When you pass an exact TOML type that does not require type conversion, toml::find returns a reference without copying the value.

const auto  data   = toml::parse("sample.toml");
const auto& answer = toml::find<toml::integer>(data, "answer");

If the specified type requires conversion, you can't take a reference to the value. See also underlying types.

NOTE: For some technical reason, automatic conversion between integer and floating is not supported. If you want to get a floating value even if a value has integer value, you need to convert it manually after obtaining a value, like the following.

const auto vx = toml::find(data, "x");
double x = vx.is_floating() ? vx.as_floating(std::nothrow) :
           static_cast<double>(vx.as_integer()); // it throws if vx is neither
                                                 // floating nor integer.

Finding a value in a table

There are several way to get a value defined in a table. First, you can get a table as a normal value and find a value from the table.

[fruit]
name = "apple"
[fruit.physical]
color = "red"
shape = "round"
const auto  data  = toml::parse("fruit.toml");
const auto& fruit = toml::find(data, "fruit");
const auto  name  = toml::find<std::string>(fruit, "name");

const auto& physical = toml::find(fruit, "physical");
const auto  color    = toml::find<std::string>(physical, "color");
const auto  shape    = toml::find<std::string>(physical, "shape");

Here, variable fruit is a toml::value and can be used as the first argument of toml::find.

Second, you can pass as many arguments as the number of subtables to toml::find.

const auto data  = toml::parse("fruit.toml");
const auto color = toml::find<std::string>(data, "fruit", "physical", "color");
const auto shape = toml::find<std::string>(data, "fruit", "physical", "shape");

Finding a value in an array

You can find n-th value in an array by toml::find.

values = ["foo", "bar", "baz"]
const auto data   = toml::parse("sample.toml");
const auto values = toml::find(data, "values");
const auto bar    = toml::find<std::string>(values, 1);

toml::find can also search array recursively.

const auto data = toml::parse("fruit.toml");
const auto bar  = toml::find<std::string>(data, "values", 1);

Before calling toml::find, you can check if a value corresponding to a key exists. You can use both bool toml::value::contains(const key&) const and std::size_t toml::value::count(const key&) const. Those behaves like the std::map::contains and std::map::count.

const auto data = toml::parse("fruit.toml");
if(data.contains("fruit") && data.at("fruit").count("physical") != 0)
{
    // ...
}

In case of error

If the value does not exist, toml::find throws std::out_of_range with the location of the table.

terminate called after throwing an instance of 'std::out_of_range'
  what():  [error] key "answer" not found
 --> example.toml
 6 | [tab]
   | ~~~~~ in this table

If the specified type differs from the actual value contained, it throws toml::type_error that inherits std::exception.

Similar to the case of syntax error, toml11 also displays clean error messages. The error message when you choose int to get string value would be like this.

terminate called after throwing an instance of 'toml::type_error'
  what():  [error] toml::value bad_cast to integer
 --> example.toml
 3 | title = "TOML Example"
   |         ~~~~~~~~~~~~~~ the actual type is string

NOTE: In order to show this kind of error message, all the toml values have a pointer to represent its range in a file. The entire contents of a file is shared by toml::values and remains on the heap memory. It is recommended to destruct all the toml::value classes after configuring your application if you have a large TOML file compared to the memory resource.

Dotted keys

TOML v0.5.0 has a new feature named "dotted keys". You can chain keys to represent the structure of the data.

physical.color = "orange"
physical.shape = "round"

This is equivalent to the following.

[physical]
color = "orange"
shape = "round"

You can get both of the above tables with the same c++ code.

const auto physical = toml::find(data, "physical");
const auto color    = toml::find<std::string>(physical, "color");

The following code does not work for the above toml file.

// XXX this does not work!
const auto color = toml::find<std::string>(data, "physical.color");

The above code works with the following toml file.

"physical.color" = "orange"
# equivalent to {"physical.color": "orange"},
# NOT {"physical": {"color": "orange"}}.

Casting a toml value

toml::get

toml::parse returns toml::value. toml::value is a union type that can contain one of the following types.

  • toml::boolean (bool)
  • toml::integer (std::int64_t)
  • toml::floating (double)
  • toml::string (a type convertible to std::string)
  • toml::local_date
  • toml::local_time
  • toml::local_datetime
  • toml::offset_datetime
  • toml::array (by default, std::vector<toml::value>)
  • toml::table (by default, std::unordered_map<toml::key, toml::value>)

To get a value inside, you can use toml::get<T>(). The usage is the same as toml::find<T> (actually, toml::find internally uses toml::get after casting a value to toml::table).

const toml::value  data    = toml::parse("sample.toml");
const toml::value  answer_ = toml::get<toml::table >(data).at("answer");
const std::int64_t answer  = toml::get<std::int64_t>(answer_);

When you pass an exact TOML type that does not require type conversion, toml::get returns a reference through which you can modify the content (if the toml::value is const, it returns const reference).

toml::value   data    = toml::parse("sample.toml");
toml::value   answer_ = toml::get<toml::table >(data).at("answer");
toml::integer& answer = toml::get<toml::integer>(answer_);
answer = 6 * 9; // write to data.answer. now `answer_` contains 54.

If the specified type requires conversion, you can't take a reference to the value. See also underlying types.

It also throws a toml::type_error if the type differs.

as_xxx

You can also use a member function to cast a value.

const std::int64_t answer = data.as_table().at("answer").as_integer();

It also throws a toml::type_error if the type differs. If you are sure that the value v contains a value of the specified type, you can suppress checking by passing std::nothrow.

const auto& answer = data.as_table().at("answer");
if(answer.is_integer() && answer.as_integer(std::nothrow) == 42)
{
    std::cout << "value is 42" << std::endl;
}

If std::nothrow is passed, the functions are marked as noexcept.

By casting a toml::value into an array or a table, you can iterate over the elements.

const auto data = toml::parse("example.toml");
std::cout << "keys in the top-level table are the following: \n";
for(const auto& kv : data.as_table())
{
    std::cout << kv.first << '\n';
}
for(const auto& [k, v] : data.as_table()) // or in C++17
{
    std::cout << k << '\n';
}


const auto& fruits = toml::find(data, "fruits");
for(const auto& v : fruits.as_array())
{
    std::cout << toml::find<std::string>(v, "name") << '\n';
}

The full list of the functions is below.

namespace toml {
class value {
    // ...
    const boolean&         as_boolean()         const&;
    const integer&         as_integer()         const&;
    const floating&        as_floating()        const&;
    const string&          as_string()          const&;
    const offset_datetime& as_offset_datetime() const&;
    const local_datetime&  as_local_datetime()  const&;
    const local_date&      as_local_date()      const&;
    const local_time&      as_local_time()      const&;
    const array&           as_array()           const&;
    const table&           as_table()           const&;
    // --------------------------------------------------------
    // non-const version
    boolean&               as_boolean()         &;
    // ditto...
    // --------------------------------------------------------
    // rvalue version
    boolean&&              as_boolean()         &&;
    // ditto...

    // --------------------------------------------------------
    // noexcept versions ...
    const boolean&         as_boolean(const std::nothrow_t&) const& noexcept;
    boolean&               as_boolean(const std::nothrow_t&) &      noexcept;
    boolean&&              as_boolean(const std::nothrow_t&) &&     noexcept;
    // ditto...
};
} // toml

at()

You can access to the element of a table and an array by toml::basic_value::at.

const toml::value v{1,2,3,4,5};
std::cout << v.at(2).as_integer() << std::endl; // 3

const toml::value v{{"foo", 42}, {"bar", 3.14}};
std::cout << v.at("foo").as_integer() << std::endl; // 42

If an invalid key (integer for a table, string for an array), it throws toml::type_error for the conversion. If the provided key is out-of-range, it throws std::out_of_range.

Note that, although std::string has at() member function, toml::value::at throws if the contained type is a string. Because std::string does not contain toml::value.

operator[]

You can also access to the element of a table and an array by toml::basic_value::operator[].

const toml::value v{1,2,3,4,5};
std::cout << v[2].as_integer() << std::endl; // 3

const toml::value v{{"foo", 42}, {"bar", 3.14}};
std::cout << v["foo"].as_integer() << std::endl; // 42

When you access to a toml::value that is not initialized yet via operator[](const std::string&), the toml::value will be a table, just like the std::map.

toml::value v; // not initialized as a table.
v["foo"] = 42; // OK. `v` will be a table.

Contrary, if you access to a toml::value that contains an array via operator[], it does not check anything. It converts toml::value without type check and then access to the n-th element without boundary check, just like the std::vector::operator[].

toml::value v; // not initialized as an array
v[2] = 42;     // error! UB

Please make sure that the toml::value has an array inside when you access to its element via operator[].

Checking value type

You can check the type of a value by is_xxx function.

const toml::value v = /* ... */;
if(v.is_integer())
{
    std::cout << "value is an integer" << std::endl;
}

The complete list of the functions is below.

namespace toml {
class value {
    // ...
    bool is_boolean()         const noexcept;
    bool is_integer()         const noexcept;
    bool is_floating()        const noexcept;
    bool is_string()          const noexcept;
    bool is_offset_datetime() const noexcept;
    bool is_local_datetime()  const noexcept;
    bool is_local_date()      const noexcept;
    bool is_local_time()      const noexcept;
    bool is_array()           const noexcept;
    bool is_table()           const noexcept;
    bool is_uninitialized()   const noexcept;
    // ...
};
} // toml

Also, you can get enum class value_t from toml::value::type().

switch(data.at("something").type())
{
    case toml::value_t::integer:  /*do some stuff*/ ; break;
    case toml::value_t::floating: /*do some stuff*/ ; break;
    case toml::value_t::string :  /*do some stuff*/ ; break;
    default : throw std::runtime_error(
        "unexpected type : " + toml::stringize(data.at("something").type()));
}

The complete list of the enums can be found in the section underlying types.

The enums can be used as a parameter of toml::value::is function like the following.

toml::value v = /* ... */;
if(v.is(toml::value_t::boolean)) // ...

More about conversion

Since toml::find internally uses toml::get, all the following examples work with both toml::get and toml::find.

Converting an array

You can get any kind of container class from a toml::array except for map-like classes.

// # sample.toml
// numbers = [1,2,3]

const auto numbers = toml::find(data, "numbers");

const auto vc  = toml::get<std::vector<int>  >(numbers);
const auto ls  = toml::get<std::list<int>    >(numbers);
const auto dq  = toml::get<std::deque<int>   >(numbers);
const auto ar  = toml::get<std::array<int, 3>>(numbers);
// if the size of data.at("numbers") is larger than that of std::array,
// it will throw toml::type_error because std::array is not resizable.

Surprisingly, you can convert toml::array into std::pair and std::tuple.

// numbers = [1,2,3]
const auto tp = toml::get<std::tuple<short, int, unsigned int>>(numbers);

This functionality is helpful when you have a toml file like the following.

array_of_arrays = [[1, 2, 3], ["foo", "bar", "baz"]] # toml allows this

What is the corresponding C++ type? Obviously, it is a std::pair of std::vectors.

const auto array_of_arrays = toml::find(data, "array_of_arrays");
const auto aofa = toml::get<
    std::pair<std::vector<int>, std::vector<std::string>>
    >(array_of_arrays);

If you don't know the type of the elements, you can use toml::array, which is a std::vector of toml::value, instead.

const auto a_of_a = toml::get<toml::array>(array_of_arrays);
const auto first  = toml::get<std::vector<int>>(a_of_a.at(0));

You can change the implementation of toml::array with std::deque or some other array-like container. See Customizing containers for detail.

Converting a table

When all the values of the table have the same type, toml11 allows you to convert a toml::table to a map that contains the convertible type.

[tab]
key1 = "foo" # all the values are
key2 = "bar" # toml String
const auto data = toml::parse("sample.toml");
const auto tab = toml::find<std::map<std::string, std::string>>(data, "tab");
std::cout << tab["key1"] << std::endl; // foo
std::cout << tab["key2"] << std::endl; // bar

But since toml::table is just an alias of std::unordered_map<toml::key, toml::value>, normally you don't need to convert it because it has all the functionalities that std::unordered_map has (e.g. operator[], count, and find). In most cases toml::table is sufficient.

toml::table tab = toml::get<toml::table>(data);
if(data.count("title") != 0)
{
    data["title"] = std::string("TOML example");
}

You can change the implementation of toml::table with std::map or some other map-like container. See Customizing containers for detail.

Getting an array of tables

An array of tables is just an array of tables. You can get it in completely the same way as the other arrays and tables.

# sample.toml
array_of_inline_tables = [{key = "value1"}, {key = "value2"}, {key = "value3"}]

[[array_of_tables]]
key = "value4"
[[array_of_tables]]
key = "value5"
[[array_of_tables]]
key = "value6"
const auto data = toml::parse("sample.toml");
const auto aot1 = toml::find<std::vector<toml::table>>(data, "array_of_inline_tables");
const auto aot2 = toml::find<std::vector<toml::table>>(data, "array_of_tables");

Cost of conversion

Although conversion through toml::(get|find) is convenient, it has additional copy-cost because it copies data contained in toml::value to the user-specified type. Of course in some cases this overhead is not ignorable.

// the following code constructs a std::vector.
// it requires heap allocation for vector and element conversion.
const auto array = toml::find<std::vector<int>>(data, "foo");

By passing the exact types, toml::get returns reference that has no overhead.

const auto& tab     = toml::find<toml::table>(data, "tab");
const auto& numbers = toml::find<toml::array>(data, "numbers");

Also, as_xxx are zero-overhead because they always return a reference.

const auto& tab     = toml::find(data, "tab"    ).as_table();
const auto& numbers = toml::find(data, "numbers").as_array();

In this case you need to call toml::get each time you access to the element of toml::array because toml::array is an array of toml::value.

const auto& num0 = toml::get<toml::integer>(numbers.at(0));
const auto& num1 = toml::get<toml::integer>(numbers.at(1));
const auto& num2 = toml::get<toml::integer>(numbers.at(2));

Converting datetime and its variants

TOML v0.5.0 has 4 different datetime objects, local_date, local_time, local_datetime, and offset_datetime.

Since local_date, local_datetime, and offset_datetime represent a time point, you can convert them to std::chrono::system_clock::time_point.

Contrary, local_time does not represents a time point because they lack a date information, but it can be converted to std::chrono::duration that represents a duration from the beginning of the day, 00:00:00.000.

# sample.toml
date = 2018-12-23
time = 12:30:00
l_dt = 2018-12-23T12:30:00
o_dt = 2018-12-23T12:30:00+09:30
const auto data = toml::parse("sample.toml");

const auto date = toml::get<std::chrono::system_clock::time_point>(data.at("date"));
const auto l_dt = toml::get<std::chrono::system_clock::time_point>(data.at("l_dt"));
const auto o_dt = toml::get<std::chrono::system_clock::time_point>(data.at("o_dt"));

const auto time = toml::get<std::chrono::minutes>(data.at("time")); // 12 * 60 + 30 min

local_date and local_datetime are assumed to be in the local timezone when they are converted into time_point. On the other hand, offset_datetime only uses the offset part of the data and it does not take local timezone into account.

To contain datetime data, toml11 defines its own datetime types. For more detail, you can see the definitions in toml/datetime.hpp.

Getting with a fallback

toml::find_or returns a default value if the value is not found or has a different type.

const auto data = toml::parse("example.toml");
const auto num  = toml::find_or(data, "num", 42);

It works recursively if you pass several keys for subtables. In that case, the last argument is considered to be the optional value. All other arguments between toml::value and the optional value are considered as keys.

// [fruit.physical]
// color = "red"
auto data  = toml::parse("fruit.toml");
auto color = toml::find_or(data, "fruit", "physical", "color", "red");
//                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^
//                               arguments                     optional value

Also, toml::get_or returns a default value if toml::get<T> failed.

toml::value v("foo"); // v contains String
const int value = toml::get_or(v, 42); // conversion fails. it returns 42.

These functions automatically deduce what type you want to get from the default value you passed.

To get a reference through this function, take care about the default value.

toml::value v("foo"); // v contains String
toml::integer& i = toml::get_or(v, 42); // does not work because binding `42`
                                        // to `integer&` is invalid
toml::integer opt = 42;
toml::integer& i = toml::get_or(v, opt); // this works.

Expecting conversion

By using toml::expect, you will get your expected value or an error message without throwing toml::type_error.

const auto value = toml::expect<std::string>(data.at("title"));
if(value.is_ok()) {
    std::cout << value.unwrap() << std::endl;
} else {
    std::cout << value.unwrap_err() << std::endl;
}

Also, you can pass a function object to modify the expected value.

const auto value = toml::expect<int>(data.at("number"))
    .map(// function that receives expected type (here, int)
    [](const int number) -> double {
        return number * 1.5 + 1.0;
    }).unwrap_or(/*default value =*/ 3.14);

Visiting a toml::value

toml11 provides toml::visit to apply a function to toml::value in the same way as std::variant.

const toml::value v(3.14);
toml::visit([](const auto& val) -> void {
        std::cout << val << std::endl;
    }, v);

The function object that would be passed to toml::visit must be able to receive all the possible TOML types. Also, the result types should be the same each other.

Constructing a toml::value

toml::value can be constructed in various ways.

toml::value v(true);     // boolean
toml::value v(42);       // integer
toml::value v(3.14);     // floating
toml::value v("foobar"); // string
toml::value v(toml::local_date(2019, toml::month_t::Apr, 1)); // date
toml::value v{1, 2, 3, 4, 5};                                 // array
toml::value v{{"foo", 42}, {"bar", 3.14}, {"baz", "qux"}};    // table

When constructing a string, you can choose to use either literal or basic string. By default, it will be a basic string.

toml::value v("foobar", toml::string_t::basic  );
toml::value v("foobar", toml::string_t::literal);

Datetime objects can be constructed from std::tm and std::chrono::system_clock::time_point. But you need to specify what type you use to avoid ambiguity.

const auto now = std::chrono::system_clock::now();
toml::value v(toml::local_date(now));
toml::value v(toml::local_datetime(now));
toml::value v(toml::offset_datetime(now));

Since local time is not equivalent to a time point, because it lacks date information, it will be constructed from std::chrono::duration.

toml::value v(toml::local_time(std::chrono::hours(10)));

You can construct an array object not only from initializer_list, but also from STL containers. In that case, the element type must be convertible to toml::value.

std::vector<int> vec{1,2,3,4,5};
toml::value v(vec);

When you construct an array value, all the elements of initializer_list must be convertible into toml::value.

If a toml::value has an array, you can push_back an element in it.

toml::value v{1,2,3,4,5};
v.push_back(6);

emplace_back also works.

Preserving comments

toml11 v3 or later allows you yo choose whether comments are preserved or not via template parameter

const auto data1 = toml::parse<toml::discard_comments >("example.toml");
const auto data2 = toml::parse<toml::preserve_comments>("example.toml");

or macro definition.

#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT
#include <toml11/toml.hpp>

This feature is controlled by template parameter in toml::basic_value<...>. toml::value is an alias of toml::basic_value<...>.

If template parameter is explicitly specified, the return value of toml::parse will be toml::basic_value<toml::preserve_comments>. If the macro is defined, the alias toml::value will be toml::basic_value<toml::preserve_comments>.

Comments related to a value can be obtained by toml::value::comments(). The return value has the same interface as std::vector<std::string>.

const auto& com = v.comments();
for(const auto& c : com)
{
    std::cout << c << std::endl;
}

Comments just before and just after (within the same line) a value are kept in a value.

# this is a comment for v1.
v1 = "foo"

v2 = "bar" # this is a comment for v2.
# Note that this comment is NOT a comment for v2.

# this comment is not related to any value
# because there are empty lines between v3.
# this comment will be ignored even if you set `preserve_comments`.

# this is a comment for v3
# this is also a comment for v3.
v3 = "baz" # ditto.

Each comment line becomes one element of a std::vector.

Hash signs will be removed, but spaces after hash sign will not be removed.

v1.comments().at(0) == " this is a comment for v1."s;

v2.comments().at(1) == " this is a comment for v1."s;

v3.comments().at(0) == " this is a comment for v3."s;
v3.comments().at(1) == " this is also a comment for v3."s;
v3.comments().at(2) == " ditto."s;

Note that a comment just after an opening brace of an array will not be a comment for the array.

# this is a comment for a.
a = [ # this is not a comment for a. this will be ignored.
  1, 2, 3,
  # this is a comment for `42`.
  42, # this is also a comment for `42`.
  5
] # this is a comment for a.

You can also append and modify comments. The interfaces are the same as std::vector<std::string>.

toml::basic_value<toml::preserve_comments> v(42);
v.comments().push_back(" add this comment.");
// # add this comment.
// i = 42

Also, you can pass a std::vector<std::string> when constructing a toml::basic_value<toml::preserve_comments>.

std::vector<std::string> comments{"comment 1", "comment 2"};
const toml::basic_value<toml::preserve_comments> v1(42, std::move(comments));
const toml::basic_value<toml::preserve_comments> v2(42, {"comment 1", "comment 2"});

When toml::discard_comments is chosen, comments will not be contained in a value. value::comments() will always be kept empty. All the modification on comments would be ignored. All the element access in a discard_comments causes the same error as accessing an element of an empty std::vector.

The comments will also be serialized. If comments exist, those comments will be added just before the values.

NOTE: Result types from toml::parse(...) and toml::parse<toml::preserve_comments>(...) are different.

Customizing containers

Actually, toml::basic_value has 3 template arguments.

template<typename Comment, // discard/preserve_comment
         template<typename ...> class Table = std::unordered_map,
         template<typename ...> class Array = std::vector>
class basic_value;

This enables you to change the containers used inside. E.g. you can use std::map to contain a table object instead of std::unordered_map. And also can use std::deque as a array object instead of std::vector.

You can set these parameters while calling toml::parse function.

const auto data = toml::parse<
    toml::preserve_comments, std::map, std::deque
    >("example.toml");

Needless to say, the result types from toml::parse(...) and toml::parse<Com, Map, Cont>(...) are different (unless you specify the same types as default).

Note that, since toml::table and toml::array is an alias for a table and an array of a default toml::value, so it is different from the types actually contained in a toml::basic_value when you customize containers. To get the actual type in a generic way, use typename toml::basic_type<C, T, A>::table_type and typename toml::basic_type<C, T, A>::array_type.

TOML literal

toml11 supports "..."_toml literal. It accept both a bare value and a file content.

using namespace toml::literals::toml_literals;

// `_toml` can convert a bare value without key
const toml::value v = u8"0xDEADBEEF"_toml;
// v is an Integer value containing 0xDEADBEEF.

// raw string literal (`R"(...)"` is useful for this purpose)
const toml::value t = u8R"(
    title = "this is TOML literal"
    [table]
    key = "value"
)"_toml;
// the literal will be parsed and the result will be contained in t

The literal function is defined in the same way as the standard library literals such as std::literals::string_literals::operator""s.

namespace toml
{
inline namespace literals
{
inline namespace toml_literals
{
toml::value operator"" _toml(const char* str, std::size_t len);
} // toml_literals
} // literals
} // toml

Access to the operator can be gained with using namespace toml::literals;, using namespace toml::toml_literals, and using namespace toml::literals::toml_literals.

Note that a key that is composed only of digits is allowed in TOML. And, unlike the file parser, toml-literal allows a bare value without a key. Thus it is difficult to distinguish arrays having integers and definitions of tables that are named as digits. Currently, literal [1] becomes a table named "1". To ensure a literal to be considered as an array with one element, you need to add a comma after the first element (like [1,]).

"[1,2,3]"_toml;   // This is an array
"[table]"_toml;   // This is a table that has an empty table named "table" inside.
"[[1,2,3]]"_toml; // This is an array of arrays
"[[table]]"_toml; // This is a table that has an array of tables inside.

"[[1]]"_toml;     // This literal is ambiguous.
                  // Currently, it becomes an empty array of table named "1".
"1 = [{}]"_toml;  // This is a table that has an array of table named 1.
"[[1,]]"_toml;    // This is an array of arrays.
"[[1],]"_toml;    // ditto.

NOTE: _toml literal returns a toml::value that does not have comments.

Conversion between toml value and arbitrary types

You can also use toml::get and other related functions with the types you defined after you implement a way to convert it.

namespace ext
{
struct foo
{
    int         a;
    double      b;
    std::string c;
};
} // ext

const auto data = toml::parse("example.toml");

// to do this
const foo f = toml::find<ext::foo>(data, "foo");

There are 3 ways to use toml::get with the types that you defined.

The first one is to implement from_toml(const toml::value&) member function.

namespace ext
{
struct foo
{
    int         a;
    double      b;
    std::string c;

    void from_toml(const toml::value& v)
    {
        this->a = toml::find<int        >(v, "a");
        this->b = toml::find<double     >(v, "b");
        this->c = toml::find<std::string>(v, "c");
        return;
    }
};
} // ext

In this way, because toml::get first constructs foo without arguments, the type should be default-constructible.

The second is to implement constructor(const toml::value&).

namespace ext
{
struct foo
{
    explicit foo(const toml::value& v)
        : a(toml::find<int>(v, "a")), b(toml::find<double>(v, "b")),
          c(toml::find<std::string>(v, "c"))
    {}

    int         a;
    double      b;
    std::string c;
};
} // ext

Note that implicit default constructor declaration will be suppressed when a constructor is defined. If you want to use the struct (here, foo) in a container (e.g. std::vector<foo>), you may need to define default constructor explicitly.

The third is to implement specialization of toml::from for your type.

namespace ext
{
struct foo
{
    int         a;
    double      b;
    std::string c;
};
} // ext

namespace toml
{
template<>
struct from<ext::foo>
{
    static ext::foo from_toml(const value& v)
    {
        ext::foo f;
        f.a = find<int        >(v, "a");
        f.b = find<double     >(v, "b");
        f.c = find<std::string>(v, "c");
        return f;
    }
};
} // toml

In this way, since the conversion function is defined outside of the class, you can add conversion between toml::value and classes defined in another library.

In some cases, a class has a templatized constructor that takes a template, T. It confuses toml::get/find<T> because it makes the class "constructible" from toml::value. To avoid this problem, toml::from and from_toml always precede constructor. It makes easier to implement conversion between toml::value and types defined in other libraries because it skips constructor.

But, importantly, you cannot define toml::from<T> and T.from_toml at the same time because it causes ambiguity in the overload resolution of toml::get<T> and toml::find<T>.

So the precedence is toml::from<T> == T.from_toml() > T(toml::value).

If you want to convert any versions of toml::basic_value, you need to templatize the conversion function as follows.

struct foo
{
    template<typename C, template<typename ...> class M, template<typename ...> class A>
    void from_toml(const toml::basic_value<C, M, A>& v)
    {
        this->a = toml::find<int        >(v, "a");
        this->b = toml::find<double     >(v, "b");
        this->c = toml::find<std::string>(v, "c");
        return;
    }
};
// or
namespace toml
{
template<>
struct from<ext::foo>
{
    template<typename C, template<typename ...> class M, template<typename ...> class A>
    static ext::foo from_toml(const basic_value<C, M, A>& v)
    {
        ext::foo f;
        f.a = find<int        >(v, "a");
        f.b = find<double     >(v, "b");
        f.c = find<std::string>(v, "c");
        return f;
    }
};
} // toml

The opposite direction is also supported in a similar way. You can directly pass your type to toml::value's constructor by introducing into_toml or toml::into<T>.

namespace ext
{
struct foo
{
    int         a;
    double      b;
    std::string c;

    toml::value into_toml() const // you need to mark it const.
    {
        return toml::value{{"a", this->a}, {"b", this->b}, {"c", this->c}};
    }
};
} // ext

ext::foo    f{42, 3.14, "foobar"};
toml::value v(f);

The definition of toml::into<T> is similar to toml::from<T>.

namespace ext
{
struct foo
{
    int         a;
    double      b;
    std::string c;
};
} // ext

namespace toml
{
template<>
struct into<ext::foo>
{
    static toml::value into_toml(const ext::foo& f)
    {
        return toml::value{{"a", f.a}, {"b", f.b}, {"c", f.c}};
    }
};
} // toml

ext::foo    f{42, 3.14, "foobar"};
toml::value v(f);

Any type that can be converted to toml::value, e.g. int, toml::table and toml::array are okay to return from into_toml.

You can also return a custom toml::basic_value from toml::into.

namespace toml
{
template<>
struct into<ext::foo>
{
    static toml::basic_value<toml::preserve_comments> into_toml(const ext::foo& f)
    {
        toml::basic_value<toml::preserve_comments> v{{"a", f.a}, {"b", f.b}, {"c", f.c}};
        v.comments().push_back(" comment");
        return v;
    }
};
} // toml

But note that, if this basic_value would be assigned into other toml::value that discards comments, the comments would be dropped.

Macro to automatically define conversion functions

There is a helper macro that automatically generates conversion functions from and into for a simple struct.

namespace foo
{
struct Foo
{
    std::string s;
    double      d;
    int         i;
};
} // foo

TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(foo::Foo, s, d, i)

int main()
{
    const auto file = toml::parse("example.toml");
    auto f = toml::find<foo::Foo>(file, "foo");
}

And then you can use toml::find<foo::Foo>(file, "foo");

Note that, because of a slight difference in implementation of preprocessor between gcc/clang and MSVC, you need to define /Zc:preprocessor to use it in MSVC (Thank you @glebm !).

Formatting user-defined error messages

When you encounter an error after you read the toml value, you may want to show the error with the value.

toml11 provides you a function that formats user-defined error message with related values. With a code like the following,

const auto value = toml::find<int>(data, "num");
if(value < 0)
{
    std::cerr << toml::format_error("[error] value should be positive",
                                    data.at("num"), "positive number required")
              << std::endl;
}

you will get an error message like this.

[error] value should be positive
 --> example.toml
 3 | num = -42
   |       ~~~ positive number required

When you pass two values to toml::format_error,

const auto min = toml::find<int>(range, "min");
const auto max = toml::find<int>(range, "max");
if(max < min)
{
    std::cerr << toml::format_error("[error] max should be larger than min",
                                    data.at("min"), "minimum number here",
                                    data.at("max"), "maximum number here");
              << std::endl;
}

you will get an error message like this.

[error] max should be larger than min
 --> example.toml
 3 | min = 54
   |       ~~ minimum number here
 ...
 4 | max = 42
   |       ~~ maximum number here

You can print hints at the end of the message.

std::vector<std::string> hints;
hints.push_back("positive number means n >= 0.");
hints.push_back("negative number is not positive.");
std::cerr << toml::format_error("[error] value should be positive",
                                data.at("num"), "positive number required", hints)
          << std::endl;
[error] value should be positive
 --> example.toml
 2 | num = 42
   |       ~~ positive number required
   |
Hint: positive number means n >= 0.
Hint: negative number is not positive.

Obtaining location information

You can also format error messages in your own way by using source_location.

struct source_location
{
    std::uint_least32_t line()      const noexcept;
    std::uint_least32_t column()    const noexcept;
    std::uint_least32_t region()    const noexcept;
    std::string const&  file_name() const noexcept;
    std::string const&  line_str()  const noexcept;
};
// +-- line()       +--- length of the region (here, region() == 9)
// v            .---+---.
// 12 | value = "foo bar" <- line_str() returns the line itself.
//              ^-------- column() points here

You can get this by

const toml::value           v   = /*...*/;
const toml::source_location loc = v.location();

Exceptions

The following exception classes inherits toml::exception that inherits std::exception.

namespace toml {
struct exception      : public std::exception  {/**/};
struct syntax_error   : public toml::exception {/**/};
struct type_error     : public toml::exception {/**/};
struct internal_error : public toml::exception {/**/};
} // toml

toml::exception has toml::exception::location() member function that returns toml::source_location, in addition to what().

namespace toml {
struct exception : public std::exception
{
    // ...
    source_location const& location() const noexcept;
};
} // toml

It represents where the error occurs.

syntax_error will be thrown from toml::parse and _toml literal. type_error will be thrown from toml::get/find, toml::value::as_xxx(), and other functions that takes a content inside of toml::value.

Note that, currently, from toml::value::at() and toml::find(value, key) may throw an std::out_of_range that does not inherits toml::exception.

Also, in some cases, most likely in the file open error, it will throw an std::runtime_error.

Colorize Error Messages

By defining TOML11_COLORIZE_ERROR_MESSAGE, the error messages from toml::parse and toml::find|get will be colorized. By default, this feature is turned off.

With the following toml file taken from toml-lang/toml/tests/hard_example.toml,

[error]
array = [
         "This might most likely happen in multiline arrays",
         Like here,
         "or here,
         and here"
        ]     End of array comment, forgot the #

the error message would be like this.

error-message-1

With the following,

[error]
# array = [
#          "This might most likely happen in multiline arrays",
#          Like here,
#          "or here,
#          and here"
#         ]     End of array comment, forgot the #
number = 3.14  pi <--again forgot the #

the error message would be like this.

error-message-2

The message would be messy when it is written to a file, not a terminal because it uses ANSI escape code.

Without TOML11_COLORIZE_ERROR_MESSAGE, you can still colorize user-defined error message by passing true to the toml::format_error function. If you define TOML11_COLORIZE_ERROR_MESSAGE, the value is true by default. If not, the default value would be false.

std::cerr << toml::format_error("[error] value should be positive",
                                data.at("num"), "positive number required",
                                hints, /*colorize = */ true) << std::endl;

Note: It colorizes [error] in red. That means that it detects [error] prefix at the front of the error message. If there is no [error] prefix, format_error adds it to the error message.

Compared to the TOML11_COLORIZE_ERROR_MESSAGE macro that enables colorization statically, toml11 provides toml::color::enable & toml::color::disable functions to dynamically change the color mode. This feature overwrites TOML11_COLORIZE_ERROR_MESSAGE and the colorize argument of toml::format_error when you call enable.

Note: If either TOML11_COLORIZE_ERROR_MESSAGE is defined or the colorize argument is used, it takes precedence, meaning that disable won't work. Accordingly, we highly recommend using only one of them.

toml::color::enable();  // enable colorization
toml::color::disable(); // disable colorization

If you use user-defined error message, you can manage the setting as follows:

toml::color::enable();
std::cerr << toml::format_error("[error] value should be positive",
                                data.at("num"), "positive number required",
                                hints) << std::endl; // colorized

toml::color::disable();
std::cerr << toml::format_error("[error] value should be positive",
                                data.at("num"), "positive number required",
                                hints) << std::endl; // NOT colorized

Or you may use toml11 in your application like:

std::vector<std::string> args(argv + 1, argv + argc);
auto result = std::find(args.begin(), args.end(), "--color");
if (result != args.end()) {
    toml::color::enable();
} else {
    toml::color::disable();
}

// use toml11 ...

Opting out of the default [error] prefix

toml11 prints error messages with the [error] prefix by default. Defining TOML11_NO_ERROR_PREFIX will let toml11 omit the prefix regardless of colorized or not in case you would use a custom prefix, such as Error:.

#define TOML11_NO_ERROR_PREFIX

Serializing TOML data

toml11 enables you to serialize data into toml format.

const toml::value data{{"foo", 42}, {"bar", "baz"}};
std::cout << data << std::endl;
// bar = "baz"
// foo = 42

toml11 automatically makes a small table and small array inline. You can specify the width to make them inline by std::setw for streams.

const toml::value data{
    {"qux",    {{"foo", 42}, {"bar", "baz"}}},
    {"quux",   {"small", "array", "of", "strings"}},
    {"foobar", {"this", "array", "of", "strings", "is", "too", "long",
                "to", "print", "into", "single", "line", "isn't", "it?"}},
};

// the threshold becomes 80.
std::cout << std::setw(80) << data << std::endl;
// foobar = [
// "this","array","of","strings","is","too","long","to","print","into",
// "single","line","isn't","it?",
// ]
// quux = ["small","array","of","strings"]
// qux = {bar="baz",foo=42}


// the width is 0. nothing become inline.
std::cout << std::setw(0) << data << std::endl;
// foobar = [
// "this",
// ... (snip)
// "it?",
// ]
// quux = [
// "small",
// "array",
// "of",
// "strings",
// ]
// [qux]
// bar = "baz"
// foo = 42

It is recommended to set width before printing data. Some I/O functions changes width to 0, and it makes all the stuff (including toml::array) multiline. The resulting files becomes too long.

To control the precision of floating point numbers, you need to pass std::setprecision to stream.

const toml::value data{
    {"pi", 3.141592653589793},
    {"e",  2.718281828459045}
};
std::cout << std::setprecision(17) << data << std::endl;
// e = 2.7182818284590451
// pi = 3.1415926535897931
std::cout << std::setprecision( 7) << data << std::endl;
// e = 2.718282
// pi = 3.141593

There is another way to format toml values, toml::format(). It returns std::string that represents a value.

const toml::value v{{"a", 42}};
const std::string fmt = toml::format(v);
// a = 42

Note that since toml::format formats a value, the resulting string may lack the key value.

const toml::value v{3.14};
const std::string fmt = toml::format(v);
// 3.14

To control the width and precision, toml::format receives optional second and third arguments to set them. By default, the width is 80 and the precision is std::numeric_limits<double>::max_digit10.

const auto serial = toml::format(data, /*width = */ 0, /*prec = */ 17);

When you pass a comment-preserving-value, the comment will also be serialized. An array or a table containing a value that has a comment would not be inlined.

Underlying types

The toml types (can be used as toml::* in this library) and corresponding enum names are listed in the table below.

TOML type underlying c++ type enum class
Boolean bool toml::value_t::boolean
Integer std::int64_t toml::value_t::integer
Float double toml::value_t::floating
String toml::string toml::value_t::string
LocalDate toml::local_date toml::value_t::local_date
LocalTime toml::local_time toml::value_t::local_time
LocalDatetime toml::local_datetime toml::value_t::local_datetime
OffsetDatetime toml::offset_datetime toml::value_t::offset_datetime
Array array-like<toml::value> toml::value_t::array
Table map-like<toml::key, toml::value> toml::value_t::table

array-like and map-like are the STL containers that works like a std::vector and std::unordered_map, respectively. By default, std::vector and std::unordered_map are used. See Customizing containers for detail.

toml::string is effectively the same as std::string but has an additional flag that represents a kind of a string, string_t::basic and string_t::literal. Although std::string is not an exact toml type, still you can get a reference that points to internal std::string by using toml::get<std::string>() for convenience. The most important difference between std::string and toml::string is that toml::string will be formatted as a TOML string when outputted with ostream. This feature is introduced to make it easy to write a custom serializer.

Datetime variants are struct that are defined in this library. Because std::chrono::system_clock::time_point is a time point, not capable of representing a Local Time independent from a specific day.

Unreleased TOML features

After TOML v1.0.0 has been released, some features are added to the main branch of the TOML spec repository. (see: CHANGELOG.md in toml-lang/toml repository).

The following list shows available "unreleased" features that can be activated by defining a macro named TOML11_USE_UNRELEASED_FEATURES.

  • Add new \e shorthand for the escape character.

Note about heterogeneous arrays

Although toml::parse allows heterogeneous arrays, constructor of toml::value does not. Here the reason is explained.

// this won't be compiled
toml::value v{
    "foo", 3.14, 42, {1,2,3,4,5}, {{"key", "value"}}
}

There is a workaround for this. By explicitly converting values into toml::value, you can initialize toml::value with a heterogeneous array. Also, you can first initialize a toml::value with an array and then push_back into it.

// OK!
toml::value v{
    toml::value("foo"), toml::value(3.14), toml::value(42),
    toml::value{1,2,3,4,5}, toml::value{{"key", "value"}}
}

// OK!
toml::value v(toml::array{});
v.push_back("foo");
v.push_back(3.14);

// OK!
toml::array a;
a.push_back("foo");
a.push_back(3.14);
toml::value v(std::move(a));

The reason why the first example is not allowed is the following. Let's assume that you are initializing a toml::value with a table.

                    // # expecting TOML table.
toml::value v{      // [v]
    {"answer", 42}, // answer = 42
    {"pi",   3.14}, // pi = 3.14
    {"foo", "bar"}  // foo = "bar"
};

This is indistinguishable from a (heterogeneous) TOML array definition.

v = [
    ["answer", 42],
    ["pi",   3.14],
    ["foo", "bar"],
]

This means that the above C++ code makes constructor's overload resolution ambiguous. So a constructor that allows both "table as an initializer-list" and "heterogeneous array as an initializer-list" cannot be implemented.

Thus, although it is painful, we need to explicitly cast values into toml::value when you initialize heterogeneous array in a C++ code.

toml::value v{
    toml::value("foo"), toml::value(3.14), toml::value(42),
    toml::value{1,2,3,4,5}, toml::value{{"key", "value"}}
};

Breaking Changes from v2

Although toml11 is relatively new library (it's three years old now), it had some confusing and inconvenient user-interfaces because of historical reasons.

Between v2 and v3, those interfaces are rearranged.

  • toml::parse now returns a toml::value, not toml::table.
  • toml::value is now an alias of toml::basic_value<discard_comment, std::vector, std::unordered_map>.
  • The elements of toml::value_t are renamed as snake_case.
  • Supports for the CamelCaseNames are dropped.
  • (is|as)_float has been removed to make the function names consistent with others.
    • Since float is a keyword, toml11 named a float type as toml::floating.
    • Also a value_t corresponds to toml::floating is named value_t::floating.
    • So (is|as)_floating is introduced and is_float has been removed.
    • See Casting a toml::value and Checking value type for detail.
  • An overload of toml::find for toml::table has been dropped. Use toml::value version instead.
    • Because type conversion between a table and a value causes ambiguity while overload resolution
    • Since toml::parse now returns a toml::value, this feature becomes less important.
    • Also because toml::table is a normal STL container, implementing utility function is easy.
    • See Finding a toml::value for detail.
  • An overload of operator<< and toml::format for toml::tables are dropped.
  • Interface around comments.
  • An ancient from_toml/into_toml has been removed. Use arbitrary type conversion support.

Such a big change will not happen in the coming years.

Running Tests

After cloning this repository, run the following command (thank you @jwillikers for automating test set fetching!).

$ mkdir build
$ cd build
$ cmake .. -Dtoml11_BUILD_TEST=ON
$ make
$ make test

To run the language agnostic test suite, you need to compile tests/check_toml_test.cpp and pass it to the tester.

Contributors

I appreciate the help of the contributors who introduced the great feature to this library.

  • Guillaume Fraux (@Luthaf)
    • Windows support and CI on Appvayor
    • Intel Compiler support
  • Quentin Khan (@xaxousis)
    • Found & Fixed a bug around ODR
    • Improved error messages for invalid keys to show the location where the parser fails
  • Petr Beneš (@wbenny)
    • Fixed warnings on MSVC
  • Ivan Shynkarenka (@chronoxor)
    • Fixed Visual Studio 2019 warnings
    • Fix compilation error in <filesystem> with MinGW
  • Khoi Dinh Trinh (@khoitd1997)
    • Fixed warnings while type conversion
  • @KerstinKeller
    • Added installation script to CMake
  • J.C. Moyer (@jcmoyer)
    • Fixed an example code in the documentation
  • Jt Freeman (@blockparty-sh)
    • Fixed feature test macro around localtime_s
    • Suppress warnings in Debug mode
  • OGAWA Kenichi (@kenichiice)
    • Suppress warnings on intel compiler
    • Fix include path in README
  • Jordan Williams (@jwillikers)
    • Fixed clang range-loop-analysis warnings
    • Fixed feature test macro to suppress -Wundef
    • Use cache variables in CMakeLists.txt
    • Automate test set fetching, update and refactor CMakeLists.txt
  • Scott McCaskill
    • Parse 9 digits (nanoseconds) of fractional seconds in a local_time
  • Shu Wang (@halfelf)
    • fix "Finding a value in an array" example in README
  • @maass-tv and @SeverinLeonhardt
    • Fix MSVC warning C4866
  • Mohammed Alyousef (@MoAlyousef)
    • Made testing optional in CMake
  • Alex Merry (@amerry)
    • Add missing include files
  • sneakypete81 (@sneakypete81)
    • Fix typo in error message
  • Oliver Kahrmann (@founderio)
    • Fix missing filename in error message if parsed file is empty
  • Karl Nilsson (@karl-nilsson)
    • Fix many spelling errors
  • ohdarling88 (@ohdarling)
    • Fix a bug in a constructor of serializer
  • estshorter (@estshorter)
    • Fix MSVC warning C26478
  • Philip Top (@phlptp)
    • Improve checking standard library feature availability check
  • Louis Marascio (@marascio)
    • Fix free-nonheap-object warning
  • Axel Huebl (@ax3l)
    • Make installation optional if the library is embedded
  • Ken Matsui (@ken-matsui)
    • Support user-defined error message prefix
    • Support dynamic color mode
  • Giel van Schijndel (@muggenhor)
    • Remove needless copy in parse function
  • Lukáš Hrázký (@lukash)
    • Add a parse(FILE *) interface and improve file-related error messages
  • spiderman idog (@spiderman-idog)
    • Fix typo in README
  • Jajauma's GitHub (@Jajauma)
    • Avoid possible lexer truncation warnings
  • Moritz Klammler (@ctcmkl)
    • Many patches in (#200) including:
    • Improve CMake scripts, build process, and test file handling
    • Detect error when discard_comments is accessed
    • And more.
  • Chris White (@cxw42)
    • Fix address-sanitizer error when parsing literal strings having invalid UTF-8 characters
    • Fix function name in error messages
  • offa (@offa)
    • Update checkout action to v3
    • Update Required CMake version
    • Cleanup old CI settings
  • Sergey Vidyuk (@VestniK)
    • Fix for case when vector iterator is raw pointer
  • Kfir Gollan (@kfirgollan)
    • Add installation example with checkinstall and cmake
  • Martin Tournoij (@arp242)
    • Escape control characters in keys
  • @DavidKorczynski
    • Add fuzzing test based on ClusterFuzzLite
  • Esonhugh Skyworship (@Esonhugh)
    • Fix function signature of strerror_r on macos

Licensing terms

This product is licensed under the terms of the MIT License.

  • Copyright (c) 2017-2024 Toru Niina

All rights reserved.

toml11's People

Contributors

amerry avatar arp242 avatar ax3l avatar blockparty-sh avatar chronoxor avatar ctcmkl avatar cxw42 avatar davidkorczynski avatar esonhugh avatar estshorter avatar founderio avatar gielvanschijndel-tomtom avatar jajauma avatar jcmoyer avatar jwillikers avatar karl-nilsson avatar ken-matsui avatar kenichiice avatar kerstinkeller avatar kfirgollan avatar lukash avatar luthaf avatar maass-tv avatar marascio avatar moalyousef avatar offa avatar phlptp avatar toruniina avatar vestnik avatar wbenny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

toml11's Issues

Compile error in test_get_or.cpp

I get the following compile error, which I believe may be related to boost version. I'm using boost 1.65.1 which is the libboost-dev version in Ubuntu 18.04. What version of Boost are you using?

/usr/include/boost/mpl/for_each.hpp: In instantiation of 'void boost::mpl::for_each(F, Sequence*, TransformOp*) [with Sequence = std::tuple<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, toml::basic_value<toml::preserve_comments, std::unordered_map, std::vector>, toml::basic_value<toml::discard_comments, std::map, std::deque>, toml::basic_value<toml::preserve_comments, std::map, std::deque> >; TransformOp = boost::mpl::make_identity<mpl_::arg<-1> >; F = boost::unit_test::ut_detail::generate_test_case_4_type<boost::unit_test::ut_detail::template_test_case_gen<test_get_or_fallback_invoker, std::tuple<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, toml::basic_value<toml::preserve_comments, std::unordered_map, std::vector>, toml::basic_value<toml::discard_comments, std::map, std::deque>, toml::basic_value<toml::preserve_comments, std::map, std::deque> > >, test_get_or_fallback_invoker>]':
/usr/include/boost/test/tree/test_case_template.hpp:117:65:   required from 'boost::unit_test::ut_detail::template_test_case_gen<TestCaseTemplate, TestTypesList>::template_test_case_gen(boost::unit_test::const_string, boost::unit_test::const_string, std::size_t) [with TestCaseTemplate = test_get_or_fallback_invoker; TestTypesList = std::tuple<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, toml::basic_value<toml::preserve_comments, std::unordered_map, std::vector>, toml::basic_value<toml::discard_comments, std::map, std::deque>, toml::basic_value<toml::preserve_comments, std::map, std::deque> >; boost::unit_test::const_string = boost::unit_test::basic_cstring<const char>; std::size_t = long unsigned int]'
/home/user/code/toml11/tests/test_get_or.cpp:182:1:   required from here
/usr/include/boost/mpl/for_each.hpp:99:5: error: no matching function for call to 'assertion_failed<false>(mpl_::failed************ boost::mpl::is_sequence<std::tuple<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, toml::basic_value<toml::preserve_comments, std::unordered_map, std::vector>, toml::basic_value<toml::discard_comments, std::map, std::deque>, toml::basic_value<toml::preserve_comments, std::map, std::deque> > >::************)'
     BOOST_MPL_ASSERT(( is_sequence<Sequence> ));
     ^

question and suggestion

After having used toml11 for a few days, I can say that its feature set is comprehensive and a lot of works has gone into making it. Thank you for that!! It will be very useful to me!

However, in terms of usability, I find access to values a bit tedious, with multiple methods of accessing data, some of which are quite tedious.

I have the following suggestion: Add operator[] to toml::value so that we can easily chain invocations using either string or numeric arguments like so:

	toml::value root;
	auto v = root["fruit"]["apple"]["colors"][1].as_string();

This could then also be used for setting values:

            root["fruit"]["apple"]["colors"][1] = "green";

For my own usage, I tried to add these operators, but the compiler is giving me an error which I'm not sure how to fix. Maybe it's an easy fix?
I think that this would not break any existing code and would provide a unified alternate method to access data in both arrays and tables!

I added this to class basic_value in value.hpp.

basic_value operator[](size_t pos)
{
	if (!is_array())
		throw std::exception("not an array, cannot access position: " + std::to_string(pos));
	return as_array().at(pos);
}

basic_value operator[](const std::string& key)
{
	if (!is_table())
		throw std::exception("not a table, cannot access key: " + key);
	return as_table().at(pos);
}

basic_value operator[](const char* key)
{
	if (!is_table())
		throw std::exception("not a table, cannot access key: " + std::string(key));
	return as_table().at(pos);
}

the compiler error is:
1>c:\users\ritte_pr_dev_\dev_projects_git\tsapi_unit_tests\source\test_23000_toml.cpp(399): error C2678: binary '[': no operator found which takes a left-hand operand of type 'const toml::value' (or there is no acceptable conversion)
1>c:\users\ritte_pr_dev_\dev_projects_git\tsapi_source\source\tsa\toml11\toml\value.hpp(1335): note: could be 'toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector::operator [](const char *)'
1>c:\users\ritte_pr_dev_\dev_projects_git\tsapi_source\source\tsa\toml11\toml\value.hpp(1328): note: or 'toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector::operator [](const std::string &)'
1>c:\users\ritte_pr_dev_\dev_projects_git\tsapi_source\source\tsa\toml11\toml\value.hpp(1321): note: or 'toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector toml::basic_valuetoml::discard_comments,std::unordered_map,std::vector::operator '
1>c:\users\ritte_pr_dev_\dev_projects_git\tsapi_unit_tests\source\test_23000_toml.cpp(399): note: while trying to match the argument list '(const toml::value, const char [6])'

Thank you for considering this!

Issue with intel compiler

I am using this great library for a computational fluid dynamics code. I use it to process my input files. It works without any issue with GNU compilers. When running simulations on intel-based clusters, it is often recommended by the provider to use intel compilers to get the most out of optimization.

I noticed some issues with the intel compiler icpc (ICC) 18.0.3 20180410. I managed to make it work by modifying some stuff in toml/comments.hpp but I have not tried to understand what I was really doing so I don't know if my modifications will have side-effects. Maybe you can solve this compatibility issue in a better way.

Basically I get these types of errors:

/nobackupp2/kbando/Libraries/toml11/toml/comments.hpp(86): error: no instance of overloaded function "std::vector<_Tp, _Alloc>::insert [with _Tp=std::string, _Alloc=std::allocator<std::string>]" matches the argument list
            argument types are: (toml::preserve_comments::const_iterator, const std::string)
            object type is: toml::preserve_comments::container_type
          return comments.insert(p, x);
/nobackupp2/kbando/Libraries/toml11/toml/comments.hpp(112): error: no instance of overloaded function "std::vector<_Tp, _Alloc>::erase [with _Tp=std::string, _Alloc=std::allocator<std::string>]" matches the argument list
            argument types are: (toml::preserve_comments::const_iterator)
            object type is: toml::preserve_comments::container_type
      iterator erase(const_iterator pos) {return comments.erase(pos);}

I solved those by using using const_iterator = container_type::iterator and make all those insert, emplace and erase methods return nothing (void).

Then because of the first modification, I had to explicitely specify

container_type::const_iterator begin()  const noexcept {return comments.begin();}
container_type::const_iterator end()    const noexcept {return comments.end();}
container_type::const_iterator cbegin() const noexcept {return comments.cbegin();}
container_type::const_iterator cend()   const noexcept {return comments.cend();}

Please find attached the stderr output I got when compiling from the official version.

Thanks!

log.txt

Modifying in place

How does mutating existing data work?

I could do:
toml::base loaded = toml::parse(...)
toml::value& table = toml::find(loaded, "mytable");
if (!table.is_initialized())
{
/* how to set table in loaded *?
loaded = toml::value {"mytable", {}}
}
The same thing for adding values into existing tables.

The scenario I'm wondering about is loading a file where you don't know the whole contents of the data, but you want to modify a given section and add it if missing....

Pass line number and filename for exception

Hi, while the default error message for things like toml::syntax_error is quite nice, I would prefer to output my own format and the format_error doesn't quite cut it, is it possible to pass data like line number, file name and the error message in an exception?

Parse error

[error] toml::parse_table: invalid line format
--> C:\test.toml
179 | x = 2.68

This is end of file with no "LF" or "CRLF" at the end of last line.
With "CRLF" at the end of last line I get this:

[error] toml::parse_key: the next token is not a key

With "LF" line ending it parses fine. Problem is that my Windows users and their editors will probably use "CRLF"...

test.toml contains a few tables and "CRLF" line ending.

I'm using Visual Studio 2017. My project is on C++17. Any help would be appreciated, everything else seems to work fine.

I guess the problem seems to be with unterminated strings and "CRLF".

One thing that did get my attention is that getting a float that's say "1" instead of "1.0" throws an error. It would be great if it parsed the "1" and cast it to "1.0" so the config file is even more noob friendly.
I'm a beginner so I don't know if it's a limitation. Anyways, thanks for your work and sharing!

Parsing a vector of floats

I'm not sure whether this is an issue or whether it is intended : I'm not too confident about how the spec says this should be handled.

When parsing into a std::vector<float>, an array value with integers raises an error.

# This wont work
test_array = [1, 2, 3]

# This will work
test_array = [1.0, 2.0, 3.0]

If you don't manage to reproduce, I will try to come up with a code example.

Custom conversion doesn't work

I tried to define a custom specialization of toml::from for my type, but it ends up with a compile time error.

Here's the code:

#include "toml11/toml.hpp"

enum class animal {
	DOG,
	CAT,
};

namespace toml {

template<>
struct from<animal> {
	animal from_toml(const value& value) {
		return animal::DOG;
	}
};

}

int main(int argc, char** argv) {
	auto data = toml::parse(argv[1]);
	auto a = toml::find<animal>(data, "a");
}

Apart from a bunch of template substitution failure errors, I get

toml11/toml/get.hpp:415:3: note: template argument deduction/substitution failed:
toml11/toml/get.hpp:259:26: error: invalid application of ‘sizeof’ to incomplete type ‘toml::into’
std::size_t S = sizeof(::toml::into)>
^~~~~~~~~~~~~~~~~~~~~~~
toml11/toml/get.hpp:259:48: error: could not convert template argument ‘’ from ‘’ to ‘long unsigned int’
std::size_t S = sizeof(::toml::into)>

Compiled with g++ 8.3.0 with -std=c++17.
Using the latest toml11 cloned from GitHub.

Is this a bug or am I doing something wrong?

find_or is ambiguous for double and bool

Calling find_or for double or bool is ambiguous while calling for int or string works well.

#include "../toml.hpp"
int main(){
  auto data = toml::parse("data.toml");
  auto N = toml::find_or(data, "int", 2);
  auto r = toml::find_or(data, "double", 1.0);
  auto b = toml::find_or(data, "bool", true);
  auto s = toml::find_or(data, "string", "foo");
}

Error message by Apple Clang is the following (GCC-9 says the similar message):

$ g++ --std=c++11 main.cpp
main.cpp:7:12: error: call to 'find_or' is ambiguous
  auto r = toml::find_or(data, "double", 1.0);
           ^~~~~~~~~~~~~
./../toml/get.hpp:588:5: note: candidate function [with T = double, $1 = nullptr]
T&& find_or(toml::value&& v, const toml::key& ky, T&& opt)
    ^
./../toml/get.hpp:661:10: note: candidate function [with T = double, $1 = nullptr]
T const& find_or(const toml::table& tab, const toml::key& ky, const T& opt)
         ^
./../toml/get.hpp:568:10: note: candidate function [with T = double, $1 = nullptr]
T const& find_or(const toml::value& v, const toml::key& ky, const T& opt)
         ^
main.cpp:8:12: error: call to 'find_or' is ambiguous
  auto b = toml::find_or(data, "bool", true);
           ^~~~~~~~~~~~~
./../toml/get.hpp:588:5: note: candidate function [with T = bool, $1 = nullptr]
T&& find_or(toml::value&& v, const toml::key& ky, T&& opt)
    ^
./../toml/get.hpp:661:10: note: candidate function [with T = bool, $1 = nullptr]
T const& find_or(const toml::table& tab, const toml::key& ky, const T& opt)
         ^
./../toml/get.hpp:568:10: note: candidate function [with T = bool, $1 = nullptr]
T const& find_or(const toml::value& v, const toml::key& ky, const T& opt)
         ^
2 errors generated.

Even if I declare data as toml::table explicitly, the problem still occurs.

Both the latest develop version (dee32e7) and the latest release (v2.3.1) of toml11 face this problem.

(By the way, could you put find_or into README?)

Multiple definition error when including toml in multiple translation units.

Including toml in multiple translation units gives the following error (sorry, linker compiled with french error messages).

file1.o:(.rodata+0x6) : définitions multiples de « toml::detail::toml_value_t<toml::local_date>::value »
file2.o:(.rodata+0x6) : défini pour la première fois ici
file1.o:(.rodata+0x7) : définitions multiples de « toml::detail::toml_value_t<toml::local_time>::value »
file2.o:(.rodata+0x7) : défini pour la première fois ici
file1.o:(.rodata+0x5) : définitions multiples de « toml::detail::toml_value_t<toml::local_datetime>::value »
file2.o:(.rodata+0x5) : défini pour la première fois ici
file1.o:(.rodata+0x4) : définitions multiples de « toml::detail::toml_value_t<toml::offset_datetime>::value »
file2.o:(.rodata+0x4) : défini pour la première fois ici
file1.o:(.rodata+0x3) : définitions multiples de « toml::detail::toml_value_t<toml::string>::value »
file2.o:(.rodata+0x3) : défini pour la première fois ici
file1.o:(.rodata+0x9) : définitions multiples de « toml::detail::toml_value_t<std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, toml::value, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, toml::value> > > >::value »
file2.o:(.rodata+0x9) : défini pour la première fois ici
file1.o:(.rodata+0x8) : définitions multiples de « toml::detail::toml_value_t<std::vector<toml::value, std::allocator<toml::value> > >::value »
file2.o:(.rodata+0x8) : défini pour la première fois ici
file1.o:(.rodata+0x0) : définitions multiples de « toml::detail::toml_value_t<bool>::value »
file2.o:(.rodata+0x0) : défini pour la première fois ici
file1.o:(.rodata+0x2) : définitions multiples de « toml::detail::toml_value_t<double>::value »
file2.o:(.rodata+0x2) : défini pour la première fois ici
file1.o:(.rodata+0x1) : définitions multiples de « toml::detail::toml_value_t<long>::value »
config.o:(.rodata+0x1) : défini pour la première fois ici

To fix this, you need to delete the following lines as those static constexpr class variable do not need to be defined.

toml11/toml/types.hpp

Lines 140 to 150 in 5dfdbe4

template<typename T> constexpr value_t toml_value_t<T>::value;
constexpr value_t toml_value_t<Boolean >::value;
constexpr value_t toml_value_t<Integer >::value;
constexpr value_t toml_value_t<Float >::value;
constexpr value_t toml_value_t<String >::value;
constexpr value_t toml_value_t<OffsetDatetime>::value;
constexpr value_t toml_value_t<LocalDatetime >::value;
constexpr value_t toml_value_t<LocalDate >::value;
constexpr value_t toml_value_t<LocalTime >::value;
constexpr value_t toml_value_t<Array >::value;
constexpr value_t toml_value_t<Table >::value;

OR

You can wrap those lines in an anonymous namespace to make them have static linkage.

toml11/toml/types.hpp

Lines 129 to 150 in 5dfdbe4

template<typename T> struct toml_value_t {static constexpr value_t value = value_t::Unknown ;};
template<> struct toml_value_t<Boolean >{static constexpr value_t value = value_t::Boolean ;};
template<> struct toml_value_t<Integer >{static constexpr value_t value = value_t::Integer ;};
template<> struct toml_value_t<Float >{static constexpr value_t value = value_t::Float ;};
template<> struct toml_value_t<String >{static constexpr value_t value = value_t::String ;};
template<> struct toml_value_t<OffsetDatetime>{static constexpr value_t value = value_t::OffsetDatetime;};
template<> struct toml_value_t<LocalDatetime >{static constexpr value_t value = value_t::LocalDatetime ;};
template<> struct toml_value_t<LocalDate >{static constexpr value_t value = value_t::LocalDate ;};
template<> struct toml_value_t<LocalTime >{static constexpr value_t value = value_t::LocalTime ;};
template<> struct toml_value_t<Array >{static constexpr value_t value = value_t::Array ;};
template<> struct toml_value_t<Table >{static constexpr value_t value = value_t::Table ;};
template<typename T> constexpr value_t toml_value_t<T>::value;
constexpr value_t toml_value_t<Boolean >::value;
constexpr value_t toml_value_t<Integer >::value;
constexpr value_t toml_value_t<Float >::value;
constexpr value_t toml_value_t<String >::value;
constexpr value_t toml_value_t<OffsetDatetime>::value;
constexpr value_t toml_value_t<LocalDatetime >::value;
constexpr value_t toml_value_t<LocalDate >::value;
constexpr value_t toml_value_t<LocalTime >::value;
constexpr value_t toml_value_t<Array >::value;
constexpr value_t toml_value_t<Table >::value;

I'd be happy to submit a pull request.

Clang Undefined Warnings

The following warnings are given by the -Wundef flag on Clang.

toml/datetime.hpp:23:5: error: '_POSIX_C_SOURCE' is not defined, evaluates to 0 [-Werror,-Wundef]
#if _POSIX_C_SOURCE >= 1 || defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_SOURCE)
    ^
toml/datetime.hpp:38:7: error: '_MSC_VER' is not defined, evaluates to 0 [-Werror,-Wundef]
#elif _MSC_VER

Should these be macros be checked to see if they are defined?

Does this library have any way to output TOML?

There are a handful of ostream extractor functions, but nothing I can find that converts a toml::table to a string or an ostream. This is a shame considering how nice the parsing is.

I would have used the boost version of this library, but didn't want to wait for it to compile the world. ;) I had to kill it when ninja had all 8 cores going and each instance of the compiler had 2+GB of memory.

test fails

I recently tried make test with the newest release v2.2.2, and the following tests fail:

The following tests FAILED:
	 26 - test_parse_file (Failed)
	 27 - test_serialize_file (Failed)
	 28 - test_parse_unicode (Failed)

Do these tests matter or are they important?

Using parse() fails on strings.

Consider:

std::string valid_toml("foo = 32\nbar = 3");
std::istringstream is(valid_toml, std::ios_base::binary | std::ios_base::in);
auto table = toml::parse(is, path.string());

This is when you need to pass not a file, but a string to be parsed. The parse step creates an invalid line error. I suspect it is because it doesn't check for the end of the string correctly, but I'm not sure. I tried various combinations of line endings, but maybe there is a simpler way to parse a string?

Doesn't correctly parse nan or inf.

The special values (taken from the TOML github page), are not parsed correctly:

infinity
sf1 = inf # positive infinity
sf2 = +inf # positive infinity
sf3 = -inf # negative infinity

not a number
sf4 = nan # actual sNaN/qNaN encoding is implementation specific
sf5 = +nan # same as nan
sf6 = -nan # valid, actual encoding is implementation specific

How to write/save toml file

In README, there are a lot of examples for reading/parsing, casting the value into the program. But I don't see any example to write to a toml file. Would you please provide where I can find them?
Thanks!

Including toml.hpp in multiple translation units

Hi, and thanks for the awesome project.

I don't know if I'm doing something wrong, but if I #include <toml11/toml.hpp> in more than one translation unit, I get linker errors:

manifest.obj : error LNK2005: "class std::unordered_map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class toml::value,struct std::hash<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,struct std::equal_to<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class toml::value> > > __cdecl toml::parse(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?parse@toml@@YA?AV?$unordered_map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@Vvalue@toml@@U?$hash@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@U?$equal_to@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@Vvalue@toml@@@std@@@2@@std@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@@Z) already defined in config.obj

I think I should only include a forward declaration of toml11 symbols in the second source file, but I could not find one.

I'm on Windows, compiling with Clang, using Visual Studio 2017 Community libraries and headers.

Thanks in advance.

Compile error on GCC 4.8.5 ( commit: 70d0049)

Cannot compile on Centos 7 with GCC 4.8.5:

command: g++ -std=c++11 toml.cpp

error info:

In file included from toml11/toml.hpp:37:0,
                 from toml.cpp:1:
toml11/toml/literal.hpp:14:22: error: missing space between ‘""’ and suffix identifier
 inline ::toml::value operator""_toml(const char* str, std::size_t len)
                      ^
toml.cpp:16:1: error: expected ‘}’ at end of input
 }
 ^
toml.cpp:16:1: error: expected ‘}’ at end of input
toml.cpp:16:1: error: expected ‘}’ at end of input

toml.cpp

#include "toml11/toml.hpp"
#include <iostream>

int main() {
  const auto data = toml::parse("example.toml");

  // title = "an example toml file"
  std::string title = toml::get<std::string>(data.at("title"));
  std::cout << "the title is " << title << std::endl;

  // nums = [1, 2, 3, 4, 5]
  std::vector<int> nums = toml::get<std::vector<int>>(data.at("nums"));
  std::cout << "the length of `nums` is" << nums.size() << std::endl;

  return 0;
}

Extract data from array of tables (C++)

What is the best method to output the name from the first table in the array below (name = "roll")?

When using toml::find to select a robot.segment table I have encountered an error saying it is a table, and then when treating it as a table, an error that says it is an array (see screenshots).

I have also tried changing the table names to robot_segment but still no success.

[[robot]]
name = "HelloBot"
[[robot.segment]]
name = "Roll"
type = "RotZ"
translation = [1, 2, 3]
rotation = [0.1, 0.2, 0.3]
limit = [-180, 180]

[[robot.segment]]
name = "Translation"
type = "TransZ"
translation = [0, 0, 10]
rotation = [0, 0, 0]
limit = [-40, 40]

[[robot.segment]]
name = "ElbowWrist"
type = "RotX"
translation = [4, 5, 6]
rotation = [1.1, 1.2, 1.3]
limit = [-90, 90]

Screenshot from 2019-10-30 15-53-39
Screenshot from 2019-10-30 15-55-27

Unused variable warning

toml/combinator.hpp:46:20: warning: 
      unused variable 'r' [-Wunused-variable]
        const auto r = std::snprintf(

This could be fixed by adding:

(void)r; // unused variable warning

Above the assert.

You could also just toss the sprintf inside the assert as well, which might be cleaner.

unknown table when using find without data.at(table)

Using the find function gives good log messages when there's a chance the config file might get broken from someone editing it. But it has unexpected behavior when using an already built toml::table value instead of data.at("table").

For example, when a key is not found in a subtable:

[table]
vallue = "num"

I would expect the following exmaples to be the same, but it is not the case as you can see from the output.

toml::table data = toml::parse(config_filename);
const auto table = toml::get<toml::table>(data.at("table"));
std::string value = toml::find<std::string>(table, "value");
[error] key "value" not found in unknown table

but by getting the (non-casted to toml::table, if I understand correctly) value from the table, it prints out a better error message

toml::table data = toml::parse(config_filename);
std::string value = toml::find<std::string>(data.at("table"), "value");
[error] key "value" not found
 --> ~/config.toml
 74 | [table]
    | ~~~~~~~ in this table

Support C++20 and std::u8string

When compiling for C++20, the following error occurs:

../tests/test_parse_unicode.cpp:51:23: error: no matching conversion for functional-style cast from 'const char8_t [53]' to 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >')
                      std::string(u8"Ýôú'ℓℓ λáƭè ₥è áƒƭèř ƭλïƨ - #"));

It looks like the introduction of std::u8string is causing problems for conversions between char8_t and std::string types.

I'm not sure the best way to handle this. My first though is to create a type alias which can be configured to std::string for C++11, C++14, and C++17 or std::u8string for C++20 and newer. That brings up an important question. Should the API for toml11 only support std::u8string for C++20 and beyond?

Set CMake Standard Cache Variables

When setting the CMake standard and extensions variables, cache variables are usually used instead of normal variables.
Also, CXX_STANDARD_REQUIRED is the target property, but I think that the CMakeLists.txt meant to set the CMAKE_CXX_STANDARD_REQUIRED variable.
Below is the current section in the CMakeLists.txt.

set(CMAKE_CXX_EXTENSIONS OFF)
if(NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 11)
endif()
set(CXX_STANDARD_REQUIRED ON)

I would propose to make the CMakeLists.txt more like the following:

set(CMAKE_CXX_STANDARD 11 CACHE STRING "The C++ standard whose features are requested to build all targets.")
set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE BOOL "Boolean describing whether the value of CXX_STANDARD is a requirement.")
set(CMAKE_CXX_EXTENSIONS OFF CACHE BOOL "Boolean specifying whether compiler specific extensions are requested.")

Preserving order for tables

Is there a way to control the order of how elements in a table are serialized? From the readme it seems the tables are implemented as unorderd_map, so I guess that currently results in a (more or less) random order.

For example, it would be nice to optionally have the output preserve the order of the how keys are entered into the table.

For example in https://github.com/nlohmann/json this is realized by providing a template argument that specifies the type used internally to store the table, where one can specify a map data structure that preserves order, see nlohmann/json#546 (comment) and nlohmann/json#485 (comment)

2 tests failed out of 35 on Fedora 31 with GCC 9

I cloned toml-lang as described in README.md. I still get:

$ ninja test
[0/1] Running tests...
Test project /home/vedranmiletic/workspace/toml11/build
      Start  1: test_datetime
 1/35 Test  #1: test_datetime ....................   Passed    0.01 sec
      Start  2: test_string
 2/35 Test  #2: test_string ......................   Passed    0.00 sec
      Start  3: test_utility
 3/35 Test  #3: test_utility .....................   Passed    0.00 sec
      Start  4: test_result
 4/35 Test  #4: test_result ......................   Passed    0.00 sec
      Start  5: test_traits
 5/35 Test  #5: test_traits ......................   Passed    0.00 sec
      Start  6: test_value
 6/35 Test  #6: test_value .......................   Passed    0.01 sec
      Start  7: test_lex_boolean
 7/35 Test  #7: test_lex_boolean .................   Passed    0.00 sec
      Start  8: test_lex_integer
 8/35 Test  #8: test_lex_integer .................   Passed    0.00 sec
      Start  9: test_lex_floating
 9/35 Test  #9: test_lex_floating ................   Passed    0.00 sec
      Start 10: test_lex_datetime
10/35 Test #10: test_lex_datetime ................   Passed    0.00 sec
      Start 11: test_lex_string
11/35 Test #11: test_lex_string ..................   Passed    0.01 sec
      Start 12: test_lex_key_comment
12/35 Test #12: test_lex_key_comment .............   Passed    0.00 sec
      Start 13: test_parse_boolean
13/35 Test #13: test_parse_boolean ...............   Passed    0.00 sec
      Start 14: test_parse_integer
14/35 Test #14: test_parse_integer ...............   Passed    0.00 sec
      Start 15: test_parse_floating
15/35 Test #15: test_parse_floating ..............   Passed    0.00 sec
      Start 16: test_parse_string
16/35 Test #16: test_parse_string ................   Passed    0.01 sec
      Start 17: test_parse_datetime
17/35 Test #17: test_parse_datetime ..............   Passed    0.00 sec
      Start 18: test_parse_array
18/35 Test #18: test_parse_array .................   Passed    0.00 sec
      Start 19: test_parse_table
19/35 Test #19: test_parse_table .................   Passed    0.00 sec
      Start 20: test_parse_inline_table
20/35 Test #20: test_parse_inline_table ..........   Passed    0.00 sec
      Start 21: test_parse_key
21/35 Test #21: test_parse_key ...................   Passed    0.00 sec
      Start 22: test_parse_table_key
22/35 Test #22: test_parse_table_key .............   Passed    0.00 sec
      Start 23: test_literals
23/35 Test #23: test_literals ....................   Passed    0.00 sec
      Start 24: test_comments
24/35 Test #24: test_comments ....................   Passed    0.00 sec
      Start 25: test_get
25/35 Test #25: test_get .........................***Failed    0.01 sec
      Start 26: test_get_or
26/35 Test #26: test_get_or ......................   Passed    0.01 sec
      Start 27: test_find
27/35 Test #27: test_find ........................***Failed    0.01 sec
      Start 28: test_find_or
28/35 Test #28: test_find_or .....................   Passed    0.01 sec
      Start 29: test_expect
29/35 Test #29: test_expect ......................   Passed    0.00 sec
      Start 30: test_parse_file
30/35 Test #30: test_parse_file ..................   Passed    0.11 sec
      Start 31: test_serialize_file
31/35 Test #31: test_serialize_file ..............   Passed    0.07 sec
      Start 32: test_parse_unicode
32/35 Test #32: test_parse_unicode ...............   Passed    0.01 sec
      Start 33: test_error_detection
33/35 Test #33: test_error_detection .............   Passed    0.00 sec
      Start 34: test_format_error
34/35 Test #34: test_format_error ................   Passed    0.00 sec
      Start 35: test_extended_conversions
35/35 Test #35: test_extended_conversions ........   Passed    0.00 sec

94% tests passed, 2 tests failed out of 35

Total Test time (real) =   0.34 sec

The following tests FAILED:
	 25 - test_get (Failed)
	 27 - test_find (Failed)
Errors while running CTest
FAILED: CMakeFiles/test.util 
cd /home/vedranmiletic/workspace/toml11/build && /usr/bin/ctest --force-new-ctest-process
ninja: build stopped: subcommand failed.

Details:

$ ./tests/test_get
Running 44 test cases...
../tests/test_get.cpp(481): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_get.cpp(504): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_get.cpp(481): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_get.cpp(504): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_get.cpp(481): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__map, std__deque>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_get.cpp(504): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_get.cpp(481): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__map, std__deque>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_get.cpp(504): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]

*** 8 failures are detected in the test module "test_get"
$ ./tests/test_find
Running 54 test cases...
../tests/test_find.cpp(641): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_find.cpp(664): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(687): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(641): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_find.cpp(664): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(687): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__unordered_map, std__vector>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(641): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__map, std__deque>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_find.cpp(664): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(687): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__discard_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(641): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__map, std__deque>>": check tm.tm_hour == 3 has failed [2 != 3]
../tests/test_find.cpp(664): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]
../tests/test_find.cpp(687): error: in "test_get_toml_offset_datetime<toml__basic_value<toml__preserve_comments, std__map, std__deque>>": check tm.tm_hour == 20 has failed [19 != 20]

*** 12 failures are detected in the test module "test_find"

error: no match for 'operator<<'

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>} ' and 'const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >') std::cout << data << std::endl; Can you assist me with this error?

I've only added in the following code const auto data = toml::table{{"foo", 42}, {"bar", "baz"}}; std::cout << data << std::endl; from the README

I imported the header library via #include "toml11/toml.hpp" instead of installing it and importing it. Could this be the reason?

python binding?

This is a feature suggestion: as all current python implementation of toml are in pure python and potentially slow, shall we try use pybind11 and have a python binding for this, to give user more choice when it comes to python toml libraries?

table parse problem

Hi! Thanks for your library.
I found a small problem with dot key tables.

Suppose we have a dot key table

fruit.apple="red"

auto table=toml::find(data,"fruit");
auto color=toml::find<std::string>(table,"apple");

works.

However,
auto color=toml::find<std::string>(table,"fruit","apple")
fails.

find string from table compile failed with gcc 5.4

These are test TOML file, c++ source code and g++ version.

$ cat sample.toml 
[physical]
color = "orange"
shape = "round"
$ cat sample.cc 
#include <iostream>
#include "toml.hpp"

int main(int argc, char* argv[]) {
  const auto data = toml::parse("sample.toml");
  const auto physical = toml::find<toml::table>(data, "physical");
  const auto color = toml::find<std::string>(physical, "color");
  std::cout << color << std::endl;
  return 0;
}
$ g++ --version
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Now compile it, failed ……

$ g++ sample.cc -std=c++11
sample.cc: In function ‘int main(int, char**)’:
sample.cc:7:63: error: no matching function for call to ‘find(const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >&, const char [6])’
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:430:29: note: candidate: template<class C, template<class ...> class M, template<class ...> class V> const toml::basic_value<C, M, V>& toml::find(const toml::basic_value<C, M, V>&, const key&)
 basic_value<C, M, V> const& find(const basic_value<C, M, V>& v, const key& ky)
                             ^
toml/get.hpp:430:29: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ is not derived from ‘const toml::basic_value<std::__cxx11::basic_string<char>, M, V>’
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:444:23: note: candidate: template<class C, template<class ...> class M, template<class ...> class V> toml::basic_value<C, M, V>& toml::find(toml::basic_value<C, M, V>&, const key&)
 basic_value<C, M, V>& find(basic_value<C, M, V>& v, const key& ky)
                       ^
toml/get.hpp:444:23: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<std::__cxx11::basic_string<char>, M, V>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:458:24: note: candidate: template<class C, template<class ...> class M, template<class ...> class V> toml::basic_value<C, M, V>&& toml::find(toml::basic_value<C, M, V>&&, const key&)
 basic_value<C, M, V>&& find(basic_value<C, M, V>&& v, const key& ky)
                        ^
toml/get.hpp:458:24: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<std::__cxx11::basic_string<char>, M, V>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:477:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V> decltype (get<T>(declval<const toml::basic_value<C, Tb, A>&>())) toml::find(const toml::basic_value<C, Tb, A>&, const key&)
 find(const basic_value<C, M, V>& v, const key& ky)
 ^
toml/get.hpp:477:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ is not derived from ‘const toml::basic_value<C, Tb, A>’
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:493:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V> decltype (get<T>(declval<toml::basic_value<C, Tb, A>&>())) toml::find(toml::basic_value<C, Tb, A>&, const key&)
 find(basic_value<C, M, V>& v, const key& ky)
 ^
toml/get.hpp:493:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<C, Tb, A>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:509:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V> decltype (get<T>(declval<toml::basic_value<C, Tb, A>&&>())) toml::find(toml::basic_value<C, Tb, A>&&, const key&)
 find(basic_value<C, M, V>&& v, const key& ky)
 ^
toml/get.hpp:509:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<C, Tb, A>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:529:1: note: candidate: template<class C, template<class ...> class M, template<class ...> class V, class ... Ts> const toml::basic_value<C, M, V>& toml::find(const toml::basic_value<C, M, V>&, const key&, Ts&& ...)
 find(const basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:529:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ is not derived from ‘const toml::basic_value<std::__cxx11::basic_string<char>, M, V>’
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:537:1: note: candidate: template<class C, template<class ...> class M, template<class ...> class V, class ... Ts> toml::basic_value<C, M, V>& toml::find(toml::basic_value<C, M, V>&, const key&, Ts&& ...)
 find(basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:537:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<std::__cxx11::basic_string<char>, M, V>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:545:1: note: candidate: template<class C, template<class ...> class M, template<class ...> class V, class ... Ts> toml::basic_value<C, M, V>&& toml::find(toml::basic_value<C, M, V>&&, const key&, Ts&& ...)
 find(basic_value<C, M, V>&& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:545:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<std::__cxx11::basic_string<char>, M, V>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:554:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V, class ... Ts> decltype (get<T>(declval<const toml::basic_value<C, Tb, A>&>())) toml::find(const toml::basic_value<C, Tb, A>&, const key&, Ts&& ...)
 find(const basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:554:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ is not derived from ‘const toml::basic_value<C, Tb, A>’
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:562:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V, class ... Ts> decltype (get<T>(declval<toml::basic_value<C, Tb, A>&>())) toml::find(toml::basic_value<C, Tb, A>&, const key&, Ts&& ...)
 find(basic_value<C, M, V>& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:562:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<C, Tb, A>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");
                                                               ^
In file included from toml.hpp:39:0,
                 from sample.cc:2:
toml/get.hpp:570:1: note: candidate: template<class T, class C, template<class ...> class M, template<class ...> class V, class ... Ts> decltype (get<T>(declval<toml::basic_value<C, Tb, A>&&>())) toml::find(toml::basic_value<C, Tb, A>&&, const key&, Ts&& ...)
 find(basic_value<C, M, V>&& v, const ::toml::key& ky, Ts&& ... keys)
 ^
toml/get.hpp:570:1: note:   template argument deduction/substitution failed:
sample.cc:7:63: note:   types ‘toml::basic_value<C, Tb, A>’ and ‘const std::unordered_map<std::__cxx11::basic_string<char>, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> >’ have incompatible cv-qualifiers
   const auto color = toml::find<std::string>(physical, "color");

But a range loop of physical works well:

  for (const auto& kv : physical)
    std::cout << kv.first << " => " << kv.second << std::endl;

Output is:

shape => "round"
color => "orange"

GDB debug info:

(gdb) p kv
$1 = (const std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> > &) @0x6bd1c8: {first = "shape", 
  second = {type_ = toml::value_t::string, {boolean_ = false, integer_ = 0, floating_ = 0, string_ = {
        kind = toml::string_t::basic, str = "round"}, offset_datetime_ = {date = {year = 0, 
          month = 0 '\000', day = 0 '\000'}, time = {hour = 0 '\000', minute = 0 '\000', second = 0 '\000', 
          millisecond = 53768, microsecond = 107, nanosecond = 0}, offset = {hour = 0 '\000', 
          minute = 0 '\000'}}, local_datetime_ = {date = {year = 0, month = 0 '\000', day = 0 '\000'}, 
        time = {hour = 0 '\000', minute = 0 '\000', second = 0 '\000', millisecond = 53768, 
          microsecond = 107, nanosecond = 0}}, local_date_ = {year = 0, month = 0 '\000', day = 0 '\000'}, 
      local_time_ = {hour = 0 '\000', minute = 0 '\000', second = 0 '\000', millisecond = 0, 
        microsecond = 0, nanosecond = 53768}, array_ = {
        ptr = std::unique_ptr<std::vector<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, std::allocator<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> > >> containing 0x0}, table_ = {
        ptr = std::unique_ptr<std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> > > >> containing 0x0}}, 
    region_info_ = std::shared_ptr (count 2, weak 0) 0x6bd040, comments_ = {<No data fields>}}}
(gdb) p kv.first
$2 = "shape"
(gdb) p kv.second 
$3 = {type_ = toml::value_t::string, {boolean_ = false, integer_ = 0, floating_ = 0, string_ = {
      kind = toml::string_t::basic, str = "round"}, offset_datetime_ = {date = {year = 0, month = 0 '\000', 
        day = 0 '\000'}, time = {hour = 0 '\000', minute = 0 '\000', second = 0 '\000', millisecond = 53768, 
        microsecond = 107, nanosecond = 0}, offset = {hour = 0 '\000', minute = 0 '\000'}}, 
    local_datetime_ = {date = {year = 0, month = 0 '\000', day = 0 '\000'}, time = {hour = 0 '\000', 
        minute = 0 '\000', second = 0 '\000', millisecond = 53768, microsecond = 107, nanosecond = 0}}, 
    local_date_ = {year = 0, month = 0 '\000', day = 0 '\000'}, local_time_ = {hour = 0 '\000', 
      minute = 0 '\000', second = 0 '\000', millisecond = 0, microsecond = 0, nanosecond = 53768}, array_ = {
      ptr = std::unique_ptr<std::vector<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, std::allocator<toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> > >> containing 0x0}, table_ = {
      ptr = std::unique_ptr<std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector>, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, toml::basic_value<toml::discard_comments, std::unordered_map, std::vector> > > >> containing 0x0}}, 
  region_info_ = std::shared_ptr (count 2, weak 0) 0x6bd040, comments_ = {<No data fields>}}

Question regarding include

Hi, thank you for the work you have been putting into toml11, it's really nice. I just want to have a quick question about including toml11 in a project, currently #include "toml11/toml.hpp" is used, I was wondering why is it not simply #include "toml11.hpp"? Are the files in the directory toml potentially necessary for using the library?

internal compiler error with gcc-7.3.1

Hi!

With gcc-7.3.1, I am suddenly getting an internal compiler error when using toml11. With gcc-7.2, everything still seems fine.

[ 12%] Building CXX object examples/CMakeFiles/fit-model.dir/fit-model.cpp.o
In file included from /home/patrik/eos/3rdparty/toml11/toml.hpp:39:0,
                 from /home/patrik/eos/include/eos/core/LandmarkMapper.hpp:27,
                 from /home/patrik/eos/examples/fit-model.cpp:23:
/home/patrik/eos/3rdparty/toml11/toml/get.hpp: In substitution of ‘template<class T, toml::value_t vT, typename std::enable_if<((vT == Unknown) && toml::detail::is_map<T>::value), std::nullptr_t>::type <anonymous> > T toml::get(const toml::value&) [with T = std::unordered_map<std::__cxx11::basic_string<char>, toml::value>; toml::value_t vT = <missing>; typename std::enable_if<((vT == Unknown) && toml::detail::is_map<T>::value), std::nullptr_t>::type <anonymous> = <missing>]’:
/home/patrik/eos/include/eos/core/LandmarkMapper.hpp:77:88:   required from here
/home/patrik/eos/3rdparty/toml11/toml/get.hpp:44:69: internal compiler error: unexpected expression ‘check_type<std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, toml::value, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, toml::value> > > >’ of kind template_id_expr
 template<typename T, toml::value_t vT = toml::detail::check_type<T>(),

The error seems to happen just from the #include "toml.hpp".

I'm building the default example from my open-source library eos.

Questions regarding warnings

Hi, I noticed that toml11 warnings mostly come from type conversion warnings such as using size_t in places that use int. Are they deliberate choices? The ones that I have looked at don't seem to look that serious so I want to put PRs in to make the warning list cleaner and thus making it easier to use with projects that have stringent compilation requirements like -Werror without adding additional options for toml11 in CMake such as making it be considered as a system header. Thank you for your work, toml11 is a joy to use.

using numbers as find() argument

the toml::find() function seems to allow any number of strings (keys) as argument but no numbers. So to access values withing tables of tables of tables this seems to work, such as

int max_conn = toml::find(toml_root, "database", "connection_max");

but what if I want to access an array element? Then I can't use a string key - I'd have to use a positional number, like:

int port_2 = toml::find(toml_root, "database", "ports", 2 );

where "ports" refers to an array of port numbers - and I want to retrieve the port at position 2. Is there any reason why arguments to toml::find() can only be strings? I think it would be great if every value in a toml file had its own 'path' consiting of strings and numbers, so that we can access each value directly without roundabout syntax. This approach could also be used for modifying values.

transport type for toml values

I looked around for a ini file library and found your library recently.
It seems to be exactly what I am looking for.
There is a little problem though. Including toml in all our headers would let our compile times explode, so I am looking for a way to get around that.

My current idea would be, how about if there was a separate header, containing a toml::erased_value type, and this header is absolutely minimal.
The value is defined there and only supports copy, move, and all semantic operations, but no construction besides emtpy default-construction and nothing else.
Ideally this type is not even templated.
In the main header of toml there are explicit conversion functions defined, which allow me to convert a toml::Data or toml::Value into this type and back.

Then I would be able to only include the minimal header for the erased value in my API part, keeping the compile times low.
In the few places where I actually want to parse or read the values, I would then include the full library and then do the conversion and my work.

Is such a container type easy to build? How would one do this?

add option to load default values

Hello!
It would be awesome to have the option to load default Values. That if a key isn't found in the toml-file, the default value is taken.

Best Regards!
Thomas

Clang Range Loop Analysis - Use Reference Type

Clang issues the following warning:

toml-src/toml/region.hpp:514:24: error: loop variable 'help' of type 'const std::__1::basic_string<char>' creates a copy from type 'const std::__1::basic_string<char>' [-Werror,-Wrange-loop-analysis]
        for(const auto help : helps)
                       ^
toml-src/toml/region.hpp:514:13: note: use reference type 'const std::__1::basic_string<char> &' to prevent copying
        for(const auto help : helps)

Would it be possible to fix this warning by passing by reference in the for loop?

for(const auto &help : helps)

Key parsing errors should show the error position

When parsing an invalid file, the error should show where the parser died.

For example the following raises an exception toml::syntax_error with the message [error] toml::parse_key: the next token is not a key. There is no way to get where what went wrong in the file.

[ a test]

For the specific issue, I narrowed the code down to :

toml11/toml/parser.hpp

Lines 820 to 826 in 83bf83b

// simple key -> foo
if(const auto smpl = parse_simple_key(loc))
{
return ok(std::vector<key>(1, smpl.unwrap()));
}
return err(std::string("[error] toml::parse_key: "
"the next token is not a key"));

We can replace the last line with:

    return err(format_underline("[error] toml::parse_key: ", loc, "is not a valid key"));

Which then would store the following message in the exception:

[error] toml::parse_key: 
 --> ../tests/res/dev.toml
 41 | [a test]
    | ^---------- is not a valid key

An other solution would be to throw a syntax_error directly, as done there:

toml11/toml/parser.hpp

Lines 898 to 899 in 83bf83b

throw syntax_error(format_underline("[error] toml::parse_array: "
"array did not closed by `]`", loc, "should be closed"));

There are several other places where this kind of behaviour could be implemented.

Error reporting

The error reporting is nice, but for tools that need to present parse errors to users, it would be nice if the parse_exception could contain a list of line numbers and error strings associated with them. That way a tool that compiles a toml file can highlight and show line errors without having to parse the whole syntax error string.

Unhandled exception in parser

Hi,

I've noticed that inside the parser.hpp you throw some std::pairs as exceptions, then you catch them inside the parse function and rethrow only the syntax_error part.

That's fine, but in one of my tests, I used an invalid toml file (with two "[table]", instead of "[[table]]") and I've got an unhandled exception.

The code inside the parse function is like this (lines 1081-1088):

     // ...
    typedef std::vector<toml::character>::const_iterator iterator_type;
    is.read(contents.data(), size);
    try
    {
        return parse_data::invoke(contents.cbegin(), contents.cend());
    }
    catch(std::pair<iterator_type, syntax_error> iter_except)
    {
     // ...

But in this specific case, the exception being thrown is not caught by that. I managed to catch it with code like this:

  toml::Table data;
  typedef std::vector<std::string>::iterator iterator_type;
  try {
    data = parse(path.string());
  } catch(const std::pair<iterator_type, syntax_error>& e) {
    fatal(e.second.what());
  }

Note the different iterator_type.

Maybe there is something missing in the parse method...

include toml.hpp in example is incorrect?

Hi,

The example in the README currently shows:

#include <toml11/toml.hpp>
#include <iostream>

But that file doesn't exist with my CMake installed library. I have to use #include <toml.hpp>. Maybe the example[s] needs an update?

std::localtime error in VS2017.3 (Preview)

Hi!

Thanks for this great project, it seems like one of the best header-only modern C++ TOML APIs out there! :-)

I've got a compilation error when compiling with VS2017, 15.3.0 Preview:

Severity Code Description Project File Line Suppression State
Error C4996 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. toml-test projects\toml-test\toml11\toml\datetime.hpp 71

The line in question is std::tm* t_ = std::localtime(&t);.

I think this is probably a longstanding warning that got turned into an error in VS2017.
I tried to fix it but haven't gotten too far yet: Changing it into std::localtime_s doesn't work as that function doesn't exist - there exists one called localtime_s from C11 (without the std:: prefix) but it does not take 1 argument.

Fedora Package

Hi, my name is Khoi Trinh, I recently started using TOML with C++ and find your library really helpful, as such, I want to help make it a Fedora package so that other users can simply do #include <toml11/toml.hpp> after a quick installation using dnf. Is there any potential issues or major features that toml11 is expecting? Thank you for your work in this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.