Make iterators real iterators (#77)
They were missing the iterator_category and thus could not be used with some standard algorithms.
This commit is contained in:
parent
ea064da16d
commit
05f8b1f1cc
@ -38,6 +38,7 @@ TOML_IMPL_NAMESPACE_START
|
|||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
using pointer = value_type*;
|
using pointer = value_type*;
|
||||||
using difference_type = ptrdiff_t;
|
using difference_type = ptrdiff_t;
|
||||||
|
using iterator_category = typename std::iterator_traits<raw_iterator>::iterator_category;
|
||||||
|
|
||||||
array_iterator() noexcept = default;
|
array_iterator() noexcept = default;
|
||||||
array_iterator(const array_iterator&) noexcept = default;
|
array_iterator(const array_iterator&) noexcept = default;
|
||||||
|
@ -16,6 +16,7 @@ TOML_DISABLE_WARNINGS
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cfloat>
|
#include <cfloat>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <iterator>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
@ -75,6 +75,8 @@ TOML_IMPL_NAMESPACE_START
|
|||||||
using value_type = table_proxy_pair<IsConst>;
|
using value_type = table_proxy_pair<IsConst>;
|
||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
using pointer = value_type*;
|
using pointer = value_type*;
|
||||||
|
using difference_type = typename std::iterator_traits<raw_iterator>::difference_type;
|
||||||
|
using iterator_category = typename std::iterator_traits<raw_iterator>::iterator_category;
|
||||||
|
|
||||||
table_iterator& operator++() noexcept // ++pre
|
table_iterator& operator++() noexcept // ++pre
|
||||||
{
|
{
|
||||||
|
@ -21,6 +21,7 @@ test_sources = [
|
|||||||
'manipulating_values.cpp',
|
'manipulating_values.cpp',
|
||||||
'unicode.cpp',
|
'unicode.cpp',
|
||||||
'user_feedback.cpp',
|
'user_feedback.cpp',
|
||||||
|
'using_iterators.cpp',
|
||||||
'windows_compat.cpp'
|
'windows_compat.cpp'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
55
tests/using_iterators.cpp
Normal file
55
tests/using_iterators.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#include "tests.h"
|
||||||
|
|
||||||
|
TOML_DISABLE_WARNINGS
|
||||||
|
#include <algorithm>
|
||||||
|
TOML_ENABLE_WARNINGS
|
||||||
|
|
||||||
|
TEST_CASE("Using std::distance, std::count_if, etc. with Iterators")
|
||||||
|
{
|
||||||
|
constexpr auto data = R"(array=[1,"Foo",true]
|
||||||
|
string="Bar"
|
||||||
|
number=5)"sv;
|
||||||
|
parsing_should_succeed(FILE_LINE_ARGS, data, [](auto&& table)
|
||||||
|
{
|
||||||
|
const auto table_begin = table.begin();
|
||||||
|
const auto table_end = table.end();
|
||||||
|
|
||||||
|
auto count_table_lambda = [table_begin, table_end](node_type type) noexcept
|
||||||
|
{
|
||||||
|
return std::count_if(table_begin, table_end, [type](const auto& pair) noexcept
|
||||||
|
{
|
||||||
|
return pair.second.type() == type;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
CHECK(std::distance(table_begin, table_end) == 3);
|
||||||
|
CHECK(count_table_lambda(node_type::table) == 0);
|
||||||
|
CHECK(count_table_lambda(node_type::integer) == 1);
|
||||||
|
CHECK(count_table_lambda(node_type::string) == 1);
|
||||||
|
CHECK(std::next(table_begin, 3) == table_end);
|
||||||
|
|
||||||
|
const auto array_iter = std::find_if(table_begin, table_end, [](const auto& pair) noexcept
|
||||||
|
{
|
||||||
|
return pair.second.is_array();
|
||||||
|
});
|
||||||
|
|
||||||
|
REQUIRE(array_iter != table_end);
|
||||||
|
const auto& array = array_iter->second.as_array();
|
||||||
|
const auto array_begin = array->begin();
|
||||||
|
const auto array_end = array->end();
|
||||||
|
|
||||||
|
auto count_array_lambda = [array_begin, array_end](node_type type) noexcept
|
||||||
|
{
|
||||||
|
return std::count_if(array_begin, array_end, [type](const auto& node) noexcept
|
||||||
|
{
|
||||||
|
return node.type() == type;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
CHECK(std::distance(array_begin, array_end) == 3);
|
||||||
|
CHECK(count_array_lambda(node_type::table) == 0);
|
||||||
|
CHECK(count_array_lambda(node_type::integer) == 1);
|
||||||
|
CHECK(count_array_lambda(node_type::string) == 1);
|
||||||
|
CHECK(std::next(array_begin, 2) != array_end);
|
||||||
|
});
|
||||||
|
}
|
4
toml.hpp
4
toml.hpp
@ -613,6 +613,7 @@ TOML_DISABLE_WARNINGS
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cfloat>
|
#include <cfloat>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <iterator>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@ -3433,6 +3434,7 @@ TOML_IMPL_NAMESPACE_START
|
|||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
using pointer = value_type*;
|
using pointer = value_type*;
|
||||||
using difference_type = ptrdiff_t;
|
using difference_type = ptrdiff_t;
|
||||||
|
using iterator_category = typename std::iterator_traits<raw_iterator>::iterator_category;
|
||||||
|
|
||||||
array_iterator() noexcept = default;
|
array_iterator() noexcept = default;
|
||||||
array_iterator(const array_iterator&) noexcept = default;
|
array_iterator(const array_iterator&) noexcept = default;
|
||||||
@ -4047,6 +4049,8 @@ TOML_IMPL_NAMESPACE_START
|
|||||||
using value_type = table_proxy_pair<IsConst>;
|
using value_type = table_proxy_pair<IsConst>;
|
||||||
using reference = value_type&;
|
using reference = value_type&;
|
||||||
using pointer = value_type*;
|
using pointer = value_type*;
|
||||||
|
using difference_type = typename std::iterator_traits<raw_iterator>::difference_type;
|
||||||
|
using iterator_category = typename std::iterator_traits<raw_iterator>::iterator_category;
|
||||||
|
|
||||||
table_iterator& operator++() noexcept // ++pre
|
table_iterator& operator++() noexcept // ++pre
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user