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.
Public Member Functions | Friends | List of all members
celix::UseServiceBuilder< I > Class Template Reference

Fluent builder API to use a service or services. More...

#include <UseServiceBuilder.h>

Public Member Functions

 UseServiceBuilder (std::shared_ptr< celix_bundle_context_t > _cCtx, std::string _name, bool _useSingleService=true)
 
UseServiceBuilderoperator= (UseServiceBuilder &&)=delete
 
 UseServiceBuilder (const UseServiceBuilder &)=delete
 
UseServiceBuilder operator= (const UseServiceBuilder &)=delete
 
UseServiceBuildersetFilter (const std::string &f)
 Set filter to be used to select a service. More...
 
UseServiceBuildersetFilter (celix::Filter f)
 Set filter to be used to matching services. More...
 
template<typename Rep , typename Period >
UseServiceBuildersetTimeout (std::chrono::duration< Rep, Period > duration)
 Sets a optional timeout. More...
 
UseServiceBuilderaddUseCallback (std::function< void(I &)> cb)
 Adds a use callback function which will be called when the UseServiceBuilder is "build". More...
 
UseServiceBuilderaddUseCallback (std::function< void(I &, const celix::Properties &)> cb)
 Adds a use callback function which will be called when the UseServiceBuilder is "build". More...
 
UseServiceBuilderaddUseCallback (std::function< void(I &, const celix::Properties &, const celix::Bundle &)> cb)
 Adds a use callback function which will be called when the UseServiceBuilder is "build". More...
 
std::size_t build ()
 "Builds" the UseServiceBuild. More...
 

Friends

class BundleContext
 

Detailed Description

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

Fluent builder API to use a service or services.

The UseServiceBuilder can be used to use a service or a set of services registered in the celix framework. The builder can be used to specify additional match filtering for services, configure use callbacks and whether a single or multiple services will be used. For a single service the highest ranking service will be used. The builder should be finished with a build call.

 ctx->useService<ICalc>()
     .addUseCallback([](ICalc& calc) {
         std::cout << "result is " << calc.add(2, 3) << std::endl;
      })
      .build();
Note
Not thread safe.
Template Parameters
IThe service type to use

Constructor & Destructor Documentation

◆ UseServiceBuilder() [1/2]

template<typename I >
celix::UseServiceBuilder< I >::UseServiceBuilder ( std::shared_ptr< celix_bundle_context_t >  _cCtx,
std::string  _name,
bool  _useSingleService = true 
)
inlineexplicit

◆ UseServiceBuilder() [2/2]

template<typename I >
celix::UseServiceBuilder< I >::UseServiceBuilder ( const UseServiceBuilder< I > &  )
delete

Member Function Documentation

◆ addUseCallback() [1/3]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::addUseCallback ( std::function< void(I &)>  cb)
inline

Adds a use callback function which will be called when the UseServiceBuilder is "build".

The use callback function has 1 argument: a reference to the matching service.

◆ addUseCallback() [2/3]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::addUseCallback ( std::function< void(I &, const celix::Properties &)>  cb)
inline

Adds a use callback function which will be called when the UseServiceBuilder is "build".

The use callback function has 2 arguments:

  • A reference to the matching service.
  • A const reference to the matching service properties.

◆ addUseCallback() [3/3]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::addUseCallback ( std::function< void(I &, const celix::Properties &, const celix::Bundle &)>  cb)
inline

Adds a use callback function which will be called when the UseServiceBuilder is "build".

The use callback function has 3 arguments:

  • A reference to the matching service.
  • A const reference to the matching service properties.
  • A const reference to the bundle owning the matching service.

◆ build()

template<typename I >
std::size_t celix::UseServiceBuilder< I >::build ( )
inline

"Builds" the UseServiceBuild.

Blocks until:

  • All the use callback functions are called with the highest ranking matching service
  • The timeout has expired

This means this if no services are found and the timeout is 0 (default) the function will return without blocking.

Returns
the number of services found and provided to the callback(s).

◆ operator=() [1/2]

template<typename I >
UseServiceBuilder celix::UseServiceBuilder< I >::operator= ( const UseServiceBuilder< I > &  )
delete

◆ operator=() [2/2]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::operator= ( UseServiceBuilder< I > &&  )
delete

◆ setFilter() [1/2]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::setFilter ( celix::Filter  f)
inline

Set filter to be used to matching services.

◆ setFilter() [2/2]

template<typename I >
UseServiceBuilder& celix::UseServiceBuilder< I >::setFilter ( const std::string &  f)
inline

Set filter to be used to select a service.

The filter must be LDAP filter. Example: "(property_key=value)"

◆ setTimeout()

template<typename I >
template<typename Rep , typename Period >
UseServiceBuilder& celix::UseServiceBuilder< I >::setTimeout ( std::chrono::duration< Rep, Period >  duration)
inline

Sets a optional timeout.

If the timeout is > 0 and there is no matching service, the "build" will block until a matching service is found or the timeout is expired.

Note: timeout is only valid if the a single service is used.

Friends And Related Function Documentation

◆ BundleContext

template<typename I >
friend class BundleContext
friend

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