posix/coil/DynamicLib.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #include <coil/DynamicLib.h>
20 
21 namespace coil
22 {
30  DynamicLib::DynamicLib(int close_handle_on_destruction)
31  : m_closeflag(close_handle_on_destruction)
32  {
33  }
34 
42  DynamicLib::DynamicLib(const char* dynlib_name,
43  int open_mode,
44  int close_handle_on_destruction)
45  : m_name(dynlib_name), m_mode(open_mode),
46  m_closeflag(close_handle_on_destruction)
47  {
48  if (open(m_name.c_str(), m_mode, m_closeflag) != 0)
49  {
50  throw std::bad_alloc();
51  }
52  }
53 
62  {
63  close();
64  }
65 
74  : m_name(""), m_mode(0), m_closeflag(0), m_handle(0)
75  {
76  if (!rhs.m_name.empty() &&
77  open(rhs.m_name.c_str(), rhs.m_mode, rhs.m_closeflag) == 0)
78  return;
79 // throw std::bad_alloc();
80  }
81 
90  {
91  DynamicLib tmp(rhs);
92  std::swap(this->m_name, tmp.m_name);
93  std::swap(this->m_mode, tmp.m_mode);
94  std::swap(this->m_closeflag, tmp.m_closeflag);
95  std::swap(this->m_handle, tmp.m_handle);
96  return *this;
97  }
98 
106  int DynamicLib::open(const char* dll_name,
107  int open_mode,
108  int close_handle_on_destruction)
109  {
110  void* handle = ::dlopen(dll_name, open_mode);
111  if (handle == NULL)
112  {
113  return -1;
114  }
115  m_handle = handle;
116  m_name = dll_name;
117  return 0;
118  }
119 
128  {
129  if (m_handle == NULL)
130  return -1;
131  if (m_name.empty())
132  {
133  return -1;
134  }
135  ::dlclose(m_handle);
136  m_handle = NULL;
137  m_name = "";
138  return 0;
139  }
140 
148  void* DynamicLib::symbol(const char* symbol_name)
149  {
150  if (m_handle == NULL) return NULL;
151  return ::dlsym(m_handle, symbol_name);
152  }
153 
161  const char* DynamicLib::error(void) const
162  {
163  return ::dlerror();
164  }
165 };
166 
170 extern "C"
171 {
173 }
static int ForExternTest(void)
Unit Test.
void * symbol(const char *symbol_name)
Return an address of the memory where a symbol was loaded.
DynamicLib & operator=(const DynamicLib &rhs)
Assignment operator.
DynamicLib(int close_handle_on_destruction=1)
Constructor.
virtual ~DynamicLib()
Destructor.
virtual int close(void)
Unload of the Dynamic link library.
const char * error(void) const
Return the explanation message about the error.
virtual int open(const char *dll_name, int open_mode=COIL_DEFAULT_DYNLIB_MODE, int close_handle_on_destruction=1)
Load of the Dynamic link library.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58