DynamicLib.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <coil/DynamicLib.h>
00020 
00021 namespace coil
00022 {
00030   DynamicLib::DynamicLib(int close_handle_on_destruction)
00031     : m_closeflag(close_handle_on_destruction)
00032   {
00033   }
00034 
00042   DynamicLib::DynamicLib(const char* dynlib_name,
00043                          int open_mode,
00044                          int close_handle_on_destruction)
00045     : m_name(dynlib_name), m_mode(open_mode),
00046       m_closeflag(close_handle_on_destruction)
00047   {
00048     if (open(m_name.c_str(), m_mode, m_closeflag) != 0)
00049       {
00050         throw std::bad_alloc();
00051       }
00052   }
00053 
00061   DynamicLib::~DynamicLib()
00062   {
00063     close();
00064   }
00065 
00073   DynamicLib::DynamicLib(const DynamicLib& rhs)
00074     : m_name(""), m_mode(0), m_closeflag(0), m_handle(0)
00075   {
00076     if (!rhs.m_name.empty() &&
00077         open(rhs.m_name.c_str(), rhs.m_mode, rhs.m_closeflag) == 0)
00078       return;
00079 //    throw std::bad_alloc();
00080   }
00081 
00089   DynamicLib& DynamicLib::operator=(const DynamicLib& rhs)
00090   {
00091     DynamicLib tmp(rhs);
00092     std::swap(this->m_name, tmp.m_name);
00093     std::swap(this->m_mode, tmp.m_mode);
00094     std::swap(this->m_closeflag, tmp.m_closeflag);
00095     std::swap(this->m_handle, tmp.m_handle);
00096     return *this;
00097   }
00098 
00106   int DynamicLib::open(const char* dll_name,
00107                    int open_mode,
00108                    int close_handle_on_destruction)
00109   {
00110     void* handle = ::dlopen(dll_name, open_mode);
00111     if (handle == NULL)
00112       {
00113         return -1;
00114       }
00115     m_handle = handle;
00116     m_name = dll_name;
00117     return 0;
00118   }
00119 
00127   int DynamicLib::close(void)
00128   {
00129     if (m_handle == NULL)
00130       return -1;
00131     if (m_name.empty())
00132       {
00133         return -1;
00134       }
00135     ::dlclose(m_handle);
00136     m_handle = NULL;
00137     m_name = "";
00138     return 0;
00139   }
00140 
00148   void* DynamicLib::symbol(const char* symbol_name)
00149   {
00150     if (m_handle == NULL) return NULL;
00151     return ::dlsym(m_handle, symbol_name);
00152   }
00153 
00161   const char* DynamicLib::error(void) const
00162   {
00163     return ::dlerror();
00164   }
00165 };
00166 
00170 extern "C"
00171 {
00172   int ForExternTest(void) { return coil::DynamicLib::ForExternTest(); }
00173 }


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:04