Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
usModuleContext.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef USMODULECONTEXT_H_
14 #define USMODULECONTEXT_H_
15 
16 // TODO: Replace includes with forward directives!
17 
18 #include <usListenerFunctors_p.h>
19 #include <usServiceInterface.h>
20 #include <usServiceEvent.h>
21 #include <usServiceRegistration.h>
22 #include <usServiceException.h>
23 #include <usModuleEvent.h>
24 
25 namespace us {
26 
27 typedef US_SERVICE_LISTENER_FUNCTOR ServiceListener;
28 typedef US_MODULE_LISTENER_FUNCTOR ModuleListener;
29 
30 class ModuleContextPrivate;
31 class ServiceFactory;
32 
33 template<class S> class ServiceObjects;
34 
83 {
84 
85 public:
86 
89 
99  Module* GetModule() const;
100 
108  Module* GetModule(long id) const;
109 
116  Module* GetModule(const std::string& name);
117 
128  std::vector<Module*> GetModules() const;
129 
195  const ServiceProperties& properties = ServiceProperties());
196 
223  template<class S>
225  {
226  InterfaceMap servicePointers = MakeInterfaceMap<S>(service);
227  return RegisterService(servicePointers, properties);
228  }
229 
257  template<class I1, class I2, class Impl>
259  {
260  InterfaceMap servicePointers = MakeInterfaceMap<I1, I2>(impl);
261  return RegisterService(servicePointers, properties);
262  }
263 
286  template<class I1, class I2, class I3, class Impl>
288  {
289  InterfaceMap servicePointers = MakeInterfaceMap<I1, I2, I3>(impl);
290  return RegisterService(servicePointers, properties);
291  }
292 
319  template<class S>
321  {
322  InterfaceMap servicePointers = MakeInterfaceMap<S>(factory);
323  return RegisterService(servicePointers, properties);
324  }
325 
351  template<class I1, class I2>
353  {
354  InterfaceMap servicePointers = MakeInterfaceMap<I1,I2>(factory);
355  return RegisterService(servicePointers, properties);
356  }
357 
380  template<class I1, class I2, class I3>
382  {
383  InterfaceMap servicePointers = MakeInterfaceMap<I1,I2,I3>(factory);
384  return RegisterService(servicePointers, properties);
385  }
386 
432  std::vector<ServiceReferenceU> GetServiceReferences(const std::string& clazz, const std::string& filter = std::string());
433 
457  template<class S>
458  std::vector<ServiceReference<S> > GetServiceReferences(const std::string& filter = std::string())
459  {
460  std::string clazz = us_service_interface_iid<S>();
461  if (clazz.empty()) throw ServiceException("The service interface class has no US_DECLARE_SERVICE_INTERFACE macro");
462  typedef std::vector<ServiceReferenceU> BaseVectorT;
463  BaseVectorT serviceRefs = GetServiceReferences(clazz, filter);
464  std::vector<ServiceReference<S> > result;
465  for(BaseVectorT::const_iterator i = serviceRefs.begin(); i != serviceRefs.end(); ++i)
466  {
467  result.push_back(ServiceReference<S>(*i));
468  }
469  return result;
470  }
471 
503  ServiceReferenceU GetServiceReference(const std::string& clazz);
504 
521  template<class S>
523  {
524  std::string clazz = us_service_interface_iid<S>();
525  if (clazz.empty()) throw ServiceException("The service interface class has no US_DECLARE_SERVICE_INTERFACE macro");
526  return ServiceReference<S>(GetServiceReference(clazz));
527  }
528 
579  void* GetService(const ServiceReferenceBase& reference);
580 
591 
612  template<class S>
613  S* GetService(const ServiceReference<S>& reference)
614  {
615  const ServiceReferenceBase& baseRef = reference;
616  return reinterpret_cast<S*>(GetService(baseRef));
617  }
618 
639  template<class S>
641  {
642  return ServiceObjects<S>(this, reference);
643  }
644 
680  bool UngetService(const ServiceReferenceBase& reference);
681 
691  void AddServiceListener(const ServiceListener& delegate,
692  const std::string& filter = std::string());
693 
701  void RemoveServiceListener(const ServiceListener& delegate);
702 
711  void AddModuleListener(const ModuleListener& delegate);
712 
720  void RemoveModuleListener(const ModuleListener& delegate);
721 
772  template<class R>
773  void AddServiceListener(R* receiver, void(R::*callback)(const ServiceEvent),
774  const std::string& filter = std::string())
775  {
776  AddServiceListener(ServiceListenerMemberFunctor(receiver, callback),
777  static_cast<void*>(receiver), filter);
778  }
779 
795  template<class R>
796  void RemoveServiceListener(R* receiver, void(R::*callback)(const ServiceEvent))
797  {
798  RemoveServiceListener(ServiceListenerMemberFunctor(receiver, callback),
799  static_cast<void*>(receiver));
800  }
801 
819  template<class R>
820  void AddModuleListener(R* receiver, void(R::*callback)(const ModuleEvent))
821  {
822  AddModuleListener(ModuleListenerMemberFunctor(receiver, callback),
823  static_cast<void*>(receiver));
824  }
825 
841  template<class R>
842  void RemoveModuleListener(R* receiver, void(R::*callback)(const ModuleEvent))
843  {
844  RemoveModuleListener(ModuleListenerMemberFunctor(receiver, callback),
845  static_cast<void*>(receiver));
846  }
847 
859  std::string GetDataFile(const std::string& filename) const;
860 
861 
862 private:
863 
864  friend class Module;
865  friend class ModulePrivate;
866 
867  ModuleContext(ModulePrivate* module);
868 
869  // purposely not implemented
871  ModuleContext& operator=(const ModuleContext&);
872 
873  void AddServiceListener(const ServiceListener& delegate, void* data,
874  const std::string& filter);
875  void RemoveServiceListener(const ServiceListener& delegate, void* data);
876 
877  void AddModuleListener(const ModuleListener& delegate, void* data);
878  void RemoveModuleListener(const ModuleListener& delegate, void* data);
879 
880  ModuleContextPrivate * const d;
881 };
882 
883 }
884 
885 #endif /* USMODULECONTEXT_H_ */
#define MITKCPPMICROSERVICES_EXPORT
void * GetService(const ServiceReferenceBase &reference)
Returns the service object referenced by the specified ServiceReferenceBase object.
std::vector< ServiceReferenceU > 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 ...
ServiceReferenceU GetServiceReference(const std::string &clazz)
Returns a ServiceReference object for a service that implements and was registered under the specifie...
void RemoveServiceListener(const ServiceListener &delegate)
Removes the specified service listener from the context module's list of listeners.
void AddModuleListener(const ModuleListener &delegate)
Adds the specified module listener to the context module's list of listeners.
ServiceRegistration< I1, I2 > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
Registers the specified service factory as a service with the specified properties using the specifie...
Module * GetModule(const std::string &name)
Get the module that with the specified module name.
ServiceRegistration< S > RegisterService(S *service, const ServiceProperties &properties=ServiceProperties())
Registers the specified service object with the specified properties using the specified template arg...
ServiceRegistration< S > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
Registers the specified service factory as a service with the specified properties using the specifie...
void RemoveModuleListener(const ModuleListener &delegate)
Removes the specified module listener from the context module's list of listeners.
ServiceRegistration< I1, I2, I3 > RegisterService(Impl *impl, const ServiceProperties &properties=ServiceProperties())
Registers the specified service object with the specified properties using the specified template arg...
bool UngetService(const ServiceReferenceBase &reference)
Releases the service object referenced by the specified ServiceReference object. If the context modul...
ServiceRegistrationU RegisterService(const InterfaceMap &service, const ServiceProperties &properties=ServiceProperties())
Registers the specified service object with the specified properties under the specified class names ...
ServiceObjects< S > GetServiceObjects(const ServiceReference< S > &reference)
Returns the ServiceObjects object for the service referenced by the specified ServiceReference object...
void AddServiceListener(const ServiceListener &delegate, const std::string &filter=std::string())
Adds the specified service listener with the specified filter to the context module's list of listene...
ServiceReference< S > GetServiceReference()
Returns a ServiceReference object for a service that implements and was registered under the specifie...
~ModuleContext()
Destructor.
InterfaceMap GetService(const ServiceReferenceU &reference)
Returns the service interface map for the service referenced by the specified ServiceReferenceU objec...
ServiceRegistration< I1, I2, I3 > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
Registers the specified service factory as a service with the specified properties using the specifie...
S * GetService(const ServiceReference< S > &reference)
Returns the service object referenced by the specified ServiceReference object.
void RemoveModuleListener(R *receiver, void(R::*callback)(const ModuleEvent))
Removes the specified callback from the context module's list of listeners.
Module * GetModule() const
Returns the Module object associated with this ModuleContext. This module is called the context modul...
std::string GetDataFile(const std::string &filename) const
Get the absolute path for a file or directory in the persistent storage area provided for the module....
std::vector< ServiceReference< S > > GetServiceReferences(const std::string &filter=std::string())
Returns a list of ServiceReference objects. The returned list contains services that were registered ...
void RemoveServiceListener(R *receiver, void(R::*callback)(const ServiceEvent))
Removes the specified callback from the context module's list of listeners.
ServiceRegistration< I1, I2 > RegisterService(Impl *impl, const ServiceProperties &properties=ServiceProperties())
Registers the specified service object with the specified properties using the specified template arg...
void AddServiceListener(R *receiver, void(R::*callback)(const ServiceEvent), const std::string &filter=std::string())
Adds the specified callback with the specified filter to the context modules's list of listeners....
Module * GetModule(long id) const
Returns the module with the specified identifier.
void AddModuleListener(R *receiver, void(R::*callback)(const ModuleEvent))
Adds the specified callback to the context modules's list of listeners. Listeners are notified when a...
std::vector< Module * > GetModules() const
Returns a list of all known modules.
An event from the Micro Services framework describing a module lifecycle change.
Definition: usModuleEvent.h:38
Represents a CppMicroServices module.
Definition: usModule.h:70
Allows multiple service objects for a service to be obtained.
std::map< std::string, void * > InterfaceMap
A map containing interfaces ids and their corresponding service object pointers.
std::unordered_map< std::string, Any > ServiceProperties
US_MODULE_LISTENER_FUNCTOR ModuleListener
US_SERVICE_LISTENER_FUNCTOR ServiceListener
Helper class for constructing InterfaceMap instances based on service implementations or service fact...