inlib  1.2.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
inlib::plugin Class Reference

List of all members.

Public Types

typedef void(* procedure )()

Public Member Functions

 plugin (const std::string &a_name, std::ostream &a_out, bool a_verbose=false, bool a_quiet=false)
virtual ~plugin ()
bool open ()
void close ()
bool is_opened () const
const std::string & file_name () const
procedure find (const std::string &a_name, bool a_quiet=false) const

Static Public Member Functions

static bool is_valid (const std::string &a_name, std::ostream &a_out)

Protected Member Functions

 plugin (const plugin &a_from)
pluginoperator= (const plugin &)

Static Protected Member Functions

static std::vector< std::string > paths ()

Protected Attributes

void * m_module
std::ostream & m_out
std::string m_name
std::string m_plugin
bool m_verbose
std::string m_error

Detailed Description

Definition at line 21 of file plugin.


Member Typedef Documentation

typedef void(* inlib::plugin::procedure)()

Definition at line 132 of file plugin.


Constructor & Destructor Documentation

inlib::plugin::plugin ( const std::string &  a_name,
std::ostream &  a_out,
bool  a_verbose = false,
bool  a_quiet = false 
) [inline]

Definition at line 23 of file plugin.

  :m_module(0)
  ,m_out(a_out)
  ,m_name(a_name)
  ,m_verbose(a_verbose)
  {
    // a_name is the full name (path + libname).
    if(!open()) {
      if(!a_quiet) {
        m_out << "inlib::plugin :" 
              << " can't load " << sout(m_name) << " library." << std::endl;
        m_out << "inlib::plugin :" 
              << " error  : " << m_error 
              << std::endl;
      }
    }
  }
virtual inlib::plugin::~plugin ( ) [inline, virtual]

Definition at line 41 of file plugin.

{close();}
inlib::plugin::plugin ( const plugin a_from) [inline, protected]

Definition at line 44 of file plugin.

:m_out(a_from.m_out){}

Member Function Documentation

void inlib::plugin::close ( ) [inline]

Definition at line 107 of file plugin.

              {
    if(!m_module) return; //done
    if(m_verbose) {
      m_out << "inlib::close :" 
            << " close " << sout(m_name) << "..." << std::endl;
    }
#ifdef WIN32
    ::FreeLibrary((HMODULE)m_module);
#else
    if(::dlclose(m_module)) {
      m_out << "inlib::~plugin :" 
            << " error  : " << ::dlerror() << std::endl;
    }
#endif
    if(m_verbose) {
      m_out << "inlib::~plugin :" 
            << " close " << sout(m_name) << " done." << std::endl;
    }
    m_module = 0;
  }
const std::string& inlib::plugin::file_name ( ) const [inline]

Definition at line 130 of file plugin.

{return m_name;}
procedure inlib::plugin::find ( const std::string &  a_name,
bool  a_quiet = false 
) const [inline]

Definition at line 134 of file plugin.

                                                                     {
    if(!m_module) return 0;
    std::string name = a_name;
    procedure p = 0;
    if(m_verbose) {
      m_out << "inlib::find :" 
            << " search symbol " 
            << sout(name) << "..." << std::endl;
    }
#ifdef WIN32
    p = (procedure)::GetProcAddress((HMODULE)m_module,name.c_str());
    if (!p) {
      name = "_" + a_name;
      p = (procedure)::GetProcAddress((HMODULE)m_module,name.c_str());
      if (!p) {
        if(!a_quiet) {
          m_out << "Lib : Can't find symbol " << sout(a_name) << "." 
                << std::endl;
        }
      }
    }
#else
    void* a = ::dlsym(m_module,(char*)name.c_str());
    //trick to avoid the warning :
    //ISO C++ forbids casting between pointer-to-function and pointer-to-object
    ::memcpy(&p,&a,sizeof(void*)); //beurk

    const char* cerror;
    if ((cerror=dlerror())!=NULL) {
      std::string serror = cerror;
      name = "_" + a_name;
      void* a = ::dlsym(m_module,(char*)name.c_str());
      ::memcpy(&p,&a,sizeof(void*)); //rebeurk

      if ((cerror=dlerror())!=NULL) {
        if(!a_quiet) {
          m_out << "inlib::find :" 
                << " problem when searching " << sout(a_name)
                << " in " << sout(m_plugin) << "."
                << " dlerror  : " << serror << std::endl;
        } 
      } 
    }
#endif
    if(p && m_verbose) {
      m_out << "inlib::find :" 
            << " symbol " << sout(name) << " found." << std::endl;
    }
    return p;
  }
bool inlib::plugin::is_opened ( ) const [inline]

Definition at line 128 of file plugin.

{return m_module?true:false;}
static bool inlib::plugin::is_valid ( const std::string &  a_name,
std::ostream &  a_out 
) [inline, static]

Definition at line 186 of file plugin.

                                                                  {
    plugin plg(a_name,a_out,0,true);
    bool status = plg.is_opened();
    plg.close();
    return status;
  }
bool inlib::plugin::open ( ) [inline]

Definition at line 48 of file plugin.

              {
    m_error = "";
    std::string splg = m_name;  
    if(m_verbose) {
      m_out << "inlib::plugin :" 
            << " open " 
            << sout(splg) << "..." << std::endl;
    }
#ifdef WIN32
    m_module = ::LoadLibrary(splg.c_str());
#else
   {std::string path,name,sfx;
    path_name_suffix(m_name,path,name,sfx);
    if(path.empty()) { //Relative path
      std::string sfx = suffix(splg);
      if(sfx.empty()) {
        // Only the name "Xxx" had been given, we have to
#if defined(__CYGWIN__) && defined(__GNUC__)
        // build the name "Xxx.dll" :      
        splg = m_name + ".dll";
#elif defined(__APPLE__)
        splg = m_name + ".bundle";
#else
        // build the name "libXxx.so" :      
        splg = "lib" + m_name + ".so";
#endif
      }
    }}
    int flags = RTLD_NOW | RTLD_GLOBAL;
    m_module = ::dlopen(splg.c_str(),flags);
#if defined(__APPLE__)
    if(!m_module) {
      std::vector<std::string> ps = paths(); 
      unsigned int pathn = ps.size();
      for(unsigned int index=0;index<pathn;index++) {
        splg = ps[index]+"/" + m_name + ".bundle";
        if(m_verbose) {
          m_out << "inlib::plugin::open :" 
                << " open " 
                << sout(splg) << "..." 
                << std::endl;
        }
        m_module = ::dlopen(splg.c_str(),flags);
        if(m_module) break;
      }
    }
#endif
    const char* cerror;
    if ((cerror=dlerror())!=NULL) m_error = cerror;
#endif
    if(!m_module) return false;
    if(m_verbose) {
      m_out << "inlib::plugin :" 
            << " " << sout(splg) << " opened." << std::endl;
    }
    m_plugin = splg;  
    return true;
  }
plugin& inlib::plugin::operator= ( const plugin ) [inline, protected]

Definition at line 45 of file plugin.

{return *this;}
static std::vector<std::string> inlib::plugin::paths ( ) [inline, static, protected]

Definition at line 194 of file plugin.

                                      { //APPLE only.
    const char* env = ::getenv("DYLD_LIBRARY_PATH");
    if(!env) env = ::getenv("GDB_DYLD_LIBRARY_PATH");
    std::string senv = (env==NULL ? std::string("") : std::string(env));
    return words(senv,":");
  }

Member Data Documentation

std::string inlib::plugin::m_error [protected]

Definition at line 207 of file plugin.

void* inlib::plugin::m_module [protected]

Definition at line 202 of file plugin.

std::string inlib::plugin::m_name [protected]

Definition at line 204 of file plugin.

std::ostream& inlib::plugin::m_out [protected]

Definition at line 203 of file plugin.

std::string inlib::plugin::m_plugin [protected]

Definition at line 205 of file plugin.

bool inlib::plugin::m_verbose [protected]

Definition at line 206 of file plugin.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines