NAP
modulecache.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7  // Local Includes
8 #include "projectinfo.h"
9 
10 // External Includes
11 #include <utility/module.h>
12 #include <assert.h>
13 #include <vector>
14 #include <string>
15 #include <mutex>
16 
17 namespace nap
18 {
19  // Forward declare
20  class ModuleCache;
21 
26  class NAPAPI Module final
27  {
28  friend class ModuleCache;
29  public:
30  Module() = default;
31 
32  // Copy and move is not allowed
33  Module(Module&) = delete;
34  Module& operator=(const Module&) = delete;
35  Module(Module&&) = delete;
36  Module& operator=(Module&&) = delete;
37 
41  const std::string& getName() const { return mName; }
42 
46  const ModuleDescriptor& getDescriptor() const { assert(mDescriptor != nullptr); return *mDescriptor; }
47 
51  const ModuleInfo& getInformation() const { assert(mInfo != nullptr); return *mInfo; }
52 
58  std::string findAsset(const std::string& name) const;
59 
64  const rtti::TypeInfo getServiceType() const { return mService; }
65 
70  const std::vector<const nap::Module*> getDependencies() const { return mDependencies; }
71 
72  private:
73  std::string mName;
74  ModuleDescriptor* mDescriptor = nullptr;
75  std::unique_ptr<ModuleInfo> mInfo = nullptr;
76  void* mHandle = nullptr;
77  rtti::TypeInfo mService = rtti::TypeInfo::empty();
78  std::vector<const nap::Module*> mDependencies;
79  };
80 
81 
83  // Module Cache
85 
95  class NAPAPI ModuleCache final
96  {
97  public:
104  class Handle;
105  static Handle getHandle();
106 
107  // Copy and move is not allowed
108  ModuleCache(ModuleCache&) = delete;
109  ModuleCache& operator=(const ModuleCache&) = delete;
110  ModuleCache(ModuleCache&&) = delete;
111  ModuleCache& operator=(ModuleCache&&) = delete;
112 
118  class Handle final
119  {
120  friend class ModuleCache;
121  public:
129  const nap::Module* sourceModule(const ProjectInfo& projectinfo, const std::string& moduleName, utility::ErrorState& err);
130 
131  // Copy is not allowed
132  Handle(Handle&) = delete;
133  Handle& operator=(const Handle&) = delete;
134 
135  // Move is allowed
136  Handle(Handle&& other) noexcept;
137  Handle& operator=(Handle&& other) noexcept;
138 
139  private:
140  Handle(nap::ModuleCache& cache, std::mutex& mutex);
141  std::unique_lock<std::mutex> mLock;
142  nap::ModuleCache* mCache = nullptr;
143  };
144 
145  private:
146  // Hide constructor
147  ModuleCache() = default;
148 
149  // Attempts to load a NAP module into the current NAP process.
150  const nap::Module* sourceModule(const ProjectInfo& projectinfo, const std::string& moduleName, utility::ErrorState& err);
151 
152  // Find a loaded module by its name as defined in its descriptor file
153  const Module* findModule(const std::string& moduleName) const;
154 
155  // List of unique modules loaded in current host process
156  std::vector<std::unique_ptr<Module>> mModules;
157  };
158 }
159 
nap::ModuleDescriptor
Definition: utility/src/utility/module.h:44
nap::Module::getInformation
const ModuleInfo & getInformation() const
Definition: modulecache.h:51
nap::utility::ErrorState
Definition: errorstate.h:19
nap::Module::getName
const std::string & getName() const
Definition: modulecache.h:41
nap::Module::getDependencies
const std::vector< const nap::Module * > getDependencies() const
Definition: modulecache.h:70
nap::ModuleInfo
Definition: projectinfo.h:204
nap::ModuleCache::Handle
Definition: modulecache.h:118
nap
Definition: templateapp.h:17
nap::ModuleCache
Definition: modulecache.h:95
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:141
nap::Module::getServiceType
const rtti::TypeInfo getServiceType() const
Definition: modulecache.h:64
nap::ProjectInfo
Definition: projectinfo.h:53
nap::Module
Definition: modulecache.h:26
nap::Module::getDescriptor
const ModuleDescriptor & getDescriptor() const
Definition: modulecache.h:46