Apache Celix  latest
Apache Celix is a framework for C, C++14 and C++17 to develop dynamic modular software applications using component and in-process service-oriented programming.
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
celix::ServiceTracker< I > Class Template Reference

The ServiceTracker class tracks services. More...

#include <Trackers.h>

Inheritance diagram for celix::ServiceTracker< I >:
Inheritance graph
[legend]
Collaboration diagram for celix::ServiceTracker< I >:
Collaboration graph
[legend]

Classes

struct  SvcEntry
 
struct  SvcEntryCompare
 

Public Member Functions

std::shared_ptr< I > getHighestRankingService ()
 Get the current highest ranking service tracked by this tracker. More...
 
std::vector< std::shared_ptr< I > > getServices ()
 Get a vector of all the currently found services for this tracker. More...
 
template<typename F >
size_t useServices (const F &f)
 Applies the provided function to each service being tracked. More...
 
template<typename F >
size_t useServicesWithProperties (const F &f)
 Applies the provided function to each service being tracked, along with its properties. More...
 
template<typename F >
size_t useServicesWithOwner (const F &f)
 Applies the provided function to each service being tracked, along with its properties and owner bundle. More...
 
template<typename F >
bool useService (const F &f)
 Applies the provided function to the highest ranking service being tracked. More...
 
template<typename F >
bool useServiceWithProperties (const F &f)
 Applies the provided function to the highest ranking service being tracked, along with its properties. More...
 
template<typename F >
bool useServiceWithOwner (const F &f)
 Applies the provided function to the highest ranking service being tracked, along with its properties and owner bundle. More...
 
- Public Member Functions inherited from celix::GenericServiceTracker
 GenericServiceTracker (std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _svcName, std::string _svcVersionRange, celix::Filter _filter)
 
 ~GenericServiceTracker () override=default
 
void open () override
 
const std::string & getServiceName () const
 The service name tracked by this service tracker. More...
 
const std::string & getServiceRange () const
 The service version range tracked by this service tracker. More...
 
const celix::Filter & getFilter () const
 The additional filter for services tracked by this service tracker. More...
 
std::size_t getServiceCount () const
 The nr of services currently tracked by this tracker. More...
 
- Public Member Functions inherited from celix::AbstractTracker
 AbstractTracker (std::shared_ptr< celix_bundle_context_t > _cCtx)
 
virtual ~AbstractTracker () noexcept=default
 
bool isOpen () const
 Check if the tracker is open (state == OPEN) More...
 
TrackerState getState () const
 Get the current state of the tracker. More...
 
void close ()
 Close the tracker (of the state is not CLOSED or CLOSING). More...
 
void wait () const
 Wait until a service tracker is completely OPEN or CLOSED. More...
 

Static Public Member Functions

static std::shared_ptr< ServiceTracker< I > > create (std::shared_ptr< celix_bundle_context_t > cCtx, std::string svcName, std::string svcVersionRange, celix::Filter filter, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> setCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> addCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> remCallbacks)
 Creates a new service tracker and opens the tracker. More...
 

Protected Member Functions

 ServiceTracker (std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _svcName, std::string _svcVersionRange, celix::Filter _filter, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> _setCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> _addCallbacks, std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>> _remCallbacks)
 
void waitForExpiredSvcEntry (std::shared_ptr< SvcEntry > &entry)
 
template<typename U >
void waitForExpired (std::weak_ptr< U > observe, long svcId, const char *objName)
 
void invokeUpdateCallbacks ()
 
- Protected Member Functions inherited from celix::AbstractTracker
void waitIfAble () const
 Wait (if not on the Celix event thread) for the tracker to be OPEN or CLOSED. More...
 

Static Protected Member Functions

static std::shared_ptr< SvcEntrycreateEntry (void *voidSvc, const celix_properties_t *cProps, const celix_bundle_t *cBnd)
 
- Static Protected Member Functions inherited from celix::AbstractTracker
template<typename T >
static std::function< void(T *)> delCallback ()
 

Protected Attributes

const std::chrono::milliseconds warningTimoutForNonExpiredSvcObject {1000}
 
const std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)> > setCallbacks
 
const std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)> > addCallbacks
 
const std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)> > remCallbacks
 
const std::vector< std::function< void(const std::vector< std::shared_ptr< I >>)> > updateCallbacks {}
 
const std::vector< std::function< void(const std::vector< std::pair< std::shared_ptr< I >, std::shared_ptr< const celix::Properties >>>)> > updateWithPropertiesCallbacks {}
 
const std::vector< std::function< void(const std::vector< std::tuple< std::shared_ptr< I >, std::shared_ptr< const celix::Properties >, std::shared_ptr< const celix::Bundle >>>)> > updateWithOwnerCallbacks {}
 
std::mutex mutex {}
 
std::set< std::shared_ptr< SvcEntry >, SvcEntryCompareentries {}
 
std::unordered_map< long, std::shared_ptr< SvcEntry > > cachedEntries {}
 
std::shared_ptr< SvcEntryhighestRankingServiceEntry {}
 
- Protected Attributes inherited from celix::GenericServiceTracker
const std::string svcName
 
const std::string svcVersionRange
 
const celix::Filter filter
 
celix_service_tracking_options opts {}
 
std::atomic< size_t > svcCount {0}
 
- Protected Attributes inherited from celix::AbstractTracker
const std::shared_ptr< celix_bundle_context_t > cCtx
 
std::mutex mutex {}
 
long trkId {-1L}
 
TrackerState state {TrackerState::CLOSED}
 

Detailed Description

template<typename I>
class celix::ServiceTracker< I >

The ServiceTracker class tracks services.

Tracking in this case means that the ServiceTracker maintains and informs

Note
Thread safe.
Template Parameters
IThe service type to track

Constructor & Destructor Documentation

◆ ServiceTracker()

template<typename I >
celix::ServiceTracker< I >::ServiceTracker ( std::shared_ptr< celix_bundle_context_t >  _cCtx,
std::string  _svcName,
std::string  _svcVersionRange,
celix::Filter  _filter,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
_setCallbacks  ,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
_addCallbacks  ,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
_remCallbacks   
)
inlineprotected

Member Function Documentation

◆ create()

template<typename I >
static std::shared_ptr<ServiceTracker<I> > celix::ServiceTracker< I >::create ( std::shared_ptr< celix_bundle_context_t >  cCtx,
std::string  svcName,
std::string  svcVersionRange,
celix::Filter  filter,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
setCallbacks  ,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
addCallbacks  ,
std::vector< std::function< void(const std::shared_ptr< I > &, const std::shared_ptr< const celix::Properties > &, const std::shared_ptr< const celix::Bundle > &)>  ,
remCallbacks   
)
inlinestatic

Creates a new service tracker and opens the tracker.

Parameters
cCtxThe c bundle context.
svcNameThe service name to filter for.
svcVersionRangeThe optional service range to filter for.
filterThe optional (and additional with respect to svcName and svcVersionRange) LDAP filter to filter for.
setCallbacksThe callback which is called when a new service needs te be set which matches the trackers filter.
addCallbacksThe callback which is called when a new service is added to the Celix framework which matches the trackers filter.
remCallbacksThe callback which is called when a service is removed from the Celix framework which matches the trackers filter.
Returns
The new service tracker as shared ptr.
Exceptions
celix::Exception

◆ createEntry()

template<typename I >
static std::shared_ptr<SvcEntry> celix::ServiceTracker< I >::createEntry ( void *  voidSvc,
const celix_properties_t *  cProps,
const celix_bundle_t *  cBnd 
)
inlinestaticprotected

◆ getHighestRankingService()

template<typename I >
std::shared_ptr<I> celix::ServiceTracker< I >::getHighestRankingService ( )
inline

Get the current highest ranking service tracked by this tracker.

Note that this can be a nullptr if there are no services found.

The return shared ptr should not be stored and only be used shortly, otherwise the framework can hangs during service un-registrations.

◆ getServices()

template<typename I >
std::vector<std::shared_ptr<I> > celix::ServiceTracker< I >::getServices ( )
inline

Get a vector of all the currently found services for this tracker.

This vector is ordered by service ranking (descending, highest ranking service first).

The returned result not be stored and only be used shortly, otherwise the framework can hangs during service un-registrations.

◆ invokeUpdateCallbacks()

template<typename I >
void celix::ServiceTracker< I >::invokeUpdateCallbacks ( )
inlineprotected

◆ useService()

template<typename I >
template<typename F >
bool celix::ServiceTracker< I >::useService ( const F &  f)
inline

Applies the provided function to the highest ranking service being tracked.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to the highest ranking service.
Returns
True if the function was applied to a service, false otherwise.

◆ useServices()

template<typename I >
template<typename F >
size_t celix::ServiceTracker< I >::useServices ( const F &  f)
inline

Applies the provided function to each service being tracked.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to each service.
Returns
The number of services to which the function was applied.

◆ useServicesWithOwner()

template<typename I >
template<typename F >
size_t celix::ServiceTracker< I >::useServicesWithOwner ( const F &  f)
inline

Applies the provided function to each service being tracked, along with its properties and owner bundle.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc, const celix::Properties& props, const celix::Bundle& bnd) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to each service.
Returns
The number of services to which the function was applied.

◆ useServicesWithProperties()

template<typename I >
template<typename F >
size_t celix::ServiceTracker< I >::useServicesWithProperties ( const F &  f)
inline

Applies the provided function to each service being tracked, along with its properties.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc, const celix::Properties& props) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to each service.
Returns
The number of services to which the function was applied.

◆ useServiceWithOwner()

template<typename I >
template<typename F >
bool celix::ServiceTracker< I >::useServiceWithOwner ( const F &  f)
inline

Applies the provided function to the highest ranking service being tracked, along with its properties and owner bundle.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc, const celix::Properties& props, const celix::Bundle& bnd) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to the highest ranking service.
Returns
True if the function was applied to a service, false otherwise.

◆ useServiceWithProperties()

template<typename I >
template<typename F >
bool celix::ServiceTracker< I >::useServiceWithProperties ( const F &  f)
inline

Applies the provided function to the highest ranking service being tracked, along with its properties.

Template Parameters
FA function or callable object type. The function signature should be equivalent to the following: void func(I& svc, const celix::Properties& props) where I is the service type being tracked.
Parameters
fThe function or callable object to apply to the highest ranking service.
Returns
True if the function was applied to a service, false otherwise.

◆ waitForExpired()

template<typename I >
template<typename U >
void celix::ServiceTracker< I >::waitForExpired ( std::weak_ptr< U >  observe,
long  svcId,
const char *  objName 
)
inlineprotected

◆ waitForExpiredSvcEntry()

template<typename I >
void celix::ServiceTracker< I >::waitForExpiredSvcEntry ( std::shared_ptr< SvcEntry > &  entry)
inlineprotected

Member Data Documentation

◆ addCallbacks

template<typename I >
const std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)> > celix::ServiceTracker< I >::addCallbacks
protected

◆ cachedEntries

template<typename I >
std::unordered_map<long, std::shared_ptr<SvcEntry> > celix::ServiceTracker< I >::cachedEntries {}
protected

◆ entries

template<typename I >
std::set<std::shared_ptr<SvcEntry>, SvcEntryCompare> celix::ServiceTracker< I >::entries {}
protected

◆ highestRankingServiceEntry

template<typename I >
std::shared_ptr<SvcEntry> celix::ServiceTracker< I >::highestRankingServiceEntry {}
protected

◆ mutex

template<typename I >
std::mutex celix::ServiceTracker< I >::mutex {}
mutableprotected

◆ remCallbacks

template<typename I >
const std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)> > celix::ServiceTracker< I >::remCallbacks
protected

◆ setCallbacks

template<typename I >
const std::vector<std::function<void(const std::shared_ptr<I>&, const std::shared_ptr<const celix::Properties>&, const std::shared_ptr<const celix::Bundle>&)> > celix::ServiceTracker< I >::setCallbacks
protected

◆ updateCallbacks

template<typename I >
const std::vector<std::function<void(const std::vector<std::shared_ptr<I>>)> > celix::ServiceTracker< I >::updateCallbacks {}
protected

◆ updateWithOwnerCallbacks

template<typename I >
const std::vector<std::function<void(const std::vector<std::tuple<std::shared_ptr<I>, std::shared_ptr<const celix::Properties>, std::shared_ptr<const celix::Bundle>>>)> > celix::ServiceTracker< I >::updateWithOwnerCallbacks {}
protected

◆ updateWithPropertiesCallbacks

template<typename I >
const std::vector<std::function<void(const std::vector<std::pair<std::shared_ptr<I>, std::shared_ptr<const celix::Properties>>>)> > celix::ServiceTracker< I >::updateWithPropertiesCallbacks {}
protected

◆ warningTimoutForNonExpiredSvcObject

template<typename I >
const std::chrono::milliseconds celix::ServiceTracker< I >::warningTimoutForNonExpiredSvcObject {1000}
protected

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