/*============================================================================= Library: CppMicroServices Copyright (c) The CppMicroServices developers. See the COPYRIGHT file at the top-level directory of this distribution and at https://github.com/CppMicroServices/CppMicroServices/COPYRIGHT . Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ #ifndef CPPMICROSERVICES_BUNDLECONTEXT_H #define CPPMICROSERVICES_BUNDLECONTEXT_H #include "cppmicroservices/GlobalConfig.h" #include "cppmicroservices/ListenerFunctors.h" #include "cppmicroservices/ListenerToken.h" #include "cppmicroservices/ServiceInterface.h" #include "cppmicroservices/ServiceRegistration.h" #include namespace cppmicroservices { class AnyMap; class Bundle; class BundleContext; class BundleContextPrivate; class ServiceFactory; namespace detail { class LogSink; template class BundleAbstractTracked; template class ServiceTrackerPrivate; template class TrackedService; } template class ServiceObjects; template struct ServiceHolder; /** * \ingroup MicroServices * * A bundle's execution context within the framework. The context is used to * grant access to other methods so that this bundle can interact with the * framework. * *

* BundleContext methods allow a bundle to: *

    *
  • Install other bundles. *
  • Subscribe to events published by the framework. *
  • Register service objects with the framework service registry. *
  • Retrieve ServiceReferences from the framework service * registry. *
  • Get and release service objects for a referenced service. *
  • Get the list of bundles installed in the framework. *
  • Get the {@link Bundle} object for a bundle. *
* *

* A BundleContext object will be created and provided to the * bundle associated with this context when it is started using the * {@link BundleActivator::Start} method. The same BundleContext * object will be passed to the bundle associated with this context when it is * stopped using the {@link BundleActivator::Stop} method. A * BundleContext object is generally for the private use of its * associated bundle and is not meant to be shared with other bundles in the * bundle environment. * *

* The Bundle object associated with a BundleContext * object is called the context bundle. * *

* The BundleContext object is only valid during the execution of * its context bundle; that is, during the period when the context bundle * is started. If the BundleContext * object is used subsequently, a std::runtime_error is * thrown. The BundleContext object is never reused after * its context bundle is stopped. * *

* The framework is the only entity that can create BundleContext * objects. * * @remarks This class is thread safe. */ class US_Framework_EXPORT BundleContext { public: /** * Constructs an invalid %BundleContext object. * * Valid bundle context objects can only be created by the framework * and are supplied to a bundle via its \c BundleActivator or as a * return value of the \c GetBundleContext() method. * * @see operator bool() const */ BundleContext(); /** * Compares this \c BundleContext object with the specified * bundle context. * * Valid \c BundleContext objects are equal if and only if * they represent the same context. Invalid \c BundleContext * objects are always considered to be equal. * * @param rhs The \c BundleContext object to compare this object with. * @return \c true if this \c BundleContext object is equal to \c rhs, * \c false otherwise. */ bool operator==(const BundleContext& rhs) const; /** * Compares this \c BundleContext object with the specified bundle * context for inequality. * * @param rhs The \c BundleContext object to compare this object with. * @return Returns the result of !(*this == rhs). */ bool operator!=(const BundleContext& rhs) const; /** * Compares this \c BundleContext with the specified bundle * context for order. * * How valid %BundleContext objects are ordered is an implementation * detail and must not be relied on. Invalid \c BundleContext objects * will always compare greater then valid \c BundleContext objects. * * @param rhs The \c BundleContext object to compare this object with. * @return \c true if this object is orderded before \c rhs, \c false * otherwise. */ bool operator<(const BundleContext& rhs) const; /** * Tests this %BundleContext object for validity. * * Invalid \c BundleContext objects are created by the default constructor or * can be returned by certain framework methods if the context bundle has been * uninstalled. * * A \c BundleContext object can become invalid by assigning a \c nullptr to * it or if the context bundle is stopped. * * @return \c true if this %BundleContext object is valid and can safely be used, * \c false otherwise. */ explicit operator bool() const; /** * Releases any resources held or locked by this * \c BundleContext and renders it invalid. */ BundleContext& operator=(std::nullptr_t); /** * Returns the value of the specified property. If the key is not found in * the Framework properties, the method returns an empty \c Any. * * @param key The name of the requested property. * @return The value of the requested property, or an empty \c Any if the * property is undefined. */ Any GetProperty(const std::string& key) const; /** * Returns all known properties. * * @return A map of all framework properties. */ AnyMap GetProperties() const; /** * Returns the Bundle object associated with this * BundleContext. This bundle is called the context bundle. * * @return The Bundle object associated with this * BundleContext. * @throws std::runtime_error If this BundleContext is no * longer valid. */ Bundle GetBundle() const; /** * Returns the bundle with the specified identifier. * * @param id The identifier of the bundle to retrieve. * @return A Bundle object or nullptr if the * identifier does not match any previously installed bundle. * @throws std::logic_error If the framework instance is not active. * @throws std::runtime_error If this BundleContext is no * longer valid. */ Bundle GetBundle(long id) const; /** * Get the bundles with the specified bundle location. * * @param location The location of the bundles to get. * @return The requested {\c Bundle}s or an empty list. * @throws std::logic_error If the framework instance is not active. * @throws std::runtime_error If the BundleContext is no longer valid. */ std::vector GetBundles(const std::string& location) const; /** * Returns a list of all known bundles. *

* This method returns a list of all bundles installed in the bundle * environment at the time of the call to this method. This list will * also contain bundles which might already have been stopped. * * @return A std::vector of Bundle objects which * will hold one object per known bundle. * @throws std::runtime_error If the BundleContext is no longer valid. */ std::vector GetBundles() const; /** * Registers the specified service object with the specified properties * under the specified class names into the framework. A * ServiceRegistration object is returned. The * ServiceRegistration object is for the private use of the * bundle registering the service and should not be shared with other * bundles. The registering bundle is defined to be the context bundle. * Other bundles can locate the service by using either the * {@link #GetServiceReferences} or {@link #GetServiceReference} method. * *

* A bundle can register a service object that implements the * ServiceFactory or PrototypeServiceFactory interface to have more * flexibility in providing service objects to other bundles. * *

* The following steps are taken when registering a service: *

    *
  1. The framework adds the following service properties to the service * properties from the specified ServiceProperties (which may be * omitted):
    * A property named Constants#SERVICE_ID identifying the * registration number of the service
    * A property named Constants#OBJECTCLASS containing all the * specified classes.
    * A property named Constants#SERVICE_SCOPE identifying the scope * of the service.
    * Properties with these names in the specified ServiceProperties will * be ignored. *
  2. The service is added to the framework service registry and may now be * used by other bundles. *
  3. A service event of type ServiceEvent#SERVICE_REGISTERED is fired. *
  4. A ServiceRegistration object for this registration is * returned. *
* * @note This is a low-level method and should normally not be used directly. * Use one of the templated RegisterService methods instead. * * @param service A shared_ptr to a map of interface identifiers to service objects. * @param properties The properties for this service. The keys in the * properties object must all be std::string objects. See * {@link Constants} for a list of standard service property keys. * Changes should not be made to this object after calling this * method. To update the service's properties the * {@link ServiceRegistration::SetProperties} method must be called. * The set of properties may be omitted if the service has * no properties. * @return A ServiceRegistration object for use by the bundle * registering the service to update the service's properties or to * unregister the service. * * @throws std::runtime_error If this BundleContext is no longer valid, or if there are case variants of the same key in the supplied properties map. * @throws std::invalid_argument If the InterfaceMap is empty, or * if a service is registered as a null class. * * @see ServiceRegistration * @see ServiceFactory * @see PrototypeServiceFactory */ ServiceRegistrationU RegisterService( const InterfaceMapConstPtr& service, const ServiceProperties& properties = ServiceProperties()); /** * Registers the specified service object with the specified properties * using the specified interfaces types with the framework. * *

* This method is provided as a convenience when registering a service under * two interface classes whose type is available to the caller. It is otherwise identical to * RegisterService(const InterfaceMap&, const ServiceProperties&) but should be preferred * since it avoids errors in the string literal identifying the class name or interface identifier. * * Example usage: * \snippet uServices-registration/main.cpp 2-1 * \snippet uServices-registration/main.cpp 2-2 * * @tparam I1 The first interface type under which the service can be located. * @tparam Interfaces Additional interface types under which the service can be located. * @param impl A \c shared_ptr to the service object * @param properties The properties for this service. * @return A ServiceRegistration object for use by the bundle * registering the service to update the service's properties or to * unregister the service. * @throws std::logic_error If this BundleContext is no longer valid. * @throws ServiceException If the service type \c S is invalid or the * \c service object is nullptr. * * @see RegisterService(const InterfaceMap&, const ServiceProperties&) */ template ServiceRegistration RegisterService( const std::shared_ptr& impl, const ServiceProperties& properties = ServiceProperties()) { InterfaceMapConstPtr servicePointers = MakeInterfaceMap(impl); return RegisterService(servicePointers, properties); } /** * Registers the specified service factory as a service with the specified properties * using the specified template argument as service interface type with the framework. * *

* This method is provided as a convenience when factory will only be registered under * a single class name whose type is available to the caller. It is otherwise identical to * RegisterService(const InterfaceMap&, const ServiceProperties&) but should be preferred * since it avoids errors in the string literal identifying the class name or interface identifier. * * Example usage: * \snippet uServices-registration/main.cpp 2-1 * \snippet uServices-registration/main.cpp f2 * * @tparam I1 The first interface type under which the service can be located. * @tparam Interfaces Additional interface types under which the service can be located. * @param factory A \c shared_ptr to the ServiceFactory object. * @param properties The properties for this service. * @return A ServiceRegistration object for use by the bundle * registering the service to update the service's properties or to * unregister the service. * @throws std::logic_error If this BundleContext is no longer valid. * @throws ServiceException If the service type \c S is invalid or the * \c service factory object is nullptr. * * @see RegisterService(const InterfaceMap&, const ServiceProperties&) */ template ServiceRegistration RegisterService( const std::shared_ptr& factory, const ServiceProperties& properties = ServiceProperties()) { InterfaceMapConstPtr servicePointers = MakeInterfaceMap(factory); return RegisterService(servicePointers, properties); } /** * Returns a list of ServiceReference objects. The returned * list contains services that were registered under the specified class * and match the specified filter expression. * *

* The list is valid at the time of the call to this method. However, since * the framework is a very dynamic environment, services can be modified or * unregistered at any time. * *

* The specified filter expression is used to select the * registered services whose service properties contain keys and values * that satisfy the filter expression. See LDAPFilter for a description * of the filter syntax. If the specified filter is * empty, all registered services are considered to match the * filter. If the specified filter expression cannot be parsed, * an std::invalid_argument will be thrown with a human-readable * message where the filter became unparsable. * *

* The result is a list of ServiceReference objects for all * services that meet all of the following conditions: *

    *
  • If the specified class name, clazz, is not * empty, the service must have been registered with the * specified class name. The complete list of class names with which a * service was registered is available from the service's * {@link Constants#OBJECTCLASS objectClass} property. *
  • If the specified filter is not empty, the * filter expression must match the service. *
* * @param clazz The class name with which the service was registered or * an empty string for all services. * @param filter The filter expression or empty for all * services. * @return A list of ServiceReference objects or * an empty list if no services are registered that satisfy the * search. * @throws std::invalid_argument If the specified filter * contains an invalid filter expression that cannot be parsed. * @throws std::runtime_error If this BundleContext is no longer valid. * @throws std::logic_error If the ServiceRegistrationBase object is invalid, * or if the service is unregistered. */ std::vector GetServiceReferences( const std::string& clazz, const std::string& filter = std::string()); /** * Returns a list of ServiceReference objects. The returned * list contains services that * were registered under the interface id of the template argument S * and match the specified filter expression. * *

* This method is identical to GetServiceReferences(const std::string&, const std::string&) except that * the class name for the service object is automatically deduced from the template argument. * * @tparam S The type under which the requested service objects must have been registered. * @param filter The filter expression or empty for all * services. * @return A list of ServiceReference objects or * an empty list if no services are registered which satisfy the * search. * @throws std::invalid_argument If the specified filter * contains an invalid filter expression that cannot be parsed. * @throws std::logic_error If this BundleContext is no longer valid. * @throws ServiceException If the service interface id of \c S is empty, see @gr_serviceinterface. * * @see GetServiceReferences(const std::string&, const std::string&) */ template std::vector> GetServiceReferences( const std::string& filter = std::string()) { auto& clazz = us_service_interface_iid(); if (clazz.empty()) throw ServiceException( "The service interface class has no " "CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE macro"); typedef std::vector BaseVectorT; BaseVectorT serviceRefs = GetServiceReferences(clazz, filter); std::vector> result; for (BaseVectorT::const_iterator i = serviceRefs.begin(); i != serviceRefs.end(); ++i) { result.push_back(ServiceReference(*i)); } return result; } /** * Returns a ServiceReference object for a service that * implements and was registered under the specified class. * *

* The returned ServiceReference object is valid at the time of * the call to this method. However as the Micro Services framework is a very dynamic * environment, services can be modified or unregistered at any time. * *

* This method is the same as calling * {@link BundleContext::GetServiceReferences(const std::string&, const std::string&)} with an * empty filter expression. It is provided as a convenience for * when the caller is interested in any service that implements the * specified class. *

* If multiple such services exist, the service with the highest ranking (as * specified in its Constants::SERVICE_RANKING property) is returned. *

* If there is a tie in ranking, the service with the lowest service ID (as * specified in its Constants::SERVICE_ID property); that is, the * service that was registered first is returned. * * @param clazz The class name with which the service was registered. * @return A ServiceReference object, or an invalid ServiceReference if * no services are registered which implement the named class. * @throws std::runtime_error If this BundleContext is no longer valid. * * @see #GetServiceReferences(const std::string&, const std::string&) */ ServiceReferenceU GetServiceReference(const std::string& clazz); /** * Returns a ServiceReference object for a service that * implements and was registered under the specified template class argument. * *

* This method is identical to GetServiceReference(const std::string&) except that * the class name for the service object is automatically deduced from the template argument. * * @tparam S The type under which the requested service must have been registered. * @return A ServiceReference object, or an invalid ServiceReference if * no services are registered which implement the type S. * @throws std::runtime_error If this BundleContext is no longer valid. * @throws ServiceException If the service interface id of \c S is empty, see @gr_serviceinterface. * @see #GetServiceReference(const std::string&) * @see #GetServiceReferences(const std::string&) */ template ServiceReference GetServiceReference() { auto& clazz = us_service_interface_iid(); if (clazz.empty()) throw ServiceException( "The service interface class has no " "CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE macro"); return ServiceReference(GetServiceReference(clazz)); } /** * Returns the service object referenced by the specified * ServiceReferenceBase object. *

* A bundle's use of a service is tracked by the bundle's use count of that * service. Each call to {@link #GetService(const ServiceReference&)} increments * the context bundle's use count by one. The deleter function of the returned shared_ptr * object is responsible for decrementing the context bundle's use count. *

* When a bundle's use count for a service drops to zero, the bundle should * no longer use that service. * *

* This method will always return an empty object when the service * associated with this reference has been unregistered. * *

* The following steps are taken to get the service object: *

    *
  1. If the service has been unregistered, empty object is returned. *
  2. The context bundle's use count for this service is incremented by * one. *
  3. If the context bundle's use count for the service is currently one * and the service was registered with an object implementing the * ServiceFactory interface, the * {@link ServiceFactory::GetService} method is * called to create a service object for the context bundle. This service * object is cached by the framework. While the context bundle's use count * for the service is greater than zero, subsequent calls to get the * services's service object for the context bundle will return the cached * service object.
    * If the ServiceFactory object throws an * exception, empty object is returned and a warning is logged. *
  4. A shared_ptr to the service object is returned. *
* * @param reference A reference to the service. * @return A shared_ptr to the service object associated with reference. * An empty shared_ptr is returned if the service is not registered or the * ServiceFactory threw an exception * @throws std::runtime_error If this BundleContext is no longer valid. * @throws std::invalid_argument If the specified * ServiceReferenceBase is invalid (default constructed). * @see ServiceFactory */ std::shared_ptr GetService(const ServiceReferenceBase& reference); InterfaceMapConstPtr GetService(const ServiceReferenceU& reference); /** * Returns the service object referenced by the specified * ServiceReference object. *

* This is a convenience method which is identical to void* GetService(const ServiceReferenceBase&) * except that it casts the service object to the supplied template argument type * * @tparam S The type the service object will be cast to. * @return A shared_ptr to the service object associated with reference. * An empty object is returned if the service is not registered, the * ServiceFactory threw an exception or the service could not be * cast to the desired type. * @throws std::runtime_error If this BundleContext is no * longer valid. * @throws std::invalid_argument If the specified * ServiceReference is invalid (default constructed). * @see #GetService(const ServiceReferenceBase&) * @see ServiceFactory */ template std::shared_ptr GetService(const ServiceReference& reference) { const ServiceReferenceBase& baseRef = reference; return std::static_pointer_cast(GetService(baseRef)); } /** * Returns the ServiceObjects object for the service referenced by the specified * ServiceReference object. The ServiceObjects object can be used to obtain * multiple service objects for services with prototype scope. For services with * singleton or bundle scope, the ServiceObjects::GetService() method behaves * the same as the GetService(const ServiceReference&) method and the * ServiceObjects::UngetService(const ServiceReferenceBase&) method behaves the * same as the UngetService(const ServiceReferenceBase&) method. That is, only one, * use-counted service object is available from the ServiceObjects object. * * @tparam S Type of Service. * @param reference A reference to the service. * @return A ServiceObjects object for the service associated with the specified * reference or an invalid instance if the service is not registered. * @throws std::runtime_error If this BundleContext is no longer valid. * @throws std::invalid_argument If the specified ServiceReference is invalid * (default constructed or the service has been unregistered) * * @see PrototypeServiceFactory */ template ServiceObjects GetServiceObjects(const ServiceReference& reference) { return ServiceObjects(d, reference); } /** * Adds the specified listener with the * specified filter to the context bundles's list of listeners. * See LDAPFilter for a description of the filter syntax. Listeners * are notified when a service has a lifecycle state change. * *

* The framework takes care of removing all listeners registered by this * context bundle's classes after the bundle is stopped. * *

* The listener is called if the filter criteria is met. * To filter based upon the class of the service, the filter should reference * the Constants#OBJECTCLASS property. If filter is * empty, all services are considered to match the filter. * *

* When using a filter, it is possible that the * ServiceEvents for the complete lifecycle of a service * will not be delivered to the listener. For example, if the * filter only matches when the property example_property * has the value 1, the listener will not be called if the * service is registered with the property example_property not * set to the value 1. Subsequently, when the service is modified * setting property example_property to the value 1, * the filter will match and the listener will be called with a * ServiceEvent of type SERVICE_MODIFIED. Thus, the * listener will not be called with a ServiceEvent of type * SERVICE_REGISTERED. * * @param listener Any callable object. * @param filter The filter criteria. * @returns a ListenerToken object which can be used to remove the * listener from the list of registered listeners. * @throws std::invalid_argument If filter contains an * invalid filter string that cannot be parsed. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see ServiceEvent * @see ServiceListener * @see RemoveServiceListener() */ ListenerToken AddServiceListener(const ServiceListener& listener, const std::string& filter = std::string()); /** * Removes the specified listener from the context bundle's * list of listeners. * *

* If the listener is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @param listener The callable object to remove. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see AddServiceListener() */ US_DEPRECATED void RemoveServiceListener(const ServiceListener& listener); /** * Adds the specified listener to the context bundles's list * of listeners. Listeners are notified when a bundle has a lifecycle * state change. * * @param listener Any callable object. * @returns a ListenerToken object which can be used to remove the * listener from the list of registered listeners. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see BundleEvent * @see BundleListener */ ListenerToken AddBundleListener(const BundleListener& listener); /** * Removes the specified listener from the context bundle's * list of listeners. * *

* If the listener is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @param listener The callable object to remove. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see AddBundleListener() * @see BundleListener */ US_DEPRECATED void RemoveBundleListener(const BundleListener& listener); /** * Adds the specified listener to the context bundles's list * of framework listeners. Listeners are notified of framework events. * * @param listener Any callable object. * @returns a ListenerToken object which can be used to remove the * listener from the list of registered listeners. * @throws std::runtime_error If this BundleContext is no longer valid. * @see FrameworkEvent * @see FrameworkListener */ ListenerToken AddFrameworkListener(const FrameworkListener& listener); /** * Removes the specified listener from the context bundle's * list of framework listeners. * *

* If the listener is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @param listener The callable object to remove. * @throws std::runtime_error If this BundleContext is no longer valid. * @see AddFrameworkListener() * @see FrameworkListener */ US_DEPRECATED void RemoveFrameworkListener(const FrameworkListener& listener); /** * Removes the registered listener associated with the token * *

* If the listener associated with the token is not contained in this * context bundle's list of listeners or if token is an invalid token, * this method does nothing. * *

* The token can correspond to one of Service, Bundle or Framework listeners. Using * this function to remove the registered listeners is the recommended approach over * using any of the other deprecated functions - * Remove{Bundle,Framework,Service}Listener. * * @param token is an object of type ListenerToken. * @throws std::runtime_error If this BundleContext is no longer valid. * @see AddServiceListener() * @see AddBundleListener() * @see AddFrameworkListener() * */ void RemoveListener(ListenerToken token); /** * Adds the specified callback with the * specified filter to the context bundles's list of listeners. * See LDAPFilter for a description of the filter syntax. Listeners * are notified when a service has a lifecycle state change. * *

* You must take care to remove registered listeners before the receiver * object is destroyed. However, the Micro Services framework takes care * of removing all listeners registered by this context bundle's classes * after the bundle is stopped. * *

* If the context bundle's list of listeners already contains a pair (r,c) * of receiver and callback such that * (r == receiver && c == callback), then this * method replaces that callback's filter (which may be empty) * with the specified one (which may be empty). * *

* The callback is called if the filter criteria is met. To filter based * upon the class of the service, the filter should reference the * Constants#OBJECTCLASS property. If filter is * empty, all services are considered to match the filter. * *

* When using a filter, it is possible that the * ServiceEvents for the complete lifecycle of a service * will not be delivered to the callback. For example, if the * filter only matches when the property example_property * has the value 1, the callback will not be called if the * service is registered with the property example_property not * set to the value 1. Subsequently, when the service is modified * setting property example_property to the value 1, the * filter will match and the callback will be called with a * ServiceEvent of type SERVICE_MODIFIED. Thus, the * callback will not be called with a ServiceEvent of type * SERVICE_REGISTERED. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use `std::bind` to bind the member function and then pass the result * to :any:`AddServiceListener(const ServiceListener&) ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be called) * @param receiver The object to connect to. * @param callback The member function pointer to call. * @param filter The filter criteria. * @returns a ListenerToken object which can be used to remove the callable from the * registered listeners. * @throws std::invalid_argument If filter contains an * invalid filter string that cannot be parsed. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see ServiceEvent * @see RemoveServiceListener() */ template US_DEPRECATED ListenerToken AddServiceListener(R* receiver, void (R::*callback)(const ServiceEvent&), const std::string& filter = std::string()) { return AddServiceListener(ServiceListenerMemberFunctor(receiver, callback), static_cast(receiver), filter); } /** * Removes the specified callback from the context bundle's * list of listeners. * *

* If the (receiver,callback) pair is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be removed) * @param receiver The object from which to disconnect. * @param callback The member function pointer to remove. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see AddServiceListener() */ template US_DEPRECATED void RemoveServiceListener( R* receiver, void (R::*callback)(const ServiceEvent&)) { RemoveServiceListener(ServiceListenerMemberFunctor(receiver, callback), static_cast(receiver)); } /** * Adds the specified callback to the context bundles's list * of listeners. Listeners are notified when a bundle has a lifecycle * state change. * *

* If the context bundle's list of listeners already contains a pair (r,c) * of receiver and callback such that * (r == receiver && c == callback), then this method does nothing. * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use `std::bind` to bind the member function and then pass the result to * :any:`AddBundleListener(const BundleListener&) ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be called) * @param receiver The object to connect to. * @param callback The member function pointer to call. * @returns a ListenerToken object which can be used to remove the callable from the * registered listeners. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see BundleEvent */ template US_DEPRECATED ListenerToken AddBundleListener(R* receiver, void (R::*callback)(const BundleEvent&)) { return AddBundleListener(BundleListenerMemberFunctor(receiver, callback), static_cast(receiver)); } /** * Removes the specified callback from the context bundle's * list of listeners. * *

* If the (receiver,callback) pair is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be removed) * @param receiver The object from which to disconnect. * @param callback The member function pointer to remove. * @throws std::runtime_error If this BundleContext is no * longer valid. * @see AddBundleListener() */ template US_DEPRECATED void RemoveBundleListener( R* receiver, void (R::*callback)(const BundleEvent&)) { RemoveBundleListener(BundleListenerMemberFunctor(receiver, callback), static_cast(receiver)); } /** * Adds the specified callback to the context bundles's list * of framework listeners. Listeners are notified of framework events. * *

* If the context bundle's list of listeners already contains a pair (r,c) * of receiver and callback such that * (r == receiver && c == callback), then this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use `std::bind` to bind the member function and then pass the result to * :any:`AddFrameworkListener(const FrameworkListener&) ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be called) * @param receiver The object to connect to. * @param callback The member function pointer to call. * @returns a ListenerToken object which can be used to remove the callable from the * registered listeners. * @throws std::runtime_error If this BundleContext is no longer valid. * @see FrameworkEvent */ template US_DEPRECATED ListenerToken AddFrameworkListener(R* receiver, void (R::*callback)(const FrameworkEvent&)) { return AddFrameworkListener( BindFrameworkListenerToFunctor(receiver, callback)); } /** * Removes the specified callback from the context bundle's * list of framework listeners. * *

* If the (receiver,callback) pair is not contained in this * context bundle's list of listeners, this method does nothing. * * \rststar * .. deprecated:: 3.1.0 * * This function exists only to maintain backwards compatibility * and will be removed in the next major release. * Use :any:`RemoveListener() ` instead. * \endrststar * * @tparam R The type of the receiver (containing the member function to be removed) * @param receiver The object from which to disconnect. * @param callback The member function pointer to remove. * @throws std::runtime_error If this BundleContext is no longer valid. * @see AddFrameworkListener() */ template US_DEPRECATED void RemoveFrameworkListener( R* receiver, void (R::*callback)(const FrameworkEvent&)) { RemoveFrameworkListener(BindFrameworkListenerToFunctor(receiver, callback)); } /** * Get the absolute path for a file or directory in the persistent * storage area provided for the bundle. * * The absolute path for the base directory of the persistent storage * area provided for the context bundle by the Framework can be obtained by * calling this method with an empty string as \c filename. * * @param filename A relative name to the file or directory to be accessed. * @return The absolute path to the persistent storage area for the given file name. * @throws std::runtime_error If this BundleContext is no longer valid. * @throws std::invalid_argument If the input param filename is not a valid * UTF-8 string. */ std::string GetDataFile(const std::string& filename) const; /** * Installs all bundles from the bundle library at the specified location. * * The following steps are required to install a bundle: * -# If a bundle containing the same install location is already installed, the Bundle object for that * bundle is returned. * -# The bundle's associated resources are allocated. The associated resources minimally consist of a * unique identifier and a persistent storage area if the platform has file system support. If this step * fails, a std::runtime_error is thrown. * -# A bundle event of type BundleEvent::BUNDLE_INSTALLED is fired. * -# The Bundle object for the newly or previously installed bundle is returned. * * @remarks An install location is an absolute path to a shared library or executable file * which may contain several bundles, i. e. acts as a bundle library. * * @param location The location of the bundle library to install. * @return The Bundle objects of the installed bundle library. * @throws std::runtime_error If the BundleContext is no longer valid, or if the installation failed. * @throws std::logic_error If the framework instance is no longer active * @throws std::invalid_argument If the location is not a valid UTF8 string */ std::vector InstallBundles(const std::string& location); private: friend US_Framework_EXPORT BundleContext MakeBundleContext(BundleContextPrivate*); friend BundleContext MakeBundleContext( const std::shared_ptr&); friend std::shared_ptr GetPrivate(const BundleContext&); BundleContext(const std::shared_ptr& ctx); // allow templated code to use the internal logger template friend class detail::BundleAbstractTracked; template friend class ServiceTracker; template friend class detail::ServiceTrackerPrivate; template friend class detail::TrackedService; friend class BundleResource; // Not for use by clients of the Framework. // Provides access to the Framework's log sink to allow templated code // to log diagnostic information. std::shared_ptr GetLogSink() const; ListenerToken AddServiceListener(const ServiceListener& delegate, void* data, const std::string& filter); void RemoveServiceListener(const ServiceListener& delegate, void* data); ListenerToken AddBundleListener(const BundleListener& delegate, void* data); void RemoveBundleListener(const BundleListener& delegate, void* data); std::shared_ptr d; }; } // namespace cppmicroservices #endif /* CPPMICROSERVICES_BUNDLECONTEXT_H */