Cogs.Core
Public Member Functions | Private Attributes | List of all members
Cogs::ModuleAPI< API > Class Template Reference

Helper class for shared libraries that implement the getPublicAPI function and provide a structure of function pointers that contain the module's public API. More...

#include <Module.h>

Inheritance diagram for Cogs::ModuleAPI< API >:
Cogs::Module

Public Member Functions

 ModuleAPI (void *h, const void *apiPtr)
 
 ModuleAPI (const std::string &moduleName)
 
API * operator-> ()
 
virtual void initPtrs () override
 
- Public Member Functions inherited from Cogs::Module
virtual void initPtrs ()
 
template<typename T >
void getProcAddress (T &ptr, const char *procName)
 
void * getProcAddress (const char *procName)
 Retrieves a pointer to the named function.
 
bool operator== (const Module &rhs) const
 

Private Attributes

const API * api = nullptr
 

Additional Inherited Members

- Public Types inherited from Cogs::Module
enum class  LoadStatus { Unloaded , Loaded , Failed }
 
- Static Public Member Functions inherited from Cogs::Module
static void load (const std::string &path, LoadStatus &status, void *&handle)
 Attempts to load a shared library with the given name.
 
static void unload (void *handle)
 
template<typename T = Module>
static T load (const std::string &path)
 Loads the named shared library.
 
static void * getProcAddress (void *handle, const char *procName)
 Retrives the address of the named function within the shared library identified by the specified handle.
 
- Public Attributes inherited from Cogs::Module
LoadStatus status = LoadStatus::Unloaded
 
void * handle = nullptr
 
- Protected Member Functions inherited from Cogs::Module
template<typename RET , typename FN , typename... ARGS>
RET Call (FN *fn, ARGS... args) const
 

Detailed Description

template<typename API>
class Cogs::ModuleAPI< API >

Helper class for shared libraries that implement the getPublicAPI function and provide a structure of function pointers that contain the module's public API.

For any shared library that implements a public function called getPublicAPI that returns a pointer to a struct of function pointers like this:

struct PublicAPI { using SomePublicFn = void();

SomePublicFn* somePublicFn = nullptr;

PublicAPI(); // Initialises member pointers to real function addresses. };

MYLIBRARY_API const void* getPublicAPI(); // Declare static instance of PublicAPI and return address.

An application just needs to implement the following class that encapsulates the module innards to be able to call those functions in a shared library:

class MyModule : public Cogs::Application::ModuleAPI<PublicAPI> { public: MyModule() : ModuleAPI("MySharedLibrary") {}

static MyModule& instance() { static MyModule i; return i; } };

And then it can simply call any defined function like this:

MyModule::instance()->somePublicFn(); *============================================================================================

Definition at line 113 of file Module.h.

Constructor & Destructor Documentation

◆ ModuleAPI() [1/2]

template<typename API >
Cogs::ModuleAPI< API >::ModuleAPI ( void *  h,
const void *  apiPtr 
)
inline

Definition at line 117 of file Module.h.

◆ ModuleAPI() [2/2]

template<typename API >
Cogs::ModuleAPI< API >::ModuleAPI ( const std::string &  moduleName)
inline

Definition at line 123 of file Module.h.

Member Function Documentation

◆ initPtrs()

template<typename API >
virtual void Cogs::ModuleAPI< API >::initPtrs ( )
inlineoverridevirtual

Reimplemented from Cogs::Module.

Definition at line 131 of file Module.h.

◆ operator->()

template<typename API >
API * Cogs::ModuleAPI< API >::operator-> ( )
inline

Definition at line 127 of file Module.h.

Member Data Documentation

◆ api

template<typename API >
const API* Cogs::ModuleAPI< API >::api = nullptr
private

Definition at line 140 of file Module.h.


The documentation for this class was generated from the following file: