107 #include <unordered_set>
111 #include "../base/functional.h"
112 #include "../base/logger.h"
135 template <
typename FilterT>
208 [[nodiscard]]
bool has_impl()
const;
390 struct TraverseGuard final {
394 --
object.traverse_depth_;
396 if VLIKELY (
object.traverse_depth_ == 0) {
397 object.apply_deferred_removals();
402 template <
typename CallbackMapT,
typename CallbackT>
403 bool register_internal_callback(CallbackMapT& map, NodeImpl* impl, CallbackT&& callback);
405 template <
typename CallbackMapT>
406 [[nodiscard]]
bool is_map_effectively_empty(
const CallbackMapT& map)
const;
408 template <
typename CallbackMapT,
typename CallbackT>
409 void traverse_internal_callback(
const CallbackMapT& map,
const CallbackT& callback);
411 template <
typename CallbackMapT,
typename CallbackT>
412 bool invoke_internal_callback(
const CallbackMapT& map, NodeImpl* owner,
const CallbackT& callback);
414 [[nodiscard]]
bool is_deferred_removed(NodeImpl* impl)
const;
416 void erase_impl_callbacks(NodeImpl* impl);
418 void apply_deferred_removals();
420 bool has_called_{
false};
421 bool ignore_called_{
false};
422 size_t traverse_depth_{0};
424 std::vector<NodeImpl*> deferred_remove_list_;
425 mutable std::recursive_mutex mtx_;
432 NodeImpl* first_impl_{
nullptr};
453 template <
typename FilterT>
456 using Map = std::map<FilterT, std::weak_ptr<Object>>;
457 using Set = std::unordered_set<Object*>;
483 template <
typename ObjectT>
484 [[nodiscard]] std::shared_ptr<ObjectT>
get_object(
const FilterT& filter);
500 mutable std::mutex mtx_;
509 template <
typename FilterT>
511 std::lock_guard lock(mtx_);
513 if VUNLIKELY (is_deferred_removed(impl)) {
519 return impl_list_.emplace(impl).second;
522 template <
typename FilterT>
524 std::lock_guard lock(mtx_);
527 if VUNLIKELY (impl_list_.find(impl) == impl_list_.end() || is_deferred_removed(impl)) {
531 deferred_remove_list_.push_back(impl);
533 if (first_impl_ == impl) {
534 first_impl_ =
nullptr;
540 if VUNLIKELY (impl_list_.erase(impl) == 0) {
544 if (first_impl_ == impl) {
545 first_impl_ = impl_list_.empty() ? nullptr : *impl_list_.begin();
548 erase_impl_callbacks(impl);
553 template <
typename FilterT>
555 std::lock_guard lock(mtx_);
559 template <
typename FilterT>
561 std::lock_guard lock(mtx_);
562 return impl_list_.find(impl) != impl_list_.end() && !is_deferred_removed(impl);
565 template <
typename FilterT>
567 std::lock_guard lock(mtx_);
569 if VLIKELY (deferred_remove_list_.empty()) {
570 return !impl_list_.empty();
573 for (
auto* impl : impl_list_) {
574 if (!is_deferred_removed(impl)) {
582 template <
typename FilterT>
585 return register_internal_callback(server_connect_callback_map_, impl, std::move(callback));
588 template <
typename FilterT>
591 return register_internal_callback(sub_connect_callback_map_, impl, std::move(callback));
594 template <
typename FilterT>
596 return register_internal_callback(req_resp_callback_map_, impl, std::move(callback));
599 template <
typename FilterT>
601 return register_internal_callback(msg_callback_map_, impl, std::move(callback));
604 template <
typename FilterT>
607 return register_internal_callback(intra_msg_callback_map_, impl, std::move(callback));
610 template <
typename FilterT>
612 return register_internal_callback(status_callback_map_, impl, std::move(callback));
615 template <
typename FilterT>
617 std::lock_guard lock(this->mtx_);
618 return is_map_effectively_empty(server_connect_callback_map_);
621 template <
typename FilterT>
623 std::lock_guard lock(this->mtx_);
624 return is_map_effectively_empty(sub_connect_callback_map_);
627 template <
typename FilterT>
629 std::lock_guard lock(this->mtx_);
630 return is_map_effectively_empty(req_resp_callback_map_);
633 template <
typename FilterT>
635 std::lock_guard lock(this->mtx_);
636 return is_map_effectively_empty(msg_callback_map_);
639 template <
typename FilterT>
641 std::lock_guard lock(this->mtx_);
642 return is_map_effectively_empty(intra_msg_callback_map_);
645 template <
typename FilterT>
647 std::lock_guard lock(this->mtx_);
648 return is_map_effectively_empty(status_callback_map_);
651 template <
typename FilterT>
653 this->traverse_internal_callback(server_connect_callback_map_, callback);
656 template <
typename FilterT>
658 this->traverse_internal_callback(sub_connect_callback_map_, callback);
661 template <
typename FilterT>
663 this->traverse_internal_callback(req_resp_callback_map_, callback);
666 template <
typename FilterT>
668 this->traverse_internal_callback(msg_callback_map_, callback);
671 template <
typename FilterT>
673 this->traverse_internal_callback(intra_msg_callback_map_, callback);
676 template <
typename FilterT>
678 this->traverse_internal_callback(status_callback_map_, callback);
681 template <
typename FilterT>
682 template <
typename CallbackMapT,
typename CallbackT>
684 const CallbackT& callback) {
685 std::lock_guard lock(mtx_);
687 auto iter = map.find(owner);
689 if VUNLIKELY (iter == map.end() || is_deferred_removed(owner)) {
695 TraverseGuard guard{*
this};
697 callback(iter->second);
702 template <
typename FilterT>
704 return invoke_internal_callback(msg_callback_map_, owner, [&bytes](
const auto& callback) { callback(bytes); });
707 template <
typename FilterT>
709 return invoke_internal_callback(req_resp_callback_map_, owner,
710 [owner, &callback](
const auto& target) { callback(owner, target); });
713 template <
typename FilterT>
715 std::lock_guard lock(mtx_);
717 if VUNLIKELY (impl_list_.find(owner) == impl_list_.end() || is_deferred_removed(owner)) {
723 TraverseGuard guard{*
this};
730 template <
typename FilterT>
733 template <
typename FilterT>
736 template <
typename FilterT>
741 template <
typename FilterT>
743 ignore_called_ =
true;
746 template <
typename FilterT>
747 template <
typename CallbackMapT,
typename CallbackT>
749 CallbackT&& callback) {
750 std::lock_guard lock(mtx_);
752 if VUNLIKELY (is_deferred_removed(impl)) {
756 return map.try_emplace(impl, std::forward<CallbackT>(callback)).second;
759 template <
typename FilterT>
760 template <
typename CallbackMapT>
761 inline bool AbstractObject<FilterT>::is_map_effectively_empty(
const CallbackMapT& map)
const {
762 if VLIKELY (deferred_remove_list_.empty()) {
766 for (
const auto& item : map) {
767 if (!is_deferred_removed(item.first)) {
775 template <
typename FilterT>
776 template <
typename CallbackMapT,
typename CallbackT>
777 inline void AbstractObject<FilterT>::traverse_internal_callback(
const CallbackMapT& map,
const CallbackT& callback) {
778 std::lock_guard lock(mtx_);
780 this->ignore_called_ =
false;
781 this->has_called_ =
false;
785 TraverseGuard guard{*
this};
787 for (
const auto& [impl, target_callback] : map) {
788 if VUNLIKELY (is_deferred_removed(impl)) {
792 callback(impl, target_callback);
795 this->ignore_called_ =
false;
797 this->has_called_ =
true;
802 template <
typename FilterT>
803 inline bool AbstractObject<FilterT>::is_deferred_removed(NodeImpl* impl)
const {
804 for (
auto* target : deferred_remove_list_) {
805 if (target == impl) {
813 template <
typename FilterT>
814 inline void AbstractObject<FilterT>::erase_impl_callbacks(NodeImpl* impl) {
815 server_connect_callback_map_.erase(impl);
816 sub_connect_callback_map_.erase(impl);
817 req_resp_callback_map_.erase(impl);
818 msg_callback_map_.erase(impl);
819 intra_msg_callback_map_.erase(impl);
820 status_callback_map_.erase(impl);
823 template <
typename FilterT>
824 inline void AbstractObject<FilterT>::apply_deferred_removals() {
825 if VLIKELY (deferred_remove_list_.empty()) {
829 for (
auto* impl : deferred_remove_list_) {
830 impl_list_.erase(impl);
831 erase_impl_callbacks(impl);
834 deferred_remove_list_.clear();
836 if (first_impl_ ==
nullptr && !impl_list_.empty()) {
837 first_impl_ = *impl_list_.begin();
841 template <
typename FilterT>
843 std::lock_guard lock(mtx_);
844 return set_.count(ptr) > 0;
847 template <
typename FilterT>
848 template <
typename ObjectT>
850 static_assert(std::is_base_of_v<Object, ObjectT>,
"ObjectT must be derived from AbstractObject");
851 std::shared_ptr<ObjectT> obj;
853 std::unique_lock lock(mtx_);
855 const auto& deleter = [
this, filter](ObjectT* obj) {
857 std::lock_guard lock(mtx_);
861 auto iter = map_.find(filter);
863 if VUNLIKELY (iter != map_.end() && iter->second.expired()) {
871 auto iter = map_.find(filter);
873 if (iter != map_.end()) {
874 obj = std::static_pointer_cast<ObjectT>(iter->second.lock());
885 auto* obj_ptr =
new ObjectT(filter);
888 auto [it, inserted] = map_.try_emplace(filter, std::weak_ptr<Object>());
891 obj = std::static_pointer_cast<ObjectT>(it->second.lock());
894 if (inserted || !obj) {
897 it = map_.try_emplace(filter, std::weak_ptr<Object>()).first;
900 obj = std::shared_ptr<ObjectT>(obj_ptr, deleter);
902 set_.emplace(obj_ptr);
911 template <
typename FilterT>
914 template <
typename FilterT>
Lazily allocates and caches AbstractObject instances keyed by FilterT.
Definition: abstract_factory.h:454
virtual ~AbstractFactory()
Destroys the factory and releases the cache.
bool has_object(Object *ptr) const
Tests whether ptr corresponds to a live object created by this factory.
Definition: abstract_factory.h:842
AbstractFactory()
Constructs an empty factory.
std::shared_ptr< ObjectT > get_object(const FilterT &filter)
Looks up or creates the ObjectT registered against filter.
Definition: abstract_factory.h:849
Tiny mix-in that exposes the backend-native handle behind a node.
Definition: node_impl.h:123
Topic-scoped fan-out store of NodeImpl peers and their callbacks.
Definition: abstract_factory.h:136
bool add_impl(NodeImpl *impl)
Registers impl as an active peer on this topic.
Definition: abstract_factory.h:510
bool req_resp_map_is_empty() const
Reports whether the request/response dictionary is empty.
Definition: abstract_factory.h:628
NodeImpl * get_first_impl() const
Returns the most recently registered peer.
Definition: abstract_factory.h:554
bool register_msg_callback(NodeImpl *impl, NodeImpl::MsgCallback &&callback)
Stores callback as the serialised-message handler for impl.
Definition: abstract_factory.h:600
bool server_connect_map_is_empty() const
Reports whether the server-connect dictionary is empty.
Definition: abstract_factory.h:616
void traverse_server_connect_callback(const FindConnectCallback &callback)
Walks the server-connect dictionary and invokes callback for every entry.
Definition: abstract_factory.h:652
bool register_req_resp_callback(NodeImpl *impl, NodeImpl::ReqRespCallback &&callback)
Stores callback as the request/response handler for impl.
Definition: abstract_factory.h:595
std::map< NodeImpl *, NodeImpl::MsgCallback > MsgCallbackMap
Message callbacks, keyed by impl.
Definition: abstract_factory.h:142
bool invoke_callback(NodeImpl *owner, const Function< void()> &callback)
Runs a callback only while its owner remains registered.
Definition: abstract_factory.h:714
bool has_called() const
Definition: abstract_factory.h:737
bool sub_connect_map_is_empty() const
Reports whether the subscriber-connect dictionary is empty.
Definition: abstract_factory.h:622
bool invoke_req_resp_callback(NodeImpl *owner, const FindReqRespCallback &callback)
Visits a registered owner's request handler while keeping its registration live.
Definition: abstract_factory.h:708
bool remove_impl(NodeImpl *impl)
Removes impl from the peer set and forgets every associated callback.
Definition: abstract_factory.h:523
std::map< NodeImpl *, NodeImpl::ConnectCallback > ConnectCallbackMap
Connect handlers per impl.
Definition: abstract_factory.h:140
bool register_server_connect_callback(NodeImpl *impl, NodeImpl::ConnectCallback &&callback)
Stores callback as the server-side connect handler for impl.
Definition: abstract_factory.h:583
std::map< NodeImpl *, NodeImpl::IntraMsgCallback > IntraMsgCallbackMap
Intra-message callbacks.
Definition: abstract_factory.h:143
std::unordered_set< NodeImpl * > ImplList
Set of currently registered NodeImpl peers.
Definition: abstract_factory.h:138
bool status_map_is_empty() const
Reports whether the transport-status dictionary is empty.
Definition: abstract_factory.h:646
bool register_sub_connect_callback(NodeImpl *impl, NodeImpl::ConnectCallback &&callback)
Stores callback as the subscriber-side connect handler for impl.
Definition: abstract_factory.h:589
bool register_intra_msg_callback(NodeImpl *impl, NodeImpl::IntraMsgCallback &&callback)
Stores callback as the intra-process message handler for impl.
Definition: abstract_factory.h:605
bool msg_map_is_empty() const
Reports whether the serialised-message dictionary is empty.
Definition: abstract_factory.h:634
void ignore_called()
Definition: abstract_factory.h:742
void traverse_intra_msg_callback(const FindIntraMsgCallback &callback)
Walks the intra-process dictionary and invokes callback for every entry.
Definition: abstract_factory.h:672
void traverse_status_callback(const FindStatusCallback &callback)
Walks the transport-status dictionary and invokes callback for every entry.
Definition: abstract_factory.h:677
std::map< NodeImpl *, NodeImpl::StatusCallback > StatusCallbackMap
Status callbacks per impl.
Definition: abstract_factory.h:144
void traverse_sub_connect_callback(const FindConnectCallback &callback)
Walks the subscriber-connect dictionary and invokes callback for every entry.
Definition: abstract_factory.h:657
bool has_impl() const
Indicates whether at least one peer has been registered.
Definition: abstract_factory.h:566
std::map< NodeImpl *, NodeImpl::ReqRespCallback > ReqRespCallbackMap
Req/resp callbacks, keyed by impl.
Definition: abstract_factory.h:141
void traverse_req_resp_callback(const FindReqRespCallback &callback)
Walks the request/response dictionary and invokes callback for every entry.
Definition: abstract_factory.h:662
bool is_contains_impl(NodeImpl *impl) const
Tests whether impl is currently part of the peer set.
Definition: abstract_factory.h:560
bool intra_msg_map_is_empty() const
Reports whether the intra-process message dictionary is empty.
Definition: abstract_factory.h:640
bool invoke_msg_callback(NodeImpl *owner, const Bytes &bytes)
Invokes a registered owner's message handler without copying its state.
Definition: abstract_factory.h:703
~AbstractObject() override
void traverse_msg_callback(const FindMsgCallback &callback)
Walks the serialised-message dictionary and invokes callback for every entry.
Definition: abstract_factory.h:667
bool register_status_callback(NodeImpl *impl, NodeImpl::StatusCallback &&callback)
Stores callback as the transport-status handler for impl.
Definition: abstract_factory.h:611
Fixed-size 128-byte buffer holder with SBO, five ownership modes and integrated codecs.
Definition: bytes.h:120
Copyable type-erased callable analogue of std::function with a tunable SBO and pool spill.
Definition: functional.h:132
Backbone of every transport-specific VLink node implementation.
Definition: node_impl.h:163
#define VUNLIKELY(...)
Short alias for VLINK_UNLIKELY.
Definition: macros.h:292
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Foundational base classes shared by every transport-backed VLink node.