Skip to content

Commit ff160aa

Browse files
committed
Add clear_user() function to OSMObject and Changeset.
1 parent 63a6cf4 commit ff160aa

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

include/osmium/osm/changeset.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ namespace osmium {
366366
return reinterpret_cast<const char*>(data() + sizeof(Changeset));
367367
}
368368

369+
/// Clear user name.
370+
void clear_user() noexcept {
371+
std::memset(data() + sizeof(Changeset), 0, user_size());
372+
}
373+
369374
/// Get the list of tags.
370375
const TagList& tags() const {
371376
return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());

include/osmium/osm/object.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ namespace osmium {
318318
return reinterpret_cast<const char*>(data() + sizeof_object());
319319
}
320320

321+
/// Clear user name.
322+
void clear_user() noexcept {
323+
std::memset(data() + sizeof_object(), 0, user_size());
324+
}
325+
321326
/// Get the list of tags for this object.
322327
const TagList& tags() const {
323328
return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());

test/t/builder/test_object_builder.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,40 @@ TEST_CASE("set_user with length on changeset") {
441441
REQUIRE(std::string{"user"} == changeset.user());
442442
}
443443

444+
TEST_CASE("clear_user should clear the user field but nothing else") {
445+
osmium::memory::Buffer buffer{1024 * 10};
446+
std::string user = "user";
447+
448+
{
449+
osmium::builder::NodeBuilder builder{buffer};
450+
builder.set_id(17)
451+
.set_visible(true)
452+
.set_version(1)
453+
.set_changeset(123)
454+
.set_uid(555)
455+
.set_timestamp("2015-07-01T00:00:01Z")
456+
.set_location(osmium::Location{1.2, 3.4})
457+
.set_user(user);
458+
builder.add_tags({{"highway", "primary"}, {"oneway", "yes"}});
459+
}
460+
461+
auto& node = buffer.get<osmium::Node>(buffer.commit());
462+
463+
REQUIRE(std::string{"user"} == node.user());
464+
465+
node.clear_user();
466+
467+
REQUIRE(std::string{""} == node.user());
468+
REQUIRE(node.uid() == 555);
469+
REQUIRE(node.tags().size() == 2);
470+
471+
auto it = node.tags().begin();
472+
REQUIRE(it->key() == std::string{"highway"});
473+
REQUIRE(it->value() == std::string{"primary"});
474+
++it;
475+
REQUIRE(it->key() == std::string{"oneway"});
476+
REQUIRE(it->value() == std::string{"yes"});
477+
++it;
478+
REQUIRE(it == node.tags().end());
479+
}
480+

test/t/osm/test_changeset.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,45 @@ TEST_CASE("Create changeset without helper") {
142142
REQUIRE(++cit == cs.discussion().end());
143143
}
144144

145+
TEST_CASE("Change changeset") {
146+
osmium::memory::Buffer buffer{10 * 1000};
147+
148+
osmium::builder::add_changeset(buffer,
149+
_cid(42),
150+
_created_at(time_t(100)),
151+
_closed_at(time_t(200)),
152+
_num_changes(7),
153+
_num_comments(3),
154+
_uid(9),
155+
_user("user"),
156+
_tag("comment", "foo")
157+
);
158+
159+
osmium::Changeset& cs = buffer.get<osmium::Changeset>(0);
160+
161+
cs.set_id(12);
162+
cs.set_created_at(time_t(200));
163+
cs.set_closed_at(time_t(300));
164+
cs.set_num_changes(3);
165+
cs.set_num_comments(4);
166+
cs.set_uid(10);
167+
cs.clear_user();
168+
169+
REQUIRE(12 == cs.id());
170+
REQUIRE(10 == cs.uid());
171+
REQUIRE(3 == cs.num_changes());
172+
REQUIRE(4 == cs.num_comments());
173+
REQUIRE(cs.closed());
174+
REQUIRE(osmium::Timestamp{200} == cs.created_at());
175+
REQUIRE(osmium::Timestamp{300} == cs.closed_at());
176+
REQUIRE(1 == cs.tags().size());
177+
REQUIRE(std::string("") == cs.user());
178+
REQUIRE(cs.tags().size() == 1);
179+
180+
auto it = cs.tags().begin();
181+
REQUIRE(it->key() == std::string{"comment"});
182+
REQUIRE(it->value() == std::string{"foo"});
183+
++it;
184+
REQUIRE(it == cs.tags().end());
185+
}
186+

0 commit comments

Comments
 (0)