/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_DYNAMIC_LIBRARY_LINUX_HPP
#define NMM_DYNAMIC_LIBRARY_LINUX_HPP NMM_DYNAMIC_LIBRARY_LINUX_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/Exception.hpp"

#include <string>
#include <ltdl.h>

namespace NMM {

  /** This a C++ wrapper class for the ltdl library.
   */
  class DynamicLibrary {
  public:
    /** Constructor
     */
    DynamicLibrary();

    /** Destructor. Calls also close.
     *�\sa close
     */
    ~DynamicLibrary();

    /** Loads a shared library.
     *
     * @exception Exception if an error occurs.
     * @param name name of library
     * @param flag flags for opening library. These flags are defines by c method dlopen. 
     */
    void open(const std::string& name, int flag = 0)
      throw (const Exception& );

    /** Close a loaded shared library.
     * @exception Exception if an error occur.
     */
    void close()
      throw (const Exception& );

    /** Resolves a symbol with given name.
     * @param symbol name of symbol to resolve
     * @return pointer to the given symbol, 0 otherwise
     * \sa getError
     */
    void* resolve(const std::string& symbol);

    /** Returns the last error message occured.
     * @return string with the error message
     */
    inline  const std::string& getError() const  {return error;}

    /** Returns the name of the current library
     * @return string with the library name
     */
    inline  const std::string& getName()  const  {return name;}

    /** This method returns the current handle.
     * @return current handle to the shared library
     */
    inline  const void* getHandle()  const  {return handle;}
  private:
    lt_dlhandle  handle;  ///<current handle pointer
    std::string error;    ///<latest error message
    std::string name;     ///<current library name. is set when open is called

  private: //disabled methods
    /** Disabled copy constructor. */
    DynamicLibrary(const DynamicLibrary&);
    /** Disabled assignment operator. */
    DynamicLibrary& operator=(const DynamicLibrary&);
    /** Disabled comparison operator. */
    DynamicLibrary& operator==(const DynamicLibrary&);
    bool __is_valid;      ///< is true if lt_dlinit() successfully returnd inside the constructor, else false
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
