Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
usModuleContext.h
Go to the documentation of this file.
1 /*============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center (DKFZ)
6  All rights reserved.
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 ============================================================================*/
21 
22 #ifndef USMODULECONTEXT_H_
23 #define USMODULECONTEXT_H_
24 
25 // TODO: Replace includes with forward directives!
26 
27 #include "usListenerFunctors_p.h"
28 #include "usServiceInterface.h"
29 #include "usServiceEvent.h"
30 #include "usServiceRegistration.h"
31 #include "usServiceException.h"
32 #include "usModuleEvent.h"
33 
34 US_BEGIN_NAMESPACE
35 
36 typedef US_SERVICE_LISTENER_FUNCTOR ServiceListener;
37 typedef US_MODULE_LISTENER_FUNCTOR ModuleListener;
38 
39 class ModuleContextPrivate;
40 class ServiceFactory;
41 
42 template<class S> class ServiceObjects;
43 
91 class US_Core_EXPORT ModuleContext
92 {
93 
94 public:
95 
96  ~ModuleContext();
97 
107  Module* GetModule() const;
108 
116  Module* GetModule(long id) const;
117 
124  Module* GetModule(const std::string& name);
125 
136  std::vector<Module*> GetModules() const;
137 
202  ServiceRegistrationU RegisterService(const InterfaceMap& service,
203  const ServiceProperties& properties = ServiceProperties());
204 
231  template<class S>
232  ServiceRegistration<S> RegisterService(S* service, const ServiceProperties& properties = ServiceProperties())
233  {
234  InterfaceMap servicePointers = MakeInterfaceMap<S>(service);
235  return RegisterService(servicePointers, properties);
236  }
237 
265  template<class I1, class I2, class Impl>
266  ServiceRegistration<I1,I2> RegisterService(Impl* impl, const ServiceProperties& properties = ServiceProperties())
267  {
268  InterfaceMap servicePointers = MakeInterfaceMap<I1, I2>(impl);
269  return RegisterService(servicePointers, properties);
270  }
271 
294  template<class I1, class I2, class I3, class Impl>
295  ServiceRegistration<I1,I2,I3> RegisterService(Impl* impl, const ServiceProperties& properties = ServiceProperties())
296  {
297  InterfaceMap servicePointers = MakeInterfaceMap<I1, I2, I3>(impl);
298  return RegisterService(servicePointers, properties);
299  }
300 
327  template<class S>
328  ServiceRegistration<S> RegisterService(ServiceFactory* factory, const ServiceProperties& properties = ServiceProperties())
329  {
330  InterfaceMap servicePointers = MakeInterfaceMap<S>(factory);
331  return RegisterService(servicePointers, properties);
332  }
333 
359  template<class I1, class I2>
360  ServiceRegistration<I1,I2> RegisterService(ServiceFactory* factory, const ServiceProperties& properties = ServiceProperties())
361  {
362  InterfaceMap servicePointers = MakeInterfaceMap<I1,I2>(factory);
363  return RegisterService(servicePointers, properties);
364  }
365 
388  template<class I1, class I2, class I3>
389  ServiceRegistration<I1,I2,I3> RegisterService(ServiceFactory* factory, const ServiceProperties& properties = ServiceProperties())
390  {
391  InterfaceMap servicePointers = MakeInterfaceMap<I1,I2,I3>(factory);
392  return RegisterService(servicePointers, properties);
393  }
394 
440  std::vector<ServiceReferenceU> GetServiceReferences(const std::string& clazz, const std::string& filter = std::string());
441 
465  template<class S>
466  std::vector<ServiceReference<S> > GetServiceReferences(const std::string& filter = std::string())
467  {
468  std::string clazz = us_service_interface_iid<S>();
469  if (clazz.empty()) throw ServiceException("The service interface class has no US_DECLARE_SERVICE_INTERFACE macro");
470  typedef std::vector<ServiceReferenceU> BaseVectorT;
471  BaseVectorT serviceRefs = GetServiceReferences(clazz, filter);
472  std::vector<ServiceReference<S> > result;
473  for(BaseVectorT::const_iterator i = serviceRefs.begin(); i != serviceRefs.end(); ++i)
474  {
475  result.push_back(ServiceReference<S>(*i));
476  }
477  return result;
478  }
479 
511  ServiceReferenceU GetServiceReference(const std::string& clazz);
512 
529  template<class S>
530  ServiceReference<S> GetServiceReference()
531  {
532  std::string clazz = us_service_interface_iid<S>();
533  if (clazz.empty()) throw ServiceException("The service interface class has no US_DECLARE_SERVICE_INTERFACE macro");
534  return ServiceReference<S>(GetServiceReference(clazz));
535  }
536 
587  void* GetService(const ServiceReferenceBase& reference);
588 
589  InterfaceMap GetService(const ServiceReferenceU& reference);
590 
611  template<class S>
612  S* GetService(const ServiceReference<S>& reference)
613  {
614  const ServiceReferenceBase& baseRef = reference;
615  return reinterpret_cast<S*>(GetService(baseRef));
616  }
617 
638  template<class S>
639  ServiceObjects<S> GetServiceObjects(const ServiceReference<S>& reference)
640  {
641  return ServiceObjects<S>(this, reference);
642  }
643 
679  bool UngetService(const ServiceReferenceBase& reference);
680 
681  void AddServiceListener(const ServiceListener& delegate,
682  const std::string& filter = std::string());
683  void RemoveServiceListener(const ServiceListener& delegate);
684 
685  void AddModuleListener(const ModuleListener& delegate);
686  void RemoveModuleListener(const ModuleListener& delegate);
687 
738  template<class R>
739  void AddServiceListener(R* receiver, void(R::*callback)(const ServiceEvent),
740  const std::string& filter = std::string())
741  {
742  AddServiceListener(ServiceListenerMemberFunctor(receiver, callback),
743  static_cast<void*>(receiver), filter);
744  }
745 
761  template<class R>
762  void RemoveServiceListener(R* receiver, void(R::*callback)(const ServiceEvent))
763  {
764  RemoveServiceListener(ServiceListenerMemberFunctor(receiver, callback),
765  static_cast<void*>(receiver));
766  }
767 
785  template<class R>
786  void AddModuleListener(R* receiver, void(R::*callback)(const ModuleEvent))
787  {
788  AddModuleListener(ModuleListenerMemberFunctor(receiver, callback),
789  static_cast<void*>(receiver));
790  }
791 
807  template<class R>
808  void RemoveModuleListener(R* receiver, void(R::*callback)(const ModuleEvent))
809  {
810  RemoveModuleListener(ModuleListenerMemberFunctor(receiver, callback),
811  static_cast<void*>(receiver));
812  }
813 
825  std::string GetDataFile(const std::string& filename) const;
826 
827 
828 private:
829 
830  friend class Module;
831  friend class ModulePrivate;
832 
833  ModuleContext(ModulePrivate* module);
834 
835  // purposely not implemented
837  ModuleContext& operator=(const ModuleContext&);
838 
839  void AddServiceListener(const ServiceListener& delegate, void* data,
840  const std::string& filter);
841  void RemoveServiceListener(const ServiceListener& delegate, void* data);
842 
843  void AddModuleListener(const ModuleListener& delegate, void* data);
844  void RemoveModuleListener(const ModuleListener& delegate, void* data);
845 
846  ModuleContextPrivate * const d;
847 };
848 
849 US_END_NAMESPACE
850 
851 #endif /* USMODULECONTEXT_H_ */
ServiceRegistration< I1, I2 > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
ServiceReference< void > ServiceReferenceU
Definition: usModule.h:40
ServiceRegistration< I1, I2 > RegisterService(Impl *impl, const ServiceProperties &properties=ServiceProperties())
ServiceReference< S > GetServiceReference()
std::vector< ServiceReference< S > > GetServiceReferences(const std::string &filter=std::string())
std::map< std::string, void * > InterfaceMap
void AddServiceListener(R *receiver, void(R::*callback)(const ServiceEvent), const std::string &filter=std::string())
US_MODULE_LISTENER_FUNCTOR ModuleListener
S * GetService(const ServiceReference< S > &reference)
void RemoveServiceListener(R *receiver, void(R::*callback)(const ServiceEvent))
void AddModuleListener(R *receiver, void(R::*callback)(const ModuleEvent))
void RemoveModuleListener(R *receiver, void(R::*callback)(const ModuleEvent))
ServiceRegistration< void > ServiceRegistrationU
ServiceRegistration< S > RegisterService(S *service, const ServiceProperties &properties=ServiceProperties())
US_BEGIN_NAMESPACE typedef US_SERVICE_LISTENER_FUNCTOR ServiceListener
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
ServiceRegistration< I1, I2, I3 > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
ServiceRegistration< S > RegisterService(ServiceFactory *factory, const ServiceProperties &properties=ServiceProperties())
ServiceRegistration< I1, I2, I3 > RegisterService(Impl *impl, const ServiceProperties &properties=ServiceProperties())
ServiceObjects< S > GetServiceObjects(const ServiceReference< S > &reference)