|
@@ -36,71 +36,60 @@ namespace robotics {
|
|
|
}
|
|
|
/**
|
|
|
* @brief 注册消息
|
|
|
- * @tparam _Fn
|
|
|
- * @param str_topic
|
|
|
- * @param f
|
|
|
- */
|
|
|
- template<typename _Fn>
|
|
|
- void bind_multiple(const std::string& str_topic, _Fn&& f) {
|
|
|
- auto func = v3::to_function(std::forward<_Fn>(f));
|
|
|
- add_multiple(str_topic, std::move(func));
|
|
|
- }
|
|
|
- /**
|
|
|
- * @brief 注册消息
|
|
|
- * @tparam _Fn
|
|
|
- * @tparam _Obj
|
|
|
- * @param str_topic
|
|
|
- * @param f
|
|
|
- * @param obj
|
|
|
- */
|
|
|
- template<typename _Fn, typename _Obj>
|
|
|
- void bind_multiple(const std::string& str_topic, _Fn&& f, _Obj&& obj) {
|
|
|
- auto func = v3::to_function((v3::function_traits<_Fn>::stl_function_type)(std::bind_front(std::forward<_Fn>(f), std::forward<_Obj>(obj))));
|
|
|
- add_multiple(str_topic, std::move(func));
|
|
|
- }
|
|
|
- /**
|
|
|
- * @brief 注册消息
|
|
|
- * @tparam _Fn
|
|
|
- * @param str_topic
|
|
|
- * @param f
|
|
|
+ * @tparam _Fn
|
|
|
+ * @param str_topic
|
|
|
+ * @param observer 观察者
|
|
|
+ * @param f
|
|
|
+ * @return
|
|
|
*/
|
|
|
template<typename _Fn>
|
|
|
- bool bind(const std::string& str_topic, _Fn&& f) {
|
|
|
+ bool bind(const std::string& str_topic, bool observer, _Fn&& f) {
|
|
|
auto func = v3::to_function(std::forward<_Fn>(f));
|
|
|
- return add(str_topic, std::move(func));
|
|
|
+ return add(str_topic, observer, std::move(func));
|
|
|
}
|
|
|
/**
|
|
|
* @brief 注册消息
|
|
|
- * @tparam _Fn
|
|
|
- * @tparam _Obj
|
|
|
- * @param str_topic
|
|
|
- * @param f
|
|
|
- * @param obj
|
|
|
+ * @tparam _Fn
|
|
|
+ * @tparam _Obj
|
|
|
+ * @param str_topic
|
|
|
+ * @param observer 观察者
|
|
|
+ * @param f
|
|
|
+ * @param obj
|
|
|
+ * @return
|
|
|
*/
|
|
|
template<typename _Fn, typename _Obj>
|
|
|
- bool bind(const std::string& str_topic, _Fn&& f, _Obj&& obj) {
|
|
|
+ bool bind(const std::string& str_topic, bool observer, _Fn&& f, _Obj&& obj) {
|
|
|
auto func = v3::to_function((v3::function_traits<_Fn>::stl_function_type)(std::bind_front(std::forward<_Fn>(f), std::forward<_Obj>(obj))));
|
|
|
- return add(str_topic, std::move(func));
|
|
|
+ return add(str_topic, observer, std::move(func));
|
|
|
}
|
|
|
/**
|
|
|
* @brief 发送消息
|
|
|
* @param str_topic
|
|
|
*/
|
|
|
- template<typename _Ret>
|
|
|
+ template<typename _Ret,typename _BCast>
|
|
|
_Ret invoke(const std::string& str_topic) {
|
|
|
using function_type = std::function<_Ret()>;
|
|
|
std::string str_msg_type = str_topic + typeid(function_type).name();
|
|
|
auto funcs = message_bus_impl_->find(str_msg_type);
|
|
|
- if constexpr(std::is_same<_Ret, void>::value) {
|
|
|
- for(auto &fn : funcs) {
|
|
|
- std::any_cast<function_type>(fn)();
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (funcs.empty())
|
|
|
+ if constexpr (!_BCast) {
|
|
|
+ if (std::find_if(funcs.begin(), funcs.end(), [](auto it) {return !it.first; }) == funcs.end()) {
|
|
|
throw std::runtime_error("no function binded to topic " + str_topic);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if constexpr (std::is_same<_Ret, void>::value) {
|
|
|
+ for (auto& fn : funcs) {
|
|
|
+ std::any_cast<function_type>(fn.second)();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
_Ret result = _Ret{};
|
|
|
- for(auto &fn : funcs) {
|
|
|
- result = std::any_cast<function_type>(fn)();
|
|
|
+ for (auto& fn : funcs) {
|
|
|
+ if (!fn.first) {
|
|
|
+ result = std::any_cast<function_type>(fn.second)();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ std::any_cast<function_type>(fn.second)();
|
|
|
+ }
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -111,11 +100,16 @@ namespace robotics {
|
|
|
* @param str_topic
|
|
|
* @param ...args
|
|
|
*/
|
|
|
- template<typename _Ret,typename... _Args>
|
|
|
+ template<typename _Ret, typename _BCast,typename... _Args>
|
|
|
_Ret invoke(const std::string& str_topic, _Args ...args) {
|
|
|
using function_type = std::function<_Ret(_Args...)>;
|
|
|
std::string str_msg_type = str_topic + typeid(function_type).name();
|
|
|
auto funcs = message_bus_impl_->find(str_msg_type);
|
|
|
+ if constexpr (!_BCast) {
|
|
|
+ if (std::find_if(funcs.begin(), funcs.end(), [](auto it) {return !it.first; }) == funcs.end()) {
|
|
|
+ throw std::runtime_error("no function binded to topic " + str_topic);
|
|
|
+ }
|
|
|
+ }
|
|
|
if constexpr(std::is_same<_Ret, void>::value) {
|
|
|
for(auto &fn : funcs) {
|
|
|
std::any_cast<function_type>(fn)(args...);
|
|
@@ -126,7 +120,12 @@ namespace robotics {
|
|
|
throw std::runtime_error("no function binded to topic " + str_topic);
|
|
|
_Ret result = _Ret{};
|
|
|
for (auto& fn : funcs) {
|
|
|
- result = std::any_cast<function_type>(fn)(args...);
|
|
|
+ if (!fn.first) {
|
|
|
+ result = std::any_cast<function_type>(fn.second)(args...);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ std::any_cast<function_type>(fn.second)(args...);
|
|
|
+ }
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -143,17 +142,6 @@ namespace robotics {
|
|
|
message_bus_impl_->remove(str_msg_type);
|
|
|
}
|
|
|
private:
|
|
|
- /**
|
|
|
- * @brief 多个添加
|
|
|
- * @tparam _Fn
|
|
|
- * @param str_topic
|
|
|
- * @param f
|
|
|
- */
|
|
|
- template<typename _Fn>
|
|
|
- void add_multiple(const std::string& str_topic, _Fn&& f) {
|
|
|
- std::string str_msg_type = str_topic + typeid(f).name();
|
|
|
- message_bus_impl_->bind_multiple(str_msg_type, std::forward<_Fn>(f));
|
|
|
- }
|
|
|
/**
|
|
|
* @brief 单个添加
|
|
|
* @tparam _Fn
|