27 #include "usModulePrivate.h" 30 #include "usCoreModuleContext_p.h" 32 #include "usCoreConfig.h" 36 const std::string& Module::PROP_ID()
38 static const std::string s(
"module.id");
41 const std::string& Module::PROP_NAME()
43 static const std::string s(
"module.name");
46 const std::string& Module::PROP_LOCATION()
48 static const std::string s(
"module.location");
51 const std::string& Module::PROP_VERSION()
53 static const std::string s(
"module.version");
57 const std::string&Module::PROP_VENDOR()
59 static const std::string s(
"module.vendor");
63 const std::string&Module::PROP_DESCRIPTION()
65 static const std::string s(
"module.description");
69 const std::string&Module::PROP_AUTOLOAD_DIR()
71 static const std::string s(
"module.autoload_dir");
75 const std::string&Module::PROP_AUTOLOADED_MODULES()
77 static const std::string s(
"module.autoloaded_modules");
92 void Module::Init(CoreModuleContext* coreCtx,
95 ModulePrivate* mp =
new ModulePrivate(
this, coreCtx, info);
100 void Module::Uninit()
102 if (d->moduleContext !=
nullptr)
105 d->RemoveModuleResources();
106 delete d->moduleContext;
107 d->moduleContext =
nullptr;
108 d->coreCtx->listeners.ModuleChanged(
ModuleEvent(ModuleEvent::UNLOADED,
this));
110 d->moduleActivator =
nullptr;
114 bool Module::IsLoaded()
const 116 return d->moduleContext !=
nullptr;
122 if (d->moduleContext)
124 US_WARN <<
"Module " << d->info.name <<
" already started.";
131 ModuleActivatorHook activatorHook =
nullptr;
133 std::string activator_func =
"_us_module_activator_instance_" + d->info.name;
134 void* activatorHookSym = ModuleUtils::GetSymbol(d->info, activator_func.c_str());
135 std::memcpy(&activatorHook, &activatorHookSym,
sizeof(
void*));
137 d->coreCtx->listeners.ModuleChanged(
ModuleEvent(ModuleEvent::LOADING,
this));
144 d->moduleActivator = activatorHook();
148 US_ERROR <<
"Creating the module activator of " << d->info.name <<
" failed";
156 d->moduleActivator->Load(d->moduleContext);
159 #ifdef US_ENABLE_AUTOLOADING_SUPPORT 160 if (ModuleSettings::IsAutoLoadingEnabled())
163 if (!loadedPaths.empty())
165 d->moduleManifest.SetValue(PROP_AUTOLOADED_MODULES(),
Any(loadedPaths));
170 d->coreCtx->listeners.ModuleChanged(
ModuleEvent(ModuleEvent::LOADED,
this));
175 if (d->moduleContext ==
nullptr)
177 US_WARN <<
"Module " << d->info.name <<
" already stopped.";
183 d->coreCtx->listeners.ModuleChanged(
ModuleEvent(ModuleEvent::UNLOADING,
this));
185 if (d->moduleActivator)
187 d->moduleActivator->Unload(d->moduleContext);
192 US_WARN <<
"Calling the module activator Unload() method of " << d->info.name <<
" failed!";
208 return d->moduleContext;
211 long Module::GetModuleId()
const 216 std::string Module::GetLocation()
const 218 return d->info.location;
231 Any Module::GetProperty(
const std::string& key)
const 233 return d->moduleManifest.GetValue(key);
236 std::vector<std::string> Module::GetPropertyKeys()
const 238 return d->moduleManifest.GetKeys();
241 std::vector<ServiceReferenceU> Module::GetRegisteredServices()
const 243 std::vector<ServiceRegistrationBase> sr;
244 std::vector<ServiceReferenceU> res;
245 d->coreCtx->services.GetRegisteredByModule(d, sr);
246 for (std::vector<ServiceRegistrationBase>::const_iterator i = sr.begin();
249 res.push_back(i->GetReference());
254 std::vector<ServiceReferenceU> Module::GetServicesInUse()
const 256 std::vector<ServiceRegistrationBase> sr;
257 std::vector<ServiceReferenceU> res;
258 d->coreCtx->services.GetUsedByModule(const_cast<Module*>(
this), sr);
259 for (std::vector<ServiceRegistrationBase>::const_iterator i = sr.begin();
262 res.push_back(i->GetReference());
269 if (!d->resourceContainer.IsValid())
274 if (result)
return result;
278 std::vector<ModuleResource> Module::FindResources(
const std::string& path,
const std::string& filePattern,
281 std::vector<ModuleResource> result;
282 if (!d->resourceContainer.IsValid())
287 std::string normalizedPath = path;
289 if (normalizedPath.empty()) normalizedPath.push_back(
'/');
290 if (*normalizedPath.begin() !=
'/') normalizedPath =
'/' + normalizedPath;
291 if (*normalizedPath.rbegin() !=
'/') normalizedPath.push_back(
'/');
292 d->resourceContainer.FindNodes(d->info.name + normalizedPath,
293 filePattern.empty() ?
"*" : filePattern,
302 std::ostream&
operator<<(std::ostream& os,
const Module& module)
304 os <<
"Module[" <<
"id=" << module.GetModuleId() <<
305 ", loc=" << module.GetLocation() <<
306 ", name=" << module.GetName() <<
"]";
310 std::ostream&
operator<<(std::ostream& os, Module
const * module)
static void info(const char *fmt,...)
std::vector< std::string > AutoLoadModules(const ModuleInfo &moduleInfo)
static void swap(T &x, T &y)
static std::string GetName(std::string fileName, std::string suffix)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
US_USE_NAMESPACE std::ostream & operator<<(std::ostream &os, const Module &module)