mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-12-16 03:08:52 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0ae1a6bfd | ||
|
|
044a66210d | ||
|
|
c4fb41b812 | ||
|
|
a610e75df0 | ||
|
|
0e84d0591b |
@@ -92,6 +92,32 @@ std::cout << v.at(1);
|
|||||||
|
|
||||||
If the stored type is not `array_type`, a `type_error` is thrown.
|
If the stored type is not `array_type`, a `type_error` is thrown.
|
||||||
|
|
||||||
|
### `try_at(std::size_t i)`
|
||||||
|
|
||||||
|
Performs the same operation as `at(i)`, but instead of throwing an exception on failure, it always returns a [`toml::result<T&, error_info>`]({{<ref "docs/reference/result">}}). It does not throw an exception on failure.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::array{1,2,3});
|
||||||
|
|
||||||
|
auto res1 = v.try_at(1);
|
||||||
|
assert(res1.is_ok());
|
||||||
|
std::cout << res1.unwrap() << std::endl;
|
||||||
|
|
||||||
|
auto res5 = v.try_at(5);
|
||||||
|
assert(res1.is_err());
|
||||||
|
std::cout << toml::format_error(res1.unwrap_err()) << std::endl;
|
||||||
|
```
|
||||||
|
|
||||||
|
Additionally, since this `toml::result` holds a reference, it is possible to update the value.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::array{1,2,3});
|
||||||
|
|
||||||
|
auto res1 = v.try_at(1);
|
||||||
|
assert(res1.is_ok());
|
||||||
|
res1.unwrap() = 42;
|
||||||
|
```
|
||||||
|
|
||||||
#### `at(std::string key)`, `operator[](std::string key)`
|
#### `at(std::string key)`, `operator[](std::string key)`
|
||||||
|
|
||||||
These are equivalent to `as_table().at(key)` and `as_table()[key]`.
|
These are equivalent to `as_table().at(key)` and `as_table()[key]`.
|
||||||
@@ -105,6 +131,32 @@ v["a"] = 42;
|
|||||||
|
|
||||||
If the stored type is not `table_type`, a `type_error` is thrown.
|
If the stored type is not `table_type`, a `type_error` is thrown.
|
||||||
|
|
||||||
|
### `try_at(std::string key)`
|
||||||
|
|
||||||
|
Performs the same operation as `at(key)`, but instead of throwing an exception on failure, it always returns a [`toml::result<T&, error_info>`]({{<ref "docs/reference/result">}}). It does not throw an exception on failure.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::table{ {"a", 42}, {"b", "foo"} });
|
||||||
|
|
||||||
|
auto res_a = v.try_at("a");
|
||||||
|
assert(res_a.is_ok());
|
||||||
|
std::cout << res_a.unwrap() << std::endl;
|
||||||
|
|
||||||
|
auto res_c = v.try_at("c");
|
||||||
|
assert(res_c.is_err());
|
||||||
|
std::cout << toml::format_error(res_c.unwrap_err()) << std::endl;
|
||||||
|
```
|
||||||
|
|
||||||
|
Additionally, since this `toml::result` holds a reference, it is possible to update the value.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::table{ {"a", 42}, {"b", "foo"} });
|
||||||
|
|
||||||
|
auto res_a = v.try_at("a");
|
||||||
|
assert(res_a.is_ok());
|
||||||
|
res_a.unwrap() = 6 * 9;
|
||||||
|
```
|
||||||
|
|
||||||
#### `size()`
|
#### `size()`
|
||||||
|
|
||||||
Returns the length.
|
Returns the length.
|
||||||
|
|||||||
@@ -585,6 +585,27 @@ Throws `std::out_of_range` if the `table` does not contain the specified element
|
|||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
### `try_at(key)`
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
result<std::reference_wrapper<value_type>, error_info> try_at(const key_type& key) noexcept;
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info> try_at(const key_type& key) const noexcept;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
After casting the current `value` to a `table`, it returns the element specified by the `key`.
|
||||||
|
|
||||||
|
If successful, it returns a `reference_wrapper` holding a reference to that element.
|
||||||
|
|
||||||
|
If unsuccessful, it returns an `error_info` corresponding to `type_error` or `out_of_range`.
|
||||||
|
|
||||||
|
#### Exceptions
|
||||||
|
|
||||||
|
Does not throw.
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
#### `operator[](key)`
|
#### `operator[](key)`
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -652,6 +673,26 @@ 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`.
|
Throws `std::out_of_range` if the specified element does not exist in the `array`.
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
### `try_at(idx)`
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
result<std::reference_wrapper<value_type>, error_info> try_at(const std::size_t idx) noexcept;
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info> try_at(const std::size_t idx) const noexcept;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
After casting the current `value` to an `array`, it returns the element specified by the `idx`.
|
||||||
|
|
||||||
|
If successful, it returns a `reference_wrapper` holding a reference to that element.
|
||||||
|
|
||||||
|
If unsuccessful, it returns an `error_info` corresponding to `type_error` or `out_of_range`.
|
||||||
|
|
||||||
|
#### Exceptions
|
||||||
|
|
||||||
|
Does not throw.
|
||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,32 @@ std::cout << v.at(1);
|
|||||||
|
|
||||||
格納している型が `array_type` ではなかった場合、 `type_error` を送出します。
|
格納している型が `array_type` ではなかった場合、 `type_error` を送出します。
|
||||||
|
|
||||||
|
#### `try_at(std::size_t i)`
|
||||||
|
|
||||||
|
`at(i)`と同様の操作をしますが、失敗時に例外を投げる代わりに、常に[`toml::result<T&, error_info>`]({{<ref "docs/reference/result">}})を返します。失敗時に例外は投げません。
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::array{1,2,3});
|
||||||
|
|
||||||
|
auto res1 = v.try_at(1);
|
||||||
|
assert(res1.is_ok());
|
||||||
|
std::cout << res1.unwrap() << std::endl;
|
||||||
|
|
||||||
|
auto res5 = v.try_at(5);
|
||||||
|
assert(res1.is_err());
|
||||||
|
std::cout << toml::format_error(res1.unwrap_err()) << std::endl;
|
||||||
|
```
|
||||||
|
|
||||||
|
また、この`toml::result`は成功値として参照を持つので、値を更新することも可能です。
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::array{1,2,3});
|
||||||
|
|
||||||
|
auto res1 = v.try_at(1);
|
||||||
|
assert(res1.is_ok());
|
||||||
|
res1.unwrap() = 42;
|
||||||
|
```
|
||||||
|
|
||||||
#### `at(std::string key)`, `operator[](std::string key)`
|
#### `at(std::string key)`, `operator[](std::string key)`
|
||||||
|
|
||||||
`as_table().at(key)`, `as_table()[key]` と同等です。
|
`as_table().at(key)`, `as_table()[key]` と同等です。
|
||||||
@@ -111,6 +137,32 @@ v["a"] = 42;
|
|||||||
|
|
||||||
格納している型が `table_type` ではなかった場合、 `type_error` を送出します。
|
格納している型が `table_type` ではなかった場合、 `type_error` を送出します。
|
||||||
|
|
||||||
|
#### `try_at(std::string key)`
|
||||||
|
|
||||||
|
`at(key)`と同様の操作をしますが、失敗時に例外を投げる代わりに、常に[`toml::result<T&, error_info>`]({{<ref "docs/reference/result">}})を返します。失敗時に例外は投げません。
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::table{ {"a", 42}, {"b", "foo"} });
|
||||||
|
|
||||||
|
auto res_a = v.try_at("a");
|
||||||
|
assert(res_a.is_ok());
|
||||||
|
std::cout << res_a.unwrap() << std::endl;
|
||||||
|
|
||||||
|
auto res_c = v.try_at("c");
|
||||||
|
assert(res_c.is_err());
|
||||||
|
std::cout << toml::format_error(res_c.unwrap_err()) << std::endl;
|
||||||
|
```
|
||||||
|
|
||||||
|
また、この`toml::result`は成功値として参照を持つので、値を更新することも可能です。
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
toml::value v(toml::table{ {"a", 42}, {"b", "foo"} });
|
||||||
|
|
||||||
|
auto res_a = v.try_at("a");
|
||||||
|
assert(res_a.is_ok());
|
||||||
|
res_a.unwrap() = 6 * 9;
|
||||||
|
```
|
||||||
|
|
||||||
#### `size()`
|
#### `size()`
|
||||||
|
|
||||||
長さを返します。
|
長さを返します。
|
||||||
|
|||||||
@@ -583,6 +583,27 @@ value_type const& at(const key_type& key) const;
|
|||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
### `try_at(key)`
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
result<std::reference_wrapper<value_type>, error_info> try_at(const key_type& key) noexcept;
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info> try_at(const key_type& key) const noexcept;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 戻り値
|
||||||
|
|
||||||
|
今の`value`を`table`にキャストしたあと、`key`によって指定される要素を返します。
|
||||||
|
|
||||||
|
成功した場合、その要素への参照を持つ`reference_wrapper`を返します。
|
||||||
|
|
||||||
|
失敗した場合、 `type_error` または `out_of_range` に対応する `error_info` を返します。
|
||||||
|
|
||||||
|
#### 例外
|
||||||
|
|
||||||
|
投げません。
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
#### `operator[](key)`
|
#### `operator[](key)`
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -652,6 +673,27 @@ value_type const& at(const std::size_t idx) const;
|
|||||||
|
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
### `try_at(idx)`
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
result<std::reference_wrapper<value_type>, error_info> try_at(const std::size_t idx) noexcept;
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info> try_at(const std::size_t idx) const noexcept;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 戻り値
|
||||||
|
|
||||||
|
今の`value`を`array`にキャストしたあと、`idx`によって指定される要素を返します。
|
||||||
|
|
||||||
|
成功した場合、その要素への参照を持つ`reference_wrapper`を返します。
|
||||||
|
|
||||||
|
失敗した場合、 `type_error` または `out_of_range` に対応する `error_info` を返します。
|
||||||
|
|
||||||
|
#### 例外
|
||||||
|
|
||||||
|
投げません。
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
### `operator[](idx)`
|
### `operator[](idx)`
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|||||||
@@ -244,13 +244,13 @@ struct result
|
|||||||
{
|
{
|
||||||
if(other.is_ok())
|
if(other.is_ok())
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->succ_)) success_type(other.as_ok());
|
auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_);
|
||||||
assert(tmp == std::addressof(this->succ_));
|
assert(tmp == std::addressof(this->succ_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.as_err());
|
auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_);
|
||||||
assert(tmp == std::addressof(this->fail_));
|
assert(tmp == std::addressof(this->fail_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
@@ -259,13 +259,13 @@ struct result
|
|||||||
{
|
{
|
||||||
if(other.is_ok())
|
if(other.is_ok())
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok()));
|
auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_));
|
||||||
assert(tmp == std::addressof(this->succ_));
|
assert(tmp == std::addressof(this->succ_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err()));
|
auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_));
|
||||||
assert(tmp == std::addressof(this->fail_));
|
assert(tmp == std::addressof(this->fail_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
@@ -276,13 +276,13 @@ struct result
|
|||||||
this->cleanup();
|
this->cleanup();
|
||||||
if(other.is_ok())
|
if(other.is_ok())
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->succ_)) success_type(other.as_ok());
|
auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_);
|
||||||
assert(tmp == std::addressof(this->succ_));
|
assert(tmp == std::addressof(this->succ_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.as_err());
|
auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_);
|
||||||
assert(tmp == std::addressof(this->fail_));
|
assert(tmp == std::addressof(this->fail_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
@@ -294,13 +294,13 @@ struct result
|
|||||||
this->cleanup();
|
this->cleanup();
|
||||||
if(other.is_ok())
|
if(other.is_ok())
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok()));
|
auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_));
|
||||||
assert(tmp == std::addressof(this->succ_));
|
assert(tmp == std::addressof(this->succ_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err()));
|
auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_));
|
||||||
assert(tmp == std::addressof(this->fail_));
|
assert(tmp == std::addressof(this->fail_));
|
||||||
(void)tmp;
|
(void)tmp;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ error_info make_type_error(const basic_value<TC>&, const std::string&, const val
|
|||||||
template<typename TC>
|
template<typename TC>
|
||||||
error_info make_not_found_error(const basic_value<TC>&, const std::string&, const std::string&);
|
error_info make_not_found_error(const basic_value<TC>&, const std::string&, const std::string&);
|
||||||
|
|
||||||
|
template<typename TC>
|
||||||
|
error_info make_not_found_error(const basic_value<TC>&, const std::string&, const std::size_t);
|
||||||
|
|
||||||
template<typename TC>
|
template<typename TC>
|
||||||
void change_region_of_value(basic_value<TC>&, const basic_value<TC>&);
|
void change_region_of_value(basic_value<TC>&, const basic_value<TC>&);
|
||||||
|
|
||||||
@@ -1613,6 +1616,44 @@ class basic_value
|
|||||||
assert(found->first == k);
|
assert(found->first == k);
|
||||||
return found->second;
|
return found->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result<std::reference_wrapper<value_type>, error_info>
|
||||||
|
try_at(const key_type& k) noexcept
|
||||||
|
{
|
||||||
|
if(!this->is_table())
|
||||||
|
{
|
||||||
|
return err(detail::make_type_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", value_t::table));
|
||||||
|
}
|
||||||
|
auto& table = this->as_table(std::nothrow);
|
||||||
|
const auto found = table.find(k);
|
||||||
|
if(found == table.end())
|
||||||
|
{
|
||||||
|
return err(detail::make_not_found_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", k));
|
||||||
|
}
|
||||||
|
assert(found->first == k);
|
||||||
|
return ok(std::ref(found->second));
|
||||||
|
}
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info>
|
||||||
|
try_at(const key_type& k) const noexcept
|
||||||
|
{
|
||||||
|
if(!this->is_table())
|
||||||
|
{
|
||||||
|
return err(detail::make_type_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", value_t::table));
|
||||||
|
}
|
||||||
|
const auto& table = this->as_table(std::nothrow);
|
||||||
|
const auto found = table.find(k);
|
||||||
|
if(found == table.end())
|
||||||
|
{
|
||||||
|
return err(detail::make_not_found_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", k));
|
||||||
|
}
|
||||||
|
assert(found->first == k);
|
||||||
|
return ok(std::cref(found->second));
|
||||||
|
}
|
||||||
|
|
||||||
value_type& operator[](const key_type& k)
|
value_type& operator[](const key_type& k)
|
||||||
{
|
{
|
||||||
if(this->is_empty())
|
if(this->is_empty())
|
||||||
@@ -1688,6 +1729,41 @@ class basic_value
|
|||||||
return ar.at(idx);
|
return ar.at(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result<std::reference_wrapper<value_type>, error_info>
|
||||||
|
try_at(const std::size_t& idx) noexcept
|
||||||
|
{
|
||||||
|
if(!this->is_array())
|
||||||
|
{
|
||||||
|
return err(detail::make_type_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", value_t::array));
|
||||||
|
}
|
||||||
|
auto& ar = this->as_array(std::nothrow);
|
||||||
|
|
||||||
|
if(ar.size() <= idx)
|
||||||
|
{
|
||||||
|
return err(detail::make_not_found_error(*this,
|
||||||
|
"toml::value::try_at(idx)", idx));
|
||||||
|
}
|
||||||
|
return ok(std::ref(ar[idx]));
|
||||||
|
}
|
||||||
|
result<std::reference_wrapper<const value_type>, error_info>
|
||||||
|
try_at(const std::size_t idx) const noexcept
|
||||||
|
{
|
||||||
|
if(!this->is_array())
|
||||||
|
{
|
||||||
|
return err(detail::make_type_error(*this,
|
||||||
|
"toml::value::try_at(key_type)", value_t::array));
|
||||||
|
}
|
||||||
|
const auto& ar = this->as_array(std::nothrow);
|
||||||
|
|
||||||
|
if(ar.size() <= idx)
|
||||||
|
{
|
||||||
|
return err(detail::make_not_found_error(*this,
|
||||||
|
"toml::value::try_at(idx)", idx));
|
||||||
|
}
|
||||||
|
return ok(std::cref(ar[idx]));
|
||||||
|
}
|
||||||
|
|
||||||
value_type& operator[](const std::size_t idx) noexcept
|
value_type& operator[](const std::size_t idx) noexcept
|
||||||
{
|
{
|
||||||
// no check...
|
// no check...
|
||||||
@@ -2092,6 +2168,19 @@ error_info make_not_found_error(const basic_value<TC>& v, const std::string& fna
|
|||||||
}
|
}
|
||||||
return error_info(title, locs);
|
return error_info(title, locs);
|
||||||
}
|
}
|
||||||
|
template<typename TC>
|
||||||
|
error_info make_not_found_error(const basic_value<TC>& v, const std::string& fname, const std::size_t idx)
|
||||||
|
{
|
||||||
|
if( ! v.is_array())
|
||||||
|
{
|
||||||
|
return make_type_error(v, fname, toml::value_t::array);
|
||||||
|
}
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "actual length (" << v.as_array(std::nothrow).size()
|
||||||
|
<< ") is shorter than the specified index (" << idx << ").";
|
||||||
|
return make_error_info(fname + ": no element corresponding to the index",
|
||||||
|
v, oss.str());
|
||||||
|
}
|
||||||
|
|
||||||
#define TOML11_DETAIL_GENERATE_COMPTIME_GETTER(ty) \
|
#define TOML11_DETAIL_GENERATE_COMPTIME_GETTER(ty) \
|
||||||
template<typename TC> \
|
template<typename TC> \
|
||||||
|
|||||||
@@ -1062,21 +1062,89 @@ TEST_CASE("testing array-accessors")
|
|||||||
{
|
{
|
||||||
toml::value x({true, 42, "hoge"});
|
toml::value x({true, 42, "hoge"});
|
||||||
|
|
||||||
|
// at
|
||||||
|
|
||||||
CHECK_UNARY(x.at(0).is_boolean());
|
CHECK_UNARY(x.at(0).is_boolean());
|
||||||
CHECK_UNARY(x.at(1).is_integer());
|
CHECK_UNARY(x.at(1).is_integer());
|
||||||
CHECK_UNARY(x.at(2).is_string());
|
CHECK_UNARY(x.at(2).is_string());
|
||||||
|
CHECK_EQ(x.at(0).as_boolean(), true);
|
||||||
|
CHECK_EQ(x.at(1).as_integer(), 42);
|
||||||
|
CHECK_EQ(x.at(2).as_string(), std::string("hoge"));
|
||||||
|
CHECK_THROWS_AS(x.at(3), std::out_of_range);
|
||||||
|
|
||||||
CHECK_UNARY(as_const(x).at(0).is_boolean());
|
CHECK_UNARY(as_const(x).at(0).is_boolean());
|
||||||
CHECK_UNARY(as_const(x).at(1).is_integer());
|
CHECK_UNARY(as_const(x).at(1).is_integer());
|
||||||
CHECK_UNARY(as_const(x).at(2).is_string());
|
CHECK_UNARY(as_const(x).at(2).is_string());
|
||||||
|
|
||||||
CHECK_EQ(x.at(0).as_boolean(), true);
|
|
||||||
CHECK_EQ(x.at(1).as_integer(), 42);
|
|
||||||
CHECK_EQ(x.at(2).as_string(), std::string("hoge"));
|
|
||||||
|
|
||||||
CHECK_EQ(as_const(x).at(0).as_boolean(), true);
|
CHECK_EQ(as_const(x).at(0).as_boolean(), true);
|
||||||
CHECK_EQ(as_const(x).at(1).as_integer(), 42);
|
CHECK_EQ(as_const(x).at(1).as_integer(), 42);
|
||||||
CHECK_EQ(as_const(x).at(2).as_string(), std::string("hoge"));
|
CHECK_EQ(as_const(x).at(2).as_string(), std::string("hoge"));
|
||||||
|
CHECK_THROWS_AS(as_const(x).at(3), std::out_of_range);
|
||||||
|
|
||||||
|
// update through at
|
||||||
|
|
||||||
|
x.at(0) = false;
|
||||||
|
x.at(1) = 6 * 9;
|
||||||
|
x.at(2) = 3.14;
|
||||||
|
|
||||||
|
CHECK_UNARY(x.at(0).is_boolean());
|
||||||
|
CHECK_UNARY(x.at(1).is_integer());
|
||||||
|
CHECK_UNARY(x.at(2).is_floating());
|
||||||
|
CHECK_EQ(x.at(0).as_boolean(), false);
|
||||||
|
CHECK_EQ(x.at(1).as_integer(), 6*9);
|
||||||
|
CHECK_EQ(x.at(2).as_floating(), 3.14);
|
||||||
|
|
||||||
|
x.at(0) = true;
|
||||||
|
x.at(1) = 42;
|
||||||
|
x.at(2) = "hoge";
|
||||||
|
|
||||||
|
// try_at
|
||||||
|
|
||||||
|
CHECK_UNARY(x.try_at(0).is_ok());
|
||||||
|
CHECK_UNARY(x.try_at(1).is_ok());
|
||||||
|
CHECK_UNARY(x.try_at(2).is_ok());
|
||||||
|
CHECK_UNARY(x.try_at(0).as_ok().is_boolean());
|
||||||
|
CHECK_UNARY(x.try_at(1).as_ok().is_integer());
|
||||||
|
CHECK_UNARY(x.try_at(2).as_ok().is_string());
|
||||||
|
CHECK_UNARY_FALSE(x.try_at(0).is_err());
|
||||||
|
CHECK_UNARY_FALSE(x.try_at(1).is_err());
|
||||||
|
CHECK_UNARY_FALSE(x.try_at(2).is_err());
|
||||||
|
CHECK_EQ(x.try_at(0).as_ok().as_boolean(), true);
|
||||||
|
CHECK_EQ(x.try_at(1).as_ok().as_integer(), 42);
|
||||||
|
CHECK_EQ(x.try_at(2).as_ok().as_string(), std::string("hoge"));
|
||||||
|
CHECK_UNARY(x.try_at(3).is_err());
|
||||||
|
|
||||||
|
CHECK_UNARY(as_const(x).try_at(0).is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at(1).is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at(2).is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at(0).as_ok().is_boolean());
|
||||||
|
CHECK_UNARY(as_const(x).try_at(1).as_ok().is_integer());
|
||||||
|
CHECK_UNARY(as_const(x).try_at(2).as_ok().is_string());
|
||||||
|
CHECK_UNARY_FALSE(as_const(x).try_at(0).is_err());
|
||||||
|
CHECK_UNARY_FALSE(as_const(x).try_at(1).is_err());
|
||||||
|
CHECK_UNARY_FALSE(as_const(x).try_at(2).is_err());
|
||||||
|
CHECK_EQ(as_const(x).try_at(0).as_ok().as_boolean(), true);
|
||||||
|
CHECK_EQ(as_const(x).try_at(1).as_ok().as_integer(), 42);
|
||||||
|
CHECK_EQ(as_const(x).try_at(2).as_ok().as_string(), std::string("hoge"));
|
||||||
|
CHECK_UNARY(as_const(x).try_at(3).is_err());
|
||||||
|
|
||||||
|
// update through try_at
|
||||||
|
|
||||||
|
x.try_at(0).as_ok() = false;
|
||||||
|
x.try_at(1).as_ok() = 6 * 9;
|
||||||
|
x.try_at(2).as_ok() = 3.14;
|
||||||
|
|
||||||
|
CHECK_UNARY(x.at(0).is_boolean());
|
||||||
|
CHECK_UNARY(x.at(1).is_integer());
|
||||||
|
CHECK_UNARY(x.at(2).is_floating());
|
||||||
|
CHECK_EQ(x.at(0).as_boolean(), false);
|
||||||
|
CHECK_EQ(x.at(1).as_integer(), 6*9);
|
||||||
|
CHECK_EQ(x.at(2).as_floating(), 3.14);
|
||||||
|
|
||||||
|
x.try_at(0).as_ok() = true;
|
||||||
|
x.try_at(1).as_ok() = 42;
|
||||||
|
x.try_at(2).as_ok() = "hoge";
|
||||||
|
|
||||||
|
// operator[]
|
||||||
|
|
||||||
CHECK_UNARY(x[0].is_boolean());
|
CHECK_UNARY(x[0].is_boolean());
|
||||||
CHECK_UNARY(x[1].is_integer());
|
CHECK_UNARY(x[1].is_integer());
|
||||||
@@ -1086,6 +1154,8 @@ TEST_CASE("testing array-accessors")
|
|||||||
CHECK_EQ(x[1].as_integer(), 42);
|
CHECK_EQ(x[1].as_integer(), 42);
|
||||||
CHECK_EQ(x[2].as_string(), std::string("hoge"));
|
CHECK_EQ(x[2].as_string(), std::string("hoge"));
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
const toml::value v1(3.14);
|
const toml::value v1(3.14);
|
||||||
toml::value v2(2.71);
|
toml::value v2(2.71);
|
||||||
|
|
||||||
@@ -1143,13 +1213,83 @@ TEST_CASE("testing table-accessors")
|
|||||||
CHECK_EQ(x.count("d"), 0);
|
CHECK_EQ(x.count("d"), 0);
|
||||||
CHECK_EQ(x.count("e"), 0);
|
CHECK_EQ(x.count("e"), 0);
|
||||||
|
|
||||||
|
// at
|
||||||
|
|
||||||
CHECK_UNARY(x.at("a").is_boolean());
|
CHECK_UNARY(x.at("a").is_boolean());
|
||||||
CHECK_UNARY(x.at("b").is_integer());
|
CHECK_UNARY(x.at("b").is_integer());
|
||||||
CHECK_UNARY(x.at("c").is_string());
|
CHECK_UNARY(x.at("c").is_string());
|
||||||
|
|
||||||
CHECK_EQ(x.at("a").as_boolean(), true);
|
CHECK_EQ(x.at("a").as_boolean(), true);
|
||||||
CHECK_EQ(x.at("b").as_integer(), 42);
|
CHECK_EQ(x.at("b").as_integer(), 42);
|
||||||
CHECK_EQ(x.at("c").as_string(), std::string("hoge"));
|
CHECK_EQ(x.at("c").as_string(), std::string("hoge"));
|
||||||
|
CHECK_THROWS_AS(x.at("d"), std::out_of_range);
|
||||||
|
|
||||||
|
CHECK_UNARY(as_const(x).at("a").is_boolean());
|
||||||
|
CHECK_UNARY(as_const(x).at("b").is_integer());
|
||||||
|
CHECK_UNARY(as_const(x).at("c").is_string());
|
||||||
|
CHECK_EQ(as_const(x).at("a").as_boolean(), true);
|
||||||
|
CHECK_EQ(as_const(x).at("b").as_integer(), 42);
|
||||||
|
CHECK_EQ(as_const(x).at("c").as_string(), std::string("hoge"));
|
||||||
|
CHECK_THROWS_AS(as_const(x).at("d"), std::out_of_range);
|
||||||
|
|
||||||
|
// rewrite using at
|
||||||
|
|
||||||
|
x.at("a") = false;
|
||||||
|
x.at("b") = 6*9;
|
||||||
|
x.at("c") = 3.14;
|
||||||
|
|
||||||
|
CHECK_UNARY(x.at("a").is_boolean());
|
||||||
|
CHECK_UNARY(x.at("b").is_integer());
|
||||||
|
CHECK_UNARY(x.at("c").is_floating());
|
||||||
|
CHECK_EQ(x.at("a").as_boolean(), false);
|
||||||
|
CHECK_EQ(x.at("b").as_integer(), 6*9);
|
||||||
|
CHECK_EQ(x.at("c").as_floating(), 3.14);
|
||||||
|
|
||||||
|
x.at("a") = true;
|
||||||
|
x.at("b") = 42;
|
||||||
|
x.at("c") = "hoge";
|
||||||
|
|
||||||
|
// try_at
|
||||||
|
|
||||||
|
CHECK_UNARY(x.try_at("a").is_ok());
|
||||||
|
CHECK_UNARY(x.try_at("b").is_ok());
|
||||||
|
CHECK_UNARY(x.try_at("c").is_ok());
|
||||||
|
CHECK_UNARY(x.try_at("a").as_ok().is_boolean());
|
||||||
|
CHECK_UNARY(x.try_at("b").as_ok().is_integer());
|
||||||
|
CHECK_UNARY(x.try_at("c").as_ok().is_string());
|
||||||
|
CHECK_EQ(x.try_at("a").as_ok().as_boolean(), true);
|
||||||
|
CHECK_EQ(x.try_at("b").as_ok().as_integer(), 42);
|
||||||
|
CHECK_EQ(x.try_at("c").as_ok().as_string(), std::string("hoge"));
|
||||||
|
CHECK_UNARY(x.try_at("d").is_err());
|
||||||
|
|
||||||
|
CHECK_UNARY(as_const(x).try_at("a").is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at("b").is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at("c").is_ok());
|
||||||
|
CHECK_UNARY(as_const(x).try_at("a").as_ok().is_boolean());
|
||||||
|
CHECK_UNARY(as_const(x).try_at("b").as_ok().is_integer());
|
||||||
|
CHECK_UNARY(as_const(x).try_at("c").as_ok().is_string());
|
||||||
|
CHECK_EQ(as_const(x).try_at("a").as_ok().as_boolean(), true);
|
||||||
|
CHECK_EQ(as_const(x).try_at("b").as_ok().as_integer(), 42);
|
||||||
|
CHECK_EQ(as_const(x).try_at("c").as_ok().as_string(), std::string("hoge"));
|
||||||
|
CHECK_UNARY(as_const(x).try_at("d").is_err());
|
||||||
|
|
||||||
|
// rewrite using try_at
|
||||||
|
|
||||||
|
x.try_at("a").as_ok() = false;
|
||||||
|
x.try_at("b").as_ok() = 6*9;
|
||||||
|
x.try_at("c").as_ok() = 3.14;
|
||||||
|
|
||||||
|
CHECK_UNARY(x.at("a").is_boolean());
|
||||||
|
CHECK_UNARY(x.at("b").is_integer());
|
||||||
|
CHECK_UNARY(x.at("c").is_floating());
|
||||||
|
CHECK_EQ(x.at("a").as_boolean(), false);
|
||||||
|
CHECK_EQ(x.at("b").as_integer(), 6*9);
|
||||||
|
CHECK_EQ(x.at("c").as_floating(), 3.14);
|
||||||
|
|
||||||
|
x.try_at("a").as_ok() = true;
|
||||||
|
x.try_at("b").as_ok() = 42;
|
||||||
|
x.try_at("c").as_ok() = "hoge";
|
||||||
|
|
||||||
|
// operator[]
|
||||||
|
|
||||||
CHECK_UNARY(x["a"].is_boolean());
|
CHECK_UNARY(x["a"].is_boolean());
|
||||||
CHECK_UNARY(x["b"].is_integer());
|
CHECK_UNARY(x["b"].is_integer());
|
||||||
|
|||||||
Reference in New Issue
Block a user