Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
berryIServiceLocator.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 BlueBerry Platform
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 
18 #ifndef BERRYISERVICELOCATOR_H_
19 #define BERRYISERVICELOCATOR_H_
20 
22 
23 #include <berryObject.h>
24 #include <berryMacros.h>
25 #include <berryLog.h>
26 
27 #include <QObject>
28 
29 #include <typeinfo>
30 
31 namespace berry {
32 
47 struct BERRY_UI_QT IServiceLocator : public virtual Object
48 {
49 
51 
52  ~IServiceLocator();
53 
63  template<class S>
65  {
66  const char* typeName = qobject_interface_iid<S*>();
67  if (typeName == nullptr)
68  {
69  BERRY_WARN << "Error getting service: Cannot get the interface id for type '" << Reflection::GetClassName<S>()
70  << "'. It is probably missing a Q_DECLARE_INTERFACE macro in its header.";
71  return nullptr;
72  }
73  Object* obj = this->GetService(typeName);
74  S* service = dynamic_cast<S*>(obj);
75  if (obj != nullptr && service == nullptr)
76  {
77  BERRY_WARN << "Error getting service: Class '" << obj->GetClassName() << "' cannot be cast to service interface "
78  << "'" << Reflection::GetClassName<S>() << "'";
79  }
80  return service;
81  }
82 
95  template<class S>
96  bool HasService() const
97  {
98  return this->HasService(qobject_interface_iid<S*>());
99  }
100 
101  virtual Object* GetService(const QString& api) = 0;
102  virtual bool HasService(const QString& api) const = 0;
103 };
104 
105 }
106 
107 #endif /* BERRYISERVICELOCATOR_H_ */
Light weight base class for most BlueBerry classes.
Definition: berryObject.h:78
#define berryObjectMacro(...)
Definition: berryMacros.h:37
virtual QString GetClassName() const
Definition: berryObject.cpp:75
#define BERRY_WARN
Definition: berryLog.h:25
#define BERRY_UI_QT