Skip to content

Commit 1dc7327

Browse files
committed
comments wip [ci skip]
1 parent 6c44061 commit 1dc7327

6 files changed

Lines changed: 453 additions & 60 deletions

File tree

src/c4/yml/tree.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ Tree::Tree(id_type node_capacity, size_t arena_capacity, id_type comment_capacit
114114
reserve_arena(arena_capacity);
115115
reserve_comments(comment_capacity);
116116
}
117+
Tree::Tree(id_type node_capacity, size_t arena_capacity, id_type comment_capacity)
118+
: Tree(node_capacity, arena_capacity, comment_capacity, get_callbacks())
119+
{
120+
}
117121
#endif // RYML_WITH_COMMENTS
118122

119123
Tree::~Tree()
@@ -161,20 +165,20 @@ void Tree::_free()
161165
_RYML_ASSERT_VISIT_(m_callbacks, m_cap > 0, this, NONE);
162166
_RYML_CB_FREE(m_callbacks, m_buf, NodeData, (size_t)m_cap);
163167
}
164-
if(m_arena.str)
165-
{
166-
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.len > 0, this, NONE);
167-
_RYML_CB_FREE(m_callbacks, m_arena.str, char, m_arena.len);
168-
}
169-
_clear();
170168
#ifdef RYML_WITH_COMMENTS
171169
if(m_comments_buf)
172170
{
173171
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_cap > 0, this, NONE);
174172
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_cap >= m_comments_size, this, NONE);
175-
_RYML_CB_FREE(m_callbacks, m_comments_buf, CommentData, m_comments_cap);
173+
_RYML_CB_FREE(m_callbacks, m_comments_buf, CommentData, (size_t)m_comments_cap);
176174
}
177175
#endif // RYML_WITH_COMMENTS
176+
if(m_arena.str)
177+
{
178+
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.len > 0, this, NONE);
179+
_RYML_CB_FREE(m_callbacks, m_arena.str, char, m_arena.len);
180+
}
181+
_clear();
178182
}
179183

180184

@@ -192,20 +196,22 @@ void Tree::_clear()
192196
m_free_tail = 0;
193197
m_arena = {};
194198
m_arena_pos = 0;
195-
for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
196-
m_tag_directives[i] = {};
197199
#ifdef RYML_WITH_COMMENTS
198200
m_comments_buf = nullptr;
199201
m_comments_cap = 0;
200202
m_comments_size = 0;
201203
#endif // RYML_WITH_COMMENTS
204+
for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
205+
m_tag_directives[i] = {};
202206
}
203207

204208
void Tree::_copy(Tree const& that)
205209
{
206210
_RYML_ASSERT_VISIT_(m_callbacks, m_buf == nullptr, this, NONE);
211+
_RYML_ASSERT_VISIT_(m_callbacks, m_cap == 0, this, NONE);
207212
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.str == nullptr, this, NONE);
208213
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.len == 0, this, NONE);
214+
_RYML_ASSERT_VISIT_(m_callbacks, that.m_cap >= that.m_size, this, NONE);
209215
if(that.m_cap)
210216
{
211217
m_buf = _RYML_CB_ALLOC_HINT(m_callbacks, NodeData, (size_t)that.m_cap, that.m_buf);
@@ -217,6 +223,19 @@ void Tree::_copy(Tree const& that)
217223
m_free_tail = that.m_free_tail;
218224
m_arena_pos = that.m_arena_pos;
219225
m_arena = that.m_arena;
226+
#ifdef RYML_WITH_COMMENTS
227+
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_buf == nullptr, this, NONE);
228+
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_cap == 0, this, NONE);
229+
_RYML_ASSERT_VISIT_(m_callbacks, that.m_comments_cap >= that.m_comments_size, this, NONE);
230+
_RYML_ASSERT_VISIT_(m_callbacks, !!that.m_comments_cap == !!that.m_comments_buf, this, NONE);
231+
if(that.m_comments_size)
232+
{
233+
m_comments_buf = _RYML_CB_ALLOC_HINT(m_callbacks, CommentData, (size_t)that.m_comments_cap, that.m_comments_buf);
234+
memcpy(m_comments_buf, that.m_comments_buf, (size_t)that.m_comments_size * sizeof(CommentData));
235+
}
236+
m_comments_size = that.m_comments_size;
237+
m_comments_cap = that.m_comments_cap;
238+
#endif // RYML_WITH_COMMENTS
220239
if(that.m_arena.str)
221240
{
222241
_RYML_ASSERT_VISIT_(m_callbacks, that.m_arena.len > 0, this, NONE);
@@ -228,21 +247,12 @@ void Tree::_copy(Tree const& that)
228247
}
229248
for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
230249
m_tag_directives[i] = that.m_tag_directives[i];
231-
#ifdef RYML_WITH_COMMENTS
232-
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_buf == nullptr, this, NONE);
233-
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_cap == 0, this, NONE);
234-
if(that.m_comments_size)
235-
{
236-
_RYML_ASSERT_VISIT_(m_callbacks, that.m_comments_cap >= that.m_comments_size, this, NONE);
237-
m_comments_buf = _RYML_CB_ALLOC(m_callbacks, CommentData, that.m_comments_size);
238-
memcpy(m_comments_buf, that.m_comments_buf, (size_t)that.m_comments_size * sizeof(CommentData));
239-
}
240-
#endif // RYML_WITH_COMMENTS
241250
}
242251

243252
void Tree::_move(Tree & that) noexcept
244253
{
245254
_RYML_ASSERT_VISIT_(m_callbacks, m_buf == nullptr, this, NONE);
255+
_RYML_ASSERT_VISIT_(m_callbacks, m_cap == 0, this, NONE);
246256
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.str == nullptr, this, NONE);
247257
_RYML_ASSERT_VISIT_(m_callbacks, m_arena.len == 0, this, NONE);
248258
m_buf = that.m_buf;
@@ -252,16 +262,16 @@ void Tree::_move(Tree & that) noexcept
252262
m_free_tail = that.m_free_tail;
253263
m_arena = that.m_arena;
254264
m_arena_pos = that.m_arena_pos;
255-
for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
256-
m_tag_directives[i] = that.m_tag_directives[i];
257-
that._clear();
258265
#ifdef RYML_WITH_COMMENTS
259266
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_buf == nullptr, this, NONE);
260267
_RYML_ASSERT_VISIT_(m_callbacks, m_comments_cap == 0, this, NONE);
261268
m_comments_buf = that.m_comments_buf;
262269
m_comments_cap = that.m_comments_cap;
263270
m_comments_size = that.m_comments_size;
264271
#endif // RYML_WITH_COMMENTS
272+
for(id_type i = 0; i < RYML_MAX_TAG_DIRECTIVES; ++i)
273+
m_tag_directives[i] = that.m_tag_directives[i];
274+
that._clear();
265275
}
266276

267277
void Tree::_relocate(substr next_arena)

src/c4/yml/tree.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ class RYML_EXPORT Tree
244244
Tree(id_type node_capacity, size_t arena_capacity, Callbacks const& cb);
245245

246246
#ifdef RYML_WITH_COMMENTS
247+
Tree(id_type node_capacity, size_t arena_capacity, id_type comment_capacity);
247248
Tree(id_type node_capacity, size_t arena_capacity, id_type comment_capacity, Callbacks const& cb);
248249
#endif
249250

@@ -1144,6 +1145,8 @@ class RYML_EXPORT Tree
11441145
/** @{ */
11451146

11461147
void reserve_comments(id_type comment_capacity);
1148+
size_t comments_capacity() const { return m_comments_cap; }
1149+
size_t comments_size() const { return m_comments_size; }
11471150

11481151
CommentData const* comment(id_type node_id, comment_data_type type_flags=COMM_ANY) const;
11491152
CommentData const* comment(id_type node_id, id_type comment_id, comment_data_type type_flags=COMM_ANY) const;

test/test_lib/test_case.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ void test_arena_not_shared(Tree const& a, Tree const& b)
201201
EXPECT_FALSE(a.in_arena(td.prefix));
202202
}
203203
#ifdef RYML_WITH_COMMENTS
204+
for(id_type i = 0; i < a.m_comments_size; ++i)
205+
{
206+
EXPECT_FALSE(b.in_arena(a.m_comments_buf[i].m_text)) << i;
207+
}
208+
for(id_type i = 0; i < b.m_comments_size; ++i)
209+
{
210+
EXPECT_FALSE(a.in_arena(b.m_comments_buf[i].m_text)) << i;
211+
}
204212
#endif
205213
}
206214

0 commit comments

Comments
 (0)