Add implicit conversion operator from node to node_view (#52)

- Creates a node_view pointing to that node. This is similar to how std::string has a conversion operator creating a string_view of the string.
- Also, enabled the move assignment operator for node_view, but made both copy and move assignment operators lvalue-only to avoid the "misleading assignment" issue (see https://github.com/marzer/tomlplusplus/issues/49#issuecomment-664432777)
This commit is contained in:
Nathan Reed 2020-08-08 16:12:17 -07:00 committed by GitHub
parent ad6be8e51e
commit 3b44bd504b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 6 deletions

View File

@ -819,6 +819,12 @@ TOML_NAMESPACE_START
{
return do_ref<T>(*this);
}
/// \brief Creates a `node_view` pointing to this node.
[[nodiscard]] operator node_view<node>() noexcept;
/// \brief Creates a `node_view` pointing to this node (const overload).
[[nodiscard]] operator node_view<const node>() const noexcept;
};
}
TOML_NAMESPACE_END

View File

@ -87,6 +87,18 @@ TOML_NAMESPACE_START
TOML_MEMBER_ATTR(const) const source_region& node::source() const noexcept { return source_; }
#undef TOML_MEMBER_ATTR
TOML_EXTERNAL_LINKAGE
node::operator node_view<node>() noexcept
{
return node_view<node>(this);
}
TOML_EXTERNAL_LINKAGE
node::operator node_view<const node>() const noexcept
{
return node_view<const node>(this);
}
}
TOML_NAMESPACE_END

View File

@ -63,6 +63,7 @@ TOML_NAMESPACE_START
using viewed_type = ViewedType;
private:
friend class TOML_NAMESPACE::node;
friend class TOML_NAMESPACE::table;
template <typename T> friend class TOML_NAMESPACE::node_view;
@ -88,13 +89,14 @@ TOML_NAMESPACE_START
node_view(const node_view&) noexcept = default;
///// \brief Copy-assignment operator.
TOML_NODISCARD_CTOR
node_view& operator= (const node_view&) noexcept = default;
node_view& operator= (const node_view&) & noexcept = default;
///// \brief Move constructor.
TOML_NODISCARD_CTOR
node_view(node_view&&) noexcept = default;
node_view& operator= (node_view&&) noexcept = delete;
///// \brief Move-assignment operator.
node_view& operator= (node_view&&) & noexcept = default;
/// \brief Returns true if the view references a node.
[[nodiscard]] explicit operator bool() const noexcept { return node_ != nullptr; }

View File

@ -2488,6 +2488,9 @@ TOML_NAMESPACE_START
{
return do_ref<T>(*this);
}
[[nodiscard]] operator node_view<node>() noexcept;
[[nodiscard]] operator node_view<const node>() const noexcept;
};
}
TOML_NAMESPACE_END
@ -4420,6 +4423,7 @@ TOML_NAMESPACE_START
using viewed_type = ViewedType;
private:
friend class TOML_NAMESPACE::node;
friend class TOML_NAMESPACE::table;
template <typename T> friend class TOML_NAMESPACE::node_view;
@ -4442,12 +4446,12 @@ TOML_NAMESPACE_START
TOML_NODISCARD_CTOR
node_view(const node_view&) noexcept = default;
TOML_NODISCARD_CTOR
node_view& operator= (const node_view&) noexcept = default;
node_view& operator= (const node_view&) & noexcept = default;
TOML_NODISCARD_CTOR
node_view(node_view&&) noexcept = default;
node_view& operator= (node_view&&) noexcept = delete;
node_view& operator= (node_view&&) & noexcept = default;
[[nodiscard]] explicit operator bool() const noexcept { return node_ != nullptr; }
[[nodiscard]] viewed_type* node() const noexcept { return node_; }
@ -7507,6 +7511,18 @@ TOML_NAMESPACE_START
TOML_MEMBER_ATTR(const) const source_region& node::source() const noexcept { return source_; }
#undef TOML_MEMBER_ATTR
TOML_EXTERNAL_LINKAGE
node::operator node_view<node>() noexcept
{
return node_view<node>(this);
}
TOML_EXTERNAL_LINKAGE
node::operator node_view<const node>() const noexcept
{
return node_view<const node>(this);
}
}
TOML_NAMESPACE_END