Cogs.Core
ExtensionRegistry.h
1#pragma once
2
3#include "Base.h"
4
5#include "Context.h"
6#include "Engine.h"
7
8#include "CogsVersion.h"
9
10#include "Foundation/Platform/Module.h"
11
12#include <vector>
13#include <deque>
14
15namespace Cogs
16{
17 namespace Core
18 {
53 struct COGSCORE_DLL_API Extension
54 {
62 virtual bool initializeStatic() { return true; }
63
73 virtual bool initialize(Context * /*context*/) { return true; }
74
80 virtual void cleanup(Context * /*context*/) {}
81
88 virtual const char * getExtensionKey() const = 0;
89
93 virtual void * getSymbolPointer(const char* /*symbol*/) const { return nullptr; }
94
100 virtual const void * getPublicAPI() const { return reinterpret_cast<const void*>(-1); }
101 };
102
106 class COGSCORE_DLL_API ExtensionRegistry
107 {
108 public:
115 static void add(Extension * extension, StringView version);
116
123 static bool hasExtension(const StringView & key, bool silent = false);
124
132 static void* getExtensionSymbol(const char* extension, const char* name);
133
138 static void initializeStatic();
139
144 static void initialize(Context * context);
145
149 static void cleanup(Context * context);
150
154 static void remove(Context * context);
155
158 {
160 Success,
162 Failure,
165 NoExtensionsFound,
166 };
167
177 static const void * loadExtensionModule(const std::string & path, void ** modulehandle = nullptr, ExtensionModuleLoadResult * result = nullptr);
178
187 template<typename API>
188 static ModuleAPI<API> loadExtensionModule(const std::string& path) {
189 void* moduleHandle = nullptr;
190 const void* api = loadExtensionModule(path, &moduleHandle);
191
192 if (moduleHandle && api && (api != reinterpret_cast<void*>(-1))) {
193 return ModuleAPI<API>(moduleHandle, api);
194 }
195 return ModuleAPI<API>();
196 }
197
207 template<typename ComponentSystemType>
208 static ComponentSystemType * registerExtensionSystem(Context * context, SystemPriority::ESystemPriority systemPriority, uint32_t capacity)
209 {
210 auto system = context->engine->registerSystem<ComponentSystemType>(systemPriority, nullptr, capacity);
211
212 context->registerExtensionSystem(system->getComponentType(), system);
213
214 return system;
215 }
216
226 template<typename ComponentSystemType>
227 static ComponentSystemType * getExtensionSystem(Context * context)
228 {
229 return static_cast<ComponentSystemType *>(context->getExtensionSystem(ComponentSystemType::getTypeId()));
230 }
231
232 private:
236 static std::vector<Extension *> & getExtensions();
237
238 static std::deque<Extension *> & getDelayedExtensions();
239
240 static std::vector<Context *> contexts;
241 static bool initialized;
242 static bool locked;
243 };
244 }
245}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
class ComponentSystemBase * getExtensionSystem(const uint16_t id)
Retrieve the system with the given id.
Definition: Context.cpp:415
void registerExtensionSystem(const uint16_t id, class ComponentSystemBase *system)
Register an extension component system using the given id.
Definition: Context.cpp:404
std::unique_ptr< class Engine > engine
Engine instance.
Definition: Context.h:222
Extension registry used to host and manage extension instances.
static ComponentSystemType * getExtensionSystem(Context *context)
Retrieve the extension component system of the given template type.
static ModuleAPI< API > loadExtensionModule(const std::string &path)
Loads the extension module with the given name.
static ComponentSystemType * registerExtensionSystem(Context *context, SystemPriority::ESystemPriority systemPriority, uint32_t capacity)
Registers an instance of the given component system type in the extension system registry.
ExtensionModuleLoadResult
Defines possible results of an extension module load.
Helper class for shared libraries that implement the getPublicAPI function and provide a structure of...
Definition: Module.h:113
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
@ Success
Resource activated successfully.
@ Failure
Resource activation failed.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Defines an extension to Cogs.Core and provides methods to override in order to initialize extension c...
virtual void cleanup(Context *)
Cleanup context bound extension content.
virtual bool initialize(Context *)
Initialize extension for the given context.
virtual const void * getPublicAPI() const
Retrieve a pointer to a struct containing all publicly available function pointers.
virtual bool initializeStatic()
Initialize extension statically.
virtual void * getSymbolPointer(const char *) const
Get pointer to symbol defined by extension.
virtual const char * getExtensionKey() const =0
Get the extensions unique key, used to check for extension presence and retrieve extension specific d...
ESystemPriority
Defines priority values for systems.
Definition: Engine.h:44