added additional node_view constructors

also made node conversion operators explicit
This commit is contained in:
Mark Gillard 2020-08-13 14:02:40 +03:00
parent 9b4614a4e7
commit 11a0e84ced
5 changed files with 85 additions and 40 deletions

View File

@ -123,6 +123,7 @@ int main(int argc, char** argv)
tree.push_back(&root); tree.push_back(&root);
constexpr size_t max_depth = 10u; constexpr size_t max_depth = 10u;
int container_min_values = 10; int container_min_values = 10;
bool in_arr = false;
const auto add = [&](auto&& obj) noexcept -> toml::node& const auto add = [&](auto&& obj) noexcept -> toml::node&
{ {
@ -143,6 +144,7 @@ int main(int argc, char** argv)
{ {
tree.push_back(new_node); tree.push_back(new_node);
container_min_values = rand(1, 4); container_min_values = rand(1, 4);
in_arr = toml::is_array<decltype(obj)>;
} }
else else
container_min_values--; container_min_values--;
@ -153,14 +155,20 @@ int main(int argc, char** argv)
while (node_budget) while (node_budget)
{ {
if (rand(100) >= 75) if (!in_arr && rand(100) >= 75)
{ {
if (container_min_values <= 0 && tree.size() < max_depth) if (container_min_values <= 0 && tree.size() < max_depth)
add(toml::table{}).ref<toml::table>().is_inline(tree.size() >= max_depth - 2u && rand(100) >= 85); add(toml::table{}).ref<toml::table>().is_inline(tree.size() >= max_depth - 2u && rand(100) >= 85);
} }
else else
{ {
switch (static_cast<toml::node_type>((rand() % 8) + 2)) toml::node_type new_node_type;
if (auto arr = tree.back()->as_array(); arr && !arr->empty())
new_node_type = arr->front().type();
else
new_node_type = static_cast<toml::node_type>((rand() % 8) + 2);
switch (new_node_type)
{ {
case toml::node_type::array: case toml::node_type::array:
if (container_min_values <= 0 && tree.size() < max_depth) if (container_min_values <= 0 && tree.size() < max_depth)
@ -199,7 +207,10 @@ int main(int argc, char** argv)
break; break;
} }
if (container_min_values <= 0 && tree.size() >= 2u && rand(100) >= 85) if (container_min_values <= 0 && tree.size() >= 2u && rand(100) >= 85)
{
tree.pop_back(); tree.pop_back();
in_arr = !tree.empty() && tree.back()->type() == toml::node_type::array;
}
} }
} }

View File

@ -816,10 +816,10 @@ TOML_NAMESPACE_START
} }
/// \brief Creates a `node_view` pointing to this node. /// \brief Creates a `node_view` pointing to this node.
[[nodiscard]] operator node_view<node>() noexcept; [[nodiscard]] explicit operator node_view<node>() noexcept;
/// \brief Creates a `node_view` pointing to this node (const overload). /// \brief Creates a `node_view` pointing to this node (const overload).
[[nodiscard]] operator node_view<const node>() const noexcept; [[nodiscard]] explicit operator node_view<const node>() const noexcept;
}; };
} }
TOML_NAMESPACE_END TOML_NAMESPACE_END

View File

@ -59,21 +59,19 @@ TOML_NAMESPACE_START
template <typename ViewedType> template <typename ViewedType>
class TOML_API TOML_TRIVIAL_ABI node_view class TOML_API TOML_TRIVIAL_ABI node_view
{ {
static_assert(
impl::is_one_of<ViewedType, toml::node, const toml::node>,
"A toml::node_view<> must wrap toml::node or const toml::node."
);
public: public:
using viewed_type = ViewedType; using viewed_type = ViewedType;
private: private:
friend class TOML_NAMESPACE::node;
friend class TOML_NAMESPACE::table;
template <typename T> friend class TOML_NAMESPACE::node_view; template <typename T> friend class TOML_NAMESPACE::node_view;
mutable viewed_type* node_ = nullptr; mutable viewed_type* node_ = nullptr;
TOML_NODISCARD_CTOR
node_view(viewed_type* node) noexcept
: node_{ node }
{}
template <typename Func> template <typename Func>
static constexpr bool visit_is_nothrow static constexpr bool visit_is_nothrow
= noexcept(std::declval<viewed_type*>()->visit(std::declval<Func&&>())); = noexcept(std::declval<viewed_type*>()->visit(std::declval<Func&&>()));
@ -84,6 +82,18 @@ TOML_NAMESPACE_START
TOML_NODISCARD_CTOR TOML_NODISCARD_CTOR
node_view() noexcept = default; node_view() noexcept = default;
/// \brief Constructs node_view of a specific node.
TOML_NODISCARD_CTOR
explicit node_view(viewed_type* node) noexcept
: node_{ node }
{}
/// \brief Constructs node_view of a specific node.
TOML_NODISCARD_CTOR
explicit node_view(viewed_type& node) noexcept
: node_{ &node }
{}
///// \brief Copy constructor. ///// \brief Copy constructor.
TOML_NODISCARD_CTOR TOML_NODISCARD_CTOR
node_view(const node_view&) noexcept = default; node_view(const node_view&) noexcept = default;
@ -547,8 +557,8 @@ TOML_NAMESPACE_START
node_view operator[] (std::string_view key) const noexcept node_view operator[] (std::string_view key) const noexcept
{ {
if (auto tbl = this->as_table()) if (auto tbl = this->as_table())
return { tbl->get(key) }; return node_view{ tbl->get(key) };
return { nullptr }; return node_view{ nullptr };
} }
#if TOML_WINDOWS_COMPAT #if TOML_WINDOWS_COMPAT
@ -565,8 +575,8 @@ TOML_NAMESPACE_START
node_view operator[] (std::wstring_view key) const noexcept node_view operator[] (std::wstring_view key) const noexcept
{ {
if (auto tbl = this->as_table()) if (auto tbl = this->as_table())
return { tbl->get(key) }; return node_view{ tbl->get(key) };
return { nullptr }; return node_view{ nullptr };
} }
#endif // TOML_WINDOWS_COMPAT #endif // TOML_WINDOWS_COMPAT
@ -581,13 +591,21 @@ TOML_NAMESPACE_START
node_view operator[] (size_t index) const noexcept node_view operator[] (size_t index) const noexcept
{ {
if (auto arr = this->as_array()) if (auto arr = this->as_array())
return { arr->get(index) }; return node_view{ arr->get(index) };
return { nullptr }; return node_view{ nullptr };
} }
template <typename Char, typename T> template <typename Char, typename T>
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&); friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&);
}; };
template <typename T> node_view(const value<T>&) -> node_view<const node>;
node_view(const table&) -> node_view<const node>;
node_view(const array&) -> node_view<const node>;
template <typename T> node_view(value<T>&) -> node_view<node>;
node_view(table&) -> node_view<node>;
node_view(array&) -> node_view<node>;
template <typename T> node_view(const T*) -> node_view<const node>;
template <typename T> node_view(T*) -> node_view<node>;
/// \brief Prints the viewed node out to a stream. /// \brief Prints the viewed node out to a stream.
template <typename Char, typename T> template <typename Char, typename T>

View File

@ -160,12 +160,12 @@ TOML_NAMESPACE_START
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::string_view key) noexcept node_view<node> table::operator[] (std::string_view key) noexcept
{ {
return { this->get(key) }; return node_view<node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::string_view key) const noexcept node_view<const node> table::operator[] (std::string_view key) const noexcept
{ {
return { this->get(key) }; return node_view<const node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
@ -232,12 +232,12 @@ TOML_NAMESPACE_START
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::wstring_view key) noexcept node_view<node> table::operator[] (std::wstring_view key) noexcept
{ {
return { this->get(key) }; return node_view<node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::wstring_view key) const noexcept node_view<const node> table::operator[] (std::wstring_view key) const noexcept
{ {
return { this->get(key) }; return node_view<const node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE

View File

@ -2475,8 +2475,8 @@ TOML_NAMESPACE_START
return do_ref<T>(*this); return do_ref<T>(*this);
} }
[[nodiscard]] operator node_view<node>() noexcept; [[nodiscard]] explicit operator node_view<node>() noexcept;
[[nodiscard]] operator node_view<const node>() const noexcept; [[nodiscard]] explicit operator node_view<const node>() const noexcept;
}; };
} }
TOML_NAMESPACE_END TOML_NAMESPACE_END
@ -4379,21 +4379,19 @@ TOML_NAMESPACE_START
template <typename ViewedType> template <typename ViewedType>
class TOML_API TOML_TRIVIAL_ABI node_view class TOML_API TOML_TRIVIAL_ABI node_view
{ {
static_assert(
impl::is_one_of<ViewedType, toml::node, const toml::node>,
"A toml::node_view<> must wrap toml::node or const toml::node."
);
public: public:
using viewed_type = ViewedType; using viewed_type = ViewedType;
private: private:
friend class TOML_NAMESPACE::node;
friend class TOML_NAMESPACE::table;
template <typename T> friend class TOML_NAMESPACE::node_view; template <typename T> friend class TOML_NAMESPACE::node_view;
mutable viewed_type* node_ = nullptr; mutable viewed_type* node_ = nullptr;
TOML_NODISCARD_CTOR
node_view(viewed_type* node) noexcept
: node_{ node }
{}
template <typename Func> template <typename Func>
static constexpr bool visit_is_nothrow static constexpr bool visit_is_nothrow
= noexcept(std::declval<viewed_type*>()->visit(std::declval<Func&&>())); = noexcept(std::declval<viewed_type*>()->visit(std::declval<Func&&>()));
@ -4403,6 +4401,16 @@ TOML_NAMESPACE_START
TOML_NODISCARD_CTOR TOML_NODISCARD_CTOR
node_view() noexcept = default; node_view() noexcept = default;
TOML_NODISCARD_CTOR
explicit node_view(viewed_type* node) noexcept
: node_{ node }
{}
TOML_NODISCARD_CTOR
explicit node_view(viewed_type& node) noexcept
: node_{ &node }
{}
TOML_NODISCARD_CTOR TOML_NODISCARD_CTOR
node_view(const node_view&) noexcept = default; node_view(const node_view&) noexcept = default;
@ -4655,8 +4663,8 @@ TOML_NAMESPACE_START
node_view operator[] (std::string_view key) const noexcept node_view operator[] (std::string_view key) const noexcept
{ {
if (auto tbl = this->as_table()) if (auto tbl = this->as_table())
return { tbl->get(key) }; return node_view{ tbl->get(key) };
return { nullptr }; return node_view{ nullptr };
} }
#if TOML_WINDOWS_COMPAT #if TOML_WINDOWS_COMPAT
@ -4665,8 +4673,8 @@ TOML_NAMESPACE_START
node_view operator[] (std::wstring_view key) const noexcept node_view operator[] (std::wstring_view key) const noexcept
{ {
if (auto tbl = this->as_table()) if (auto tbl = this->as_table())
return { tbl->get(key) }; return node_view{ tbl->get(key) };
return { nullptr }; return node_view{ nullptr };
} }
#endif // TOML_WINDOWS_COMPAT #endif // TOML_WINDOWS_COMPAT
@ -4675,13 +4683,21 @@ TOML_NAMESPACE_START
node_view operator[] (size_t index) const noexcept node_view operator[] (size_t index) const noexcept
{ {
if (auto arr = this->as_array()) if (auto arr = this->as_array())
return { arr->get(index) }; return node_view{ arr->get(index) };
return { nullptr }; return node_view{ nullptr };
} }
template <typename Char, typename T> template <typename Char, typename T>
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&); friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&);
}; };
template <typename T> node_view(const value<T>&) -> node_view<const node>;
node_view(const table&) -> node_view<const node>;
node_view(const array&) -> node_view<const node>;
template <typename T> node_view(value<T>&) -> node_view<node>;
node_view(table&) -> node_view<node>;
node_view(array&) -> node_view<node>;
template <typename T> node_view(const T*) -> node_view<const node>;
template <typename T> node_view(T*) -> node_view<node>;
template <typename Char, typename T> template <typename Char, typename T>
inline std::basic_ostream<Char>& operator << (std::basic_ostream<Char>& os, const node_view<T>& nv) inline std::basic_ostream<Char>& operator << (std::basic_ostream<Char>& os, const node_view<T>& nv)
@ -7908,12 +7924,12 @@ TOML_NAMESPACE_START
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::string_view key) noexcept node_view<node> table::operator[] (std::string_view key) noexcept
{ {
return { this->get(key) }; return node_view<node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::string_view key) const noexcept node_view<const node> table::operator[] (std::string_view key) const noexcept
{ {
return { this->get(key) }; return node_view<const node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
@ -7980,12 +7996,12 @@ TOML_NAMESPACE_START
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::wstring_view key) noexcept node_view<node> table::operator[] (std::wstring_view key) noexcept
{ {
return { this->get(key) }; return node_view<node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::wstring_view key) const noexcept node_view<const node> table::operator[] (std::wstring_view key) const noexcept
{ {
return { this->get(key) }; return node_view<const node>{ this->get(key) };
} }
TOML_EXTERNAL_LINKAGE TOML_EXTERNAL_LINKAGE