@@ -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