doc: add reference manual

This commit is contained in:
ToruNiina
2024-06-15 19:27:42 +09:00
parent 7210e708a6
commit 64197caa05
77 changed files with 18139 additions and 0 deletions

View File

@@ -0,0 +1,122 @@
+++
title = "reference"
type = "docs"
weight = 3
bookCollapseSection = true
+++
# Reference
Here, we explain the effects of the classes and functions provided by toml11.
## Directory Structure
`toml.hpp` and `toml_fwd.hpp` reside in `${TOML11_INCLUDE_DIR}`.
Other files are located in `${TOML11_INCLUDE_DIR}/toml11`.
If you want to `#include` each feature's file individually, use `#include <toml11/color.hpp>`.
If you want to include all at once, use `#include <toml.hpp>`.
## [color.hpp](color)
Defines functions related to colorizing error messages.
## [comments.hpp](comments)
Defines types `preserve_comment` and `discard_comment` for preserving comments.
## [conversion.hpp](conversion)
Defines macros to automatically convert `toml::value` and user-defined classes.
## [datetime.hpp](datetime)
Defines classes for datetime information.
## [error_info.hpp](error_info)
Defines a class for error information.
## [exception.hpp](exception)
Defines the base class for exceptions used in toml11, `toml::exception`.
## [find.hpp](find)
Defines the `toml::find` function to search for and convert values.
## [format.hpp](format)
Defines classes for formatting information of values.
## [from.hpp](from)
Forward declaration of the `from<T>` type for converting user-defined types.
## [get.hpp](get)
Defines the `toml::get<T>` function to retrieve and convert values from `toml::value`.
## [into.hpp](into)
Forward declaration of the `into<T>` type for converting user-defined types.
## [literal.hpp](literal)
Defines the `operator"" _toml` literal.
## [ordered_map.hpp](ordered_map)
Defines `toml::ordered_map`.
## [parser.hpp](parser)
Defines functions to parse files or strings.
## [result.hpp](result)
Defines the `result<T, E>` type for representing success or failure values used as return types in other functions.
## [serializer.hpp](serializer)
Defines the `toml::format` function and `toml::serializer` used for serialization.
## [source_location.hpp](source_location)
Defines the `source_location` type used for error information, pointing to a location within a file.
## [spec.hpp](spec)
Defines the `toml::semantic_version` and `toml::spec` types to control TOML language version information and feature flags.
## [toml.hpp](toml)
`toml.hpp` includes all other headers, making all toml11 features available.
## [toml_fwd.hpp](toml_fwd)
`toml_fwd.hpp` contains forward declarations of structs defined in toml11 and macro definitions.
## [types.hpp](types)
Defines the `toml::type_config` type for controlling the types held by `toml::value`.
## [value.hpp](value)
Defines the `toml::value` type.
## [value_t.hpp](value_t)
Defines the `toml::value_t` enumeration.
## [version.hpp](version)
Defines the version information for toml11.
## [visit.hpp](visit)
Defines the `toml::visit` function to apply functions to the values held by `toml::value`.
## Notes
Functions not explicitly mentioned here (mostly those defined under `namespace toml::detail` or `namespace toml::cxx`) are available by inspecting the source code but are not guaranteed to maintain their interface across future versions (including patch version updates).

View File

@@ -0,0 +1,138 @@
+++
title = "color.hpp"
type = "docs"
+++
# color.hpp
In `color.hpp`, functions related to colorizing error messages are defined.
Colors are specified using ANSI escape code.
In terminals or other output destinations that do not support ANSI escape code, the output may become difficult to read.
## Macros
```cpp
TOML11_COLORIZE_ERROR_MESSAGE
```
If this macro is defined during compilation (`-DTOML11_COLORIZE_ERROR_MESASGE`), error messages are colored by default.
If not defined, colors are not applied by default. You need to specify them using `toml::color::enable()`.
## Functions
### `enable()`
```cpp
namespace toml {
namespace color {
void enable();
} // color
} // toml
```
Enables colorization using ANSI escape code.
#### Example
```cpp
#include <toml.hpp>
int main()
{
toml::color::enable(); // All subsequent errors will be colored.
const auto input = toml::parse("input.toml");
return 0;
}
```
### `disable()`
```cpp
namespace toml {
namespace color {
void disable();
} // color
} // toml
```
Disables colorization using ANSI escape code.
#### Example
```cpp
#include <toml.hpp>
int main()
{
toml::color::disable(); // All subsequent errors will not be colored.
const auto input = toml::parse("input.toml");
return 0;
}
```
### `should_color()`
```cpp
namespace toml {
namespace color {
bool should_color();
} // color
} // toml
```
Returns `true` if colorization is enabled, `false` otherwise.
#### Example
```cpp
#include <toml.hpp>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "colorized? : " << std::boolalpha << toml::color::should_color() << std::endl;
return 0;
}
```
## Manipulators
```cpp
namespace toml {
namespace color {
std::ostream& reset (std::ostream&);
std::ostream& bold (std::ostream&);
std::ostream& grey (std::ostream&);
std::ostream& gray (std::ostream&);
std::ostream& red (std::ostream&);
std::ostream& green (std::ostream&);
std::ostream& yellow (std::ostream&);
std::ostream& blue (std::ostream&);
std::ostream& magenta(std::ostream&);
std::ostream& cyan (std::ostream&);
std::ostream& white (std::ostream&);
} // color
} // toml
```
Colorizes the foreground with ANSI escape code.
#### Example
```cpp
#include <toml.hpp>
#include <iostream>
int main()
{
std::cout << toml::color::red << "red!" << toml::color::reset << std::endl;
return 0;
}
```
# Related
- [error_info.hpp]({{<ref "error_info.md">}})

View File

@@ -0,0 +1,776 @@
+++
title = "comments.hpp"
type = "docs"
+++
# comments.hpp
In `comments.hpp`, comment containers are provided.
# `toml::preserve_comments`
`preserve_comments` is a container that preserves comments.
It has all the member functions of `std::vector<std::string>`.
Comments are preserved as `std::string`.
If the comment does not start with `#`, it will be prefixed with `#` during output.
However, this prefixing is not done when adding comments to the container.
Spaces are not automatically added, so if you want a space immediately after `#`,
either start the comment with a space or pass the comment with `#`.
```cpp
namespace toml
{
class preserve_comments;
bool operator==(const preserve_comments&, const preserve_comments&);
bool operator!=(const preserve_comments&, const preserve_comments&);
bool operator< (const preserve_comments&, const preserve_comments&);
bool operator<=(const preserve_comments&, const preserve_comments&);
bool operator> (const preserve_comments&, const preserve_comments&);
bool operator>=(const preserve_comments&, const preserve_comments&);
void swap(preserve_comments&, preserve_comments&);
void swap(preserve_comments&, std::vector<std::string>&);
void swap(std::vector<std::string>&, preserve_comments&);
std::ostream& operator<<(std::ostream&, const preserve_comments&);
} //toml
```
## Member types
```cpp
using container_type = std::vector<std::string>;
using size_type = container_type::size_type;
using difference_type = container_type::difference_type;
using value_type = container_type::value_type;
using reference = container_type::reference;
using const_reference = container_type::const_reference;
using pointer = container_type::pointer;
using const_pointer = container_type::const_pointer;
using iterator = container_type::iterator;
using const_iterator = container_type::const_iterator;
using reverse_iterator = container_type::reverse_iterator;
using const_reverse_iterator = container_type::const_reverse_iterator;
```
## Member Functions
### Default Constructor
```cpp
preserve_comments() = default;
```
Constructs an empty `preserve_comments`.
### Copy and Move Constructors
```cpp
preserve_comments(preserve_comments const&) = default;
preserve_comments(preserve_comments &&) = default;
```
Constructs a `preserve_comments` by copying or moving.
### Constructor (`std::vector<std::string>`)
```cpp
explicit preserve_comments(const std::vector<std::string>& c);
explicit preserve_comments(std::vector<std::string>&& c);
```
Constructs a `preserve_comments` holding the contents of `std::vector<std::string>`.
### Constructor (`discard_comments`)
```cpp
explicit preserve_comments(const discard_comments&);
```
Constructs an empty `preserve_comments`.
### Constructor (`Iterator`)
```cpp
template<typename InputIterator>
preserve_comments(InputIterator first, InputIterator last);
```
Constructs a `preserve_comments` from the range represented by `InputIterator` pointing to `std::string`.
### Constructor (`std::initializer_list`)
```cpp
preserve_comments(std::initializer_list<std::string> x);
```
Constructs a `preserve_comments` from the range represented by `std::initializer_list<std::string>`.
### Constructor (Size Specified)
```cpp
explicit preserve_comments(size_type n);
preserve_comments(size_type n, const std::string& x);
```
Constructs a `preserve_comments` with `n` comments.
If a `std::string` is passed, it replicates that comment `n` times.
### Destructor
```cpp
~preserve_comments() = default;
```
Destroys the `preserve_comments`.
### `operator=(preserve_comments)`
```cpp
preserve_comments& operator=(preserve_comments const&) = default;
preserve_comments& operator=(preserve_comments &&) = default;
```
Assigns a `preserve_comments` by copying or moving.
### `operator=(std::vector<std::string>)`
```cpp
preserve_comments& operator=(const std::vector<std::string>& c);
preserve_comments& operator=(std::vector<std::string>&& c);
```
Assigns a `std::vector<std::string>` by copying or moving.
### `assign`
```cpp
template<typename InputIterator>
void assign(InputIterator first, InputIterator last);
void assign(std::initializer_list<std::string> ini);
void assign(size_type n, const std::string& val);
```
The same as `std::vector<std::string>::assign`.
### `insert`
```cpp
iterator insert(const_iterator p, const std::string& x);
iterator insert(const_iterator p, std::string&& x);
iterator insert(const_iterator p, size_type n, const std::string& x);
template<typename InputIterator>
iterator insert(const_iterator p, InputIterator first, InputIterator last);
iterator insert(const_iterator p, std::initializer_list<std::string> ini);
```
The same as `std::vector<std::string>::insert`.
### `emplace`
```cpp
template<typename ... Ts>
iterator emplace(const_iterator p, Ts&& ... args);
```
The same as `std::vector<std::string>::emplace`.
### `erase`
```cpp
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
```
The same as `std::vector<std::string>::erase`.
### `swap`
```cpp
void swap(preserve_comments& other);
```
Swaps the comments.
### `push_back`
```cpp
void push_back(const std::string& v);
void push_back(std::string&& v);
```
The same as `std::vector<std::string>::push_back`.
### `pop_back`
```cpp
void pop_back();
```
The same as `std::vector<std::string>::pop_back`.
### `emplace_back`
```cpp
template<typename ... Ts>
void emplace_back(Ts&& ... args);
```
The same as `std::vector<std::string>::emplace_back`.
### `clear`
```cpp
void clear();
```
The same as `std::vector<std::string>::clear`.
### `size`
```cpp
size_type size() const noexcept;
```
The same as `std::vector<std::string>::size`.
### `max_size`
```cpp
size_type max_size() const noexcept;
```
The same as `std::vector<std::string>::max_size`.
### `capacity`
```cpp
size_type capacity() const noexcept;
```
The same as `std::vector<std::string>::capacity`.
### `empty`
```cpp
bool empty() const noexcept;
```
The same as `std::vector<std::string>::empty`.
### `reserve`
```cpp
void reserve(size_type n);
```
The same as `std::vector<std::string>::reserve`.
### `resize`
```cpp
void resize(size_type n);
void resize(size_type n, const std::string& c);
```
The same as `std::vector<std::string>::resize`.
### `shrink_to_fit`
```cpp
void shrink_to_fit();
```
The same as `std::vector<std::string>::shrink_to_fit`.
### `operator[]`
```cpp
reference operator[](const size_type n) noexcept;
const_reference operator[](const size_type n) const noexcept;
```
The same as `std::vector<std::string>::operator[]`.
### `at`
```cpp
reference at(const size_type n) ;
const_reference at(const size_type n) const;
```
The same as `std::vector<std::string>::at`.
### `front`
```cpp
reference front() noexcept;
const_reference front() const noexcept;
```
The same as `std::vector<std::string>::front`.
### `back`
```cpp
reference back() noexcept;
const_reference back() const noexcept;
```
The same as `std::vector<std::string>::back`.
### `data`
```cpp
pointer data() noexcept;
const_pointer data() const noexcept;
```
The same as `std::vector<std::string>::data`.
### `begin/end`
```cpp
iterator begin() noexcept;
iterator end() noexcept;
const_iterator begin() const noexcept;
const_iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
```
The same as `std::vector<std::string>::begin/end`.
### `rbegin/rend`
```cpp
reverse_iterator rbegin() noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
```
The same as `std::vector<std::string>::rbegin/rend`
## non-member functions
### Comparison operator
```cpp
bool operator==(const preserve_comments&, const preserve_comments&);
bool operator!=(const preserve_comments&, const preserve_comments&);
bool operator< (const preserve_comments&, const preserve_comments&);
bool operator<=(const preserve_comments&, const preserve_comments&);
bool operator> (const preserve_comments&, const preserve_comments&);
bool operator>=(const preserve_comments&, const preserve_comments&);
```
It compares two `preserve_comments` in the same way as `std::vector<std::string>`.
### `swap`
```cpp
void swap(preserve_comments&, preserve_comments&);
void swap(preserve_comments&, std::vector<std::string>&);
void swap(std::vector<std::string>&, preserve_comments&);
```
### Stream operator
```cpp
std::ostream& operator<<(std::ostream&, const preserve_comments&);
```
Outputs as TOML comments.
If the first character is not `#`, it adds `#`.
# `toml::discard_comments`
`discard_comments` serves as a container for discarding comments.
It encompasses all the member functions of `std::vector<std::string>`, but invoking functions that modify the content has no effect, leaving it perpetually empty.
```cpp
namespace toml
{
class discard_comments;
bool operator==(const discard_comments&, const discard_comments&);
bool operator!=(const discard_comments&, const discard_comments&);
bool operator< (const discard_comments&, const discard_comments&);
bool operator<=(const discard_comments&, const discard_comments&);
bool operator> (const discard_comments&, const discard_comments&);
bool operator>=(const discard_comments&, const discard_comments&);
void swap(discard_comments&, discard_comments&);
std::ostream& operator<<(std::ostream&, const discard_comments&);
} //toml
```
## Member types
```cpp
// container_type is not defined
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using value_type = std::string;
using reference = std::string&;
using const_reference = std::string const&;
using pointer = std::string*;
using const_pointer = std::string const*;
using iterator = /* internal type: empty-iterator */
using const_iterator = /* internal type: empty-iterator */
using reverse_iterator = /* internal type: empty-iterator */
using const_reverse_iterator = /* internal type: empty-iterator */
```
### Default Constructor
```cpp
discard_comments() = default;
```
Constructs an empty `discard_comments`.
### Copy / Move Constructor
```cpp
discard_comments(discard_comments const&) = default;
discard_comments(discard_comments &&) = default;
```
Constructs a `discard_comments` by copying or moving.
### Constructor(`std::vector<std::string>`)
```cpp
explicit discard_comments(const std::vector<std::string>& c);
explicit discard_comments(std::vector<std::string>&& c);
```
Constructs an empty `discard_comments`. Arguments are ignored.
### Constructor(`preserve_comments`)
```cpp
explicit discard_comments(const preserve_comments&);
```
Constructs an empty `discard_comments`. Arguments are ignored.
### Constructor(`Iterator`)
```cpp
template<typename InputIterator>
discard_comments(InputIterator first, InputIterator last);
```
Constructs an empty `discard_comments`. Arguments are ignored.
### Constructor(`std::initializer_list`)
```cpp
discard_comments(std::initializer_list<std::string> x);
```
Constructs an empty `discard_comments`. Arguments are ignored.
### Constructor(サイズ指定)
```cpp
explicit discard_comments(size_type n);
discard_comments(size_type n, const std::string& x);
```
Constructs an empty `discard_comments`. Arguments are ignored.
### Destructor
```cpp
~discard_comments() = default;
```
Destroys `discard_comments`.
### `operator=(discard_comments)`
```cpp
discard_comments& operator=(discard_comments const&) = default;
discard_comments& operator=(discard_comments &&) = default;
```
Assigns `discard_comments` by copying or moving.
### `operator=(std::vector<std::string>)`
```cpp
discard_comments& operator=(const std::vector<std::string>& c);
discard_comments& operator=(std::vector<std::string>&& c);
```
Does nothing.
### `assign`
```cpp
template<typename InputIterator>
void assign(InputIterator first, InputIterator last);
void assign(std::initializer_list<std::string> ini);
void assign(size_type n, const std::string& val);
```
Does nothing.
### `insert`
```cpp
iterator insert(const_iterator p, const std::string& x);
iterator insert(const_iterator p, std::string&& x);
iterator insert(const_iterator p, size_type n, const std::string& x);
template<typename InputIterator>
iterator insert(const_iterator p, InputIterator first, InputIterator last);
iterator insert(const_iterator p, std::initializer_list<std::string> ini);
```
Does nothing.
### `emplace`
```cpp
template<typename ... Ts>
iterator emplace(const_iterator p, Ts&& ... args);
```
Does nothing.
### `erase`
```cpp
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
```
Does nothing.
### `swap`
```cpp
void swap(discard_comments& other);
```
Swaps two `discard_comments`. Effectively it does nothing.
### `push_back`
```cpp
void push_back(const std::string& v);
void push_back(std::string&& v);
```
Does nothing.
### `pop_back`
```cpp
void pop_back();
```
Does nothing.
### `emplace_back`
```cpp
template<typename ... Ts>
void emplace_back(Ts&& ... args);
```
Does nothing.
### `clear`
```cpp
void clear();
```
Does nothing.
### `size`
```cpp
size_type size() const noexcept;
```
Returns `0`.
### `max_size`
```cpp
size_type max_size() const noexcept;
```
Returns `0`.
### `capacity`
```cpp
size_type capacity() const noexcept;
```
Returns `0`.
### `empty`
```cpp
bool empty() const noexcept;
```
Returns `true`.
### `reserve`
```cpp
void reserve(size_type n);
```
Does nothing.
### `resize`
```cpp
void resize(size_type n);
void resize(size_type n, const std::string& c);
```
Does nothing.
### `shrink_to_fit`
```cpp
void shrink_to_fit();
```
Does nothing.
### `operator[]`
```cpp
reference operator[](const size_type n) noexcept;
const_reference operator[](const size_type n) const noexcept;
```
Causes undefined behavior.
### `at`
```cpp
reference at(const size_type n) ;
const_reference at(const size_type n) const;
```
Throws `std::out_of_range`.
### `front`
```cpp
reference front() noexcept;
const_reference front() const noexcept;
```
Causes undefined behavior.
### `back`
```cpp
reference back() noexcept;
const_reference back() const noexcept;
```
Causes undefined behavior.
### `data`
```cpp
pointer data() noexcept;
const_pointer data() const noexcept;
```
Returns `nullptr`.
### `begin/end`
```cpp
iterator begin() noexcept;
iterator end() noexcept;
const_iterator begin() const noexcept;
const_iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
```
Returns an internally defined `empty-iterator`.
The `empty-iterator` remains at the same value after incrementing or decrementing and all values are equivalent.
Accessing the target of the `empty-iterator` always calls `std::terminate`.
### `rbegin/rend`
```cpp
reverse_iterator rbegin() noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crbegin() const noexcept;
const_reverse_iterator crend() const noexcept;
```
Returns an internally defined `empty-iterator`.
The `empty-iterator` remains at the same value after incrementing or decrementing and all values are equivalent.
Accessing the target of the `empty-iterator` always calls `std::terminate`.
## non-member functions
### Comparison operator
```cpp
bool operator==(const discard_comments&, const discard_comments&);
bool operator!=(const discard_comments&, const discard_comments&);
bool operator< (const discard_comments&, const discard_comments&);
bool operator<=(const discard_comments&, const discard_comments&);
bool operator> (const discard_comments&, const discard_comments&);
bool operator>=(const discard_comments&, const discard_comments&);
```
All the instances of `discard_comments` are the same value. `operator==` returns `true`, `operator=!` returns `false`.
### `swap`
```cpp
void swap(discard_comments&, discard_comments&);
```
Swaps `discard_comments`. Effectively it does nothing.
### Stream operator
```cpp
std::ostream& operator<<(std::ostream&, const discard_comments&);
```
Outputs nothing.
# Related
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,33 @@
+++
title = "conversion.hpp"
type = "docs"
+++
# conversion.hpp
Provides macros to automatically define conversion functions for supporting user-defined types with `toml::get` and `toml::find`.
```cpp
TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(NAME, ...)
```
## Example
```cpp
namespace foo
{
struct Foo
{
std::string s;
double d;
int i;
};
} // foo
TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(foo::Foo, s, d, i)
```
# Related
- [from.hpp]({{<ref "from.md">}})
- [into.hpp]({{<ref "into.md">}})

View File

@@ -0,0 +1,779 @@
+++
title = "datetime.hpp"
type = "docs"
+++
# datetime.hpp
Defines a class that stores date and time information used in TOML's `datetime`.
# `enum class month_t`
Enum class to specify months.
Due to its relationship with `std::tm`, `local_date` treats January as `0`.
To avoid confusion, `month_t` allows specification of months by their names.
```cpp
namespace toml
{
enum class month_t : std::uint8_t
{
Jan = 0,
Feb = 1,
Mar = 2,
Apr = 3,
May = 4,
Jun = 5,
Jul = 6,
Aug = 7,
Sep = 8,
Oct = 9,
Nov = 10,
Dec = 11
};
}
```
# `local_date`
`local_date` holds a date.
`year` represents the year in AD. For `month`, January is represented as `0` to align with `std::tm`. `day` holds the day of the month.
```cpp
namespace toml
{
struct local_date
{
std::int16_t year;
std::uint8_t month;
std::uint8_t day;
local_date() = default;
~local_date() = default;
local_date(local_date const&) = default;
local_date(local_date&&) = default;
local_date& operator=(local_date const&) = default;
local_date& operator=(local_date&&) = default;
local_date(int y, month_t m, int d);
explicit local_date(const std::tm& t);
explicit local_date(const std::chrono::system_clock::time_point& tp);
explicit local_date(const std::time_t t);
operator std::chrono::system_clock::time_point() const;
operator std::time_t() const;
};
bool operator==(const local_date&, const local_date&);
bool operator!=(const local_date&, const local_date&);
bool operator< (const local_date&, const local_date&);
bool operator<=(const local_date&, const local_date&);
bool operator> (const local_date&, const local_date&);
bool operator>=(const local_date&, const local_date&);
std::ostream& operator<<(std::ostream& os, const local_date& date);
std::string to_string(const local_date& date);
}
```
## Member Variables
### `year`
```cpp
std::int16_t year;
```
Represents the year in AD. There's no offset. `2024` is simply `2024`.
### `month`
```cpp
std::uint8_t month;
```
Represents the month. To align with `std::tm`, January is `0`, February is `1`, and so on.
To avoid confusion, use `month_t` during construction.
### `day`
```cpp
std::uint8_t day;
```
Represents the day of the month. The first day is `1`.
## Member Functions
### Constructor
```cpp
local_date() = default;
```
Uses the default implementation.
### Destructor
```cpp
~local_date() = default;
```
Uses the default implementation.
### Copy and Move Constructors
```cpp
local_date(local_date const&) = default;
local_date(local_date&&) = default;
```
Uses the default implementations.
### Copy and Move Assignment Operators
```cpp
local_date& operator=(local_date const&) = default;
local_date& operator=(local_date&&) = default;
```
Uses the default implementations.
### Constructor (`int year, month_t month, int day`)
```cpp
local_date(int y, month_t m, int d);
```
Constructs a `local_date` from the specified values.
Does not perform boundary checks.
### Constructor (`std::tm`)
```cpp
local_date(const std::tm&);
```
Constructs a `local_date` from the specified `std::tm` value.
### Constructor (`std::chrono::system_clock::time_point`)
```cpp
local_date(const std::chrono::system_clock::time_point&);
```
Constructs a `local_date` from the specified `std::chrono::system_clock::time_point` value.
Time zone is determined by the environment.
### Constructor (`std::time_t`)
```cpp
local_date(const std::time_t);
```
Constructs a `local_date` from the specified `std::time_t` value.
Time zone is determined by the environment.
### `operator std::chrono::system_clock::time_point`
```cpp
operator std::chrono::system_clock::time_point() const;
```
Converts to `std::chrono::system_clock::time_point`.
Time zone is determined by the environment.
Time is set to 0 hours and 0 minutes.
### `operator std::time_t`
```cpp
operator std::time_t() const;
```
Converts to `std::time_t`.
Time zone is determined by the execution environment.
Time is set to 0 hours and 0 minutes.
## Non-member Functions
### Comparison Operators
```cpp
bool operator==(const local_date&, const local_date&);
bool operator!=(const local_date&, const local_date&);
bool operator< (const local_date&, const local_date&);
bool operator<=(const local_date&, const local_date&);
bool operator> (const local_date&, const local_date&);
bool operator>=(const local_date&, const local_date&);
```
Compares two dates.
### Stream Operators
```cpp
std::ostream& operator<<(std::ostream& os, const local_date& date);
```
Outputs in the default TOML format.
### `to_string`
```cpp
std::string to_string(const local_date& date);
```
Converts to a string in the default TOML format.
# `local_time`
```cpp
namespace toml
{
struct local_time
{
std::uint8_t hour; // [0, 23]
std::uint8_t minute; // [0, 59]
std::uint8_t second; // [0, 60]
std::uint16_t millisecond; // [0, 999]
std::uint16_t microsecond; // [0, 999]
std::uint16_t nanosecond; // [0, 999]
local_time(int h, int m, int s, int ms = 0, int us = 0, int ns = 0);
explicit local_time(const std::tm& t);
template<typename Rep, typename Period>
explicit local_time(const std::chrono::duration<Rep, Period>& t);
operator std::chrono::nanoseconds() const;
local_time() = default;
~local_time() = default;
local_time(local_time const&) = default;
local_time(local_time&&) = default;
local_time& operator=(local_time const&) = default;
local_time& operator=(local_time&&) = default;
};
bool operator==(const local_time& lhs, const local_time& rhs);
bool operator!=(const local_time& lhs, const local_time& rhs);
bool operator< (const local_time& lhs, const local_time& rhs);
bool operator<=(const local_time& lhs, const local_time& rhs);
bool operator> (const local_time& lhs, const local_time& rhs);
bool operator>=(const local_time& lhs, const local_time& rhs);
std::ostream& operator<<(std::ostream& os, const local_time& time);
std::string to_string(const local_time& time);
}
```
## Member Values
### `hour`
```cpp
std::uint8_t hour;
```
Represents the hour. Values range from `0` to `23`.
### `minute`
```cpp
std::uint8_t minute; // [0, 59]
```
Represents the minute. Values range from `0` to `59`.
### `second`
```cpp
std::uint8_t second; // [0, 60]
```
Represents the second. Values range from `0` to `60`.
### `millisecond`
```cpp
std::uint16_t millisecond; // [0, 999]
```
Represents the millisecond. Values range from `0` to `999`.
### `microsecond`
```cpp
std::uint16_t microsecond; // [0, 999]
```
Represents the microsecond. Values range from `0` to `999`.
### `nanosecond`
```cpp
std::uint16_t nanosecond; // [0, 999]
```
Represents the nanosecond. Values range from `0` to `999`.
## Member Functions
### default constructor
```cpp
local_time() = default;
```
Initializes all values to `0`.
### constructor (h, m, s, ms = 0, us = 0, ns = 0)
```cpp
local_time(int h, int m, int s, int ms = 0, int us = 0, int ns = 0);
```
Constructs using the specified time components.
No boundary checks are performed.
### constructor(`std::tm`)
```cpp
explicit local_time(const std::tm& t);
```
Constructs using `tm_hour`, `tm_min`, and `tm_sec` from `std::tm`.
Subseconds are initialized to `0`.
### constructor(`std::chrono::duration`)
```cpp
template<typename Rep, typename Period>
explicit local_time(const std::chrono::duration<Rep, Period>& t);
```
Constructs as the time of day from `0` hours of the day specified by `duration`.
### `operator std::chrono::nanoseconds`
```cpp
operator std::chrono::nanoseconds() const;
```
Converts to `std::chrono::nanoseconds`.
## Non-member Functions
### Comparison Operators
```cpp
bool operator==(const local_time& lhs, const local_time& rhs);
bool operator!=(const local_time& lhs, const local_time& rhs);
bool operator< (const local_time& lhs, const local_time& rhs);
bool operator<=(const local_time& lhs, const local_time& rhs);
bool operator> (const local_time& lhs, const local_time& rhs);
bool operator>=(const local_time& lhs, const local_time& rhs);
```
Compares based on time values.
### Stream Operator
```cpp
std::ostream& operator<<(std::ostream& os, const local_time& time);
```
Outputs in the default TOML format.
### `to_string`
```cpp
std::string to_string(const local_time& time);
```
Converts to a string in the default TOML format.
# `time_offset`
```cpp
namespace toml
{
struct time_offset
{
std::int8_t hour{0}; // [-12, 12]
std::int8_t minute{0}; // [-59, 59]
time_offset(int h, int m);
operator std::chrono::minutes() const;
time_offset() = default;
~time_offset() = default;
time_offset(time_offset const&) = default;
time_offset(time_offset&&) = default;
time_offset& operator=(time_offset const&) = default;
time_offset& operator=(time_offset&&) = default;
};
bool operator==(const time_offset&, const time_offset&);
bool operator!=(const time_offset&, const time_offset&);
bool operator< (const time_offset&, const time_offset&);
bool operator<=(const time_offset&, const time_offset&);
bool operator> (const time_offset&, const time_offset&);
bool operator>=(const time_offset&, const time_offset&);
std::ostream& operator<<(std::ostream& os, const time_offset& offset);
std::string to_string(const time_offset& offset);
}
```
## Member Variables
### `hour`
```cpp
std::int8_t hour{0}; // [-12, 12]
```
Represents the hour offset, ranging from -12 to +12.
### `minute`
```cpp
std::int8_t minute{0}; // [-59, 59]
```
Represents the minute offset, ranging from -59 to +59.
## Member Functions
### Constructor
```cpp
time_offset(int h, int m);
```
Constructs with given hours and minutes.
No boundary checking is performed.
### `operator std::chrono::minutes`
```cpp
operator std::chrono::minutes() const;
```
Converts to `std::chrono::minutes`.
## Non-member Functions
### Comparison Operators
```cpp
bool operator==(const time_offset&, const time_offset&);
bool operator!=(const time_offset&, const time_offset&);
bool operator< (const time_offset&, const time_offset&);
bool operator<=(const time_offset&, const time_offset&);
bool operator> (const time_offset&, const time_offset&);
bool operator>=(const time_offset&, const time_offset&);
```
Compares based on time length.
### Stream Output Operator
```cpp
std::ostream& operator<<(std::ostream& os, const time_offset&);
```
Outputs in the default TOML format.
### `to_string`
```cpp
std::string to_string(const time_offset&);
```
Converts to a string in the default TOML format.
# `local_datetime`
```cpp
namespace toml
{
struct local_datetime
{
local_date date;
local_time time;
local_datetime(local_date d, local_time t);
explicit local_datetime(const std::tm& t);
explicit local_datetime(const std::chrono::system_clock::time_point& tp);
explicit local_datetime(const std::time_t t);
operator std::chrono::system_clock::time_point() const;
operator std::time_t() const;
local_datetime() = default;
~local_datetime() = default;
local_datetime(local_datetime const&) = default;
local_datetime(local_datetime&&) = default;
local_datetime& operator=(local_datetime const&) = default;
local_datetime& operator=(local_datetime&&) = default;
};
bool operator==(const local_datetime&, const local_datetime&);
bool operator!=(const local_datetime&, const local_datetime&);
bool operator< (const local_datetime&, const local_datetime&);
bool operator<=(const local_datetime&, const local_datetime&);
bool operator> (const local_datetime&, const local_datetime&);
bool operator>=(const local_datetime&, const local_datetime&);
std::ostream& operator<<(std::ostream& os, const local_datetime& dt);
std::string to_string(const local_datetime& dt);
}
```
## Member Variables
### `local_date date`
```cpp
local_date date;
```
Stores the date component data.
### `local_time time`
```cpp
local_time time;
```
Stores the time component data.
## Member Functions
### Default Constructor
Constructs both `date` and `time` with default values.
### Constructor (`local_date, local_time`)
Constructs with the specified `date` and `time`.
### Constructor (`std::tm`)
Constructs from `std::tm`.
The timezone is selected based on the execution environment.
### Constructor (`std::chrono::system_clock::time_point`)
Constructs from `std::chrono::system_clock::time_point`.
The timezone is selected based on the execution environment.
### Constructor (`std::time_t`)
Constructs from `std::time_t`.
The timezone is selected based on the execution environment.
### `operator std::chrono::system_clock::time_point`
Converts to `std::chrono::system_clock::time_point`.
### `operator std::time_t`
Converts to `std::time_t`.
## Non-member Functions
### Comparison Operators
```cpp
bool operator==(const local_datetime&, const local_datetime&);
bool operator!=(const local_datetime&, const local_datetime&);
bool operator< (const local_datetime&, const local_datetime&);
bool operator<=(const local_datetime&, const local_datetime&);
bool operator> (const local_datetime&, const local_datetime&);
bool operator>=(const local_datetime&, const local_datetime&);
```
Compares based on chronological order.
### Stream Output Operator
```cpp
std::ostream& operator<<(std::ostream& os, const local_datetime&);
```
Outputs in the default TOML format.
### `to_string`
```cpp
std::string to_string(const local_datetime&);
```
Converts to a string in the default TOML format.
# `offset_datetime`
```cpp
namespace toml
{
struct offset_datetime
{
local_date date;
local_time time;
time_offset offset;
offset_datetime(local_date d, local_time t, time_offset o);
offset_datetime(const local_datetime& dt, time_offset o);
explicit offset_datetime(const local_datetime& ld);
explicit offset_datetime(const std::chrono::system_clock::time_point& tp);
explicit offset_datetime(const std::time_t& t);
explicit offset_datetime(const std::tm& t);
operator std::chrono::system_clock::time_point() const;
operator std::time_t() const;
offset_datetime() = default;
~offset_datetime() = default;
offset_datetime(offset_datetime const&) = default;
offset_datetime(offset_datetime&&) = default;
offset_datetime& operator=(offset_datetime const&) = default;
offset_datetime& operator=(offset_datetime&&) = default;
};
bool operator==(const offset_datetime&, const offset_datetime&);
bool operator!=(const offset_datetime&, const offset_datetime&);
bool operator< (const offset_datetime&, const offset_datetime&);
bool operator<=(const offset_datetime&, const offset_datetime&);
bool operator> (const offset_datetime&, const offset_datetime&);
bool operator>=(const offset_datetime&, const offset_datetime&);
std::ostream& operator<<(std::ostream& os, const offset_datetime& dt);
std::string to_string(const offset_datetime& dt);
}
```
## Member Variables
### `date`
```cpp
local_date date;
```
Stores the date component.
### `time`
```cpp
local_time time;
```
Stores the time component.
### `offset`
```cpp
time_offset offset;
```
Stores the offset component.
## Member Functions
### Default Constructor
Constructs `date`, `time`, and `offset` with default values.
### Constructor (`local_date, local_time, time_offset`)
Constructs with the specified `date`, `time`, and `offset`.
### Constructor (`local_datetime, time_offset`)
Constructs from `local_datetime` and `offset`.
### Constructor (`std::tm`)
Constructs from `std::tm`.
The timezone is UTC (00:00).
### Constructor (`std::chrono::system_clock::time_point`)
Constructs from `std::chrono::system_clock::time_point`.
The timezone is UTC (00:00).
### Constructor (`std::time_t`)
Constructs from `std::time_t`.
The timezone is UTC (00:00).
### `operator std::chrono::system_clock::time_point`
Converts to `std::chrono::system_clock::time_point`.
The timezone is UTC (00:00).
### `operator std::time_t`
Converts to `std::time_t`.
The timezone is UTC (00:00).
## Non-member Functions
### Comparison Operators
```cpp
bool operator==(const offset_datetime&, const offset_datetime&);
bool operator!=(const offset_datetime&, const offset_datetime&);
bool operator< (const offset_datetime&, const offset_datetime&);
bool operator<=(const offset_datetime&, const offset_datetime&);
bool operator> (const offset_datetime&, const offset_datetime&);
bool operator>=(const offset_datetime&, const offset_datetime&);
```
Compares based on chronological order.
If dates are the same, compares based on timezone order.
### Stream Output Operator
```cpp
std::ostream& operator<<(std::ostream& os, const offset_datetime&);
```
Outputs in the default TOML format.
### `to_string`
```cpp
std::string to_string(const offset_datetime&);
```
Converts to a string in the default TOML format.

View File

@@ -0,0 +1,145 @@
+++
title = "error_info.hpp"
type = "docs"
+++
# error_info.hpp
In `error_info.hpp`, definitions for `error_info` and functions to format it are provided.
# `toml::error_info`
```cpp
namespace toml
{
struct error_info
{
error_info(std::string t, source_location l, std::string m, std::string s = "");
error_info(std::string t, std::vector<std::pair<source_location, std::string>> l, std::string s = "");
std::string const& title() const noexcept;
std::string & title() noexcept;
std::vector<std::pair<source_location, std::string>> const& locations() const noexcept;
void add_locations(source_location loc, std::string msg) noexcept;
std::string const& suffix() const noexcept;
std::string & suffix() noexcept;
};
template<typename ... Ts>
error_info make_error_info(
std::string title, source_location loc, std::string msg, Ts&& ... tail);
std::string format_error(const std::string& errkind, const error_info& err);
std::string format_error(const error_info& err);
template<typename ... Ts>
std::string format_error(std::string title,
source_location loc, std::string msg, Ts&& ... tail);
std::ostream& operator<<(std::ostream& os, const error_info& e);
}
```
## Member Functions
### Constructor (`title, loc, msg, suffix`)
Constructs `error_info` with specified `title`, location information `loc`, message `msg`, and optional `suffix`.
`suffix` defaults to empty.
### Constructor (`title, [{loc, msg}, ...], suffix`)
Constructs `error_info` with specified `title`, an array of location-message pairs `[{loc, msg}, ...]`, and optional `suffix`.
`suffix` defaults to empty.
### `std::string title()`
Returns the title of the error message.
### `std::vector<std::pair<source_location, std::string>> locations()`
Returns the list of locations where errors occurred along with their respective messages.
Multiple locations can be specified.
### `std::string suffix()`
Returns the suffix message to display at the end, providing hints or additional information.
## Non-Member Functions
### `make_error_info`
```cpp
template<typename ... Ts>
error_info make_error_info(
std::string title, source_location loc, std::string msg, Ts&& ... tail);
```
Creates a new `error_info`.
Must be followed by a `msg` related to `source_location` or `basic_value`.
Overloads are added in [`value.hpp`](docs/reference/value#tomlmake_error_info) when passing `toml::basic_value` instead of `source_location`.
Possible to pass `suffix` at the end.
### `format_error`
Formats `error_info` as follows:
```
{title}
--> {locations().at(0).first.file_name()}
|
1 | {locations().at(0).first.line()}
| ^-- {locations().at(0).second}
|
2 | {locations().at(1).first.line()}
| ^-- {locations().at(1).second}
{suffix}
```
If file names differ between two `source_location`, the file name is displayed again.
```cpp
std::string format_error(const std::string& errkind, const error_info& err);
std::string format_error(const error_info& err);
```
Formats `error_info`.
If `errkind` is not provided, a red-bold `[error]` prefix is added before `title`.
If `errkind` is provided (including an empty string), it replaces `[error]`.
```cpp
namespace toml
{
template<typename ... Ts>
std::string format_error(std::string title,
source_location loc, std::string msg, Ts&& ... tail);
} // toml
```
Returns a formatted string using `format_error` for `error_info` created with `make_error_info`.
Overloads are added in [`value.hpp`](docs/reference/value#tomlformat_error) when passing `toml::basic_value` instead of `source_location`.
### Stream Operator
```cpp
std::ostream& operator<<(std::ostream& os, const error_info& e);
```
Calls `format_error(e)` and outputs it.
# Related
- [color.hpp](color.md)
- [parser.hpp](parser.md)
- [source_location.hpp](source_location.md)

View File

@@ -0,0 +1,40 @@
+++
title = "exception.hpp"
type = "docs"
+++
# exception.hpp
# `toml::exception`
Base class for exception types defined in toml11.
```cpp
namespace toml
{
struct exception : public std::exception
{
public:
virtual ~exception() noexcept override = default;
virtual const char* what() const noexcept override {return "";}
};
} // toml
```
## Member Functions
### Destructor
```cpp
virtual ~exception() noexcept override = default;
```
Override when derived.
### `what`
```cpp
virtual const char* what() const noexcept override {return "";}
```
Returns the error message. Override when derived.

View File

@@ -0,0 +1,244 @@
+++
title = "find.hpp"
type = "docs"
+++
# find.hpp
This function searches for a value in a `toml::value` and performs type conversion if necessary.
{{< hint info >}}
`toml::value` can change the type it stores, and `toml::find` accommodates these types.
Technically, all functions use `toml::basic_value<TC>`.
However, for simplicity, we refer to it as `toml::value` in explanations unless a distinction is necessary.
In the documentation, if the template parameter `TC` changes the type, assume that types like `toml::value::integer_type` will also change accordingly.
{{< /hint >}}
# `toml::find`
## Overview
`toml::find` uses template arguments for the type you want to retrieve and function arguments for the key of the value you want to find.
```cpp
template<typename T, typename TC, typename ... Keys>
T find(const basic_value<TC>& v, Keys ... keys);
```
The supported types for `T` and the behavior of the conversion are the same as for `toml::get`.
If `T` is not specified, a `toml::value` will be returned.
Keys can be of type `toml::value::key_type` or `std::size_t`.
When multiple keys are provided, the function will search recursively through sub-tables or arrays.
If a `toml::value::key_type` is given, `toml::value` is interpreted as a `toml::table`; if a `std::size_t` is given, `toml::value` is interpreted as a `toml::array`.
### Note on Recursive Search
TOML allows for bare keys as well as quoted keys (enclosed in `"` or `'`). Using a quoted key like `"foo.bar" = "baz"` means no sub-table is constructed, and the key is `"foo.bar"`. To handle such patterns, toml11 does not split keys containing `.` and searches using the full string.
Consider the following TOML file:
```toml
[foo]
[foo.bar]
baz = "hoge"
["foo.bar"]
baz = "fuga"
```
The corresponding usage of `toml::find` is shown below:
```cpp
const auto input = toml::parse("input.toml");
const auto baz1 = toml::find<std::string>(input, "foo", "bar", "baz"); // hoge
const auto baz2 = toml::find<std::string>(input, "foo.bar", "baz"); // fuga
```
cf. [toml.io/en/v1.0.0#keys](https://toml.io/en/v1.0.0#keys)
## `toml::find(value, key)`
Searches for `key` in `value` as if `value` were a `toml::table`, then converts using `toml::get`.
```cpp
template<typename T, typename TC>
/* Equivalent to toml::get<T>(const value&) */ find(
const basic_value<TC>& v, const typename basic_value<TC>::key_type& ky);
template<typename T, typename TC>
/* Equivalent to toml::get<T>(value&) */ find(
basic_value<TC>& v, const typename basic_value<TC>::key_type& ky);
template<typename T, typename TC>
/* Equivalent to toml::get<T>(value&&) */ find(
basic_value<TC>&& v, const typename basic_value<TC>::key_type& ky);
```
If `T` is not specified, the function returns a `toml::value` without conversion.
```cpp
template<typename TC>
basic_value<TC> const& find(
basic_value<TC> const& v, const typename basic_value<TC>::key_type& ky);
template<typename TC>
basic_value<TC>& find(
basic_value<TC>& v, const typename basic_value<TC>::key_type& ky);
template<typename TC>
basic_value<TC> find(
basic_value<TC>&& v, const typename basic_value<TC>::key_type& ky);
```
### Exceptions
If the `toml::value` does not contain a `table`, a `toml::type_error` is thrown.
If the contained `table` does not have the specified element, a `std::out_of_range` is thrown.
If the specified element cannot be converted to `T` (i.e., `toml::get` fails), a `toml::type_error` is thrown.
## `toml::find(value, index)`
Accesses the `index`-th element of `value` as if `value` were a `toml::array`, then converts using `toml::get`.
```cpp
template<typename T, typename TC>
/* Equivalent to toml::get<T>(const value&) */ find(
const basic_value<TC>& v, const std::size_t index);
template<typename T, typename TC>
/* Equivalent to toml::get<T>(value&) */ find(
basic_value<TC>& v, const std::size_t index);
template<typename T, typename TC>
/* Equivalent to toml::get<T>(value&&) */ find(
basic_value<TC>&& v, const std::size_t index);
```
If `T` is not specified, the function returns a `toml::value` without conversion.
```cpp
template<typename TC>
basic_value<TC> const& find(basic_value<TC> const& v, const std::size_t ky);
template<typename TC>
basic_value<TC>& find(basic_value<TC>& v, const std::size_t ky);
template<typename TC>
basic_value<TC> find(basic_value<TC>&& v, const std::size_t ky);
```
### Exceptions
If the `toml::value` does not contain an `array`, a `toml::type_error` is thrown.
If the contained `array` does not have the specified number of elements, a `std::out_of_range` is thrown.
If the specified element cannot be converted to `T` (i.e., `toml::get` fails), a `toml::type_error` is thrown.
## `toml::find(value, keys...)`
```cpp
template<typename T, typename TC, typename K1, typename K2, typename ... Ks>
/* Equivalent to toml::get<T>(const value&) */ find(
const basic_value<TC>& v, const K1& k1, const K2& k2, const Ks& ... ks);
template<typename T, typename TC, typename K1, typename K2, typename ... Ks>
/* Equivalent to toml::get<T>(value&) */ find(
basic_value<TC>& v, const K1& k1, const K2& k2, const Ks& ... ks);
template<typename T, typename TC, typename K1, typename K2, typename ... Ks>
/* Equivalent to toml::get<T>(value&&) */ find(
basic_value<TC>&& v, const K1& k1, const K2& k2, const Ks& ... ks);
```
This function calls `toml::find` recursively.
The failure conditions and the exceptions thrown are the same as those for `toml::find`.
# `toml::find_or(value, key, fallback)`
```cpp
template<typename T, typename TC, typename Key>
T find_or(const basic_value<TC>& v, const Key& key, T&& opt);
```
The `find_or` function takes a default value to avoid throwing an exception when the search fails.
The default value must be of the same type as the return type `T`. Therefore, unlike `toml::find<T>`, `find_or` infers the type `T`.
You can specify `T` explicitly with `find_or<T>`, but this always returns a new value. To obtain a reference, do not specify `T`.
## When `T` is `basic_value`
```cpp
template<typename TC, typename K>
basic_value<TC>& find_or(basic_value<TC>& v, const K& key, basic_value<TC>& opt) noexcept
template<typename TC, typename K>
basic_value<TC> const& find_or(const basic_value<TC>& v, const K& key, const basic_value<TC>& opt) noexcept
```
Searches for the corresponding value and returns it without conversion. Because no conversion is needed, a reference can be returned.
If the value is not found, the default value is returned.
## When `T` is `toml::value::{some_type}`
```cpp
template<typename T, typename TC, typename K>
T& find_or(basic_value<TC>& v, const K& key, T& opt) noexcept
template<typename T, typename TC, typename K>
T const& find_or(const basic_value<TC>& v, const K& key, const T& opt) noexcept
```
Searches for the corresponding value and returns it without conversion. Because no conversion is needed, a reference can be returned.
If the value is not found or if a different type is stored, the default value is returned.
## When `T` is `const char*`
```cpp
template<typename T, typename TC, typename K>
T find_or(const basic_value<TC>& v, const K& key, T opt)
```
Searches for the corresponding value and returns it as a `std::string`.
Since the fallback is constructed from `const char*` to `std::string`, a reference cannot be returned in case of failure.
## When `T` is any other type
```cpp
template<typename T, typename TC, typename K>
T find_or(const basic_value<TC>& v, const K& key, T opt);
```
Searches for the corresponding value and converts it to `T` before returning it.
Because conversion is performed, a reference cannot be returned.
## When multiple keys are provided
```cpp
template<typename Value, typename K1, typename K2, typename K3, typename ... Ks>
auto find_or(Value&& v, const K1& k1, const K2& k2, K3&& k3, Ks&& ... keys) noexcept
-> decltype(find_or(v, k2, std::forward<K3>(k3), std::forward<Ks>(keys)...))
```
Interprets the last element in the key sequence as the default value and applies `find_or` recursively.
If the inferred type of `T` is `toml::value` or `toml::value::some_type`, a reference can be returned.
If `T` is explicitly specified, conversion is always performed.
# Related
- [get.hpp]({{<ref "get.md">}})
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,446 @@
+++
title = "format.hpp"
type = "docs"
+++
# format.hpp
Defines structures and enumerations related to formatting information for `toml::value`.
# `indent_char`
An enumeration representing the indentation character choice.
```cpp
enum class indent_char : std::uint8_t
{
space, // use space
tab, // use tab
none // no indent
};
std::ostream& operator<<(std::ostream& os, const indent_char& c);
std::string to_string(const indent_char);
```
Choosing `none` means no indentation is used, regardless of the value in super tables.
If both `space` and `tab` are specified within the serializable value, the behavior is unspecified; typically, the unspecified indentation character appears.
# `boolean_format_info`
Formatting information for `boolean`.
```cpp
struct boolean_format_info {};
bool operator==(const boolean_format_info&, const boolean_format_info&) noexcept;
bool operator!=(const boolean_format_info&, const boolean_format_info&) noexcept;
```
There is only one way to format `boolean`, so no configurable values are provided.
# `integer_format`
```cpp
enum class integer_format : std::uint8_t
{
dec = 0,
bin = 1,
oct = 2,
hex = 3,
};
std::ostream& operator<<(std::ostream& os, const integer_format f);
std::string to_string(const integer_format);
```
Specifies the radix of an `integer`.
# `integer_format_info`
```cpp
struct integer_format_info
{
integer_format fmt = integer_format::dec;
std::size_t width = 0; // minimal width (may exceed)
std::size_t spacer = 0; // position of `_` (if 0, no spacer)
std::string suffix = ""; // _suffix (library extension)
};
bool operator==(const integer_format_info&, const integer_format_info&) noexcept;
bool operator!=(const integer_format_info&, const integer_format_info&) noexcept;
```
## Member Variables
### `integer_format fmt`
Specifies the radix.
### `std::size_t width`
Specifies the minimum width. The formatted value may exceed this width.
For values smaller than this width, if `integer_format::dec`, no effect. Otherwise, leading zeros are added.
### `std::size_t spacer`
Specifies the width at which underscores `_` are inserted.
- If `3`, formatted as `1_234_567`.
- If `4`, formatted as `0xdead_beef`.
- If `0`, no underscores are inserted.
Irregular widths are not allowed.
### `std::string suffix`
Stores the suffix when `spec::ext_num_suffix` of toml11 extension is `true`.
cf. [spec.hpp](spec.md)
# `floating_format`
```cpp
enum class floating_format : std::uint8_t
{
defaultfloat = 0,
fixed = 1, // does not include exponential part
scientific = 2, // always include exponential part
hex = 3 // hexfloat extension
};
std::ostream& operator<<(std::ostream& os, const floating_format f);
std::string to_string(const floating_format);
```
Specifies the formatting style for `floating` numbers.
Corresponds to `std::defaultfloat`, `std::fixed`, `std::scientific`, `std::hexfloat`.
`hexfloat` is available only if `toml::spec::ext_hex_float` is `true`.
cf. [spec.hpp](spec.md)
# `floating_format_info`
```cpp
struct floating_format_info
{
floating_format fmt = floating_format::defaultfloat;
std::size_t prec = 0; // precision (if 0, use the default)
std::string suffix = ""; // 1.0e+2_suffix (library extension)
};
bool operator==(const floating_format_info&, const floating_format_info&) noexcept;
bool operator!=(const floating_format_info&, const floating_format_info&) noexcept;
```
## Member Variables
### `floating_format fmt`
Specifies the formatting style.
### `std::size_t prec`
Specifies the precision after the decimal point.
### `std::string suffix`
Stores the suffix when `spec::ext_num_suffix` of toml11 extension is `true`.
cf. [spec.hpp](spec.md)
# `string_format`
```cpp
enum class string_format : std::uint8_t
{
basic = 0,
literal = 1,
multiline_basic = 2,
multiline_literal = 3
};
std::ostream& operator<<(std::ostream& os, const string_format f);
std::string to_string(const string_format);
```
Specifies the formatting style for strings.
# `string_format_info`
```cpp
struct string_format_info
{
string_format fmt = string_format::basic;
bool start_with_newline = false;
};
bool operator==(const string_format_info&, const string_format_info&) noexcept;
bool operator!=(const string_format_info&, const string_format_info&) noexcept;
```
## Member Variables
### `string_format fmt`
Specifies the formatting information for strings.
### `bool start_with_newline`
For `multiline_basic` or `multiline_literal`, specifies whether to include a newline after the initial `"""` or `'''`.
# `datetime_delimiter_kind`
```cpp
enum class datetime_delimiter_kind : std::uint8_t
{
upper_T = 0,
lower_t = 1,
space = 2,
};
std::ostream& operator<<(std::ostream& os, const datetime_delimiter_kind d);
std::string to_string(const datetime_delimiter_kind);
```
Specifies the delimiter used between date and time in `datetime`.
Possible options include `T`, `t`, and a space ` `.
# `offset_datetime_format_info`
```cpp
struct offset_datetime_format_info
{
datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T;
bool has_seconds = true;
std::size_t subsecond_precision = 6; // [us]
};
bool operator==(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept;
bool operator!=(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept;
```
## Member Variables
### `datetime_delimiter_kind delimiter`
Specifies the delimiter to use.
### `bool has_seconds`
Specifies whether to omit seconds.
### `std::size_t subsecond_precision`
Specifies how many digits to output for subseconds.
# `local_datetime_format_info`
```cpp
struct local_datetime_format_info
{
datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T;
bool has_seconds = true;
std::size_t subsecond_precision = 6; // [us]
};
bool operator==(const local_datetime_format_info&, const local_datetime_format_info&) noexcept;
bool operator!=(const local_datetime_format_info&, const local_datetime_format_info&) noexcept;
```
## Member Variables
### `datetime_delimiter_kind delimiter`
Specifies the delimiter to use.
### `bool has_seconds`
Specifies whether to omit seconds.
### `std::size_t subsecond_precision`
Specifies how many digits to output for subseconds.
# `local_date_format_info`
```cpp
struct local_date_format_info
{
// nothing, for now
};
bool operator==(const local_date_format_info&, const local_date_format_info&) noexcept;
bool operator!=(const local_date_format_info&, const local_date_format_info&) noexcept;
```
No formatting parameters are specified for `local_date`.
# `local_time_format_info`
```cpp
struct local_time_format_info
{
bool has_seconds = true;
std::size_t subsecond_precision = 6; // [us]
};
bool operator==(const local_time_format_info&, const local_time_format_info&) noexcept;
bool operator!=(const local_time_format_info&, const local_time_format_info&) noexcept;
```
## Member Variables
### `bool has_seconds`
Specifies whether to omit seconds.
### `std::size_t subsecond_precision`
Specifies how many digits to output for subseconds.
# `array_format`
```cpp
enum class array_format : std::uint8_t
{
default_format = 0,
oneline = 1,
multiline = 2,
array_of_tables = 3 // [[format.in.this.way]]
};
std::ostream& operator<<(std::ostream& os, const array_format f);
std::string to_string(const array_format);
```
- `default_format`
- Automatically selects the appropriate format. Longer arrays may span multiple lines.
- `oneline`
- Formats all elements in a single line.
- `multiline`
- Outputs each element on its own line.
- `array_of_tables`
- Formats in the `[[array.of.tables]]` style. Cannot contain elements other than `table`.
# `array_format_info`
```cpp
struct array_format_info
{
array_format fmt = array_format::default_format;
indent_char indent_type = indent_char::space;
std::int32_t body_indent = 4; // indent in case of multiline
std::int32_t closing_indent = 0; // indent of `]`
};
bool operator==(const array_format_info&, const array_format_info&) noexcept;
bool operator!=(const array_format_info&, const array_format_info&) noexcept;
```
## Member Variables
### `array_format fmt`
Specifies the format style.
### `indent_char indent_type`
Selects the type of character used for indentation.
### `std::int32_t body_indent`
Specifies the number of characters to indent before each element in `array_format::multiline`.
### `std::int32_t closing_indent`
Specifies the number of characters to indent before the closing bracket `]` in `array_format::multiline`.
# `table_format`
```cpp
enum class table_format : std::uint8_t
{
multiline = 0, // [foo] \n bar = "baz"
oneline = 1, // foo = {bar = "baz"}
dotted = 2, // foo.bar = "baz"
multiline_oneline = 3, // foo = { \n bar = "baz" \n }
implicit = 4 // [x] defined by [x.y.z]. skip in serializer.
};
std::ostream& operator<<(std::ostream& os, const table_format f);
std::string to_string(const table_format);
```
- `multiline`
- Formats as a multiline normal table.
- `oneline`
- Formats as an inline table.
- `dotted`
- Formats in the form of `a.b.c = "d"`.
- `multiline_oneline`
- Formats as a multiline inline table with line breaks. Available from TOML v1.1.0 onwards.
- cf. [spec.hpp](spec.md)
- `implicit`
- Skips implicit definitions like `[x.y.z.w]`, leaving `[x]`, `[x.y]`, `[x.y.z]` as implicit.
{{< hint warning >}}
According to TOML syntax, `dotted` table can have sub-tables:
```toml
[fruit]
apple.color = "red"
apple.taste.sweet = true
# [fruit.apple] # INVALID
# [fruit.apple.taste] # INVALID
[fruit.apple.texture] # you can add sub-tables
smooth = true
```
toml11 currently does not support this format.
Sub-tables under a `dotted` table would all be `dotted`, and tables are forced into inline table format.
{{< /hint >}}
# `table_format_info`
```cpp
struct table_format_info
{
table_format fmt = table_format::multiline;
indent_char indent_type = indent_char::space;
std::int32_t body_indent = 0; // indent of values
std::int32_t name_indent = 0; // indent of [table]
std::int32_t closing_indent = 0; // in case of {inline-table}
};
bool operator==(const table_format_info&, const table_format_info&) noexcept;
bool operator!=(const table_format_info&, const table_format_info&) noexcept;
```
## Member Variables
### `table_format fmt`
Specifies the formatting method.
### `indent_char indent_type`
Specifies the character used for indentation.
### `std::int32_t body_indent`
Specifies the width of indentation before keys.
The indentation width of the super table is not added.
### `std::int32_t name_indent`
Specifies the indentation of keys in `[table]` format.
The indentation width of the super table is not added.
### `std::int32_t closing_indent`
Specifies the indentation width before the closing brace `}` in the case of `multiline_oneline`.

View File

@@ -0,0 +1,56 @@
+++
title = "from.hpp"
type = "docs"
+++
# from.hpp
Defines a `struct` used for conversion from `toml::value` in `toml::get` and `toml::find`.
You can achieve the same functionality by adding a `from_toml` member function, but for classes where you cannot add member functions, use `from<T>`.
This file does not provide specific implementations. Please specialize this `struct` when using.
```cpp
namespace toml
{
template<typename T>
struct from;
} // toml
```
## Example
```cpp
namespace extlib
{
struct foo
{
int a;
std::string b;
};
} // extlib
#include <toml11/from.hpp>
namespace toml
{
template<>
struct from<extlib::foo>
{
template<typename TC>
static extlib::foo from_toml(const toml::basic_value<TC>& v)
{
return extlib::foo{toml::find<int>(v, "a"), toml::find<std::string>(v, "b")};
}
};
} // toml
```
# Related
- [conversion.hpp]({{<ref "conversion.md">}})
- [into.hpp]({{<ref "into.md">}})

View File

@@ -0,0 +1,427 @@
+++
title = "get.hpp"
type = "docs"
+++
# get.hpp
These are functions for extracting values from `toml::value` and performing type conversions if necessary.
{{< hint info >}}
`toml::value` can change the type it stores, and `toml::get` accommodates these types.
Technically, all functions use `toml::basic_value<TC>`.
However, for simplicity, we refer to it as `toml::value` in explanations unless a distinction is necessary.
In the documentation, if the template parameter `TC` changes the type, assume that types like `toml::value::integer_type` will also change accordingly.
{{< /hint >}}
# `toml::get<T>`
## Overview
Generally, `toml::get` behaves as follows:
You specify `T` as in `toml::get<int>(v)`.
```cpp
template<typename T, typename TC>
T get(const basic_value<TC>& v);
```
However, depending on the type of `T`, `toml::get` can exhibit different behaviors.
The types of `T` can be categorized into:
1. Types that do not require conversion
2. Types that require conversion
Detailed conditions and the specific types supported are discussed later.
### Types that Do Not Require Conversion
No conversion is needed if the provided `toml::value` is already storing the desired type. For instance, since `toml::value::integer_type` is an alias for `std::int64_t`, `toml::get<std::int64_t>(v)` requires no conversion. In this case, `toml::get` retrieves the `integer` value from `toml::value` and returns a reference to it.
If the provided `toml::value` is a mutable reference (`&`), the returned value is also a mutable reference (`&`). If it is an immutable reference (`const&`), the returned value will also be an immutable reference (`const&`). Returning a mutable reference allows you to overwrite the value stored in `toml::value` through that reference.
### Types that Require Conversion
Types other than the ones mentioned above require conversion. For example, since `toml::value::integer_type` is an alias for `std::int64_t`, `toml::get<std::size_t>(toml::value&)` requires conversion. In this case, `toml::get` retrieves the `integer` value from `toml::value` and casts it to return the appropriate type.
toml11 supports not only simple casts but also complex type conversions like converting from `toml::array` to `std::tuple<int, double, std::string>`, `std::array<double, 4>`, or from `toml::table` to `std::map<std::string, int>`. For specifics, refer to the subsequent sections.
### When Conversion Fails
Sometimes, the expected type conversion cannot be performed. For example, applying `toml::get<int>(v)` to a `toml::value` that holds a `table`.
In such cases, an attempt to convert to the type most similar to the desired type (in this case, `int` using `as_integer`) fails, and a `toml::type_error` is thrown.
When parsing from a file, an error message similar to the following is output:
```
terminate called after throwing an instance of 'toml::type_error'
what(): toml::value::as_integer(): bad_cast to integer
--> input.toml
|
6 | [fruit]
| ^^^^^^^-- the actual type is table
```
## When `T` is identical to `toml::value`
```cpp
template<typename T, typename TC>
T& get(basic_value<TC>& v);
template<typename T, typename TC>
T const& get(const basic_value<TC>& v);
template<typename T, typename TC>
T get(basic_value<TC>&& v);
```
Condition:
- `std::is_same<T, basic_value<TC>>` is satisfied.
Since this involves retrieving `toml::value` from `toml::value`, no conversion is performed, and the value is returned as is. This exists solely to generalize the implementation of other functions.
This does not fail.
## When `T` is one of `toml::value::{some_type}`
```cpp
template<typename T, typename TC>
T& get(basic_value<TC>& v);
template<typename T, typename TC>
T const& get(const basic_value<TC>& v);
template<typename T, typename TC>
T get(basic_value<TC>&& v);
```
Condition:
- `T` must be the same as one of the types that `toml::value` can store (e.g., `toml::value::boolean_type`).
If `toml::value` is storing a type that matches the specified type in `toml::get<T>`, such as `toml::value::integer_type`, no type conversion is needed, and a reference can be returned.
If a different type is stored, a `toml::type_error` is thrown.
## When `T` is `basic_value<OtherTC>` with a different `TypeConfig`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `T` is not `toml::basic_value<TC>`.
- `T` is `toml::basic_value<OtherTC>`.
When a `basic_value` that can store different types is specified, conversion is performed.
Since type conversion occurs, the returned value is a new value and not a reference.
This does not fail (except in cases like memory exhaustion).
## When `T` is an integer type
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `std::is_integral<T>` is satisfied
- `T` is not `bool`
- `T` is not `toml::value::integer_type`
The function assumes that `toml::value` holds an `integer_type`, retrieves its value, converts it to `T`, and returns it.
If a type other than `toml::value::integer_type` is stored, a `toml::type_error` is thrown.
## When `T` is a floating-point type
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `std::is_floating_point<T>` is satisfied
- `T` is not `toml::value::floating_type`
The function assumes that `toml::value` holds a `floating_type`, retrieves its value, converts it to `T`, and returns it.
If a type other than `toml::value::floating_type` is stored, a `toml::type_error` is thrown.
## When `T` is `std::string_view`
This is only available in C++17 and later.
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `std::is_same<std::string_view, T>` is satisfied
The function assumes that `toml::value` holds a `string_type`, retrieves its value, constructs a `std::string_view` from it, and returns it.
If a type other than `toml::value::string_type` is stored, a `toml::type_error` is thrown.
## When `T` is `std::chrono::duration`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `T` is `std::chrono::duration<Rep, Period>`
The function assumes that `toml::value` holds a `local_time`, retrieves its value, converts it to `std::chrono::duration`, and returns it.
If a type other than `toml::value::local_time` is stored, a `toml::type_error` is thrown.
## When `T` is `std::chrono::system_clock::time_point`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `std::is_same<T, std::chrono::system_clock::time_point>` is satisfied
If the `toml::value` holds a `local_date`, `local_datetime`, or `offset_datetime`, this function retrieves the value and converts it to `std::chrono::system_clock::time_point`, returning the result.
If the value is of a type other than `local_date`, `local_datetime`, or `offset_datetime`, a `toml::type_error` is thrown.
## When `T` is array-like
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Conditions:
- `T` has an `iterator`
- `T` has a `value_type`
- `T` supports `push_back(x)`
- `T` is not `toml::value::array_type`
- `T` is not `std::string`
- `T` is not `std::string_view`
- `T` is not map-like
- `T` does not have `from_toml()` member function
- `toml::from<T>` is not defined
- A constructor from `toml::basic_value<TC>` is not defined
This includes types like `std::vector<int>` and `std::deque<std::string>`.
If the `toml::value` holds an `array`, this function retrieves the value and converts it to the specified container type, returning the result.
If the value is of a type other than `toml::value::array_type`, a `toml::type_error` is thrown.
## When `T` is `std::array`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
条件:
- `T` is `std::array<U, N>`
If the `toml::value` holds an `array`, this function retrieves the value and converts it to the specified container type, returning the result.
If the value is of a type other than `toml::value::array_type`, a `toml::type_error` is thrown.
If the `array` held by `toml::value` does not contain enough elements, a `std::out_of_range` is thrown.
## When `T` is `std::forward_list`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `T` is `std::forward_list<U>`
If the `toml::value` holds an `array`, this function retrieves the value and converts it to a `std::forward_list`, returning the result.
If the value is of a type other than `toml::value::array_type`, a `toml::type_error` is thrown.
## When `T` is `std::pair`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `T` is `std::pair<T1, T2>`
If the `toml::value` holds an `array`, this function retrieves the value and converts it to `std::pair<T1, T2>`, returning the result.
The `first` and `second` elements are recursively converted.
If the value is of a type other than `basic_value::array_type`, a `toml::type_error` is thrown.
If the `array` held by `toml::value` does not contain exactly 2 elements, a `std::out_of_range` is thrown.
## When `T` is `std::tuple`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- `T` is `std::tuple<T1, T2, ... TN>`
If the `toml::value` holds an `array`, this function retrieves the value and converts it to `std::tuple<T1, T2, ...TN>`, returning the result.
Each element is recursively converted.
If the value is of a type other than `basic_value::array_type`, a `toml::type_error` is thrown.
If the `array` held by `toml::value` does not contain exactly `std::tuple_size<T>::value` elements, a `std::out_of_range` is thrown.
## When `T` is map-like
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Conditions:
- `T` has an `iterator`
- `T` has a `key_type`
- `T` has a `value_type`
- `T` has a `mapped_type`
- `T` is not `toml::value::table_type`
- `T` does not have a `from_toml()` member function
- `toml::from<T>` is not defined
- A constructor from `toml::basic_value<TC>` is not defined
This includes types like `std::map<std::string, int>` and `std::unordered_map<std::string, float>`.
If the `toml::value` holds a `table`, this function retrieves the value and converts it to `T`, returning the result.
Elements are recursively converted.
If the value is of a type other than `basic_value::table_type`, a `toml::type_error` is thrown.
## When `T` is a user-defined type with a specialization of `toml::from<T>`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Condition:
- A specialization of `toml::from<T>` is defined
If a specialization of `toml::from` for `T` is defined, it is used for type conversion.
Ensure this does not conflict with individually supported types (`std::array`, `std::pair`, `std::tuple` etc).
## When `T` is a user-defined type with a `from_toml` member function
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Conditions:
- `toml::from<T>` is not defined
- `T` has `from_toml()` member function
If `T` has a `from_toml(toml::basic_value<TC>)` member function, it is used for type conversion.
If `toml::from<T>` is defined, it takes precedence.
## When `T` is a user-defined type with a constructor that takes `toml::basic_value<TC>`
```cpp
template<typename T, typename TC>
T get(basic_value<TC>& v);
```
Conditions:
- `toml::from<T>` is not defined
- `T` does not have `from_toml()` member function
- `T` has a constructor that takes `toml::basic_value<TC>`
If `T` has a constructor that takes `toml::basic_value<TC>`, it is used for type conversion.
If `toml::from<T>` or `T::from_toml` is defined, they take precedence.
# `toml::get_or<T>`
`get_or` takes a default value for use when the conversion fails, avoiding exceptions.
The default value must be of the same type as the target type `T`.
Therefore, unlike `toml::get<T>`, `T` can be inferred in `get_or`.
## When `T` is `basic_value<TC>`
```cpp
template<typename TC>
basic_value<TC> const& get_or(const basic_value<TC>& v, const basic_value<TC>& opt)
template<typename TC>
basic_value<TC> & get_or(basic_value<TC>& v, basic_value<TC>& opt)
template<typename TC>
basic_value<TC> get_or(basic_value<TC>&& v, basic_value<TC>&& opt)
```
Since the conversion target is the same `toml::value`, this never fails.
It exists solely to generalize the implementation of other functions.
## When `T` is `basic_value<TC>::{some_type}`
```cpp
template<typename T, typename TC>
T const& get_or(const basic_value<TC>& v, const T& opt) noexcept
template<typename T, typename TC>
T & get_or(basic_value<TC>& v, T& opt) noexcept
template<typename T, typename TC>
T get_or(basic_value<TC>&& v, T&& opt) noexcept
```
Performs the same conversion as `toml::get<T>`. If it fails, the second argument is returned.
## When `T` is `const char*`
```cpp
template<typename TC>
typename basic_value<TC>::string_type
get_or(const basic_value<TC>& v,
const typename basic_value<TC>::string_type::value_type* opt);
```
When `const char*` is passed, the conversion target is interpreted as `std::string`.
## When `T` is something else
```cpp
template<typename TC>
typename std::remove_cv<typename std::remove_reference<T>::type>::type
get_or(const basic_value<TC>& v, T&& opt);
```
Performs the same conversion as `toml::get<T>`. If it fails, the second argument is returned.
# Related
- [find.hpp]({{<ref "find.md">}})
- [from.hpp]({{<ref "from.md">}})
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,58 @@
+++
title = "into.hpp"
type = "docs"
+++
# into.hpp
Defines a `struct` used for conversion from user-defined types into `toml::value` constructors.
You can achieve the same functionality by adding an `into_toml` member function, but for classes where you cannot add member functions, use `into<T>`.
This file does not provide specific implementations. Please specialize this `struct` when using.
```cpp
namespace toml
{
template<typename T>
struct into;
} // toml
```
## Example
```cpp
namespace extlib
{
struct foo
{
int a;
std::string b;
};
} // extlib
#include <toml11/into.hpp>
namespace toml
{
template<>
struct into<extlib::foo>
{
template<typename TC>
static toml::basic_value<TC> into_toml(const extlib::foo& f)
{
using value_type = toml::basic_value<TC>;
using table_type = typename value_type::table_type;
return value_type(table_type{{"a", f.a}, {"b", f.b}});
}
};
} // toml
```
# Related
- [conversion.hpp]({{<ref "conversion.md">}})
- [from.hpp]({{<ref "from.md">}})

View File

@@ -0,0 +1,82 @@
+++
title = "literal.hpp"
type = "docs"
+++
# literal.hpp
In `literal.hpp`, the `_toml` literal is defined.
The `_toml` literal parses string literals into `toml::value`.
```cpp
namespace toml
{
inline namespace literals
{
inline namespace toml_literals
{
toml::value operator"" _toml(const char* str, std::size_t len);
toml::value operator"" _toml(const char8_t* str, std::size_t len); // Available in C++20 and later
} // toml_literals
} // literals
} // toml
```
## Free Functions
### `operator"" _toml(const char*)`
```cpp
toml::value operator"" _toml(const char* str, std::size_t len);
```
Converts a string literal into a `toml::value` by parsing it.
For typical TOML files, this performs equivalent parsing to `toml::parse`.
```cpp
const auto v1 = "a = 'foo'"_toml; // v1: {a = 'foo'}
```
When dealing with multiline content, raw string literal is convenient.
```cpp
const auto v1 = R"(
a = 42
b = "foo"
)"_toml;
```
If the value is a standalone entity, it represents that value.
```cpp
const auto v2 = "'foo'"_toml; // v2: 'foo'
```
TOML allows keys consisting solely of numbers. When distinguishing between table definitions and arrays is ambiguous (e.g., `[1]`), table definitions take precedence.
To interpret as an array, use a trailing comma.
```cpp
const auto v3 = "[1]"_toml; // v3: {1 = {}}
const auto v4 = "[1,]"_toml; // v4: [1,]
```
### `operator"" _toml(const char8_t*)`
Defined when `char8_t` is available. Otherwise identical in functionality, differing only in argument type.
# Example
```cpp
#include <toml.hpp>
int main()
{
using namespace toml::literals::toml_literals;
const auto v = "a = \"foo\""_toml;
assert(v.at("a").as_string() == "foo");
return 0;
}
```

View File

@@ -0,0 +1,344 @@
+++
title = "ordered_map.hpp"
type = "docs"
+++
# ordered_map.hpp
Defines `toml::ordered_map`, which is used to maintain the order of values in a file.
# `class ordered_map`
```cpp
namespace toml
{
template<typename Key, typename Val, typename Cmp = std::equal_to<Key>,
typename Allocator = std::allocator<std::pair<Key, Val>>>
class ordered_map;
}
```
The `ordered_map` is a `map` type that preserves the insertion order of values, allowing iteration in that order.
As a linear container, searches require `O(n)` time relative to the number of elements.
Use this when search operations are infrequent and maintaining the order of values is important.
## Non-Member Types
```cpp
namespace toml
{
struct ordered_type_config;
using ordered_value = basic_value<ordered_type_config>;
using ordered_table = typename ordered_value::table_type;
using ordered_array = typename ordered_value::array_type;
}
```
Use these in place of `toml::type_config` and `toml::value`.
{{< hint info >}}
Since `toml::parse` defaults to using `type_config`, specify
```cpp
const auto input = toml::parse<toml::ordered_type_config>("input.toml");
```
when parsing.
{{< /hint >}}
## Member Types
```cpp
using key_type = Key;
using mapped_type = Val;
using value_type = std::pair<Key, Val>;
using key_compare = Cmp;
using allocator_type = Allocator;
using container_type = std::vector<value_type, Allocator>;
using reference = typename container_type::reference;
using pointer = typename container_type::pointer;
using const_reference = typename container_type::const_reference;
using const_pointer = typename container_type::const_pointer;
using iterator = typename container_type::iterator;
using const_iterator = typename container_type::const_iterator;
using size_type = typename container_type::size_type;
using difference_type = typename container_type::difference_type;
```
## Member Functions
### Constructors
```cpp
ordered_map() = default;
```
Constructs an empty `ordered_map`.
### Constructors (Comparator, Allocator)
```cpp
explicit ordered_map(const Cmp& cmp, const Allocator& alloc = Allocator());
explicit ordered_map(const Allocator& alloc);
```
Constructs an `ordered_map` with a specified comparator for key comparison and an allocator for memory management.
### Copy and Move Constructors
```cpp
ordered_map(const ordered_map&) = default;
ordered_map(ordered_map&&) = default;
ordered_map(const ordered_map& other, const Allocator& alloc);
ordered_map(ordered_map&& other, const Allocator& alloc);
```
Constructs an `ordered_map` by copying or moving the contents from another `ordered_map`.
An allocator can also be specified for memory management.
### Constructors (Iterator)
```cpp
template<typename InputIterator>
ordered_map(InputIterator first, InputIterator last, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator());
template<typename InputIterator>
ordered_map(InputIterator first, InputIterator last, const Allocator& alloc = Allocator());
```
Constructs an `ordered_map` with a range represented by iterators.
The order of the elements follows the order of the iterators.
### Constructors (std::initializer_list)
```cpp
ordered_map(std::initializer_list<value_type> v, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator());
ordered_map(std::initializer_list<value_type> v, const Allocator& alloc);
```
Initializes the `ordered_map` using an initializer list.
### Copy and Move Assignment Operators
```cpp
ordered_map& operator=(const ordered_map&) = default;
ordered_map& operator=(ordered_map&&) = default;
```
Assigns the contents of another `ordered_map` to this one, using copy or move semantics.
### Assignment Operator (std::initializer_list)
```cpp
ordered_map& operator=(std::initializer_list<value_type> v);
```
Assigns the contents of an initializer list to the `ordered_map`.
### Destructor
```cpp
~ordered_map() = default;
```
Destroys the `ordered_map`.
### `begin()`, `end()`
```cpp
iterator begin() noexcept;
iterator end() noexcept;
const_iterator begin() const noexcept;
const_iterator end() const noexcept;
const_iterator cbegin() const noexcept;
const_iterator cend() const noexcept;
```
Returns iterators to the beginning and end of the container, allowing iteration over its contents in order.
### `empty()`
```cpp
bool empty() const noexcept;
```
Returns `true` if the `ordered_map` is empty, `false` otherwise.
### `size()`
```cpp
std::size_t size() const noexcept;
```
Returns the number of elements in the `ordered_map`.
### `max_size()`
```cpp
std::size_t max_size() const noexcept;
```
Returns the maximum number of elements the `ordered_map` can hold.
### `clear()`
```cpp
void clear();
```
Clears all elements from the `ordered_map`.
### `push_back(kv)`
```cpp
void push_back(const value_type&);
void push_back(value_type&&);
```
Appends a key-value pair to the end of the `ordered_map`.
### `emplace_back(k, v)`
```cpp
void emplace_back(key_type, mapped_type);
```
Appends a key-value pair to the end of the `ordered_map` by constructing it in place.
### `pop_back()`
```cpp
void pop_back();
```
Removes the last element from the `ordered_map`.
### `insert(kv)`
```cpp
void insert(value_type);
```
Inserts a key-value pair at the end of the `ordered_map`.
### `emplace(k, v)`
```cpp
void emplace(key_type, mapped_type);
```
Inserts a key-value pair at the end of the `ordered_map` by constructing it in place.
### `count(k)`
```cpp
std::size_t count(const key_type&) const noexcept;
```
Returns the number of elements with the specified key.
Since duplicate keys are not allowed, this will return either `1` if the key exists or `0` if it does not.
### `contains(k)`
```cpp
bool contains(const key_type&) const noexcept;
```
Returns `true` if the `ordered_map` contains an element with the specified key, `false` otherwise.
### `find(k)`
```cpp
iterator find(const key_type& key) noexcept;
const_iterator find(const key_type& key) const noexcept;
```
Finds an element with the specified key and returns an iterator to it. If the key is not found, returns `end()`.
### `at(k)`
```cpp
mapped_type& at(const key_type& k);
mapped_type const& at(const key_type& k) const;
```
Finds an element with the specified key and returns a reference to its value. Throws `std::out_of_range` if the key is not found.
### `operator[](k)`
```cpp
mapped_type& operator[](const key_type& k);
mapped_type const& operator[](const key_type& k) const;
```
Finds an element with the specified key and returns a reference to its value.
If the key is not found, a new value is constructed and returned.
If the `ordered_map` is `const`, throws `std::out_of_range` instead.
### `key_comp()`
```cpp
key_compare key_comp() const;
```
Returns the comparator used for key comparison.
## Notes
### Key Modification
{{< hint warning >}}
Since `ordered_map` uses `std::pair<Key, Val>` for `value_type`, it is possible to modify the key through an iterator. However, this practice is not recommended.
If you modify a key this way and it conflicts with an existing key, one of the conflicting keys will become unsearchable.
When using `operator[]`, `push_back`, or `insert`, collisions with existing keys are detected.
{{< /hint >}}
### Order Preservation Details
{{< hint warning >}}
`ordered_map` maintains the order of keys, but this order preservation applies only to keys defined within the same table. Order across different tables is not maintained.
For example, the order in the following file will be preserved:
```cpp
apple.type = "fruit"
apple.skin = "thin"
apple.color = "red"
orange.type = "fruit"
orange.skin = "thick"
orange.color = "orange"
```
In contrast, the order in the following file will not be preserved:
```cpp
apple.type = "fruit"
orange.type = "fruit"
apple.skin = "thin"
orange.skin = "thick"
apple.color = "red"
orange.color = "orange"
```
`ordered_map` preserves the order of the `apple` and `orange` definitions at the root table level, and the order of `type`, `skin`, `color` within each `apple` and `orange` table.
{{< /hint >}}
## Related
- [parser.hpp]({{<ref "parser.md">}})
- [types.hpp]({{<ref "types.md">}})
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,375 @@
+++
title = "parser.hpp"
type = "docs"
+++
# parser.hpp
Defines functions for parsing files or strings and the exceptions they use.
While `parse` throws an exception on failure, `try_parse` returns error information.
# `parse`
Parses the content of a given file and returns a `toml::basic_value`.
In case of failure, `toml::syntax_error` is thrown.
The type information of `basic_value` is provided by a `template`, and the TOML language version is specified by `toml::spec`.
### `parse(std::istream&, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
basic_value<TC>
parse(std::istream& is,
std::string fname = "unknown file",
spec s = spec::default_version());
}
```
Parses the content of the given `std::istream&`.
The filename information is taken as the third argument. If the filename is not provided, it defaults to `"unknown file"`.
### `parse(std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
basic_value<TC>
parse(std::string fname,
spec s = spec::default_version());
}
```
Parses the content of the given filename.
If reading the file fails, `toml::file_io_error` is thrown.
If parsing fails, `toml::syntax_error` is thrown.
### `parse(const char (&)[N] filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config, std::size_t N>
basic_value<TC>
parse(const char (&fname)[N],
spec s = spec::default_version());
}
```
Parses the content of the given filename from a string literal.
If reading the file fails, `toml::file_io_error` is thrown.
If parsing fails, `toml::syntax_error` is thrown.
### `parse(std::filesystem::path, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
basic_value<TC>
parse(const std::filesystem::path& fpath,
spec s = spec::default_version());
}
```
This is defined only if `<filesystem>` is available.
Parses the content of the file at the given file path.
If reading the file fails, `toml::file_io_error` is thrown.
If parsing fails, `toml::syntax_error` is thrown.
### `parse(FILE*, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
parse(FILE* fp,
std::string filename,
spec s = spec::default_version());
}
```
Parses the content of the file pointed to by `FILE*`.
If reading the file fails, `file_io_error` containing `errno` is thrown.
If parsing fails, `syntax_error` is thrown.
### `parse(std::vector<unsigned char>, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
basic_value<TC>
parse(std::vector<unsigned char> content,
std::string filename,
spec s = spec::default_version());
}
```
Parses the byte sequence as a TOML file.
If parsing fails, `toml::syntax_error` is thrown.
# `parse_str`
### `parse_str(std::string, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
basic_value<TC>
parse_str(std::string content,
spec s = spec::default_version(),
cxx::source_location loc = cxx::source_location::current());
}
```
Parses a string as a TOML file.
In case of failure, `toml::syntax_error` is thrown.
The type information of `basic_value` is provided by a `template`, and the TOML language version is specified by `toml::spec`.
You generally don't need to manually set the third argument, `cxx::source_location`.
If `std::source_location`, `std::experimental::source_location`, or `__builtin_FILE` is available,
the location information where `parse_str` was called will be stored.
# `try_parse`
Parses the contents of the given file and returns a `toml::basic_value` if successful, or a `std::vector<toml::error_info>` if it fails.
The type information of `basic_value` is specified by `template`, and the version of the TOML language is specified by `toml::spec`.
{{< hint warning >}}
Unlike `parse`, `try_parse` does not throw exceptions defined in toml11 such as `syntax_error`.
However, please note that exceptions thrown by the standard library will still propagate.
For instance, errors occurring internally within `std::ifstream` or memory exhaustion in `std::vector` will throw exceptions.
{{< /hint >}}
### `try_parse(std::istream&, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse(std::istream& is,
std::string fname = "unknown file",
spec s = spec::default_version());
}
```
Takes a `std::istream&` and parses its content.
The file name information is taken as the second argument. If a file name is not provided, it defaults to `"unknown file"`.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
### `try_parse(std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse(std::string fname,
spec s = spec::default_version());
}
```
Takes a file name and parses its content.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
### `try_parse(const char (&)[N] filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config, std::size_t N>
result<basic_value<TC>, std::vector<error_info>>
try_parse(const char (&fname)[N],
spec s = spec::default_version());
}
```
Takes a string literal as a file name and parses its content.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
### `try_parse(std::filesystem::path, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse(const std::filesystem::path& fpath,
spec s = spec::default_version());
}
```
Takes a file path and parses its content.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
### `try_parse(FILE*, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse(FILE* fp,
std::string filename,
spec s = spec::default_version());
}
```
Takes a `FILE*` and parses its content.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
### `try_parse(std::vector<unsigned char>, std::string filename, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse(std::vector<unsigned char> content,
std::string filename,
spec s = spec::default_version());
}
```
Takes a byte array and parses its content as a TOML file.
If parsing fails, a `result` holding the error type `std::vector<error_info>` is returned.
If successful, a `result` holding a `basic_value` is returned.
# `try_parse_str`
### `try_parse_str(std::string, toml::spec)`
```cpp
namespace toml
{
template<typename TC = type_config>
result<basic_value<TC>, std::vector<error_info>>
try_parse_str(std::string content,
spec s = spec::default_version(),
cxx::source_location loc = cxx::source_location::current());
}
```
Parses a string as a TOML file, returning a `toml::basic_value` if successful, or a `std::vector<toml::error_info>` if it fails.
Unlike `parse_str`, it does not throw `syntax_error`, but instead returns error information as the failure type of the `result`.
If `std::source_location`, `std::experimental::source_location`, or `__builtin_FILE` is available, it will record the location information.
Typically, you do not need to manually set the third argument `cxx::source_location`. If any of `std::source_location`, `std::experimental::source_location`, or `__builtin_FILE` are available, the information of the location where `parse_str` was called will be saved as the location information.
{{< hint warning >}}
Unlike `parse`, `try_parse` does not throw exceptions defined in toml11 such as `syntax_error`. However, please note that exceptions thrown by the standard library will still propagate.
For instance, errors occurring internally within `std::ifstream` or memory exhaustion in `std::vector` will throw exceptions.
{{< /hint >}}
# `syntax_error`
```cpp
namespace toml
{
struct syntax_error final : public ::toml::exception
{
public:
syntax_error(std::string what_arg, std::vector<error_info> err);
~syntax_error() noexcept override = default;
const char* what() const noexcept override;
std::vector<error_info> const& errors() const noexcept
};
}
```
An exception thrown when a syntax error is detected in TOML.
It is thrown by `parse` but not by `try_parse`.
# `file_io_error`
```cpp
namespace toml
{
struct file_io_error final : public ::toml::exception
{
public:
file_io_error(const std::string& msg, const std::string& fname);
file_io_error(int errnum, const std::string& msg, const std::string& fname);
~file_io_error() noexcept override = default;
const char* what() const noexcept override;
bool has_errno() const noexcept;
int get_errno() const noexcept;
};
}
```
An exception thrown when reading the contents of a file fails.
When using `FILE*` to read a file, `errno` is set.
### `has_errno`
If `std::ifstream` fails, `errno` is not set.
In this case, `has_errno` returns `false`.
### `get_errno`
Particularly when passing a `FILE*`, retrieves the value of `errno`.
If `has_errno` is `false`, it returns `0`.
# Related
- [error_info.hpp]({{<ref "error_info.md">}})
- [result.hpp]({{<ref "result.md">}})
- [spec.hpp]({{<ref "spec.md">}})
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,518 @@
+++
title = "result.hpp"
type = "docs"
+++
# result.hpp
`result.hpp` defines the `result` type, which can hold either a success value or a failure value.
This is used as the return type for `toml::try_parse`, which does not throw exceptions.
# success
A type that holds a success value.
```cpp
namespace toml
{
template<typename T>
struct success
{
using value_type = T;
explicit success(value_type v);
~success() = default;
success(const success&) = default;
success(success&&) = default;
success& operator=(const success&) = default;
success& operator=(success&&) = default;
template<typename U>
explicit success(U&& v);
template<typename U>
explicit success(success<U> v);
value_type& get() noexcept;
value_type const& get() const noexcept;
};
template<typename T>
success<typename std::decay<T>::type> ok(T&& v);
template<std::size_t N>
success<std::string> ok(const char (&literal)[N])
}
```
## Member Types
```cpp
using value_type = T;
```
The type of the success value.
## Member Functions
### Constructor
```cpp
explicit success(value_type v);
```
Constructs with a `value_type` argument.
```cpp
template<typename U>
explicit success(U&& v);
```
Constructs with another type that can be converted to `value_type`.
```cpp
template<typename U>
explicit success(success<U> v);
```
Constructs with another `success` type that can be converted to `value_type`.
### `get()`
```cpp
value_type& get() noexcept;
value_type const& get() const noexcept;
```
Accesses the stored value.
## Non-Member Functions
### `ok(T)`
```cpp
template<typename T>
success<typename std::decay<T>::type> ok(T&& v);
template<std::size_t N>
success<std::string> ok(const char (&literal)[N]);
```
Constructs and returns a `success` type from a success value.
Converts a string literal into `std::string`.
# `success<reference_wrapper<T>>`
Specialization of `success` for when the success value is a reference.
```cpp
namespace toml
{
template<typename T>
struct success<std::reference_wrapper<T>>
{
using value_type = T;
explicit success(std::reference_wrapper<value_type> v) noexcept;
value_type& get() noexcept;
value_type const& get() const noexcept;
};
}
```
## Member Types
```cpp
using value_type = T;
```
The type of the success value. It is `T` from `std::reference_wrapper<T>`, not the reference itself.
### `get()`
```cpp
value_type& get() noexcept;
value_type const& get() const noexcept;
```
Accesses the stored value.
# failure
A type that holds a failure value.
```cpp
namespace toml
{
template<typename T>
struct failure
{
using value_type = T;
explicit failure(value_type v);
~failure() = default;
failure(const failure&) = default;
failure(failure&&) = default;
failure& operator=(const failure&) = default;
failure& operator=(failure&&) = default;
template<typename U>
explicit failure(U&& v);
template<typename U>
explicit failure(failure<U> v);
value_type& get() noexcept;
value_type const& get() const noexcept;
};
template<typename T>
failure<typename std::decay<T>::type> err(T&& v);
template<std::size_t N>
failure<std::string> err(const char (&literal)[N]);
}
```
## Member Types
```cpp
using value_type = T;
```
The type of the failure value.
## Member Functions
### Constructor
```cpp
explicit failure(value_type v);
```
Constructs with a `value_type` argument.
```cpp
template<typename U>
explicit failure(U&& v);
```
Constructs with another type that can be converted to `value_type`.
```cpp
template<typename U>
explicit failure(failure<U> v);
```
Constructs with another `failure` type that can be converted to `value_type`.
### `get()`
```cpp
value_type& get() noexcept;
value_type const& get() const noexcept;
```
Accesses the stored value.
## Non-Member Functions
### `err(T)`
```cpp
template<typename T>
failure<typename std::decay<T>::type> err(T&& v);
template<std::size_t N>
failure<std::string> err(const char (&literal)[N]);
```
Constructs and returns a `failure` type from a failure value.
Converts a string literal into `std::string`.
# `failure<reference_wrapper<T>>`
Specialization of `failure` for when the failure value is a reference.
```cpp
namespace toml
{
template<typename T>
struct failure<std::reference_wrapper<T>>
{
using value_type = T;
explicit failure(std::reference_wrapper<value_type> v) noexcept;
value_type& get() noexcept {return value.get();}
value_type const& get() const noexcept {return value.get();}
};
}
```
## Member Types
```cpp
using value_type = T;
```
The type of the failure value. It is `T` from `std::reference_wrapper<T>`, not the reference itself.
### `get()`
```cpp
value_type& get() noexcept;
value_type const& get() const noexcept;
```
Accesses the stored value.
# result
A type that holds either a success value or a failure value.
```cpp
namespace toml
{
template<typename T, typename E>
struct result
{
using success_type = success<T>;
using failure_type = failure<E>;
using value_type = typename success_type::value_type;
using error_type = typename failure_type::value_type;
result(success_type s);
result(failure_type f);
template<typename U>
result(success<U> s);
template<typename U>
result(failure<U> f);
result& operator=(success_type s);
result& operator=(failure_type f);
template<typename U>
result& operator=(success<U> s);
template<typename U>
result& operator=(failure<U> f);
~result() noexcept;
result(const result& other);
result(result&& other);
result& operator=(const result& other);
result& operator=(result&& other);
template<typename U, typename F>
result(result<U, F> other);
template<typename U, typename F>
result& operator=(result<U, F> other);
bool is_ok() const noexcept;
bool is_err() const noexcept;
explicit operator bool() const noexcept;
value_type& unwrap(cxx::source_location loc = cxx::source_location::current());
value_type const& unwrap(cxx::source_location loc = cxx::source_location::current()) const;
value_type& unwrap_or(value_type& opt) noexcept;
value_type const& unwrap_or(value_type const& opt) const noexcept;
error_type& unwrap_err(cxx::source_location loc = cxx::source_location::current());
error_type const& unwrap_err(cxx::source_location loc = cxx::source_location::current()) const;
value_type& as_ok() noexcept;
value_type const& as_ok() const noexcept;
error_type& as_err() noexcept;
error_type const& as_err() const noexcept;
};
}
```
## Member Types
### `success_type`
`success<T>`.
### `failure_type`
`failure<E>`.
### `value_type`
The type `T` of the success value, alias for `success_type::value_type`.
If `T` is `std::reference_wrapper<U>`, then it is `U`.
### `error_type`
The type `E` of the failure value, alias for `failure_type::value_type`.
If `E` is `std::reference_wrapper<F>`, then it is `F`.
## Member Functions
### Constructor
```cpp
result() = delete;
```
Cannot construct `result` type by default. Needs to be given either a success or failure type.
```cpp
result(success_type s);
result(failure_type f);
```
Constructs with a `success_type` or `failure_type`.
```cpp
template<typename U>
result(success<U> s);
template<typename U>
result(failure<U> f);
```
Constructs with a `success<U>` or `failure<U>` that is convertible to `value_type` or `error_type`.
```cpp
template<typename U, typename F>
result(result<U, F> other);
template<typename U, typename F>
result& operator=(result<U, F> other);
```
Constructs from or assigns to another `result` with convertible `success` or `failure` types.
### Copy and Move Constructors
```cpp
result(const result& other);
result(result&& other);
```
Can be copy or move constructed.
### `operator=`
```cpp
result& operator=(const result& other);
result& operator=(result&& other);
```
Can be copy or move assigned.
```cpp
template<typename U>
result& operator=(success<U> s);
template<typename U>
result& operator=(failure<U> f);
```
Can be assigned from convertible `success` or `failure` types.
### `is_ok()`
```cpp
bool is_ok() const noexcept;
```
Returns `true` if it holds a success value, `false` otherwise.
### `is_err()`
```cpp
bool is_err() const noexcept;
```
Returns `true` if it holds a failure value, `false` otherwise.
### `operator bool()`
```cpp
explicit operator bool() const noexcept;
```
Returns `true` if it holds a success value, `false` otherwise.
### `unwrap()`
```cpp
value_type& unwrap(cxx::source_location loc = cxx::source_location::current());
value_type const& unwrap(cxx::source_location loc = cxx::source_location::current()) const;
```
Returns the stored success value.
Throws `toml::bad_result_access` if it holds a failure value.
If `std::source_location` or equivalent compiler extension is available, the file name and line number where `unwrap()` occurred are included in the `what()` string.
### `unwrap_or()`
```cpp
value_type& unwrap_or(value_type& opt) noexcept;
value_type const& unwrap_or(value_type const& opt) const noexcept;
```
Returns the stored success value if present, otherwise returns the provided default value.
### `unwrap_err()`
```cpp
error_type& unwrap_err(cxx::source_location loc = cxx::source_location::current());
error_type const& unwrap_err(cxx::source_location loc = cxx::source_location::current()) const;
```
Returns the stored failure value.
Throws `toml::bad_result_access` if it holds a success value.
If `std::source_location` or equivalent compiler extension is available, the file name and line number where `unwrap_err()` occurred are included in the `what()` string.
### `as_ok()`
```cpp
value_type& as_ok() noexcept;
value_type const& as_ok() const noexcept;
```
Returns the success value without checking.
Behavior is undefined if it holds a failure value.
### `as_err()`
```cpp
error_type& as_err() noexcept;
error_type const& as_err() const noexcept;
```
Returns the failure value without checking.
Behavior is undefined if it holds a success value.
# bad_result_access
An exception thrown when `unwrap` or `unwrap_err` fails in a `result`.
```cpp
namespace toml
{
struct bad_result_access : public ::toml::exception
{
public:
explicit bad_result_access(const std::string& what_arg);
virtual ~bad_result_access() noexcept override = default;
virtual const char* what() const noexcept override;
protected:
std::string what_;
};
}
```

View File

@@ -0,0 +1,65 @@
+++
title = "serializer.hpp"
type = "docs"
+++
# serializer.hpp
## `format`
Serializes the data.
```cpp
namespace toml
{
template<typename TC>
std::string format(const basic_value<TC>& v,
const spec s = spec::default_version());
template<typename TC>
std::string format(const typename basic_value<TC>::key_type& k,
const basic_value<TC>& v,
const spec s = spec::default_version());
template<typename TC>
std::string format(const std::vector<typename basic_value<TC>::key_type>& ks,
const basic_value<TC>& v,
const spec s = spec::default_version());
}
```
If there's a conflict between the format information and the `spec`, for example, when using `v1.0.0` with `table_format::multiline_oneline`, the `spec` takes precedence.
### `format(v, spec)`
Formats a `toml::value` according to its format information and the provided `spec`.
If it's a `table_type`, it's formatted as if it were the root table. Otherwise, only the value is formatted.
### `format(k, v, spec)`
Formats a `toml::value` along with the given key.
`v` is interpreted as being defined under that key.
### `format([k,...], v, spec)`
`v` is interpreted as being defined under those keys.
If multiple keys are provided, it's interpreted as a recursively defined table.
## `serialization_error`
Reports errors that occurred during serialization.
```cpp
namespace toml
{
struct serialization_error final : public ::toml::exception
{
public:
explicit serialization_error(std::string what_arg, source_location loc);
~serialization_error() noexcept override = default;
const char* what() const noexcept override;
source_location const& location() const noexcept;
};
}
```

View File

@@ -0,0 +1,235 @@
+++
title = "source_location.hpp"
type = "docs"
+++
# source_location.hpp
`source_location.hpp` defines a class representing a specific area within a TOML file.
This class is used to represent problematic areas in error messages.
# `toml::source_location`
`source_location` is a class representing a specific area within a TOML file.
```cpp
namespace toml
{
struct source_location
{
public:
explicit source_location(/* implementation-defined */);
~source_location() = default;
source_location(source_location const&) = default;
source_location(source_location &&) = default;
source_location& operator=(source_location const&) = default;
source_location& operator=(source_location &&) = default;
bool is_ok() const noexcept;
std::size_t length() const noexcept;
std::size_t first_line_number() const noexcept;
std::size_t first_column_number() const noexcept;
std::size_t last_line_number() const noexcept;
std::size_t last_column_number() const noexcept;
std::string const& file_name() const noexcept;
std::size_t num_lines() const noexcept;
std::string const& first_line() const;
std::string const& last_line() const;
std::vector<std::string> const& lines() const noexcept;
};
template<typename ... Ts>
std::string format_location(const source_location& loc, const std::string& msg, const Ts& ... locs_and_msgs);
} //toml
```
## Member Functions
### Constructor
```cpp
explicit source_location(/* implementation-defined */);
```
`toml::source_location` can only be constructed via `toml::parse` or the `_toml` literal.
### `is_ok()`
```cpp
bool is_ok() const noexcept;
```
Returns `true` if the `source_location` holds a valid value, `false` otherwise.
The result of `location()` from `toml::value` constructed outside of `toml::parse` or `_toml` literals returns `false` for `is_ok` as it points to nothing.
### `length()`
```cpp
std::size_t length() const noexcept;
```
Returns the length of the area pointed to by the `source_location`.
Returns `0` if it does not hold a valid value.
### `first_line_number()`
```cpp
std::size_t first_line_number() const noexcept;
```
Returns the line number of the first line of the area pointed to by the `source_location`.
Returns `1` if it does not hold a valid value.
### `first_column_number()`
```cpp
std::size_t first_column_number() const noexcept;
```
Returns the column number of the first column of the area pointed to by the `source_location`.
Returns `1` if it does not hold a valid value.
### `last_line_number()`
```cpp
std::size_t last_line_number() const noexcept;
```
Returns the line number of the last line of the area pointed to by the `source_location`.
Returns `1` if it does not hold a valid value.
### `last_column_number()`
```cpp
std::size_t last_column_number() const noexcept;
```
Returns the column number of the last column of the area pointed to by the `source_location`.
Returns `1` if it does not hold a valid value.
### `file_name()`
```cpp
std::string const& file_name() const noexcept;
```
Returns the file name containing the area pointed to by the `source_location`.
Returns `"unknown file"` if it does not hold a valid value.
### `num_lines()`
```cpp
std::size_t num_lines() const noexcept;
```
Returns the number of lines in the area pointed to by the `source_location`.
Returns `0` if it does not hold a valid value.
### `first_line()`
```cpp
std::string const& first_line() const;
```
Returns the first line of the area pointed to by the `source_location`.
Throws `std::out_of_range` if it does not hold a valid value.
### `last_line()`
```cpp
std::string const& last_line() const;
```
Returns the last line of the area pointed to by the `source_location`.
Throws `std::out_of_range` if it does not hold a valid value.
### `lines()`
```cpp
std::vector<std::string> const& lines() const noexcept;
```
Returns all lines in the area pointed to by the `source_location`.
Returns a reference to an empty `std::vector` if it does not hold a valid value.
## Non-Member Functions
### `format_location`
```cpp
template<typename ... Ts>
std::string format_location(const source_location& loc, const std::string& msg,
const Ts& ... locs_and_msgs);
```
Formats the specified `source_location` and its associated message as follows:
```
-> {filename.toml}
|
1 | a = 42
| ^-- {message}
```
If colorization is enabled, ANSI escape sequences will be added for coloring.
When multiple `locs_and_msgs` are provided, they must be in the order of `const source_location&` followed by `const std::string&`, and the next pair in the same order, and so on.
#### Example: Multiple `source_location` and `std::string`
When multiple `source_location` and `std::string` pairs are provided, they are formatted as follows:
```cpp
source_location& loc0;
source_location& loc1;
source_location& loc2;
std::string msg0;
std::string msg1;
std::string msg2;
format_location(loc0, msg0,
loc1, msg1,
loc2, msg2);
```
```
-> {filename0.toml}
|
1 | a = 42
| ^-- {message0}
|
-> {filename1.toml}
|
2 | b = 3.14
| ^-- {message1}
|
-> {filename2.toml}
|
3 | c = "foo"
| ^-- {message2}
```
# Related
- [error_info.hpp]({{<ref "error_info.md">}})
- [value.hpp]({{<ref "value.md">}})

View File

@@ -0,0 +1,309 @@
+++
title = "spec.hpp"
type = "docs"
+++
# spec.hpp
`spec.hpp` defines classes for specifying the version of TOML.
# `toml::semantic_version`
`semantic_version` is a class that stores version information.
```cpp
namespace toml
{
struct semantic_version
{
constexpr semantic_version(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept;
std::uint32_t major;
std::uint32_t minor;
std::uint32_t patch;
};
constexpr semantic_version
make_semver(std::uint32_t major, std::uint32_t minor, std::uint32_t patch) noexcept;
constexpr bool operator==(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator!=(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator< (const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator<=(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator> (const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator>=(const semantic_version&, const semantic_version&) noexcept;
std::ostream& operator<<(std::ostream& os, const semantic_version& ver);
} //toml
```
## Member Functions
### Constructor
```cpp
constexpr semantic_version(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept;
```
Constructs a `semantic_version` instance with the specified `major`, `minor`, and `patch` version numbers.
## Non-Member Functions
### Comparison Operators
```cpp
constexpr bool operator==(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator!=(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator< (const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator<=(const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator> (const semantic_version&, const semantic_version&) noexcept;
constexpr bool operator>=(const semantic_version&, const semantic_version&) noexcept;
```
Compares two `semantic_version` instances according to semantic versioning rules.
### Stream Operator
```cpp
std::ostream& operator<<(std::ostream& os, const semantic_version& ver);
```
Outputs the version in the format `{major}.{minor}.{patch}`.
### `to_string`
```cpp
std::string to_string(const semantic_version& ver);
```
Converts the version to a string in the format `{major}.{minor}.{patch}`.
# `toml::spec`
`spec` is a class that stores TOML version information.
```cpp
struct spec
{
constexpr static spec default_version() noexcept;
constexpr static spec v(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept;
constexpr explicit spec(const semantic_version& semver) noexcept;
semantic_version version; // toml version
// diff from v1.0.0 -> v1.1.0
bool v1_1_0_allow_control_characters_in_comments;
bool v1_1_0_allow_newlines_in_inline_tables;
bool v1_1_0_allow_trailing_comma_in_inline_tables;
bool v1_1_0_allow_non_english_in_bare_keys;
bool v1_1_0_add_escape_sequence_e;
bool v1_1_0_add_escape_sequence_x;
bool v1_1_0_make_seconds_optional;
// library extensions
bool ext_hex_float; // allow hex float
bool ext_num_suffix; // allow number suffix
bool ext_null_value; // allow null value
};
```
## Member Functions
### Constructor
```cpp
constexpr explicit spec(const semantic_version& semver) noexcept;
```
Constructs a `spec` with the specified TOML version.
Supports TOML v1.0.0 and TOML v1.1.0.
### `default_version()`
```cpp
constexpr static spec default_version() noexcept;
```
Constructs a `spec` with the default version.
Used as the default value for `toml::parse` and `toml::format`.
In toml11 v4.0.0, the value is v1.0.0.
### `v(major, minor, patch)`
```cpp
constexpr static spec v(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept;
```
Constructs a `spec` with the specified version.
## Member Variables
Each flag is automatically set to `true` when the specified version includes the corresponding feature.
You can modify these flags to change the behavior of `toml::parse` and `toml::format`.
{{<hint warning>}}
Some features of TOML v1.1.0 are still under fairly lengthy discussion and may still be reverted.
If they are indeed reverted, toml11 will remove those features in a minor version upgrade or move them to a corresponding later version.
As such, any features related to future versions should be considered unstable.
{{</hint>}}
### Example
```cpp
auto spec = toml::spec::v(1, 0, 0);
// Allow newlines in inline tables in addition to v1.0.0 features.
// Other v1.1.0 features are not enabled.
spec.v1_1_0_allow_newlines_in_inline_tables = true;
auto input = toml::parse("input_file.toml", spec);
```
### `v1_1_0_allow_control_characters_in_comments`
```cpp
bool v1_1_0_allow_control_characters_in_comments;
```
Allows most control characters in comments.
Added in TOML v1.1.0.
### `v1_1_0_allow_newlines_in_inline_tables`
```cpp
bool v1_1_0_allow_newlines_in_inline_tables;
```
Allows newlines in inline tables.
Added in TOML v1.1.0.
### `v1_1_0_allow_trailing_comma_in_inline_tables`
```cpp
bool v1_1_0_allow_trailing_comma_in_inline_tables;
```
Allows trailing commas in inline tables.
Added in TOML v1.1.0.
### `v1_1_0_add_escape_sequence_e`
```cpp
bool v1_1_0_add_escape_sequence_e;
```
Allows `\e` to represent the ESC character.
Added in TOML v1.1.0.
### `v1_1_0_add_escape_sequence_x`
```cpp
bool v1_1_0_add_escape_sequence_x;
```
Allows `\xHH` to represent a single byte character.
Added in TOML v1.1.0.
### `v1_1_0_make_seconds_optional`
```cpp
bool v1_1_0_make_seconds_optional;
```
Makes the seconds component in time optional.
Unspecified seconds default to `0`.
Added in TOML v1.1.0.
### `ext_hex_float`
```cpp
bool ext_hex_float;
```
This is a language extension specific to toml11.
It is initialized to `false` regardless of the specified version.
You must explicitly set it to `true` if you want to use it.
Allows hexadecimal representation of floating-point numbers.
The hexadecimal representation conforms to the `printf` format specifier `%a/%A`.
```
hexf = 0xC0FFEEp-10
```
`toml::format` will format using hexadecimal notation only if the passed `toml::spec` has `ext_hex_float` set to `true`.
If the format specifier indicates `hex` but the `toml::spec` passed to `toml::format` has `ext_hex_float` set to `false`, the hexadecimal specification is ignored, and the number is output in decimal notation with maximum precision.
### `ext_num_suffix`
```cpp
bool ext_num_suffix;
```
This is a language extension specific to toml11.
It is initialized to `false` regardless of the specified version.
You must explicitly set it to `true` if you want to use it.
Allows the addition of suffixes to decimal integers and floating-point numbers. This does not apply to hexadecimal, octal, or binary notations.
There must be an `_` separator between the number and the suffix.
The suffix cannot start with a digit to avoid confusion with the numeric part.
```
distance = 10_m # valid
distance = 10_2m # invalid
distance = 10_2_m # valid
```
The suffix is stored in the format information as `std::string suffix`.
The `_` separating the number from the suffix is not included in the `suffix`.
```cpp
toml::value distance = toml::find(input, "distance");
assert(distance.as_integer_fmt().suffix == std::string("m"));
```
`toml::format` will format the value with the suffix only if the passed `toml::spec` has `ext_num_suffix` set to `true`.
The `suffix` follows the grammar defined as:
```abnf
non-digit-graph = ALPHA / non-ascii
graph = ALPHA / DIGIT / non-ascii
suffix = _ non-digit-graph *( graph / ( _ graph ) )
```
### `ext_null_value`
```cpp
bool ext_null_value;
```
This is a language extension specific to toml11.
Allows the use of `null` as a value.
A `toml::value` specified as `null` will have no value, and `is_empty()` will return `true`.
`toml::format` will format it as `null` only if the passed `toml::spec` has `ext_null_value` set to `true`.
Otherwise, `toml::format` will terminate with an error.

View File

@@ -0,0 +1,13 @@
+++
title = "toml.hpp"
type = "docs"
+++
# toml.hpp
`toml.hpp` includes all other headers.
This allows access to all features of toml11.
This header file and `toml_fwd.hpp` are located under `${TOML11_INCLUDE_DIR}/`,
while other header files are located under `${toml11_include_dir}/toml11/`.

View File

@@ -0,0 +1,18 @@
+++
title = "toml_fwd.hpp"
type = "docs"
+++
# toml_fwd.hpp
`toml_fwd.hpp` contains forward declarations of structures defined in toml11 and macro definitions.
When only forward declarations of toml11 structures are needed and implementation is not required, including `toml_fwd.hpp` instead of `toml.hpp` can reduce compilation time.
{{<hint warning>}}
Since this file only contains forward declarations, you cannot use `toml::table`, defined as `toml::basic_value<toml::type_config>::table_type`, and similarly defined `toml::array`. This is because they require the implementation of `basic_value`.
{{</hint>}}
This header file and `toml.hpp` are located under `${TOML11_INCLUDE_DIR}/`, while other header files are located under `${TOML11_INCLUDE_DIR}/toml11/`.

View File

@@ -0,0 +1,156 @@
+++
title = "types.hpp"
type = "docs"
+++
# types.hpp
This document defines classes that specifies type information.
# `type_config`
`type_config` is a type that encapsulates parameters given to `toml::basic_value`.
When using different types within `toml::basic_value<T>`, you need to define and pass this type separately.
All elements listed are required.
If you use numerical types that cannot use standard stream operators, define and replace the equivalents for `read_int` and `read_float`.
```cpp
namespace toml
{
struct type_config
{
using comment_type = preserve_comments;
using boolean_type = bool;
using integer_type = std::int64_t;
using floating_type = double;
using string_type = std::string;
template<typename T>
using array_type = std::vector<T>;
template<typename K, typename T>
using table_type = std::unordered_map<K, T>;
static result<integer_type, error_info>
parse_int(const std::string& str, const source_location src, const std::uint8_t base);
static result<floating_type, error_info>
parse_float(const std::string& str, const source_location src, const bool is_hex);
};
using value = basic_value<type_config>;
using table = typename value::table_type;
using array = typename value::array_type;
} // toml
```
## `static` Member Functions
### `parse_int(str, src, base)`
```cpp
static result<integer_type, error_info>
parse_int(const std::string& str, const source_location src, const std::uint8_t base);
```
If you use a type as `integer_type` that cannot utilize standard stream operators, implement this function.
Otherwise, use `read_int` described later.
The `str` parameter receives a string with prefixes, leading zeros, and underscores removed.
The `src` parameter receives a `source_location` pointing to where the string was defined.
The `base` parameter receives one of `10`, `2`, `8`, or `16`.
### `parse_float(str, src, is_hex)`
```cpp
static result<floating_type, error_info>
parse_float(const std::string& str, const source_location src, const bool is_hex);
```
If you use a type as `floating_type` that cannot utilize standard stream operators, implement this function.
Otherwise, use `read_float` described later.
The `str` parameter receives a string with prefixes, leading zeros, and underscores removed.
The `src` parameter receives a `source_location` pointing to where the string was defined.
The `is_hex` parameter indicates whether the format is `hexfloat`. If you don't use the `hexfloat` extension, you don't need to implement this.
For details on the `hexfloat` extension, refer to [spec.hpp]({{<ref "spec.md">}}).
## Non-member Functions
### `read_int`
```cpp
template<typename T>
result<T, error_info>
read_int(const std::string& str, const source_location src, const std::uint8_t base);
```
This is the default function used. It parses using `std::istringstream`.
If `operator>>` and manipulators like `std::hex`, and `std::numeric_limits<T>` are defined (such as for `boost::multiprecision`), you can use this without modifications.
### `read_float`
```cpp
template<typename T>
result<T, error_info>
read_float(const std::string& str, const source_location src, const bool is_hex);
```
This is the default function used. It parses decimals using `std::istringstream` and hexfloats using `sscanf()`.
It supports `double` and `float`.
For other types, if `operator>>` is defined and `hex` is not used, you can use this function.
# `ordered_type_config`
`ordered_type_config` is a variation of `toml::type_config` where the table type is replaced with `toml::ordered_map`.
Additionally, it defines the `toml::ordered_value` alias.
Other than these changes, it is identical to `type_config`.
```cpp
namespace toml
{
struct ordered_type_config
{
using comment_type = preserve_comments;
using boolean_type = bool;
using integer_type = std::int64_t;
using floating_type = double;
using string_type = std::string;
template<typename T>
using array_type = std::vector<T>;
template<typename K, typename T>
using table_type = ordered_map<K, T>;
static result<integer_type, error_info>
parse_int(const std::string& str, const source_location src, const std::uint8_t base)
{
return read_int<integer_type>(str, src, base);
}
static result<floating_type, error_info>
parse_float(const std::string& str, const source_location src, const bool is_hex)
{
return read_float<floating_type>(str, src, is_hex);
}
};
using ordered_value = basic_value<ordered_type_config>;
using ordered_table = typename ordered_value::table_type;
using ordered_array = typename ordered_value::array_type;
} // toml
```

View File

@@ -0,0 +1,896 @@
+++
title = "value.hpp"
type = "docs"
+++
# value.hpp
`value.hpp` defines `basic_value`.
# `toml::basic_value`
`basic_value` is a class that stores TOML values.
```cpp
namespace toml
{
template <class TypeConfig>
class basic_value;
// Defined in types.hpp
// using value = basic_value<type_config>;
// using table = typename basic_value<type_config>::table_type;
// using array = typename basic_value<type_config>::array_type;
template<typename TC>
bool operator==(const basic_value<TC>&, const basic_value<TC>&);
template<typename TC>
bool operator!=(const basic_value<TC>&, const basic_value<TC>&);
template<typename TC>
bool operator< (const basic_value<TC>&, const basic_value<TC>&);
template<typename TC>
bool operator<=(const basic_value<TC>&, const basic_value<TC>&);
template<typename TC>
bool operator> (const basic_value<TC>&, const basic_value<TC>&);
template<typename TC>
bool operator>=(const basic_value<TC>&, const basic_value<TC>&);
} //toml
```
## Member Types
The following member types are defined.
You can modify the member types using `TypeConfig`.
See also: [types.hpp]({{<ref "types.md">}})
| Name | Definition |
|:-----------------------|:-------------------------------------|
| `char_type` | `typename TypeConfig::char_type` |
| `key_type` | `typename TypeConfig::string_type` |
| `value_type` | `basic_value<TypeConfig>` |
| `boolean_type` | `typename TypeConfig::boolean_type` |
| `integer_type` | `typename TypeConfig::integer_type` |
| `floating_type` | `typename TypeConfig::floating_type` |
| `string_type` | `typename TypeConfig::string_type` |
| `local_time_type` | `toml::local_time` |
| `local_date_type` | `toml::local_date` |
| `local_datetime_type` | `toml::local_datetime` |
| `offset_datetime_type` | `toml::offset_datetime` |
| `array_type` | `typename TypeConfig::template array_type<value_type>`|
| `table_type` | `typename TypeConfig::template table_type<key_type, value_type>`|
| `comment_type` | `typename TypeConfig::comment_type` |
## Member Functions
### Default Constructor
```cpp
basic_value() noexcept
```
Constructs an empty `toml::value`.
The constructed `toml::value` will be empty.
### Copy and Move Constructors
```cpp
basic_value(const basic_value& v)
basic_value(basic_value&& v)
```
Copies or moves all information including values, format information, comments, and file regions.
### Copy and Move Constructors with Comments
```cpp
basic_value(basic_value v, std::vector<std::string> com)
```
Copies or moves the object while overwriting comments.
### Conversion Constructors
```cpp
template<typename TI>
basic_value(basic_value<TI> other)
template<typename TI>
basic_value(basic_value<TI> other, std::vector<std::string> com)
```
Copies or moves from a `basic_value` with a different `type_config`.
### Constructor (boolean)
```cpp
basic_value(boolean_type x)
basic_value(boolean_type x, boolean_format_info fmt)
basic_value(boolean_type x, std::vector<std::string> com)
basic_value(boolean_type x, boolean_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `bool`, its format information, and comments.
### Constructor (integer)
```cpp
template<typename T, /* std::is_integral<T> is true */>
basic_value(T x)
template<typename T, /* std::is_integral<T> is true */>
basic_value(T x, integer_format_info fmt)
template<typename T, /* std::is_integral<T> is true */>
basic_value(T x, std::vector<std::string> com)
template<typename T, /* std::is_integral<T> is true */>
basic_value(T x, integer_format_info fmt, std::vector<std::string> com)
```
Constructs an object with an `integer`, its format information, and comments.
### Constructor (floating)
```cpp
template<typename T, /* std::is_floating_point<T> is true */>
basic_value(T x)
template<typename T, /* std::is_floating_point<T> is true */>
basic_value(T x, floating_format_info fmt)
template<typename T, /* std::is_floating_point<T> is true */>
basic_value(T x, std::vector<std::string> com)
template<typename T, /* std::is_floating_point<T> is true */>
basic_value(T x, floating_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `floating` point number, its format information, and comments.
### Constructor (string)
```cpp
basic_value(string_type x)
basic_value(string_type x, string_format_info fmt)
basic_value(string_type x, std::vector<std::string> com)
basic_value(string_type x, string_format_info fmt, std::vector<std::string> com)
basic_value(const string_type::value_type* x)
basic_value(const string_type::value_type* x, string_format_info fmt)
basic_value(const string_type::value_type* x, std::vector<std::string> com)
basic_value(const string_type::value_type* x, string_format_info fmt, std::vector<std::string> com)
// C++17以降
basic_value(string_view_type x)
basic_value(string_view_type x, string_format_info fmt)
basic_value(string_view_type x, std::vector<std::string> com)
basic_value(string_view_type x, string_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `string`, its format information, and comments.
`string_view_type` shares the same `value_type` and `traits_type` as `string_type`.
### Constructor (local_date)
```cpp
basic_value(local_date_type x)
basic_value(local_date_type x, local_date_format_info fmt)
basic_value(local_date_type x, std::vector<std::string> com)
basic_value(local_date_type x, local_date_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `local_date_type`, its format information, and comments.
### Constructor (local_time)
```cpp
basic_value(local_time_type x)
basic_value(local_time_type x, local_time_format_info fmt)
basic_value(local_time_type x, std::vector<std::string> com)
basic_value(local_time_type x, local_time_format_info fmt, std::vector<std::string> com)
template<typename Rep, typename Period>
basic_value(const std::chrono::duration<Rep, Period>& x)
template<typename Rep, typename Period>
basic_value(const std::chrono::duration<Rep, Period>& x, local_time_format_info fmt)
template<typename Rep, typename Period>
basic_value(const std::chrono::duration<Rep, Period>& x, std::vector<std::string> com)
template<typename Rep, typename Period>
basic_value(const std::chrono::duration<Rep, Period>& x, local_time_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `local_time_type`, its format information, and comments.
For `std::chrono::duration`, constructs as a time span from `00:00:00`.
### Constructor (local_datetime)
```cpp
basic_value(local_datetime_type x)
basic_value(local_datetime_type x, local_date_format_info fmt)
basic_value(local_datetime_type x, std::vector<std::string> com)
basic_value(local_datetime_type x, local_date_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `local_datetime_type`, its format information, and comments.
### Constructor (offset_datetime)
```cpp
basic_value(offset_datetime_type x)
basic_value(offset_datetime_type x, offset_date_format_info fmt)
basic_value(offset_datetime_type x, std::vector<std::string> com)
basic_value(offset_datetime_type x, offset_date_format_info fmt, std::vector<std::string> com)
basic_value(std::chrono::system_clock::time_point x)
basic_value(std::chrono::system_clock::time_point x, offset_date_format_info fmt)
basic_value(std::chrono::system_clock::time_point x, std::vector<std::string> com)
basic_value(std::chrono::system_clock::time_point x, offset_date_format_info fmt, std::vector<std::string> com)
```
Constructs an object with an `offset_datetime_type`, its format information, and comments.
For `std::chrono::system_clock::time_point`, constructs for the pointed time.
### Constructor (array)
```cpp
basic_value(array_type x)
basic_value(array_type x, integer_format_info fmt)
basic_value(array_type x, std::vector<std::string> com)
basic_value(array_type x, integer_format_info fmt, std::vector<std::string> com)
template<typename T, /* T is array-like */>
basic_value(T x)
template<typename T, /* T is array-like */>
basic_value(T x, array_format_info fmt)
template<typename T, /* T is array-like */>
basic_value(T x, std::vector<std::string> com)
template<typename T, /* T is array-like */>
basic_value(T x, array_format_info fmt, std::vector<std::string> com)
```
Constructs an object with an `array`, its format information, and comments.
`array-like` types must meet the following criteria:
- Has `T::iterator`.
- Has `T::value_type`.
- Does **not** have `T::key_type`.
- Does **not** have `T::mapped_type`.
- Is **not** `std::string`.
- Is **not** `std::string_view` (since C++17).
### Constructor (table)
```cpp
basic_value(table_type x)
basic_value(table_type x, integer_format_info fmt)
basic_value(table_type x, std::vector<std::string> com)
basic_value(table_type x, integer_format_info fmt, std::vector<std::string> com)
template<typename T, /* T is table-like */>
basic_value(T x)
template<typename T, /* T is table-like */>
basic_value(T x, table_format_info fmt)
template<typename T, /* T is table-like */>
basic_value(T x, std::vector<std::string> com)
template<typename T, /* T is table-like */>
basic_value(T x, table_format_info fmt, std::vector<std::string> com)
```
Constructs an object with a `table`, its format information, and comments.
`table-like` types must meet the following criteria:
- Has `T::iterator`.
- Has `T::value_type`.
- Has `T::key_type`.
- Has `T::mapped_type`.
### Constructor (user-defined)
```cpp
template<typename T /* toml::into<T> is defined */>
basic_value(const T& ud);
template<typename T /* toml::into<T> is defined */>
basic_value(const T& ud, std::vector<std::string> com);
template<typename T /* toml::into<T> is not defined, but T{}.into_toml() exists */>
basic_value(const T& ud);
template<typename T /* toml::into<T> is not defined, but T{}.into_toml() exists */>
basic_value(const T& ud, std::vector<std::string> com);
```
If `toml::into<T>` is defined, constructs from the result of `toml::into<T>(ud)`.
If `toml::into<T>` is not defined but `T` has a `into_toml()` member function, constructs from the result of `ud.into_toml()`.
-----
### `operator=(basic_value)`
```cpp
basic_value& operator=(const basic_value& v)
basic_value& operator=(basic_value&& v)
template<typename TI>
basic_value& operator=(basic_value<TI> other)
```
Assigns the right-hand side `basic_value` to the current object.
### `operator=(T)`
```cpp
template<typename T>
basic_value& operator=(T x)
```
Assigns a value corresponding to `T`.
The contents pointed to by `source_location` are discarded.
If the object already holds a value of the same type, the original format information is retained.
-----
### `is<T>()`
```cpp
bool is<T>() const noexcept
```
#### Requirements
`T` must be an exact TOML type, meaning it corresponds to one of the `toml::value::xxx_type`.
#### Return Value
Returns `true` if the stored type matches `T`, otherwise returns `false`.
-----
### `is(toml::value_t)`
```cpp
bool is(toml::value_t t) const noexcept
```
#### Return Value
Returns `true` if the tag of the stored type matches `t`, otherwise returns `false`.
-----
### `is_xxx()`
```cpp
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;
```
#### Return Value
Returns `true` if the stored type matches the corresponding type, otherwise returns `false`.
------
### `is_empty()`
```cpp
bool is_empty() const noexcept;
```
#### Return Value
Returns `true` if the object is default constructed and no value is assigned, otherwise returns `false`.
### `is_array_of_tables()`
```cpp
bool is_array_of_tables() const noexcept;
```
#### Return Value
Returns `true` if the stored type is an array that is not empty and all elements are tables, otherwise returns `false`.
-----
### `type()`
```cpp
toml::value_t type() const noexcept
```
#### Return Value
Returns the tag corresponding to the stored type.
-----
### `as_xxx()`
```cpp
boolean_type const& as_boolean () const;
integer_type const& as_integer () const;
floating_type const& as_floating () const;
string_type const& as_string () const;
offset_datetime_type const& as_offset_datetime() const;
local_datetime_type const& as_local_datetime () const;
local_date_type const& as_local_date () const;
local_time_type const& as_local_time () const;
array_type const& as_array () const;
table_type const& as_table () const;
boolean_type & as_boolean ();
integer_type & as_integer ();
floating_type & as_floating ();
string_type & as_string ();
offset_datetime_type& as_offset_datetime();
local_datetime_type & as_local_datetime ();
local_date_type & as_local_date ();
local_time_type & as_local_time ();
array_type & as_array ();
table_type & as_table ();
```
#### Return Value
Returns a reference to the value of the specified type.
#### Exception
Throws `toml::type_error` if the stored value's type does not match the specified type.
-----
### `as_xxx(std::nothrow)`
Invoke with a `std::nothrow` object.
```cpp
boolean_type const& as_boolean (const std::nothrow_t&) const noexcept;
integer_type const& as_integer (const std::nothrow_t&) const noexcept;
floating_type const& as_floating (const std::nothrow_t&) const noexcept;
string_type const& as_string (const std::nothrow_t&) const noexcept;
offset_datetime_type const& as_offset_datetime(const std::nothrow_t&) const noexcept;
local_datetime_type const& as_local_datetime (const std::nothrow_t&) const noexcept;
local_date_type const& as_local_date (const std::nothrow_t&) const noexcept;
local_time_type const& as_local_time (const std::nothrow_t&) const noexcept;
array_type const& as_array (const std::nothrow_t&) const noexcept;
table_type const& as_table (const std::nothrow_t&) const noexcept;
boolean_type & as_boolean (const std::nothrow_t&) noexcept;
integer_type & as_integer (const std::nothrow_t&) noexcept;
floating_type & as_floating (const std::nothrow_t&) noexcept;
string_type & as_string (const std::nothrow_t&) noexcept;
offset_datetime_type& as_offset_datetime(const std::nothrow_t&) noexcept;
local_datetime_type & as_local_datetime (const std::nothrow_t&) noexcept;
local_date_type & as_local_date (const std::nothrow_t&) noexcept;
local_time_type & as_local_time (const std::nothrow_t&) noexcept;
array_type & as_array (const std::nothrow_t&) noexcept;
table_type & as_table (const std::nothrow_t&) noexcept;
```
#### Return Value
Returns a reference to the value of the specified type.
#### Note
If the type of the stored value does not match the specified type, the behavior is undefined.
-----
### `as_xxx_fmt()`
Accesses format information.
```cpp
boolean_format_info & as_boolean_fmt ();
integer_format_info & as_integer_fmt ();
floating_format_info & as_floating_fmt ();
string_format_info & as_string_fmt ();
offset_datetime_format_info& as_offset_datetime_fmt();
local_datetime_format_info & as_local_datetime_fmt ();
local_date_format_info & as_local_date_fmt ();
local_time_format_info & as_local_time_fmt ();
array_format_info & as_array_fmt ();
table_format_info & as_table_fmt ();
boolean_format_info const& as_boolean_fmt () const;
integer_format_info const& as_integer_fmt () const;
floating_format_info const& as_floating_fmt () const;
string_format_info const& as_string_fmt () const;
offset_datetime_format_info const& as_offset_datetime_fmt() const;
local_datetime_format_info const& as_local_datetime_fmt () const;
local_date_format_info const& as_local_date_fmt () const;
local_time_format_info const& as_local_time_fmt () const;
array_format_info const& as_array_fmt () const;
table_format_info const& as_table_fmt () const;
```
#### Return Value
Returns a reference to the structure holding the format information for the specified type.
#### Exception
Throws `toml::type_error` if the stored value's type does not match the specified type.
-----
### `as_xxx_fmt(std::nothrow)`
Invoke with a `std::nothrow` object.
```cpp
boolean_format_info & as_boolean_fmt (const std::nothrow_t&) noexcept;
integer_format_info & as_integer_fmt (const std::nothrow_t&) noexcept;
floating_format_info & as_floating_fmt (const std::nothrow_t&) noexcept;
string_format_info & as_string_fmt (const std::nothrow_t&) noexcept;
offset_datetime_format_info& as_offset_datetime_fmt(const std::nothrow_t&) noexcept;
local_datetime_format_info & as_local_datetime_fmt (const std::nothrow_t&) noexcept;
local_date_format_info & as_local_date_fmt (const std::nothrow_t&) noexcept;
local_time_format_info & as_local_time_fmt (const std::nothrow_t&) noexcept;
array_format_info & as_array_fmt (const std::nothrow_t&) noexcept;
table_format_info & as_table_fmt (const std::nothrow_t&) noexcept;
boolean_format_info const& as_boolean_fmt (const std::nothrow_t&) const noexcept;
integer_format_info const& as_integer_fmt (const std::nothrow_t&) const noexcept;
floating_format_info const& as_floating_fmt (const std::nothrow_t&) const noexcept;
string_format_info const& as_string_fmt (const std::nothrow_t&) const noexcept;
offset_datetime_format_info const& as_offset_datetime_fmt(const std::nothrow_t&) const noexcept;
local_datetime_format_info const& as_local_datetime_fmt (const std::nothrow_t&) const noexcept;
local_date_format_info const& as_local_date_fmt (const std::nothrow_t&) const noexcept;
local_time_format_info const& as_local_time_fmt (const std::nothrow_t&) const noexcept;
array_format_info const& as_array_fmt (const std::nothrow_t&) const noexcept;
table_format_info const& as_table_fmt (const std::nothrow_t&) const noexcept;
```
#### Return Value
Returns a reference to the structure holding the format information for the specified type.
#### Note
If the type of the stored value does not match the specified type, the behavior is undefined.
-----
### `at(key)`
```cpp
value_type& at(const key_type& key);
value_type const& at(const key_type& key) const;
```
#### Return Value
Casts the current `value` to a `table` and returns the element specified by `key`.
#### Exception
Throws `toml::type_error` if the stored value is not a `table`.
Throws `std::out_of_range` if the `table` does not contain the specified element.
-----
#### `operator[](key)`
```cpp
value_type& operator[](const key_type& k);
```
##### Return Value
Casts the current `value` to a `table` and returns a reference to the element specified by `key`.
If the element specified by `key` does not exist, it is default-constructed.
##### Exception
Throws `toml::type_error` if the stored value is not a `table`.
-----
### `count(key)`
```cpp
std::size_t count(const key_type& key) const;
```
#### Return Value
Casts the current `value` to a `table` and returns `1` if the element corresponding to `key` is present, otherwise returns `0`.
#### Exception
Throws `toml::type_error` if the stored value is not a `table`.
-----
### `contains(key)`
```cpp
bool contains(const key_type& key) const;
```
#### Return Value
Casts the current `value` to a `table` and returns `true` if the element corresponding to `key` is present, otherwise returns `false`.
#### Exception
Throws `toml::type_error` if the stored value is not a `table`.
-----
### `at(idx)`
```cpp
value_type& at(const std::size_t idx);
value_type const& at(const std::size_t idx) const;
```
#### Return Value
Casts the current `value` to an `array` and returns the element specified by `idx`.
#### Exception
Throws `toml::type_error` if the stored value is not an `array`.
Throws `std::out_of_range` if the specified element does not exist in the `array`.
-----
### `operator[](idx)`
```cpp
value_type& operator[](const std::size_t idx) noexcept;
value_type const& operator[](const std::size_t idx) const noexcept;
```
#### Return Value
Casts the current `value` to an `array` and returns a reference to the element specified by `idx`.
#### Note
Performs no checks. Behavior is undefined if the stored value is not an `array` or if the specified element does not exist.
-----
### `push_back(value)`
```cpp
void push_back(const value_type& x);
void push_back(value_type&& x);
```
Casts the current `value` to an `array` and performs `push_back` on the `array`.
#### Return Value
None.
#### Exception
Throws `toml::type_error` if the stored value is not an `array`.
-----
### `emplace_back(args...)`
```cpp
template<typename ... Ts>
value_type& emplace_back(Ts&& ... args)
```
Casts the current `value` to an `array` and performs `emplace_back` on the `array`.
#### Return Value
A reference to the constructed value.
#### Exception
Throws `toml::type_error` if the stored value is not an `array`.
-----
### `size()`
```cpp
std::size_t size() const;
```
#### Return Value
Casts the current `value` to an `array`, `string`, or `table` and returns the number of elements. For a `string`, it returns the number of characters.
#### Exception
Throws `toml::type_error` if the stored value is not an `array`, `string`, or `table`.
-----
### `location()`
```cpp
source_location location() const;
```
#### Return Value
Returns a `source_location` object representing the position within the TOML document where the `value` is defined.
If the `value` was not constructed by parsing a TOML document, returns a `source_location` that points to nowhere.
-----
### `comments()`
```cpp
comment_type const& comments() const noexcept;
comment_type& comments() noexcept;
```
#### Return Value
Returns a reference to the comment container.
## Non-Member Functions
### `operator==`
```cpp
template<typename TC>
bool operator==(const basic_value<TC>&, const basic_value<TC>&);
```
Two `basic_value<T>` instances are considered equal if they satisfy the following conditions:
- The contained type is the same.
- The contained values are identical.
- The comments are identical at the byte level.
### `operator!=`
```cpp
template<typename TC>
bool operator!=(const basic_value<TC>& lhs, const basic_value<TC>& rhs)
{
return !(lhs == rhs);
}
```
### `operator<`
Defined only if `array_type` and `table_type` have `operator<`.
```cpp
template<typename TC>
bool operator<(const basic_value<TC>&, const basic_value<TC>&);
```
Comparison order:
1. TOML type
2. If TOML types are the same, their values
3. If both the TOML types and values are the same, the comments
TOML types have the following order (from smallest to largest):
1. `toml::value_t::empty`
2. `toml::value_t::boolean`
3. `toml::value_t::integer`
4. `toml::value_t::floating`
5. `toml::value_t::string`
6. `toml::value_t::offset_datetime`
7. `toml::value_t::local_datetime`
8. `toml::value_t::local_date`
9. `toml::value_t::local_time`
10. `toml::value_t::array`
11. `toml::value_t::table`
### `operator<=`
Defined only if `array_type` and `table_type` have `operator<`.
```cpp
template<typename TC>
bool operator<=(const basic_value<TC>& lhs, const basic_value<TC>& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
```
### `operator>`
Defined only if `array_type` and `table_type` have `operator<`.
```cpp
template<typename TC>
bool operator>(const basic_value<TC>& lhs, const basic_value<TC>& rhs)
{
return !(lhs <= rhs);
}
```
### `operator>=`
Defined only if `array_type` and `table_type` have `operator<`.
```cpp
template<typename TC>
bool operator>=(const basic_value<TC>& lhs, const basic_value<TC>& rhs)
{
return !(lhs < rhs);
}
```
# `toml::type_error`
Exception thrown in case of a type error.
Contains the location information of the value that caused the type error.
```cpp
struct type_error final : public ::toml::exception
{
public:
type_error(std::string what_arg, source_location loc);
~type_error() noexcept override = default;
const char* what() const noexcept override;
source_location const& location() const noexcept;
};
```
# `toml::make_error_info`
```cpp
template<typename TC, typename ... Ts>
error_info make_error_info(
std::string title, const basic_value<TC>& v, std::string msg, Ts&& ... tail);
```
Calls `location()` on a `basic_value`, passes the resulting `source_location` to
[`make_error_info`]({{<ref "docs/reference/error_info#make_error_info">}})
to create an `error_info`.
Refer to [`error_info`]({{<ref "docs/reference/error_info">}}) for more details.
# `toml::format_error`
```cpp
template<typename TC, typename ... Ts>
std::string format_error(std::string title,
const basic_value<TC>& v, std::string msg, Ts&& ... tail);
```
Calls `location()` on a `basic_value`, passes the resulting `source_location` to
[`format_error`]({{<ref "docs/reference/error_info#format_error">}})
to create an `error_info`, then converts it to a string and returns it.
Refer to [`error_info`]({{<ref "docs/reference/error_info">}}) for more details.
# Related
- [comments.hpp]({{<ref "comments.md">}})
- [source_location.hpp]({{<ref "source_location.md">}})
- [types.hpp]({{<ref "types.md">}})
- [visit.hpp]({{<ref "visit.md">}})

View File

@@ -0,0 +1,51 @@
+++
title = "value_t.hpp"
type = "docs"
+++
# value_t.hpp
# `value_t`
`value_t` is used to handle the type information of `toml::value`.
```cpp
namespace toml
{
enum class value_t : std::uint8_t
{
empty = 0,
boolean = 1,
integer = 2,
floating = 3,
string = 4,
offset_datetime = 5,
local_datetime = 6,
local_date = 7,
local_time = 8,
array = 9,
table = 10
};
std::ostream& operator<<(std::ostream& os, value_t t);
std::string to_string(value_t t);
} // toml
```
## Non-member Functions
### Stream Operator
```cpp
std::ostream& operator<<(std::ostream& os, value_t t);
```
Outputs the string representation of the `value_t` to the stream.
### `to_string`
```cpp
std::string to_string(value_t t);
```
Returns the string representation of the `value_t`.

View File

@@ -0,0 +1,37 @@
+++
title = "version.hpp"
type = "docs"
+++
# version.hpp
In `version.hpp`, macros related to the version information of toml11 are defined.
## Macros
### `TOML11_VERSION_MAJOR`
The major version of toml11.
### `TOML11_VERSION_MINOR`
The minor version of toml11.
### `TOML11_VERSION_PATCH`
The patch version of toml11.
## Function
### `license_notice`
```cpp
namespace toml
{
const char* license_notice() noexcept;
}
```
Returns the license notice.
Provided for convenience when redistributing without source code.

View File

@@ -0,0 +1,69 @@
+++
title = "visit.hpp"
type = "docs"
+++
# visit.hpp
In `visit.hpp`, `toml::visit` is defined.
# `toml::visit`
## Functions
```cpp
namespace toml
{
template<typename Visitor, typename TC>
/* Return value when Visitor is called with a value of basic_value<TC> */
visit(Visitor&& visitor, const basic_value<TC>& v);
template<typename Visitor, typename TC>
/* Return value when Visitor is called with a value of basic_value<TC> */
visit(Visitor&& visitor, basic_value<TC>& v);
template<typename Visitor, typename TC>
/* Return value when Visitor is called with a value of basic_value<TC> */
visit(Visitor&& visitor, basic_value<TC>&& v);
}
```
`toml::visit` calls the overload of `Visitor` corresponding to the type held by `basic_value<TC>`, and returns the result.
#### Requirements
`Visitor` must be a function or function object callable with any type held by `basic_value<TC>`.
Additionally, the return value must be consistent across all overloads.
#### Example
```cpp
#include <toml.hpp>
#include <iostream>
struct type_name_of
{
std::string operator()(const toml::value::boolean_type &) const {return "boolean";}
std::string operator()(const toml::value::integer_type &) const {return "integer";}
std::string operator()(const toml::value::floating_type &) const {return "floating";}
std::string operator()(const toml::value::string_type &) const {return "string";}
std::string operator()(const toml::value::local_time_type &) const {return "local_time";}
std::string operator()(const toml::value::local_date_type &) const {return "local_date";}
std::string operator()(const toml::value::local_datetime_type &) const {return "local_datetime";}
std::string operator()(const toml::value::offset_datetime_type&) const {return "offset_datetime";}
std::string operator()(const toml::value::array_type &) const {return "array";}
std::string operator()(const toml::value::table_type &) const {return "table";}
};
int main()
{
toml::value v(3.14);
std::cout << toml::visit(type_name_of{}, v) << std::endl; // floating
return 0;
}
```
# Related
- [value.hpp]({{<ref "value.md">}})