Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to create new instances of loaded libraries #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/LibManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ namespace lib_manager {
string name = _lib->getLibName();

newLib.destroy = 0;
newLib.create = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using 0 here, it would be better to use one of the following:

  • (void *)0
  • NULL
  • nullptr

newLib.configCreate = 0;
newLib.libInterface = _lib;
newLib.useCount = 1;
newLib.wasUnloaded = false;
Expand Down Expand Up @@ -165,6 +167,8 @@ namespace lib_manager {

libStruct newLib;
newLib.destroy = 0;
newLib.create = 0;
newLib.configCreate = 0;
newLib.libInterface = 0;
newLib.useCount = 0;
newLib.wasUnloaded = false;
Expand All @@ -177,12 +181,16 @@ namespace lib_manager {
if(newLib.destroy) {
if(!config) {
createLib *tmp_con = getFunc<createLib*>(pl, "create_c");
if(tmp_con)
if(tmp_con) {
newLib.create = tmp_con;
newLib.libInterface = tmp_con(this);
}
} else {
createLib2 *tmp_con2 = getFunc<createLib2*>(pl, "config_create_c");
if(tmp_con2)
if(tmp_con2){
newLib.configCreate = tmp_con2;
newLib.libInterface = tmp_con2(this, config);
}
}
}
}
Expand Down Expand Up @@ -303,6 +311,19 @@ namespace lib_manager {
return theLib->libInterface;
}

LibInterface* LibManager::getNewInstance(const string &libName) {
if(libMap.find(libName) == libMap.end()) {
#ifdef DEBUF
fprintf(stderr, "LibManager: could not find \"%s\"\n", libName.c_str());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use std::cerr here. We are in C++ not in C.

#endif
return 0;
}
libStruct *theLib = &(libMap[libName]);
// todo: clear how to deal with the use count in this case
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you would use shared pointers, you would not need to reference count yourself.

//theLib->useCount++;
return theLib->create(this);
}

/**
* Releases a previously acquired library
* @param libName
Expand Down
3 changes: 3 additions & 0 deletions src/LibManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace lib_manager {
struct libStruct {
LibInterface *libInterface;
destroyLib *destroy;
createLib *create;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using safer/smart pointers instead of raw ones.

createLib2 *configCreate;
int useCount;
bool wasUnloaded;
std::string path;
Expand Down Expand Up @@ -92,6 +94,7 @@ namespace lib_manager {
bool load = false)
{ return acquireLibraryAs<T>(libName, load); }

LibInterface* getNewInstance(const std::string &libName);
ErrorNumber releaseLibrary(const std::string &libName);

ErrorNumber unloadLibrary(const std::string &libPath);
Expand Down