78#include <unordered_map>
79#include <unordered_set>
100template <
typename FilterT>
107 std::unordered_map<NodeImpl*, NodeImpl::ReqRespCallback>;
110 std::unordered_map<NodeImpl*, NodeImpl::IntraMsgCallback>;
174 [[nodiscard]]
bool has_impl()
const;
331 template <
typename CallbackMapT,
typename CallbackT>
332 void traverse_internal_callback(
const CallbackMapT& map,
const CallbackT& callback);
334 bool has_called_{
false};
335 bool ignore_called_{
false};
337 mutable std::recursive_mutex mtx_;
344 NodeImpl* first_impl_{
nullptr};
364template <
typename FilterT>
367 using Map = std::map<FilterT, std::weak_ptr<Object>>;
368 using Set = std::unordered_set<Object*>;
381 [[nodiscard]]
bool has_object(Object* ptr)
const;
397 template <
typename ObjectT>
398 [[nodiscard]] std::shared_ptr<ObjectT>
get_object(
const FilterT& filter);
414 mutable std::mutex mtx_;
423template <
typename FilterT>
425 std::lock_guard lock(mtx_);
429 return impl_list_.emplace(impl).second;
432template <
typename FilterT>
434 std::lock_guard lock(mtx_);
436 if VUNLIKELY (impl_list_.erase(impl) == 0) {
440 if (first_impl_ == impl) {
441 first_impl_ = impl_list_.empty() ? nullptr : *impl_list_.begin();
444 server_connect_callback_map_.erase(impl);
445 sub_connect_callback_map_.erase(impl);
446 req_resp_callback_map_.erase(impl);
447 msg_callback_map_.erase(impl);
448 intra_msg_callback_map_.erase(impl);
449 status_callback_map_.erase(impl);
453template <
typename FilterT>
455 std::lock_guard lock(mtx_);
459template <
typename FilterT>
461 std::lock_guard lock(mtx_);
462 return impl_list_.find(impl) != impl_list_.end();
465template <
typename FilterT>
467 std::lock_guard lock(mtx_);
468 return !impl_list_.empty();
471template <
typename FilterT>
474 std::lock_guard lock(this->mtx_);
475 return server_connect_callback_map_.try_emplace(impl, std::move(callback)).second;
478template <
typename FilterT>
481 std::lock_guard lock(this->mtx_);
482 return sub_connect_callback_map_.try_emplace(impl, std::move(callback)).second;
485template <
typename FilterT>
487 std::lock_guard lock(this->mtx_);
488 return req_resp_callback_map_.try_emplace(impl, std::move(callback)).second;
491template <
typename FilterT>
493 std::lock_guard lock(this->mtx_);
494 return msg_callback_map_.try_emplace(impl, std::move(callback)).second;
497template <
typename FilterT>
500 std::lock_guard lock(this->mtx_);
501 return intra_msg_callback_map_.try_emplace(impl, std::move(callback)).second;
504template <
typename FilterT>
506 std::lock_guard lock(this->mtx_);
507 return status_callback_map_.try_emplace(impl, std::move(callback)).second;
510template <
typename FilterT>
512 std::lock_guard lock(this->mtx_);
513 return server_connect_callback_map_.empty();
516template <
typename FilterT>
518 std::lock_guard lock(this->mtx_);
519 return sub_connect_callback_map_.empty();
522template <
typename FilterT>
524 std::lock_guard lock(this->mtx_);
525 return req_resp_callback_map_.empty();
528template <
typename FilterT>
530 std::lock_guard lock(this->mtx_);
531 return msg_callback_map_.empty();
534template <
typename FilterT>
536 std::lock_guard lock(this->mtx_);
537 return status_callback_map_.empty();
540template <
typename FilterT>
542 this->traverse_internal_callback(server_connect_callback_map_, callback);
545template <
typename FilterT>
547 this->traverse_internal_callback(sub_connect_callback_map_, callback);
550template <
typename FilterT>
552 this->traverse_internal_callback(req_resp_callback_map_, callback);
555template <
typename FilterT>
557 this->traverse_internal_callback(msg_callback_map_, callback);
560template <
typename FilterT>
562 this->traverse_internal_callback(intra_msg_callback_map_, callback);
565template <
typename FilterT>
567 this->traverse_internal_callback(status_callback_map_, callback);
570template <
typename FilterT>
573template <
typename FilterT>
576template <
typename FilterT>
581template <
typename FilterT>
583 ignore_called_ =
true;
586template <
typename FilterT>
587template <
typename CallbackMapT,
typename CallbackT>
588inline void AbstractObject<FilterT>::traverse_internal_callback(
const CallbackMapT& map,
const CallbackT& callback) {
589 std::lock_guard lock(mtx_);
591 this->ignore_called_ =
false;
592 this->has_called_ =
false;
594 for (
const auto& [impl, target_callback] : map) {
595 callback(impl, target_callback);
598 this->ignore_called_ =
false;
600 this->has_called_ =
true;
605template <
typename FilterT>
607 std::lock_guard lock(mtx_);
608 return set_.count(ptr) > 0;
611template <
typename FilterT>
612template <
typename ObjectT>
614 static_assert(std::is_base_of_v<Object, ObjectT>,
"ObjectT must be derived from AbstractObject");
615 std::shared_ptr<ObjectT> obj;
617 std::unique_lock lock(mtx_);
619 const auto& deleter = [
this, filter](ObjectT* obj) {
621 std::lock_guard lock(mtx_);
629 auto iter = map_.find(filter);
630 if (iter == map_.end()) {
632 auto* obj_ptr =
new ObjectT(filter);
635 auto [it, inserted] = map_.try_emplace(filter, std::weak_ptr<Object>());
637 obj = std::shared_ptr<ObjectT>(obj_ptr, deleter);
639 set_.emplace(obj_ptr);
643 obj = std::static_pointer_cast<ObjectT>(it->second.lock());
646 obj = std::static_pointer_cast<ObjectT>(iter->second.lock());
652template <
typename FilterT>
655template <
typename FilterT>
virtual ~AbstractFactory()
Protected virtual destructor.
bool has_object(Object *ptr) const
Returns true if ptr is a live object tracked by this factory.
定义 abstract_factory.h:606
AbstractFactory()
Protected default constructor.
std::shared_ptr< ObjectT > get_object(const FilterT &filter)
Retrieves or creates the AbstractObject for the given filter key.
定义 abstract_factory.h:613
Per-topic registry of NodeImpl instances and their associated callbacks.
定义 abstract_factory.h:101
bool add_impl(NodeImpl *impl)
Registers a NodeImpl instance with this topic object.
定义 abstract_factory.h:424
std::unordered_map< NodeImpl *, NodeImpl::ConnectCallback > ConnectCallbackMap
Per-impl connect callbacks.
定义 abstract_factory.h:105
bool req_resp_map_is_empty() const
Returns true if no request/response callbacks are registered.
定义 abstract_factory.h:523
NodeImpl * get_first_impl() const
Returns the most recently added NodeImpl pointer.
定义 abstract_factory.h:454
bool register_msg_callback(NodeImpl *impl, NodeImpl::MsgCallback &&callback)
Registers a serialised-message receive callback for impl.
定义 abstract_factory.h:492
bool server_connect_map_is_empty() const
Returns true if no server-connect callbacks are registered.
定义 abstract_factory.h:511
std::unordered_map< NodeImpl *, NodeImpl::IntraMsgCallback > IntraMsgCallbackMap
Per-impl intra-msg callbacks.
定义 abstract_factory.h:109
void traverse_server_connect_callback(const FindConnectCallback &callback)
Invokes callback for each registered server-connect callback.
定义 abstract_factory.h:541
bool register_req_resp_callback(NodeImpl *impl, NodeImpl::ReqRespCallback &&callback)
Registers a request/response callback for impl.
定义 abstract_factory.h:486
bool has_called() const
定义 abstract_factory.h:577
bool sub_connect_map_is_empty() const
Returns true if no subscriber-connect callbacks are registered.
定义 abstract_factory.h:517
bool remove_impl(NodeImpl *impl)
Unregisters a NodeImpl instance and removes all its callbacks.
定义 abstract_factory.h:433
bool register_server_connect_callback(NodeImpl *impl, NodeImpl::ConnectCallback &&callback)
Registers a server-side connect-change callback for impl.
定义 abstract_factory.h:472
std::function< void( NodeImpl *, const NodeImpl::IntraMsgCallback &)> FindIntraMsgCallback
Traversal visitor for intra-msg callbacks.
定义 abstract_factory.h:119
std::unordered_set< NodeImpl * > ImplList
Set of registered impl pointers.
定义 abstract_factory.h:103
bool status_map_is_empty() const
Returns true if no status callbacks are registered.
定义 abstract_factory.h:535
std::unordered_map< NodeImpl *, NodeImpl::ReqRespCallback > ReqRespCallbackMap
Per-impl req/resp callbacks.
定义 abstract_factory.h:106
std::unordered_map< NodeImpl *, NodeImpl::StatusCallback > StatusCallbackMap
Per-impl status callbacks.
定义 abstract_factory.h:111
bool register_sub_connect_callback(NodeImpl *impl, NodeImpl::ConnectCallback &&callback)
Registers a subscriber-side connect-change callback for impl.
定义 abstract_factory.h:479
bool register_intra_msg_callback(NodeImpl *impl, NodeImpl::IntraMsgCallback &&callback)
Registers an in-process zero-copy message callback for impl.
定义 abstract_factory.h:498
bool msg_map_is_empty() const
Returns true if no message callbacks are registered.
定义 abstract_factory.h:529
void ignore_called()
定义 abstract_factory.h:582
void traverse_intra_msg_callback(const FindIntraMsgCallback &callback)
Invokes callback for each registered in-process message callback.
定义 abstract_factory.h:561
std::function< void(NodeImpl *, const NodeImpl::ConnectCallback &)> FindConnectCallback
Traversal visitor for connect callbacks.
定义 abstract_factory.h:113
std::function< void(NodeImpl *, const NodeImpl::ReqRespCallback &)> FindReqRespCallback
Traversal visitor for req/resp callbacks.
定义 abstract_factory.h:115
std::function< void(NodeImpl *, const NodeImpl::MsgCallback &)> FindMsgCallback
Traversal visitor for message callbacks.
定义 abstract_factory.h:117
void traverse_status_callback(const FindStatusCallback &callback)
Invokes callback for each registered status callback.
定义 abstract_factory.h:566
void traverse_sub_connect_callback(const FindConnectCallback &callback)
Invokes callback for each registered subscriber-connect callback.
定义 abstract_factory.h:546
std::function< void(NodeImpl *, const NodeImpl::StatusCallback &)> FindStatusCallback
Traversal visitor for status callbacks.
定义 abstract_factory.h:121
bool has_impl() const
Returns true if at least one NodeImpl is currently registered.
定义 abstract_factory.h:466
void traverse_req_resp_callback(const FindReqRespCallback &callback)
Invokes callback for each registered request/response callback.
定义 abstract_factory.h:551
std::unordered_map< NodeImpl *, NodeImpl::MsgCallback > MsgCallbackMap
Per-impl message callbacks.
定义 abstract_factory.h:108
bool is_contains_impl(NodeImpl *impl) const
Returns true if impl is currently registered with this object.
定义 abstract_factory.h:460
~AbstractObject() override
void traverse_msg_callback(const FindMsgCallback &callback)
Invokes callback for each registered serialised-message callback.
定义 abstract_factory.h:556
bool register_status_callback(NodeImpl *impl, NodeImpl::StatusCallback &&callback)
Registers a transport-status callback for impl.
定义 abstract_factory.h:505
Abstract base for all VLink transport backend node implementations.
定义 node_impl.h:141
std::function< void(uint64_t, const Bytes &, Bytes *)> ReqRespCallback
Callback for ServerImpl request/response processing.
定义 node_impl.h:170
std::function< void(const Status::BasePtr &ptr)> StatusCallback
Callback invoked on DDS status events (e.g. deadline missed).
定义 node_impl.h:155
std::function< void(const IntraData &)> IntraMsgCallback
Callback delivering an in-process IntraData message.
定义 node_impl.h:186
std::function< void(bool)> ConnectCallback
Callback invoked when the peer connection state changes.
定义 node_impl.h:148
std::function< void(const Bytes &)> MsgCallback
Callback delivering a raw serialised message to a SubscriberImpl or GetterImpl.
定义 node_impl.h:177
Global singleton logger with three output styles and pluggable backends.
#define VUNLIKELY(...)
Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.
定义 macros.h:302
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
定义 macros.h:184
Abstract transport node base classes used by all VLink node implementations.