Skip to content

Commit 4ed6e7d

Browse files
committed
wip
1 parent ce395fd commit 4ed6e7d

11 files changed

Lines changed: 1499 additions & 1099 deletions

File tree

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ c4_add_library(ryml
5656
c4/yml/fwd.hpp
5757
c4/yml/export.hpp
5858
c4/yml/node.hpp
59-
c4/yml/node.cpp
6059
c4/yml/node_type.hpp
6160
c4/yml/node_type.cpp
6261
c4/yml/parser_state.hpp

src/c4/yml/common.hpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
/** @file common.hpp Common utilities and infrastructure used by ryml. */
55

66
#include <cstddef>
7+
8+
#ifndef _C4_SUBSTR_HPP_
79
#include <c4/substr.hpp>
10+
#endif
11+
#ifndef _C4_CHARCONV_HPP_
812
#include <c4/charconv.hpp>
13+
#endif
14+
#ifndef _C4_YML_CHARCONV_HPP_
915
#include <c4/yml/export.hpp>
16+
#endif
1017

1118

1219
//-----------------------------------------------------------------------------
@@ -150,6 +157,10 @@ static_assert(RYML_LOGBUF_SIZE < RYML_ERRMSG_SIZE, "invalid size");
150157
*
151158
*/
152159

160+
/** @defgroup doc_serialization_helpers Serialization helpers
161+
*
162+
*/
163+
153164
/** @defgroup doc_ref_utils Anchor/Reference utilities
154165
*
155166
* @see sample::sample_anchors_and_aliases
@@ -602,6 +613,30 @@ struct RYML_EXPORT Callbacks
602613
/** @} */
603614

604615

616+
//-----------------------------------------------------------------------------
617+
//-----------------------------------------------------------------------------
618+
//-----------------------------------------------------------------------------
619+
620+
/** @addtogroup doc_serialization_helpers
621+
*
622+
* @{
623+
*/
624+
625+
/** A tag type to select the key when serializing with operator<< or operator== */
626+
template<class K>
627+
struct Key
628+
{
629+
K &&k; // NOLINT
630+
};
631+
template<class K>
632+
C4_ALWAYS_INLINE Key<K> key(K && k)
633+
{
634+
return Key<K>{std::forward<K>(k)};
635+
}
636+
637+
/** @} */
638+
639+
605640
//-----------------------------------------------------------------------------
606641
//-----------------------------------------------------------------------------
607642
//-----------------------------------------------------------------------------

src/c4/yml/node.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,5 @@ namespace c4 {
44
namespace yml {
55

66

7-
8-
9-
//-----------------------------------------------------------------------------
10-
//-----------------------------------------------------------------------------
11-
//-----------------------------------------------------------------------------
12-
13-
size_t NodeRef::set_key_serialized(c4::fmt::const_base64_wrapper w)
14-
{
15-
_apply_seed();
16-
csubstr encoded = this->to_arena(w);
17-
this->set_key(encoded);
18-
return encoded.len;
19-
}
20-
21-
size_t NodeRef::set_val_serialized(c4::fmt::const_base64_wrapper w)
22-
{
23-
_apply_seed();
24-
csubstr encoded = this->to_arena(w);
25-
this->set_val(encoded);
26-
return encoded.len;
27-
}
28-
297
} // namespace yml
308
} // namespace c4

src/c4/yml/node.hpp

Lines changed: 925 additions & 659 deletions
Large diffs are not rendered by default.

src/c4/yml/parse.cpp

Lines changed: 16 additions & 16 deletions
Large diffs are not rendered by default.

src/c4/yml/parse_engine.def.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,23 @@ void ParseEngine<EventHandler>::_relocate_arena(csubstr prev_arena, substr next_
461461
}
462462
_ryml_relocate(m_evt_handler->m_src);
463463
for(size_t i = 0; i < m_pending_tags.num_entries; ++i)
464-
_ryml_relocate(m_pending_tags.annotations[i].str); // LCOV_EXCL_LINE
464+
{
465+
_ryml_relocate(m_pending_tags.annotations[i].str);
466+
}
465467
for(size_t i = 0; i < m_pending_anchors.num_entries; ++i)
466-
_ryml_relocate(m_pending_anchors.annotations[i].str); // LCOV_EXCL_LINE
468+
{
469+
_ryml_relocate(m_pending_anchors.annotations[i].str);
470+
}
467471
TagDirectives &tds = m_evt_handler->tag_directives();
468472
for(size_t i = 0, sz = tds.size(); i < sz; ++i)
469473
{
470-
_ryml_relocate(tds.m_directives[i].handle); // LCOV_EXCL_LINE
471-
_ryml_relocate(tds.m_directives[i].prefix); // LCOV_EXCL_LINE
474+
_ryml_relocate(tds.m_directives[i].handle);
475+
_ryml_relocate(tds.m_directives[i].prefix);
472476
}
473477
if(other)
474-
_ryml_relocate(*other); // LCOV_EXCL_LINE
478+
{
479+
_ryml_relocate(*other);
480+
}
475481
#undef _ryml_relocate
476482
}
477483

src/c4/yml/std/map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace yml {
1616
template<class K, class V, class Less, class Alloc>
1717
void write(c4::yml::NodeRef *n, std::map<K, V, Less, Alloc> const& m)
1818
{
19-
*n |= c4::yml::MAP;
19+
n->set_map();
2020
for(auto const& C4_RESTRICT p : m)
2121
{
2222
auto ch = n->append_child();

src/c4/yml/std/vector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace yml {
1616
template<class V, class Alloc>
1717
void write(c4::yml::NodeRef *n, std::vector<V, Alloc> const& vec)
1818
{
19-
*n |= c4::yml::SEQ;
19+
n->set_seq();
2020
for(V const& v : vec)
2121
n->append_child() << v;
2222
}

src/c4/yml/tree.cpp

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@ namespace c4 {
1616
namespace yml {
1717

1818

19-
csubstr serialize_to_arena(Tree * C4_RESTRICT tree, csubstr a)
19+
csubstr serialize_to_arena_str(Tree * C4_RESTRICT tree, csubstr a)
2020
{
2121
if(a.len > 0)
2222
{
23-
substr rem(tree->m_arena.sub(tree->m_arena_pos));
24-
size_t num = to_chars(rem, a);
25-
if(num > rem.len)
26-
{
27-
rem = tree->_grow_arena(num);
28-
num = to_chars(rem, a);
29-
_RYML_ASSERT_VISIT_(tree->m_callbacks, num <= rem.len, tree, NONE);
30-
}
31-
return tree->_request_span(num);
23+
return serialize_to_arena_scalar<csubstr>(tree, a);
3224
}
3325
else
3426
{
@@ -89,7 +81,7 @@ NodeRef Tree::operator[] (csubstr key)
8981
}
9082
ConstNodeRef Tree::operator[] (csubstr key) const
9183
{
92-
return rootref()[key];
84+
return crootref()[key];
9385
}
9486

9587
NodeRef Tree::operator[] (id_type i)
@@ -98,7 +90,7 @@ NodeRef Tree::operator[] (id_type i)
9890
}
9991
ConstNodeRef Tree::operator[] (id_type i) const
10092
{
101-
return rootref()[i];
93+
return crootref()[i];
10294
}
10395

10496
NodeRef Tree::docref(id_type i)
@@ -107,11 +99,11 @@ NodeRef Tree::docref(id_type i)
10799
}
108100
ConstNodeRef Tree::docref(id_type i) const
109101
{
110-
return cref(doc(i));
102+
return ConstNodeRef(this, doc(i));
111103
}
112104
ConstNodeRef Tree::cdocref(id_type i) const
113105
{
114-
return cref(doc(i));
106+
return ConstNodeRef(this, doc(i));
115107
}
116108

117109

@@ -937,6 +929,7 @@ void Tree::remove_children(id_type node)
937929
}
938930
}
939931

932+
//-----------------------------------------------------------------------------
940933
bool Tree::change_type(id_type node, NodeType type)
941934
{
942935
_RYML_ASSERT_VISIT_(m_callbacks, type.is_val() || type.is_map() || type.is_seq(), this, node);
@@ -1150,9 +1143,8 @@ void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
11501143
remove_children(dst_node);
11511144
_clear_type(dst_node);
11521145
if(src->has_key(src_node))
1153-
to_seq(dst_node, src->key(src_node));
1154-
else
1155-
to_seq(dst_node);
1146+
set_key(dst_node, src->key(src_node));
1147+
set_seq(dst_node);
11561148
_p(dst_node)->m_type = src->_p(src_node)->m_type;
11571149
}
11581150
for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
@@ -1171,9 +1163,8 @@ void Tree::merge_with(Tree const *src, id_type src_node, id_type dst_node)
11711163
remove_children(dst_node);
11721164
_clear_type(dst_node);
11731165
if(src->has_key(src_node))
1174-
to_map(dst_node, src->key(src_node));
1175-
else
1176-
to_map(dst_node);
1166+
set_key(dst_node, src->key(src_node));
1167+
set_map(dst_node);
11771168
_p(dst_node)->m_type = src->_p(src_node)->m_type;
11781169
}
11791170
for(id_type sch = src->first_child(src_node); sch != NONE; sch = src->next_sibling(sch))
@@ -1259,7 +1250,7 @@ id_type Tree::find_child(id_type node, csubstr const& name) const
12591250
{
12601251
_RYML_ASSERT_VISIT_(m_callbacks, node != NONE, this, node);
12611252
_RYML_ASSERT_VISIT_(m_callbacks, is_map(node), this, node);
1262-
if(get(node)->m_first_child == NONE)
1253+
if(_p(node)->m_first_child == NONE)
12631254
{
12641255
_RYML_ASSERT_VISIT_(m_callbacks, _p(node)->m_last_child == NONE, this, node);
12651256
return NONE;
@@ -1335,6 +1326,7 @@ void Tree::to_val(id_type node, csubstr val, type_bits more_flags)
13351326
{
13361327
_RYML_ASSERT_VISIT_(m_callbacks, ! has_children(node), this, node);
13371328
_RYML_ASSERT_VISIT_(m_callbacks, parent(node) == NONE || ! parent_is_map(node), this, node);
1329+
_RYML_ASSERT_VISIT_(m_callbacks, !is_seq(node) && !is_map(node), this, node);
13381330
_set_flags(node, VAL|more_flags);
13391331
_p(node)->m_key.clear();
13401332
_p(node)->m_val = val;
@@ -1344,6 +1336,7 @@ void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flag
13441336
{
13451337
_RYML_ASSERT_VISIT_(m_callbacks, ! has_children(node), this, node);
13461338
_RYML_ASSERT_VISIT_(m_callbacks, parent(node) == NONE || parent_is_map(node), this, node);
1339+
_RYML_ASSERT_VISIT_(m_callbacks, !is_seq(node) && !is_map(node), this, node);
13471340
_set_flags(node, KEYVAL|more_flags);
13481341
_p(node)->m_key = key;
13491342
_p(node)->m_val = val;
@@ -1352,7 +1345,6 @@ void Tree::to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flag
13521345
void Tree::to_map(id_type node, type_bits more_flags)
13531346
{
13541347
_RYML_ASSERT_VISIT_(m_callbacks, ! has_children(node), this, node);
1355-
_RYML_ASSERT_VISIT_(m_callbacks, parent(node) == NONE || ! parent_is_map(node), this, node); // parent must not have children with keys
13561348
_set_flags(node, MAP|more_flags);
13571349
_p(node)->m_key.clear();
13581350
_p(node)->m_val.clear();
@@ -1605,10 +1597,7 @@ Tree::lookup_result Tree::lookup_path(csubstr path, id_type start) const
16051597
id_type Tree::lookup_path_or_modify(csubstr default_value, csubstr path, id_type start)
16061598
{
16071599
id_type target = _lookup_path_or_create(path, start);
1608-
if(parent_is_map(target))
1609-
to_keyval(target, key(target), default_value);
1610-
else
1611-
to_val(target, default_value);
1600+
set_val(target, default_value);
16121601
return target;
16131602
}
16141603

@@ -1732,10 +1721,7 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
17321721
//_RYML_ASSERT_VISIT_(m_callbacks, is_container(r->closest) || r->closest == NONE);
17331722
if( ! is_container(r->closest))
17341723
{
1735-
if(has_key(r->closest))
1736-
to_map(r->closest, key(r->closest));
1737-
else
1738-
to_map(r->closest);
1724+
set_map(r->closest);
17391725
}
17401726
else
17411727
{
@@ -1789,17 +1775,7 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
17891775
return NONE;
17901776
if( ! is_container(r->closest))
17911777
{
1792-
if(has_key(r->closest))
1793-
{
1794-
csubstr k = key(r->closest);
1795-
_clear_type(r->closest);
1796-
to_seq(r->closest, k);
1797-
}
1798-
else
1799-
{
1800-
_clear_type(r->closest);
1801-
to_seq(r->closest);
1802-
}
1778+
set_seq(r->closest);
18031779
}
18041780
_RYML_ASSERT_VISIT_(m_callbacks, is_container(r->closest), this, r->closest);
18051781
node = child(r->closest, idx);
@@ -1812,9 +1788,16 @@ id_type Tree::_next_node_modify(lookup_result * r, _lookup_path_token *parent)
18121788
if(i < idx)
18131789
{
18141790
if(is_map(r->closest))
1815-
to_keyval(node, /*"~"*/{}, /*"~"*/{});
1791+
{
1792+
_clear_type(node);
1793+
set_key(node, {});
1794+
set_val(node, {});
1795+
}
18161796
else if(is_seq(r->closest))
1817-
to_val(node, /*"~"*/{});
1797+
{
1798+
_clear_type(node);
1799+
set_val(node, {});
1800+
}
18181801
}
18191802
}
18201803
}

0 commit comments

Comments
 (0)