Skip to content

Commit 2d50803

Browse files
authored
Merge pull request #688 from kleunen/sub_map_test
Renamed insert_or_update and update from multiple_subscription_map bo…
2 parents d2e512e + e96095a commit 2d50803

File tree

2 files changed

+37
-65
lines changed

2 files changed

+37
-65
lines changed

test/subscription_map.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// Distributed under the Boost Software License, Version 1.0.
44
// (See accompanying file LICENSE_1_0.txt or copy at
55
// http://www.boost.org/LICENSE_1_0.txt)
6-
76
#include "test_main.hpp"
87
#include "combi_test.hpp"
98
#include "checker.hpp"
@@ -199,7 +198,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
199198
idx.modify(
200199
it,
201200
[&](sub_con_online& e) {
202-
e.h = h; // update handle
201+
e.h = h.first; // update handle
203202
}
204203
);
205204
BOOST_TEST(!elem.h.empty());
@@ -217,7 +216,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
217216
idx.modify(
218217
it,
219218
[&](sub_con_online& e) {
220-
e.h = h; // update handle
219+
e.h = h.first; // update handle
221220
}
222221
);
223222
BOOST_TEST(!elem.h.empty());
@@ -256,7 +255,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
256255
auto& idx = scos.get<tag_con_topic_filter>();
257256
auto it = idx.find(std::make_tuple(con1, "a/b/c"_mb));
258257
BOOST_TEST((it != idx.end()));
259-
m.remove(it->h, *it);
258+
m.erase(it->h, *it);
260259
idx.erase(it);
261260
m.find(
262261
"a/b/c",
@@ -293,7 +292,7 @@ BOOST_AUTO_TEST_CASE( multi_non_wc_crud ) {
293292
auto const& elem = *it;
294293
// call remove for each handle - Value pairs
295294
// it is a little inefficient but still enough efficient
296-
m.remove(elem.h, elem);
295+
m.erase(elem.h, elem);
297296
++it;
298297
}
299298
idx.erase(r.first, r.second);
@@ -359,7 +358,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
359358
idx.modify(
360359
it,
361360
[&](sub_con_online& e) {
362-
e.h = h; // update handle
361+
e.h = h.first; // update handle
363362
}
364363
);
365364
BOOST_TEST(!elem.h.empty());
@@ -377,7 +376,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
377376
idx.modify(
378377
it,
379378
[&](sub_con_online& e) {
380-
e.h = h; // update handle
379+
e.h = h.first; // update handle
381380
}
382381
);
383382
BOOST_TEST(!elem.h.empty());
@@ -436,7 +435,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
436435
auto& idx = scos.get<tag_con_topic_filter>();
437436
auto it = idx.find(std::make_tuple(con1, "a/+/c"_mb));
438437
BOOST_TEST((it != idx.end()));
439-
m.remove(it->h, *it);
438+
m.erase(it->h, *it);
440439
idx.erase(it);
441440
m.find(
442441
"a/b/c",
@@ -472,7 +471,7 @@ BOOST_AUTO_TEST_CASE( multi_wc_crud ) {
472471
auto const& elem = *it;
473472
// call remove for each handle - Value pairs
474473
// it is a little inefficient but still enough efficient
475-
m.remove(elem.h, elem);
474+
m.erase(elem.h, elem);
476475
++it;
477476
}
478477
idx.erase(r.first, r.second);

test/subscription_map.hpp

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ class single_subscription_map
309309

310310
// Insert a value at the specified subscription path
311311
handle insert(MQTT_NS::string_view subscription, Value value) {
312-
if (!this->find_subscription(subscription).empty()) {
313-
throw std::runtime_error(std::string("Subscription already exists in map: ").append(subscription.data(), subscription.size()));
314-
}
312+
auto existing_subscription = this->find_subscription(subscription);
313+
if (!existing_subscription.empty())
314+
throw std::runtime_error("Subscription already exists in map");
315315

316316
auto new_subscription_path = this->create_subscription(subscription);
317317
new_subscription_path.back()->second.value = std::move(value);
@@ -337,7 +337,7 @@ class single_subscription_map
337337
}
338338

339339
// Remove a value at the specified subscription path
340-
void remove(MQTT_NS::string_view subscription) {
340+
void erase(MQTT_NS::string_view subscription) {
341341
auto path = this->find_subscription(subscription);
342342
if (path.empty()) {
343343
return;
@@ -347,7 +347,7 @@ class single_subscription_map
347347
}
348348

349349
// Remove a value using a handle
350-
void remove(handle h) {
350+
void erase(handle h) {
351351
auto path = this->handle_to_iterators(h);
352352
if (!path.empty()) {
353353
this->remove_subscription(path);
@@ -371,29 +371,22 @@ class multiple_subscription_map
371371
// Handle of an entry
372372
using handle = typename subscription_map_base< Value >::handle;
373373

374-
// Insert or update a value at the specified subscription path
375-
handle insert_or_update(MQTT_NS::string_view subscription, Value value) {
374+
// Insert a value at the specified subscription path
375+
std::pair<handle, bool> insert(MQTT_NS::string_view subscription, Value value) {
376376
auto path = this->find_subscription(subscription);
377-
if (!path.empty()) {
378-
auto &subscription_set = path.back()->second.value;
379-
subscription_set.erase(value);
380-
subscription_set.insert(std::move(value));
381-
return this->path_to_handle(path);
382-
}
383-
else {
377+
if (path.empty()) {
384378
auto new_subscription_path = this->create_subscription(subscription);
385379
new_subscription_path.back()->second.value.insert(std::move(value));
386-
return this->path_to_handle(new_subscription_path);
380+
return std::make_pair(this->path_to_handle(new_subscription_path), true);
387381
}
388-
}
389382

390-
// Insert a value at the specified subscription path
391-
handle insert(MQTT_NS::string_view subscription, Value const& value) {
392-
return insert_or_update(subscription, value);
383+
auto &subscription_set = path.back()->second.value;
384+
bool insert_result = subscription_set.insert(std::move(value)).second;
385+
return std::make_pair(this->path_to_handle(path), insert_result);
393386
}
394387

395-
// Update an existing entry using a handle
396-
void update(handle h, Value value) {
388+
// Insert a value with a handle to the subscription
389+
std::pair<handle, bool> insert(handle h, Value value) {
397390
if (h.empty()) {
398391
throw std::runtime_error("Invalid handle was specified");
399392
}
@@ -405,20 +398,13 @@ class multiple_subscription_map
405398
}
406399

407400
auto& subscription_set = h_iter->second.value;
408-
auto subscription_iter = subscription_set.find(value);
409-
if (subscription_iter != subscription_set.end()) {
410-
auto hint = std::next(subscription_iter);
411-
subscription_set.erase(subscription_iter);
412-
subscription_set.insert(hint, std::move(value));
413-
}
414-
else{
415-
subscription_set.insert(std::move(value));
416-
}
401+
bool insert_result = subscription_set.insert(std::move(value)).second;
402+
return std::make_pair(h, insert_result);
417403
}
418404

419405
// Remove a value at the specified subscription path
420406
// returns the value of the removed element (if found)
421-
boost::optional<Value> remove(MQTT_NS::string_view subscription, Value const& value) {
407+
size_t erase(MQTT_NS::string_view subscription, Value const& value) {
422408
// Find the subscription in the map
423409
auto path = this->find_subscription(subscription);
424410
if (path.empty()) {
@@ -427,25 +413,18 @@ class multiple_subscription_map
427413

428414
// Remove the specified value
429415
auto& subscription_set = path.back()->second.value;
430-
auto subscription_iter = subscription_set.find(value);
431-
if (subscription_iter != subscription_set.end()) {
432-
auto result = boost::make_optional(*subscription_iter);
433-
subscription_set.erase(subscription_iter);
434-
435-
// Remove the subscription when all entries are removed
436-
if (subscription_set.empty()) {
437-
this->remove_subscription(path);
438-
}
416+
auto result = subscription_set.erase(value);
439417

440-
return result;
441-
}
418+
// If all values removed, remove the subscription
419+
if(subscription_set.empty())
420+
this->remove_subscription(path);
442421

443-
return boost::optional<Value>();
422+
return result;
444423
}
445424

446425
// Remove a value at the specified handle
447426
// returns the value of the removed element (if found)
448-
boost::optional<Value> remove(handle h, Value const& value) {
427+
size_t erase(handle h, Value const& value) {
449428
if (h.empty()) {
450429
throw std::runtime_error("Invalid handle was specified");
451430
}
@@ -456,21 +435,15 @@ class multiple_subscription_map
456435
throw std::runtime_error("Invalid handle was specified");
457436
}
458437

438+
// Remove the specified value
459439
auto& subscription_set = h_iter->second.value;
460-
auto subscription_iter = subscription_set.find(value);
461-
if (subscription_iter != subscription_set.end()) {
462-
auto result = boost::make_optional(*subscription_iter);
463-
subscription_set.erase(subscription_iter);
464-
465-
// Remove the subscription when all entries are removed
466-
if (subscription_set.empty()) {
467-
this->remove_subscription(this->handle_to_iterators(h));
468-
}
440+
auto result = subscription_set.erase(value);
469441

470-
return result;
471-
}
442+
// If all values removed, remove the subscription
443+
if(subscription_set.empty())
444+
this->remove_subscription(this->handle_to_iterators(h));
472445

473-
return boost::optional<Value>();
446+
return result;
474447
}
475448

476449
// Find all subscriptions that match the specified topic

0 commit comments

Comments
 (0)