Skip to content

Instantly share code, notes, and snippets.

@hamsham
Created November 3, 2014 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hamsham/611f464d81c494cf12d7 to your computer and use it in GitHub Desktop.
Save hamsham/611f464d81c494cf12d7 to your computer and use it in GitHub Desktop.
Example header for a shared library loader that loads plugin objects from the pluginAPI.h header.
#ifndef __SHARED_LIB_H__
#define __SHARED_LIB_H__
#include <string>
#include "pluginAPI.h"
//-----------------------------------------------------------------------------
// Shared Library loading object (Used for RAII purposes).
//-----------------------------------------------------------------------------
class sharedLibrary {
private:
pluginFactory_t pFactory = nullptr;
pluginDeleter_t pDeleter = nullptr;
plugin* pPlugin = nullptr;
void* pHandle = nullptr;
bool loadFactoryMethod();
bool loadDeleterMethod();
public:
/**
* Destructor to unload the shared libray when *this goes out of scope.
* It is the same thing as calling the "unload()" method.
*/
~sharedLibrary();
/**
* Default Constructor.
*/
sharedLibrary() = default;
/**
* The Copy constructor has been removed in order to avoid having two
* classes delete the same handles to a loaded library
*/
sharedLibrary(const sharedLibrary&) = delete;
/**
* Move constructor may be used to transfer ownership of *this object's
* resources to something such as an STL container.
*/
sharedLibrary(sharedLibrary&&);
/**
* The Copy operator has also been removed in order to avoid having two
* classes delete the same handles to a loaded library
*/
sharedLibrary& operator=(const sharedLibrary&) = delete;
/**
* Move operator may also be used to transfer ownership of *this
* object's resources.
*/
sharedLibrary& operator=(sharedLibrary&&);
/**
* Load a shared library from a file.
*/
bool load(const std::string& filename);
/**
* Unload all resources and file handles used by *this.
*/
void unload();
/**
* Get a pointer to the plugin object that was loaded from a file by
* *this. Returns NULL if nothing exists.
*/
const plugin* getPlugin() const;
/**
* Helper method that will be used to determine if a referenced filen
* name is a shared library that can be loaded into *this.
*/
static bool isSharedLib(const std::string& filename);
};
#endif /* __SHARED_LIB_H__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment