Skip to content

Commit

Permalink
wip [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Apr 5, 2024
1 parent 12236b7 commit af04419
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/c4/yml/emit.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ substr Emitter<Writer>::emit_as(EmitType_e type, Tree const& t, bool error_on_ex
template<class Writer>
substr Emitter<Writer>::emit_as(EmitType_e type, ConstNodeRef const& n, bool error_on_excess)
{
_RYML_CB_CHECK(n.tree()->callbacks(), n.valid());
_RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
return this->emit_as(type, *n.tree(), n.id(), error_on_excess);
}

Expand Down
8 changes: 4 additions & 4 deletions src/c4/yml/emit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ CharOwningContainer emitrs_json(Tree const& t)
template<class CharOwningContainer>
substr emitrs_yaml(ConstNodeRef const& n, CharOwningContainer * cont)
{
_RYML_CB_CHECK(n.tree()->callbacks(), n.valid());
_RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
return emitrs_yaml(*n.tree(), n.id(), cont);
}
template<class CharOwningContainer>
Expand All @@ -447,7 +447,7 @@ RYML_DEPRECATE_EMITRS substr emitrs(ConstNodeRef const& n, CharOwningContainer *
template<class CharOwningContainer>
substr emitrs_json(ConstNodeRef const& n, CharOwningContainer * cont)
{
_RYML_CB_CHECK(n.tree()->callbacks(), n.valid());
_RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
return emitrs_json(*n.tree(), n.id(), cont);
}

Expand All @@ -457,7 +457,7 @@ substr emitrs_json(ConstNodeRef const& n, CharOwningContainer * cont)
template<class CharOwningContainer>
CharOwningContainer emitrs_yaml(ConstNodeRef const& n)
{
_RYML_CB_CHECK(n.tree()->callbacks(), n.valid());
_RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
CharOwningContainer c;
emitrs_yaml(*n.tree(), n.id(), &c);
return c;
Expand All @@ -473,7 +473,7 @@ RYML_DEPRECATE_EMITRS CharOwningContainer emitrs(ConstNodeRef const& n)
template<class CharOwningContainer>
CharOwningContainer emitrs_json(ConstNodeRef const& n)
{
_RYML_CB_CHECK(n.tree()->callbacks(), n.valid());
_RYML_CB_CHECK(n.tree()->callbacks(), n.readable());
CharOwningContainer c;
emitrs_json(*n.tree(), n.id(), &c);
return c;
Expand Down
55 changes: 32 additions & 23 deletions src/c4/yml/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ struct RoNodeMethods
{
_C4RV();
ConstImpl ch = find_child(name);
if(!ch.valid())
if(!ch.readable())
return false;
ch >> *var;
return true;
Expand All @@ -620,7 +620,7 @@ struct RoNodeMethods
{
_C4RV();
ConstImpl ch = find_child(name);
if(ch.valid())
if(ch.readable())
{
ch >> *var;
return true;
Expand Down Expand Up @@ -808,16 +808,20 @@ class RYML_EXPORT ConstNodeRef : public detail::RoNodeMethods<ConstNodeRef, Cons

public:

/** @name state queries */
/** @name state queries
*
* see @ref NodeRef for an explanation on what these states mean */
/** @{ */

C4_ALWAYS_INLINE C4_PURE bool valid() const noexcept { return m_tree != nullptr && m_id != NONE; }
C4_ALWAYS_INLINE bool invalid() const noexcept { return (!m_tree) || (m_id == NONE); }
/** because a ConstNodeRef cannot be used to write to the tree,
* readable() has the same meaning as valid() */
C4_ALWAYS_INLINE C4_PURE bool readable() const noexcept { return m_tree != nullptr && m_id != NONE; }
* readable() has the same meaning as !invalid() */
C4_ALWAYS_INLINE bool readable() const noexcept { return m_tree != nullptr && m_id != NONE; }
/** because a ConstNodeRef cannot be used to write to the tree, it can never be a seed.
* This method is provided for API equivalence between ConstNodeRef and NodeRef. */
constexpr static C4_ALWAYS_INLINE C4_PURE bool is_seed() noexcept { return false; }
constexpr static C4_ALWAYS_INLINE bool is_seed() noexcept { return false; }

RYML_DEPRECATED("use !invalid() instead of valid()") bool valid() const noexcept { return m_tree != nullptr && m_id != NONE; }

/** @} */

Expand All @@ -826,8 +830,8 @@ class RYML_EXPORT ConstNodeRef : public detail::RoNodeMethods<ConstNodeRef, Cons
/** @name member getters */
/** @{ */

C4_ALWAYS_INLINE C4_PURE Tree const* tree() const noexcept { return m_tree; }
C4_ALWAYS_INLINE C4_PURE size_t id() const noexcept { return m_id; }
C4_ALWAYS_INLINE Tree const* tree() const noexcept { return m_tree; }
C4_ALWAYS_INLINE size_t id() const noexcept { return m_id; }

/** @} */

Expand Down Expand Up @@ -863,10 +867,10 @@ class RYML_EXPORT ConstNodeRef : public detail::RoNodeMethods<ConstNodeRef, Cons
* object can be in one of three states:
*
* ```
* invalid := not pointing at anything
* valid := pointing at a tree/id pair, and further the node can be...
* ` readable := the node exists now
* ` seed := the node may come to exist, if we write to it.
* invalid := not pointing at anything
* readable := points at an existing tree/node
* seed := points at an existing tree, and the node
* may come to exist, if we write to it.
* ```
*
* So both `readable` and `seed` are states where the node is also `valid`.
Expand Down Expand Up @@ -959,12 +963,14 @@ class RYML_EXPORT NodeRef : public detail::RoNodeMethods<NodeRef, ConstNodeRef>
/** @name state_queries
* @{ */

/** true if the object is pointing at a tree and id. @see the doc for the NodeRef */
inline bool valid() const { return m_tree != nullptr && m_id != NONE; }
/** true if the object is valid() and in seed state. @see the doc for the NodeRef */
inline bool is_seed() const { return valid() && (m_seed.str != nullptr || m_seed.len != NONE); }
/** true if the object is valid() and NOT in seed state. @see the doc for the NodeRef */
inline bool readable() const { return valid() && !is_seed(); }
/** true if the object is not referring to any existing or seed node @see the doc for the NodeRef */
inline bool invalid() const { return m_tree != nullptr && m_id != NONE; }
/** true if the object is not invalid and in seed state. @see the doc for the NodeRef */
inline bool is_seed() const { return (m_tree != NULL && m_id != NONE) && (m_seed.str != nullptr || m_seed.len != (size_t)NONE); }
/** true if the object is not invalid and not in seed state. @see the doc for the NodeRef */
inline bool readable() const { return (m_tree != NULL && m_id != NONE) && (m_seed.str == nullptr || m_seed.len == (size_t)NONE); }

RYML_DEPRECATED("use !invalid()") inline bool valid() const { return m_tree != nullptr && m_id != NONE; }

inline void _clear_seed() { /*do the following manually or an assert is triggered: */ m_seed.str = nullptr; m_seed.len = NONE; }

Expand All @@ -979,11 +985,14 @@ class RYML_EXPORT NodeRef : public detail::RoNodeMethods<NodeRef, ConstNodeRef>
{
if(m_tree == that.m_tree && m_id == that.m_id)
{
if(is_seed() == that.is_seed())
bool seed = is_seed();
if(seed == that.is_seed())
{
if(is_seed())
if(seed)
{
return (m_seed.len == that.m_seed.len) && (m_seed.str == that.m_seed.str || m_seed == that.m_seed);
return (m_seed.len == that.m_seed.len)
&& (m_seed.str == that.m_seed.str
|| m_seed == that.m_seed); // do strcmp only in the last resort
}
return true;
}
Expand Down Expand Up @@ -1229,7 +1238,7 @@ class RYML_EXPORT NodeRef : public detail::RoNodeMethods<NodeRef, ConstNodeRef>
}
else
{
_RYML_CB_ASSERT(m_tree->m_callbacks, valid());
_RYML_CB_ASSERT(m_tree->m_callbacks, !invalid());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/c4/yml/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5528,7 +5528,7 @@ csubstr Parser::location_contents(Location const& loc) const

Location Parser::location(ConstNodeRef node) const
{
_RYML_CB_ASSERT(m_stack.m_callbacks, node.valid());
_RYML_CB_ASSERT(m_stack.m_callbacks, node.readable());
return location(*node.tree(), node.id());
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_case.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void print_path(ConstNodeRef const& n)
C4_ASSERT(len < sizeof(buf));
size_t pos = len;
p = n;
while(p.valid() && p != nullptr)
while(p.readable())
{
if(p.has_key())
{
Expand Down
24 changes: 12 additions & 12 deletions test/test_noderef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,27 @@ TEST(NodeRef, valid_vs_seed_vs_readable)
Tree tree = parse_in_arena("foo: bar");
NodeRef foo = tree["foo"];
ConstNodeRef const_foo = tree["foo"];
EXPECT_TRUE(foo.valid());
EXPECT_FALSE(foo.invalid());
EXPECT_FALSE(foo.is_seed());
EXPECT_TRUE(foo.readable());
EXPECT_TRUE(const_foo.valid());
EXPECT_FALSE(const_foo.invalid());
EXPECT_FALSE(const_foo.is_seed());
EXPECT_TRUE(const_foo.readable());
NodeRef none;
EXPECT_FALSE(none.valid());
EXPECT_TRUE(none.invalid());
EXPECT_FALSE(none.is_seed());
EXPECT_FALSE(none.readable());
ConstNodeRef const_none;
EXPECT_FALSE(const_none.valid());
EXPECT_TRUE(const_none.invalid());
EXPECT_FALSE(const_none.is_seed());
EXPECT_FALSE(const_none.readable());
none = tree["none"];
EXPECT_TRUE(none.valid());
EXPECT_FALSE(none.invalid());
EXPECT_TRUE(none.is_seed());
EXPECT_FALSE(none.readable());
ASSERT_FALSE(tree.rootref().has_child(none));
const_none = tree["none"];
EXPECT_FALSE(const_none.valid());
EXPECT_FALSE(const_none.invalid());
EXPECT_FALSE(const_none.is_seed());
EXPECT_TRUE(none.is_seed());
EXPECT_FALSE(none.readable());
Expand Down Expand Up @@ -375,7 +375,7 @@ TEST(NodeRef, cannot_read_from_invalid)
NodeRef none;
ASSERT_EQ(none.tree(), nullptr);
ASSERT_EQ(none.id(), NONE);
EXPECT_FALSE(none.valid());
EXPECT_TRUE(none.invalid());
EXPECT_FALSE(none.is_seed());
EXPECT_FALSE(none.readable());
test_fail_read(nullptr, none);
Expand All @@ -396,7 +396,7 @@ TEST(ConstNodeRef, cannot_read_from_invalid)
ConstNodeRef const_none;
ASSERT_EQ(const_none.tree(), nullptr);
ASSERT_EQ(const_none.id(), NONE);
EXPECT_FALSE(const_none.valid());
EXPECT_TRUE(const_none.invalid());
EXPECT_FALSE(const_none.is_seed());
EXPECT_FALSE(const_none.readable());
test_fail_read(nullptr, const_none);
Expand All @@ -422,7 +422,7 @@ TEST(NodeRef, cannot_read_from_seed)
NodeRef none = tree["none"];
ASSERT_EQ(none.tree(), &tree);
ASSERT_EQ(none.id(), 0);
EXPECT_TRUE(none.valid());
EXPECT_FALSE(none.invalid());
EXPECT_TRUE(none.is_seed());
EXPECT_FALSE(none.readable());
test_fail_read(&tree, none);
Expand All @@ -436,7 +436,7 @@ TEST(NodeRef, cannot_read_from_seed_subject)
NodeRef none = tree["none"];
ASSERT_EQ(none.tree(), &tree);
ASSERT_EQ(none.id(), 0);
EXPECT_TRUE(none.valid());
EXPECT_FALSE(none.invalid());
EXPECT_TRUE(none.is_seed());
EXPECT_FALSE(none.readable());
test_fail_read(&tree, none);
Expand All @@ -450,7 +450,7 @@ TEST(ConstNodeRef, cannot_read_from_seed_subject)
ConstNodeRef const_none = tree["none"];
ASSERT_EQ(const_none.tree(), &tree);
ASSERT_EQ(const_none.id(), NONE);
EXPECT_FALSE(const_none.valid());
EXPECT_TRUE(const_none.invalid());
EXPECT_FALSE(const_none.is_seed());
EXPECT_FALSE(const_none.readable());
test_fail_read(&tree, const_none);
Expand Down Expand Up @@ -732,7 +732,7 @@ TEST(NodeRef, remove_child__issue_356)
SCOPED_TRACE(id);
NodeRef formats = root["formats"];
std::cout << id << " id=" << formats.id() << "\n";
EXPECT_TRUE(formats.valid());
EXPECT_TRUE(formats.readable());
print_tree(tree);
check_invariants(tree);
EXPECT_EQ(emitrs_yaml<std::string>(formats), expected);
Expand Down

0 comments on commit af04419

Please sign in to comment.