Create program that accept modules

This is a discussion on Create program that accept modules within the C++ Programming forums, part of the General Programming Boards category; Humm... that's it... I create interfaces to the functions the modules must implement, configure the modules to be load in ...

  1. #16
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Humm... that's it...

    I create interfaces to the functions the modules must implement, configure the modules to be load in an text file or XML file, and then load the with dlopen...

    Thanks a lot...

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by rluceac View Post
    Humm... that's it...

    I create interfaces to the functions the modules must implement, configure the modules to be load in an text file or XML file, and then load the with dlopen...

    Thanks a lot...
    Because this is C++, there are a few things to consider... The names of C++ functions are mangled, which makes it hard to use dlsym(). You have to mangle the name the exact same way the compiler did, and that's not always easy.

    Thus, public functions in loadable C++ modules are often declared extern "C" to cause them not to be name-mangled. A module for a C++ architecture might provide one or two entry points which are basically factory calls that instantiate module objects.

    Suppose that every module inherits from this class:

    Code:
    class MyModule
    {
    public:
        virtual ~MyModule() {}
    
        virtual void DoIt() = 0;
    };
    Then a module which implements a "FooModule" might look like this:

    Code:
    class FooModule : public MyModule
    {
    public:
        void DoIt()
        {
            // implement the module code
        }
    };
    
    extern "C"
    MyModule *CreateModule()
    {
        return new FooModule();
    }
    The main program might load the module like this:

    Code:
    // Load the module
    void *handle = dlopen("FooModule.so", RTLD_LAZY);
    
    // Get the factory
    MyModule *(*factory)() = (MyModule *(*)())dlsym(handle, "CreateModule");
    
    // Create the object
    MyModule *module = factory();
    
    // Do it!
    module->DoIt();
    Unloading modules can be tricky, because you can't unload safely unless all object instances from that module are destroyed. You could use some kind of reference counting, or punt the issue and just never unload a module once it has been loaded.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  2. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 11:15 AM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. program won't create status bar
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 08-30-2001, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21