Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit conversion operators from node to node_view #52

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/toml++/toml_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions include/toml++/toml_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions include/toml++/toml_node_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -88,13 +89,14 @@ TOML_NAMESPACE_START
node_view(const node_view&) noexcept = default;

///// \brief Copy-assignment operator.
TOML_NODISCARD_CTOR
Reedbeta marked this conversation as resolved.
Show resolved Hide resolved
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; }
Expand Down
22 changes: 19 additions & 3 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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_; }

Expand Down Expand Up @@ -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

Expand Down