@@ -324,7 +324,7 @@ namespace osmium {
324
324
325
325
class ChangesetDiscussionBuilder : public Builder {
326
326
327
- osmium::ChangesetComment* m_comment = nullptr ;
327
+ std:: size_t m_comment_offset = std::numeric_limits<std:: size_t >::max() ;
328
328
329
329
void add_user (osmium::ChangesetComment& comment, const char * user, const std::size_t length) {
330
330
if (length > osmium::max_osm_string_length) {
@@ -343,6 +343,20 @@ namespace osmium {
343
343
add_padding (true );
344
344
}
345
345
346
+ bool has_open_comment () const noexcept {
347
+ return m_comment_offset != std::numeric_limits<std::size_t >::max ();
348
+ }
349
+
350
+ // Get current comment pointer (recalculated each time to handle buffer reallocation)
351
+ osmium::ChangesetComment* get_comment_ptr () {
352
+ if (!has_open_comment ()) {
353
+ return nullptr ;
354
+ }
355
+ return reinterpret_cast <osmium::ChangesetComment*>(
356
+ buffer ().data () + buffer ().committed () + m_comment_offset
357
+ );
358
+ }
359
+
346
360
public:
347
361
348
362
explicit ChangesetDiscussionBuilder (osmium::memory::Buffer& buffer, Builder* parent = nullptr ) :
@@ -362,32 +376,46 @@ namespace osmium {
362
376
ChangesetDiscussionBuilder& operator =(ChangesetDiscussionBuilder&&) = delete ;
363
377
364
378
~ChangesetDiscussionBuilder () {
365
- assert (!m_comment && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
379
+ assert (!has_open_comment () && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
366
380
add_padding ();
367
381
}
368
382
369
383
void add_comment (osmium::Timestamp date, osmium::user_id_type uid, const char * user) {
370
- assert (!m_comment && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
371
- m_comment = reserve_space_for<osmium::ChangesetComment>();
372
- new (m_comment) osmium::ChangesetComment{date, uid};
384
+ assert (!has_open_comment () && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
385
+
386
+ // Store offset instead of pointer to handle buffer reallocation
387
+ m_comment_offset = buffer ().written () - buffer ().committed ();
388
+
389
+ auto * comment = reserve_space_for<osmium::ChangesetComment>();
390
+ new (comment) osmium::ChangesetComment{date, uid};
373
391
add_size (sizeof (ChangesetComment));
374
- add_user (*m_comment, user, std::strlen (user));
392
+
393
+ add_user (*comment, user, std::strlen (user));
375
394
}
376
395
377
396
void add_comment_text (const char * text) {
378
- assert (m_comment && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
379
- osmium::ChangesetComment& comment = *m_comment;
380
- m_comment = nullptr ;
381
- add_text (comment, text, std::strlen (text));
397
+ assert (has_open_comment () && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
398
+
399
+ // Get fresh pointer each time to handle buffer reallocation
400
+ auto * comment = get_comment_ptr ();
401
+
402
+ // Invalidate offset to ensure right adding order
403
+ m_comment_offset = std::numeric_limits<std::size_t >::max ();
404
+
405
+ add_text (*comment, text, std::strlen (text));
382
406
}
383
407
384
408
void add_comment_text (const std::string& text) {
385
- assert (m_comment && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
386
- osmium::ChangesetComment& comment = *m_comment;
387
- m_comment = nullptr ;
388
- add_text (comment, text.c_str (), text.size ());
409
+ assert (has_open_comment () && " You have to always call both add_comment() and then add_comment_text() in that order for each comment!" );
410
+
411
+ // Get fresh pointer each time to handle buffer reallocation
412
+ auto * comment = get_comment_ptr ();
413
+
414
+ // Invalidate offset to ensure right adding order
415
+ m_comment_offset = std::numeric_limits<std::size_t >::max ();
416
+
417
+ add_text (*comment, text.c_str (), text.size ());
389
418
}
390
-
391
419
}; // class ChangesetDiscussionBuilder
392
420
393
421
#define OSMIUM_FORWARD (setter ) \
0 commit comments