@@ -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
119123Tree::~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
204208void 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
243252void 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
267277void Tree::_relocate (substr next_arena)
0 commit comments