/** \file
 *
 *  Contains the LoadedModule class definition.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef LOADEDMODULE_H_
#define LOADEDMODULE_H_

#ifndef WINNT
#include <dlfcn.h>
#endif

#include "Module.h"

/**
 *  Contains information about a currently loaded module,
 *  including the shared library and destroy method.
 *
 *  \ingroup module
 */
class LoadedModule
{
public:
    LoadedModule( Module *module, destroy_module_t* destroyModule, void* lib )
        : module_( module ),
          destroyModule_( destroyModule ),
          lib_( lib )
    {}
    ;

    virtual ~LoadedModule()
    {
        if( destroyModule_ && module_ )
        {
            // Destroy the object
            destroyModule_( module_ );
        }
        else if( module_ )
        {
            // Delete the (most likely scripted) object
            delete module_;
        }

        if( lib_ )
        {
            // Close the library
#ifndef WINNT
            // Comment this line out to see symbols in Valgrind memory leaks
            dlclose( lib_ );
#endif
            //printf( "Closed library at 0X%08x\n", (int)lib_ );
            lib_ = 0;
        }
    };

    Module* getModule( void )
    {
        return module_;
    }

protected:

    Module *module_;
    destroy_module_t* destroyModule_;
    void* lib_;

private:
    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    LoadedModule( const LoadedModule& old ); // disallow copy constructor

};

/// Define a type for a "list of loaded module pointers".
typedef FlexArray<LoadedModule *> ListOfLoadedModulePtrs;

#endif /*LOADEDMODULE_H_*/
