Skip to content

Commit

Permalink
style API
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Mar 30, 2024
1 parent 23ae2d1 commit e0ea012
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 41 deletions.
20 changes: 10 additions & 10 deletions src/c4/yml/emit.def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ void Emitter<Writer>::_emit_yaml(size_t id)
// the visitor functions
auto dispatch = [this](size_t node){
NodeType ty = m_tree->type(node);
if(ty.marked_flow_sl())
if(ty.is_flow_sl())
_do_visit_flow_sl(node, 0);
else if(ty.marked_flow_ml())
else if(ty.is_flow_ml())
_do_visit_flow_ml(node, 0);
else
{
Expand All @@ -69,7 +69,7 @@ void Emitter<Writer>::_emit_yaml(size_t id)
};
if(!m_tree->is_root(id))
{
if(m_tree->is_container(id) && !m_tree->type(id).marked_flow())
if(m_tree->is_container(id) && !m_tree->type(id).is_flow())
{
size_t ilevel = 0;
if(m_tree->has_key(id))
Expand Down Expand Up @@ -112,7 +112,7 @@ void Emitter<Writer>::_emit_yaml(size_t id)
for(size_t child = first_child; child != NONE; child = m_tree->next_sibling(child))
{
dispatch(child);
if(m_tree->is_doc(child) && m_tree->type(child).marked_flow_sl())
if(m_tree->is_doc(child) && m_tree->type(child).is_flow_sl())
this->Writer::_do_write('\n');
if(m_tree->next_sibling(child) != NONE)
write_tag_directives(m_tree->next_sibling(child));
Expand All @@ -133,14 +133,14 @@ void Emitter<Writer>::_emit_yaml(size_t id)
_writek(id, 0);
this->Writer::_do_write(": ");
_writev(id, 0);
if(!m_tree->type(id).marked_flow())
if(!m_tree->type(id).is_flow())
this->Writer::_do_write('\n');
}
else if(m_tree->is_val(id))
{
//this->Writer::_do_write("- ");
_writev(id, 0);
if(!m_tree->type(id).marked_flow())
if(!m_tree->type(id).is_flow())
this->Writer::_do_write('\n');
}
else if(m_tree->type(id) == NOTYPE)
Expand Down Expand Up @@ -379,14 +379,14 @@ void Emitter<Writer>::_do_visit_block_container(size_t node, size_t level, bool
{
_RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_container(child));
NodeType ty = m_tree->type(child);
if(ty.marked_flow_sl())
if(ty.is_flow_sl())
{
_indent(level, do_indent);
this->Writer::_do_write("- ");
_do_visit_flow_sl(child, 0u);
this->Writer::_do_write('\n');
}
else if(ty.marked_flow_ml())
else if(ty.is_flow_ml())
{
_indent(level, do_indent);

Check warning on line 391 in src/c4/yml/emit.def.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/emit.def.hpp#L391

Added line #L391 was not covered by tests
this->Writer::_do_write("- ");
Expand Down Expand Up @@ -419,13 +419,13 @@ void Emitter<Writer>::_do_visit_block_container(size_t node, size_t level, bool
{
_RYML_CB_ASSERT(m_tree->callbacks(), m_tree->is_container(ich));
NodeType ty = m_tree->type(ich);
if(ty.marked_flow_sl())
if(ty.is_flow_sl())
{
_indent(level, do_indent);
_do_visit_flow_sl(ich, 0u);
this->Writer::_do_write('\n');
}
else if(ty.marked_flow_ml())
else if(ty.is_flow_ml())
{
_indent(level, do_indent);

Check warning on line 430 in src/c4/yml/emit.def.hpp

View check run for this annotation

Codecov / codecov/patch

src/c4/yml/emit.def.hpp#L430

Added line #L430 was not covered by tests
_do_visit_flow_ml(ich, 0u);
Expand Down
30 changes: 29 additions & 1 deletion src/c4/yml/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ struct RoNodeMethods

public:

/** @name node property predicates */
/** @name node type predicates */
/** @{ */

C4_ALWAYS_INLINE C4_PURE bool empty() const noexcept { _C4RV(); return tree_->empty(id_); }
Expand Down Expand Up @@ -231,6 +231,30 @@ struct RoNodeMethods

/** @} */

public:

/** @name node style predicates */
/** @{ */

C4_ALWAYS_INLINE C4_PURE bool is_key_literal() const noexcept { _C4RV(); return tree_->is_key_literal(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_literal() const noexcept { _C4RV(); return tree_->is_val_literal(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_key_folded() const noexcept { _C4RV(); return tree_->is_key_folded(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_folded() const noexcept { _C4RV(); return tree_->is_val_folded(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_key_squo() const noexcept { _C4RV(); return tree_->is_key_squo(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_squo() const noexcept { _C4RV(); return tree_->is_val_squo(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_key_dquo() const noexcept { _C4RV(); return tree_->is_key_dquo(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_dquo() const noexcept { _C4RV(); return tree_->is_val_dquo(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_key_plain() const noexcept { _C4RV(); return tree_->is_key_plain(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_plain() const noexcept { _C4RV(); return tree_->is_val_plain(id_); }
C4_ALWAYS_INLINE C4_PURE bool style_has_any(NodeType_e style_bits) const noexcept { _C4RV(); return tree_->style_has_any(id_, STYLE & style_bits); }
C4_ALWAYS_INLINE C4_PURE bool style_has_all(NodeType_e style_bits) const noexcept { _C4RV(); return tree_->style_has_all(id_, STYLE & style_bits); }
C4_ALWAYS_INLINE C4_PURE bool style_has_none(NodeType_e style_bits) const noexcept { _C4RV(); return tree_->style_has_none(id_, STYLE & style_bits); }

C4_ALWAYS_INLINE C4_PURE bool is_key_unfiltered() const noexcept { _C4RV(); return tree_->is_key_unfiltered(id_); }
C4_ALWAYS_INLINE C4_PURE bool is_val_unfiltered() const noexcept { _C4RV(); return tree_->is_val_unfiltered(id_); }

/** @} */

public:

/** @name hierarchy predicates */
Expand Down Expand Up @@ -768,6 +792,10 @@ class RYML_EXPORT NodeRef : public detail::RoNodeMethods<NodeRef, ConstNodeRef>
void set_key_ref(csubstr key_ref) { _C4RV(); m_tree->set_key_ref(m_id, key_ref); }
void set_val_ref(csubstr val_ref) { _C4RV(); m_tree->set_val_ref(m_id, val_ref); }

void set_container_style(NodeType_e style) { _C4RV(); m_tree->set_container_style(m_id, style); }
void set_key_style(NodeType_e style) { _C4RV(); m_tree->set_key_style(m_id, style); }
void set_val_style(NodeType_e style) { _C4RV(); m_tree->set_val_style(m_id, style); }

public:

inline void clear()
Expand Down
22 changes: 14 additions & 8 deletions src/c4/yml/node_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ struct RYML_EXPORT NodeType
C4_ALWAYS_INLINE NodeType(NodeType_e t) noexcept : type(t) {}
C4_ALWAYS_INLINE NodeType(type_bits t) noexcept : type((NodeType_e)t) {}

C4_ALWAYS_INLINE bool has_any(NodeType_e t) const noexcept { return (type & t) != 0u; }
C4_ALWAYS_INLINE bool has_all(NodeType_e t) const noexcept { return (type & t) == t; }
C4_ALWAYS_INLINE bool has_none(NodeType_e t) const noexcept { return (type & t) == 0; }

C4_ALWAYS_INLINE void set(NodeType_e t) noexcept { type = t; }
C4_ALWAYS_INLINE void add(NodeType_e t) noexcept { type = (type|t); }
C4_ALWAYS_INLINE void rem(NodeType_e t) noexcept { type = (type & ~t); }

C4_ALWAYS_INLINE void set(type_bits t) noexcept { type = (NodeType_e)t; }
C4_ALWAYS_INLINE void add(type_bits t) noexcept { type = (NodeType_e)(type|t); }
C4_ALWAYS_INLINE void rem(type_bits t) noexcept { type = (NodeType_e)(type & ~t); }
C4_ALWAYS_INLINE void addrem(NodeType_e bits_to_add, NodeType_e bits_to_remove) noexcept { type |= bits_to_add; type &= ~bits_to_remove; }

C4_ALWAYS_INLINE void clear() noexcept { type = NOTYPE; }

Expand Down Expand Up @@ -180,10 +181,12 @@ struct RYML_EXPORT NodeType
/** @name container style queries
* @{ */

C4_ALWAYS_INLINE bool marked_block() const noexcept { return (type & (BLOCK)) != 0; }
C4_ALWAYS_INLINE bool marked_flow_sl() const noexcept { return (type & (FLOW_SL)) != 0; }
C4_ALWAYS_INLINE bool marked_flow_ml() const noexcept { return (type & (FLOW_ML)) != 0; }
C4_ALWAYS_INLINE bool marked_flow() const noexcept { return (type & (FLOW_ML|FLOW_SL)) != 0; }
C4_ALWAYS_INLINE bool is_block() const noexcept { return (type & (BLOCK)) != 0; }
C4_ALWAYS_INLINE bool is_flow_sl() const noexcept { return (type & (FLOW_SL)) != 0; }
C4_ALWAYS_INLINE bool is_flow_ml() const noexcept { return (type & (FLOW_ML)) != 0; }
C4_ALWAYS_INLINE bool is_flow() const noexcept { return (type & (FLOW_ML|FLOW_SL)) != 0; }

C4_ALWAYS_INLINE void set_container_style(NodeType_e style) noexcept { type &= ~CONTAINER_STYLE; type |= (style & CONTAINER_STYLE); }

/** @} */

Expand All @@ -207,6 +210,9 @@ struct RYML_EXPORT NodeType
C4_ALWAYS_INLINE bool is_key_plain() const noexcept { return (type & (KEY_PLAIN)) != 0; }
C4_ALWAYS_INLINE bool is_val_plain() const noexcept { return (type & (VAL_PLAIN)) != 0; }

C4_ALWAYS_INLINE void set_key_style(NodeType_e style) noexcept { type &= ~KEY_STYLE; type |= (style & KEY_STYLE); }
C4_ALWAYS_INLINE void set_val_style(NodeType_e style) noexcept { type &= ~VAL_STYLE; type |= (style & VAL_STYLE); }

C4_ALWAYS_INLINE bool is_key_unfiltered() const noexcept { return (type & (KEY_UNFILT)) != 0; }
C4_ALWAYS_INLINE bool is_val_unfiltered() const noexcept { return (type & (VAL_UNFILT)) != 0; }

Expand Down
12 changes: 6 additions & 6 deletions src/c4/yml/tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,13 @@ class RYML_EXPORT Tree
C4_ALWAYS_INLINE bool is_val_quoted(size_t node) const noexcept { return _p(node)->m_type.is_val_quoted(); }
C4_ALWAYS_INLINE bool is_quoted(size_t node) const noexcept { return _p(node)->m_type.is_quoted(); }

C4_ALWAYS_INLINE bool style_has_any(size_t node, NodeType_e style_bits) const noexcept { style_bits &= STYLE; return (_p(node)->m_type & style_bits) != 0u; }
C4_ALWAYS_INLINE bool style_has_all(size_t node, NodeType_e style_bits) const noexcept { style_bits &= STYLE; return (_p(node)->m_type & style_bits) == style_bits; }
C4_ALWAYS_INLINE bool style_has_none(size_t node, NodeType_e style_bits) const noexcept { style_bits &= STYLE; return (_p(node)->m_type & style_bits) == 0; }
C4_ALWAYS_INLINE bool style_has_any(size_t node, NodeType_e style_bits) const noexcept { return _p(node)->m_type.has_any(STYLE & style_bits); }
C4_ALWAYS_INLINE bool style_has_all(size_t node, NodeType_e style_bits) const noexcept { return _p(node)->m_type.has_all(STYLE & style_bits); }
C4_ALWAYS_INLINE bool style_has_none(size_t node, NodeType_e style_bits) const noexcept { return _p(node)->m_type.has_none(STYLE & style_bits); }

C4_ALWAYS_INLINE void style_add(size_t node, NodeType_e style_bits) noexcept { _p(node)->m_type |= style_bits; }
C4_ALWAYS_INLINE void style_rem(size_t node, NodeType_e style_bits) noexcept { _p(node)->m_type &= ~style_bits; }
C4_ALWAYS_INLINE void style_addrem(size_t node, NodeType_e add_bits, NodeType_e rem_bits) noexcept { NodeType_e &type = _p(node)->m_type.type; type |= add_bits; type &= ~rem_bits; }
C4_ALWAYS_INLINE void set_container_style(size_t node, NodeType_e style) noexcept { _p(node)->m_type.set_container_style(style); }
C4_ALWAYS_INLINE void set_key_style(size_t node, NodeType_e style) noexcept { _p(node)->m_type.set_key_style(style); }
C4_ALWAYS_INLINE void set_val_style(size_t node, NodeType_e style) noexcept { _p(node)->m_type.set_val_style(style); }

/** @} */

Expand Down
32 changes: 16 additions & 16 deletions test/test_style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@ std::string emit2str(Tree const& t)

inline void test_container_nostyle(ConstNodeRef n)
{
EXPECT_FALSE(n.type().marked_block());
EXPECT_FALSE(n.type().marked_flow());
EXPECT_FALSE(n.type().marked_flow_sl());
EXPECT_FALSE(n.type().marked_flow_ml());
EXPECT_FALSE(n.type().is_block());
EXPECT_FALSE(n.type().is_flow());
EXPECT_FALSE(n.type().is_flow_sl());
EXPECT_FALSE(n.type().is_flow_ml());
}

inline void test_container_block(ConstNodeRef n)
{
EXPECT_TRUE(n.type().marked_block());
EXPECT_FALSE(n.type().marked_flow());
EXPECT_FALSE(n.type().marked_flow_sl());
EXPECT_FALSE(n.type().marked_flow_ml());
EXPECT_TRUE(n.type().is_block());
EXPECT_FALSE(n.type().is_flow());
EXPECT_FALSE(n.type().is_flow_sl());
EXPECT_FALSE(n.type().is_flow_ml());
}

inline void test_container_flow_sl(ConstNodeRef n)
{
EXPECT_FALSE(n.type().marked_block());
EXPECT_TRUE(n.type().marked_flow());
EXPECT_TRUE(n.type().marked_flow_sl());
EXPECT_FALSE(n.type().marked_flow_ml());
EXPECT_FALSE(n.type().is_block());
EXPECT_TRUE(n.type().is_flow());
EXPECT_TRUE(n.type().is_flow_sl());
EXPECT_FALSE(n.type().is_flow_ml());
}

inline void test_container_flow_ml(ConstNodeRef n)
{
EXPECT_FALSE(n.type().marked_block());
EXPECT_TRUE(n.type().marked_flow());
EXPECT_FALSE(n.type().marked_flow_sl());
EXPECT_TRUE(n.type().marked_flow_ml());
EXPECT_FALSE(n.type().is_block());
EXPECT_TRUE(n.type().is_flow());
EXPECT_FALSE(n.type().is_flow_sl());
EXPECT_TRUE(n.type().is_flow_ml());
}

inline void test_key_plain(ConstNodeRef n)
Expand Down
Loading

0 comments on commit e0ea012

Please sign in to comment.