-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unifies data structures for data publication and subscription on local and remote layers. This will be probably sufficient for a long time. Removes `RemoteMsg` as it's replaced by `SubData`.
- Loading branch information
Showing
11 changed files
with
285 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* @file pub_sub_struct.hpp | ||
* @author Dávid Benko ([email protected]) | ||
* @brief Publication/subscription structs and callbacks | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <string> | ||
|
||
namespace kvik | ||
{ | ||
/** | ||
* @brief Subscription data structure | ||
* | ||
* Contains topic, payload and in the future maybe more details of | ||
* received data for subscription. | ||
*/ | ||
struct SubData | ||
{ | ||
std::string topic = ""; //!< Topic of message | ||
std::string payload = ""; //!< Payload of message | ||
|
||
bool operator==(const SubData &other) const; | ||
bool operator!=(const SubData &other) const; | ||
|
||
/** | ||
* @brief Converts `SubData` to printable string | ||
* | ||
* Primarily for logging purposes | ||
* | ||
* @return String representation of contained data | ||
*/ | ||
std::string toString() const; | ||
}; | ||
|
||
/** | ||
* @brief Publication data structure | ||
* | ||
* Contains topic, payload and in the future maybe more settings. | ||
*/ | ||
struct PubData | ||
{ | ||
std::string topic = ""; //!< Topic of message | ||
std::string payload = ""; //!< Payload of message | ||
|
||
bool operator==(const PubData &other) const; | ||
bool operator!=(const PubData &other) const; | ||
|
||
/** | ||
* @brief Converts `PubData` to printable string | ||
* | ||
* Primarily for logging purposes | ||
* | ||
* @return String representation of contained data | ||
*/ | ||
std::string toString() const; | ||
|
||
/** | ||
* @brief Converts `PubData` to `SubData` | ||
* | ||
* Useful when publication is immediatelly sent back as subscription. | ||
* | ||
* @return Subscription data | ||
*/ | ||
SubData toSubData() const; | ||
}; | ||
|
||
/** | ||
* @brief Subscribe callback type | ||
*/ | ||
using SubCb = std::function<void(const SubData &data)>; | ||
} // namespace kvik | ||
|
||
// Define hasher function | ||
template <> | ||
struct std::hash<kvik::PubData> | ||
{ | ||
std::size_t operator()(kvik::PubData const &data) const noexcept | ||
{ | ||
return std::hash<std::string>{}(data.topic) + | ||
std::hash<std::string>{}(data.payload); | ||
} | ||
}; | ||
|
||
// Define hasher function | ||
template <> | ||
struct std::hash<kvik::SubData> | ||
{ | ||
std::size_t operator()(kvik::SubData const &data) const noexcept | ||
{ | ||
return std::hash<std::string>{}(data.topic) + | ||
std::hash<std::string>{}(data.payload); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @file pub_sub_struct.cpp | ||
* @author Dávid Benko ([email protected]) | ||
* @brief Publication/subscription structs and callbacks | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#include <string> | ||
|
||
#include "kvik/pub_sub_struct.hpp" | ||
|
||
namespace kvik | ||
{ | ||
bool PubData::operator==(const PubData &other) const | ||
{ | ||
return topic == other.topic && | ||
payload == other.payload; | ||
} | ||
|
||
bool PubData::operator!=(const PubData &other) const | ||
{ | ||
return !this->operator==(other); | ||
} | ||
|
||
std::string PubData::toString() const | ||
{ | ||
return (!topic.empty() ? topic : "(no topic)") + " " + | ||
"(" + std::to_string(payload.length()) + " B payload)"; | ||
} | ||
|
||
SubData PubData::toSubData() const | ||
{ | ||
return { | ||
.topic = topic, | ||
.payload = payload, | ||
}; | ||
} | ||
|
||
bool SubData::operator==(const SubData &other) const | ||
{ | ||
return topic == other.topic && | ||
payload == other.payload; | ||
} | ||
|
||
bool SubData::operator!=(const SubData &other) const | ||
{ | ||
return !this->operator==(other); | ||
} | ||
|
||
std::string SubData::toString() const | ||
{ | ||
return (!topic.empty() ? topic : "(no topic)") + " " + | ||
"(" + std::to_string(payload.length()) + " B payload)"; | ||
} | ||
} // namespace kvik |
Oops, something went wrong.