Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usModuleContext.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
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 #include "usModuleContext.h"
23 
24 #include "usModuleRegistry.h"
25 #include "usModulePrivate.h"
26 #include "usModuleSettings.h"
27 #include "usCoreModuleContext_p.h"
28 #include "usServiceRegistry_p.h"
29 #include "usServiceReferenceBasePrivate.h"
30 
31 #include <stdio.h>
32 
33 US_BEGIN_NAMESPACE
34 
35 class ModuleContextPrivate {
36 
37 public:
38 
39  ModuleContextPrivate(ModulePrivate* module)
40  : module(module)
41  {}
42 
43  ModulePrivate* module;
44 };
45 
46 
47 ModuleContext::ModuleContext(ModulePrivate* module)
48  : d(new ModuleContextPrivate(module))
49 {}
50 
52 {
53  delete d;
54 }
55 
56 Module* ModuleContext::GetModule() const
57 {
58  return d->module->q;
59 }
60 
61 Module* ModuleContext::GetModule(long id) const
62 {
63  return d->module->coreCtx->moduleHooks.FilterModule(this, ModuleRegistry::GetModule(id));
64 }
65 
66 Module* ModuleContext::GetModule(const std::string& name)
67 {
68  return ModuleRegistry::GetModule(name);
69 }
70 
71 std::vector<Module*> ModuleContext::GetModules() const
72 {
73  std::vector<Module*> modules = ModuleRegistry::GetModules();
74  d->module->coreCtx->moduleHooks.FilterModules(this, modules);
75  return modules;
76 }
77 
79  const ServiceProperties& properties)
80 {
81  return d->module->coreCtx->services.RegisterService(d->module, service, properties);
82 }
83 
84 std::vector<ServiceReferenceU > ModuleContext::GetServiceReferences(const std::string& clazz,
85  const std::string& filter)
86 {
87  std::vector<ServiceReferenceU> result;
88  std::vector<ServiceReferenceBase> refs;
89  d->module->coreCtx->services.Get(clazz, filter, d->module, refs);
90  for (std::vector<ServiceReferenceBase>::const_iterator iter = refs.begin();
91  iter != refs.end(); ++iter)
92  {
93  result.push_back(ServiceReferenceU(*iter));
94  }
95  return result;
96 }
97 
99 {
100  return d->module->coreCtx->services.Get(d->module, clazz);
101 }
102 
103 void* ModuleContext::GetService(const ServiceReferenceBase& reference)
104 {
105  if (!reference)
106  {
107  throw std::invalid_argument("Default constructed ServiceReference is not a valid input to GetService()");
108  }
109  return reference.d->GetService(d->module->q);
110 }
111 
113 {
114  if (!reference)
115  {
116  throw std::invalid_argument("Default constructed ServiceReference is not a valid input to GetService()");
117  }
118  return reference.d->GetServiceInterfaceMap(d->module->q);
119 }
120 
121 bool ModuleContext::UngetService(const ServiceReferenceBase& reference)
122 {
123  ServiceReferenceBase ref = reference;
124  return ref.d->UngetService(d->module->q, true);
125 }
126 
128  const std::string& filter)
129 {
130  d->module->coreCtx->listeners.AddServiceListener(this, delegate, NULL, filter);
131 }
132 
134 {
135  d->module->coreCtx->listeners.RemoveServiceListener(this, delegate, NULL);
136 }
137 
139 {
140  d->module->coreCtx->listeners.AddModuleListener(this, delegate, NULL);
141 }
142 
144 {
145  d->module->coreCtx->listeners.RemoveModuleListener(this, delegate, NULL);
146 }
147 
148 void ModuleContext::AddServiceListener(const ServiceListener& delegate, void* data,
149  const std::string &filter)
150 {
151  d->module->coreCtx->listeners.AddServiceListener(this, delegate, data, filter);
152 }
153 
154 void ModuleContext::RemoveServiceListener(const ServiceListener& delegate, void* data)
155 {
156  d->module->coreCtx->listeners.RemoveServiceListener(this, delegate, data);
157 }
158 
159 void ModuleContext::AddModuleListener(const ModuleListener& delegate, void* data)
160 {
161  d->module->coreCtx->listeners.AddModuleListener(this, delegate, data);
162 }
163 
164 void ModuleContext::RemoveModuleListener(const ModuleListener& delegate, void* data)
165 {
166  d->module->coreCtx->listeners.RemoveModuleListener(this, delegate, data);
167 }
168 
169 std::string ModuleContext::GetDataFile(const std::string &filename) const
170 {
171  // compute the module storage path
172 #ifdef US_PLATFORM_WINDOWS
173  static const char separator = '\\';
174 #else
175  static const char separator = '/';
176 #endif
177 
178  std::string baseStoragePath = ModuleSettings::GetStoragePath();
179  if (baseStoragePath.empty()) return std::string();
180  if (baseStoragePath != d->module->baseStoragePath)
181  {
182  d->module->baseStoragePath = baseStoragePath;
183  d->module->storagePath.clear();
184  }
185 
186  if (d->module->storagePath.empty())
187  {
188  char buf[50];
189  sprintf(buf, "%ld", d->module->info.id);
190  d->module->storagePath = baseStoragePath + separator + buf + "_" + d->module->info.name + separator;
191  }
192  return d->module->storagePath + filename;
193 }
194 
195 
196 US_END_NAMESPACE
ServiceReference< void > ServiceReferenceU
Definition: usModule.h:40
ServiceRegistrationU RegisterService(const InterfaceMap &service, const ServiceProperties &properties=ServiceProperties())
void RemoveModuleListener(const ModuleListener &delegate)
bool UngetService(const ServiceReferenceBase &reference)
ServiceReference< S > GetServiceReference()
std::map< std::string, void * > InterfaceMap
void * GetService(const ServiceReferenceBase &reference)
US_MODULE_LISTENER_FUNCTOR ModuleListener
Module * GetModule() const
static const std::string filename
std::string GetDataFile(const std::string &filename) const
ServiceRegistration< void > ServiceRegistrationU
US_BEGIN_NAMESPACE typedef US_SERVICE_LISTENER_FUNCTOR ServiceListener
std::vector< ServiceReferenceU > GetServiceReferences(const std::string &clazz, const std::string &filter=std::string())
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
std::vector< Module * > GetModules() const
void AddModuleListener(const ModuleListener &delegate)
void RemoveServiceListener(const ServiceListener &delegate)
void AddServiceListener(const ServiceListener &delegate, const std::string &filter=std::string())