Skip to content

Commit 228ddff

Browse files
authored
Merge pull request #687 from redboltz/sub_map_test
Added subscription_map tests.
2 parents a897c81 + d9daa56 commit 228ddff

File tree

6 files changed

+536
-59
lines changed

6 files changed

+536
-59
lines changed

.github/workflows/gha.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target check_deps
3737
- name: Compile
3838
env:
39-
CXXFLAGS: -Werror -g -pedantic -Wall -Wextra -Wno-ignored-qualifiers -Wconversion -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_ENABLE_SAFE_MODE -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
39+
CXXFLAGS: -Werror -pedantic -Wall -Wextra -Wno-ignored-qualifiers -Wconversion -DBOOST_ASIO_NO_DEPRECATED
4040
run: |
4141
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first
4242
- name: Test

include/mqtt/string_view.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using std::basic_string_view;
3232
#define MQTT_NO_BOOST_STRING_VIEW 0
3333

3434
#include <boost/utility/string_view.hpp>
35+
#include <boost/container_hash/hash_fwd.hpp>
3536

3637
namespace MQTT_NS {
3738

@@ -40,6 +41,14 @@ using string_view = boost::string_view;
4041
template<class CharT, class Traits = std::char_traits<CharT> >
4142
using basic_string_view = boost::basic_string_view<CharT, Traits>;
4243

44+
#if BOOST_VERSION < 106900
45+
46+
template <class charT, class traits>
47+
std::size_t hash_value(basic_string_view<charT, traits> s) {
48+
return boost::hash_range(s.begin(), s.end());
49+
}
50+
#endif // BOOST_VERSION < 106900
51+
4352
} // namespace MQTT_NS
4453

4554
#else // BOOST_VERSION >= 106100

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ ENDIF ()
5050
IF (MQTT_TEST_4)
5151
LIST (APPEND check_PROGRAMS
5252
async_pubsub_1.cpp
53-
async_pubsub_2.cpp
5453
)
5554
ENDIF ()
5655

@@ -79,6 +78,8 @@ IF (MQTT_TEST_7)
7978
as_buffer_pubsub.cpp
8079
as_buffer_async_pubsub_1.cpp
8180
as_buffer_async_pubsub_2.cpp
81+
subscription_map_broker.cpp
82+
async_pubsub_2.cpp
8283
)
8384
ENDIF ()
8485

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)