Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/gha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target check_deps
- name: Compile
env:
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
CXXFLAGS: -Werror -pedantic -Wall -Wextra -Wno-ignored-qualifiers -Wconversion -DBOOST_ASIO_NO_DEPRECATED
run: |
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first
- name: Test
Expand Down
9 changes: 9 additions & 0 deletions include/mqtt/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using std::basic_string_view;
#define MQTT_NO_BOOST_STRING_VIEW 0

#include <boost/utility/string_view.hpp>
#include <boost/container_hash/hash_fwd.hpp>

namespace MQTT_NS {

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

#if BOOST_VERSION < 106900

template <class charT, class traits>
std::size_t hash_value(basic_string_view<charT, traits> s) {
return boost::hash_range(s.begin(), s.end());
}
#endif // BOOST_VERSION < 106900

} // namespace MQTT_NS

#else // BOOST_VERSION >= 106100
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ ENDIF ()
IF (MQTT_TEST_4)
LIST (APPEND check_PROGRAMS
async_pubsub_1.cpp
async_pubsub_2.cpp
)
ENDIF ()

Expand Down Expand Up @@ -79,6 +78,8 @@ IF (MQTT_TEST_7)
as_buffer_pubsub.cpp
as_buffer_async_pubsub_1.cpp
as_buffer_async_pubsub_2.cpp
subscription_map_broker.cpp
async_pubsub_2.cpp
)
ENDIF ()

Expand Down
85 changes: 29 additions & 56 deletions test/subscription_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ class single_subscription_map

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

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

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

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

// Insert or update a value at the specified subscription path
handle insert_or_update(MQTT_NS::string_view subscription, Value value) {
// Insert a value at the specified subscription path
std::pair<handle, bool> insert(MQTT_NS::string_view subscription, Value value) {
auto path = this->find_subscription(subscription);
if (!path.empty()) {
auto &subscription_set = path.back()->second.value;
subscription_set.erase(value);
subscription_set.insert(std::move(value));
return this->path_to_handle(path);
}
else {
if (path.empty()) {
auto new_subscription_path = this->create_subscription(subscription);
new_subscription_path.back()->second.value.insert(std::move(value));
return this->path_to_handle(new_subscription_path);
return std::make_pair(this->path_to_handle(new_subscription_path), true);
}
}

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

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

auto& subscription_set = h_iter->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto hint = std::next(subscription_iter);
subscription_set.erase(subscription_iter);
subscription_set.insert(hint, std::move(value));
}
else{
subscription_set.insert(std::move(value));
}
bool insert_result = subscription_set.insert(std::move(value)).second;
return std::make_pair(h, insert_result);
}

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

// Remove the specified value
auto& subscription_set = path.back()->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto result = boost::make_optional(*subscription_iter);
subscription_set.erase(subscription_iter);

// Remove the subscription when all entries are removed
if (subscription_set.empty()) {
this->remove_subscription(path);
}
auto result = subscription_set.erase(value);

return result;
}
// If all values removed, remove the subscription
if(subscription_set.empty())
this->remove_subscription(path);

return boost::optional<Value>();
return result;
}

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

// Remove the specified value
auto& subscription_set = h_iter->second.value;
auto subscription_iter = subscription_set.find(value);
if (subscription_iter != subscription_set.end()) {
auto result = boost::make_optional(*subscription_iter);
subscription_set.erase(subscription_iter);

// Remove the subscription when all entries are removed
if (subscription_set.empty()) {
this->remove_subscription(this->handle_to_iterators(h));
}
auto result = subscription_set.erase(value);

return result;
}
// If all values removed, remove the subscription
if(subscription_set.empty())
this->remove_subscription(this->handle_to_iterators(h));

return boost::optional<Value>();
return result;
}

// Find all subscriptions that match the specified topic
Expand Down
Loading