VLink  2.0.0
A high-performance communication middleware
vlink::AbstractFactory< FilterT > Class Template Reference

Lazily allocates and caches AbstractObject instances keyed by FilterT. More...

#include <abstract_factory.h>

Collaboration diagram for vlink::AbstractFactory< FilterT >:

Public Member Functions

bool has_object (Object *ptr) const
 Tests whether ptr corresponds to a live object created by this factory. More...
 
template<typename ObjectT >
std::shared_ptr< ObjectT > get_object (const FilterT &filter)
 Looks up or creates the ObjectT registered against filter. More...
 

Protected Member Functions

 AbstractFactory ()
 Constructs an empty factory. More...
 
virtual ~AbstractFactory ()
 Destroys the factory and releases the cache. More...
 

Detailed Description

template<typename FilterT>
class vlink::AbstractFactory< FilterT >

Lazily allocates and caches AbstractObject instances keyed by FilterT.

Holds a std::map<FilterT, std::weak_ptr<Object>> so that multiple node peers requesting the same key reuse the same registration record. The returned std::shared_ptr carries a custom deleter that erases the map entry when the last owner releases the object, which keeps the lookup map free of stale weak_ptr slots.

Note
The factory itself is non-copyable and non-movable; share it through a singleton or a per-transport static instance.
Template Parameters
FilterTTopic-key type (typically std::string).

Constructor & Destructor Documentation

◆ AbstractFactory()

template<typename FilterT >
vlink::AbstractFactory< FilterT >::AbstractFactory ( )
inlineprotecteddefault

Constructs an empty factory.

◆ ~AbstractFactory()

template<typename FilterT >
vlink::AbstractFactory< FilterT >::~AbstractFactory ( )
inlineprotectedvirtualdefault

Destroys the factory and releases the cache.

Member Function Documentation

◆ get_object()

template<typename FilterT >
template<typename ObjectT >
std::shared_ptr< ObjectT > vlink::AbstractFactory< FilterT >::get_object ( const FilterT &  filter)
inline

Looks up or creates the ObjectT registered against filter.

If the cached weak_ptr is still valid, the existing instance is shared with the caller. Otherwise a new ObjectT is allocated (outside the factory lock so its constructor can re-enter VLink safely), the resulting shared_ptr is given a deleter that removes the cache entry on destruction, and the value is stored back into the map.

Template Parameters
ObjectTConcrete subclass of AbstractObject<FilterT> to allocate.
Parameters
filterKey identifying the topic.
Returns
Shared ownership handle for the cached object.

◆ has_object()

template<typename FilterT >
bool vlink::AbstractFactory< FilterT >::has_object ( Object ptr) const
inline

Tests whether ptr corresponds to a live object created by this factory.

Parameters
ptrRaw pointer to validate.
Returns
true when the object is still alive in this factory; false otherwise.

The documentation for this class was generated from the following file: