Skip to content

Commit

Permalink
Merge pull request #34 from rhaschke/fix-on-demand-unloading
Browse files Browse the repository at this point in the history
fix on demand unloading
  • Loading branch information
mikaelarguedas committed Apr 9, 2016
2 parents 6807355 + f32686f commit 8a80797
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/class_loader/multi_library_class_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class MultiLibraryClassLoader
for(unsigned int c = 0; c < active_loaders.size(); c++)
{
ClassLoader* current = active_loaders.at(c);
if (!current->isLibraryLoaded())
current->loadLibrary();
if(current->isClassAvailable<Base>(class_name))
return(current->createInstance<Base>(class_name));
}
Expand Down Expand Up @@ -113,6 +115,8 @@ class MultiLibraryClassLoader
for(unsigned int c = 0; c < active_loaders.size(); c++)
{
ClassLoader* current = active_loaders.at(c);
if (!current->isLibraryLoaded())
current->loadLibrary();
if(current->isClassAvailable<Base>(class_name))
return(current->createUnmanagedInstance<Base>(class_name));
}
Expand Down
62 changes: 62 additions & 0 deletions test/utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <boost/bind.hpp>
#include <iostream>
#include <class_loader/class_loader.h>
#include <class_loader/multi_library_class_loader.h>
#include "base.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -297,8 +298,69 @@ TEST(ClassLoaderTest, loadRefCountingLazy)
FAIL() << "Did not throw exception as expected.\n";
}


/*****************************************************************************/

void testMultiClassLoader(bool lazy)
{
try
{
class_loader::MultiLibraryClassLoader loader(lazy);
loader.loadLibrary(LIBRARY_1);
loader.loadLibrary(LIBRARY_2);
for (int i=0; i < 2; ++i) {
loader.createInstance<Base>("Cat")->saySomething();
loader.createInstance<Base>("Dog")->saySomething();
loader.createInstance<Base>("Robot")->saySomething();
}
}
catch(class_loader::ClassLoaderException& e)
{
FAIL() << "ClassLoaderException: " << e.what() << "\n";
}

SUCCEED();
}

TEST(MultiClassLoaderTest, lazyLoad)
{
testMultiClassLoader(true);
}
TEST(MultiClassLoaderTest, lazyLoadSecondTime)
{
testMultiClassLoader(true);
}
TEST(MultiClassLoaderTest, nonLazyLoad)
{
testMultiClassLoader(false);
}
TEST(MultiClassLoaderTest, noWarningOnLazyLoad)
{
try
{
boost::shared_ptr<Base> cat, dog, rob;
{
class_loader::MultiLibraryClassLoader loader(true);
loader.loadLibrary(LIBRARY_1);
loader.loadLibrary(LIBRARY_2);

cat = loader.createInstance<Base>("Cat");
dog = loader.createInstance<Base>("Dog");
rob = loader.createInstance<Base>("Robot");
}
cat->saySomething();
dog->saySomething();
rob->saySomething();
}
catch(class_loader::ClassLoaderException& e)
{
FAIL() << "ClassLoaderException: " << e.what() << "\n";
}

SUCCEED();
}

/*****************************************************************************/

// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
Expand Down

0 comments on commit 8a80797

Please sign in to comment.