Created
November 3, 2014 04:03
-
-
Save hamsham/d561d2d04220f3fb32d5 to your computer and use it in GitHub Desktop.
Main procedure for loading a plugin library at runtime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A test of user-defined plugins | |
* | |
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic -pedantic-errors -DSDL_MAIN_HANDLED main.cpp sharedLibrary.cpp -o PluginLoader -lSDL2 | |
*/ | |
#include <iostream> // std::cin, std::cout, std:: cerr | |
#include <vector> | |
#include <string> | |
#include <utility> // std::move() | |
#include <SDL2/SDL.h> | |
#include "pluginAPI.h" | |
#include "sharedLibrary.h" | |
//----------------------------------------------------------------------------- | |
// Parse all input files and extract only valid shared libraries | |
//----------------------------------------------------------------------------- | |
std::vector<sharedLibrary> loadSharedLibs(int argc, char** argv) { | |
std::vector<sharedLibrary> loadedLibs; | |
// iterate through all parameters in main and verify that they are dynamic | |
// libraries. Skip the first argument (argc=1) as that's the path to the | |
// current program. | |
for (int i = 1; i < argc; ++i) { | |
// convert the argument to an std::string for better functionality. | |
std::string libPath = argv[i]; | |
// verify the file is a shared library | |
if (sharedLibrary::isSharedLib(libPath)) { | |
std::cout | |
<< "The file " << libPath << " may be a valid shared library." | |
<< " An attempt will be made to load it." << std::endl; | |
// file path verified, attempt to load it through a sharedLibrary | |
// instance. | |
sharedLibrary lib; | |
// Add the object to a list of shared libraries once its validated. | |
if (lib.load(libPath)) { | |
loadedLibs.emplace_back(std::move(lib)); | |
} | |
} | |
else { | |
std::cerr | |
<< "The file " << libPath << " is not a valid shared library." | |
<< " No attempt will be made to load it." << std::endl; | |
} | |
} | |
// get the list of successfully loaded dynamic library handles. | |
return loadedLibs; | |
} | |
//----------------------------------------------------------------------------- | |
// Main() | |
//----------------------------------------------------------------------------- | |
int main(int argc, char** argv) { | |
// Initialize SDL. | |
SDL_SetMainReady(); | |
SDL_Init(0); | |
// Construct a list of shared library handles (r-value reference used in | |
// order to avoid unnecessary copying of data. | |
std::vector<sharedLibrary>&& sharedLibs = loadSharedLibs(argc, argv); | |
// Ensure that the input paths led to successfully loaded libraries. | |
if (sharedLibs.size() == 0) { | |
std::cout << "No libraries to load" << std::endl; | |
return 0; | |
} | |
else { | |
// Run each loaded plugin object from the loaded libraries. | |
for (const sharedLibrary& lib : sharedLibs) { | |
const plugin* const pPlugin = lib.getPlugin(); | |
pPlugin->printMessage(); | |
} | |
} | |
// unload all contained library handles before SDL terminates. | |
sharedLibs.clear(); | |
// terminate SDL. | |
SDL_Quit(); | |
// pause the program in order to validate execution output. | |
std::cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment