YARP
Yet Another Robot Platform
SharedLibrary.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
10 
11 #include <yarp/conf/system.h>
12 
13 #include <yarp/os/Log.h>
14 
15 #include <string>
16 
17 #ifdef YARP_HAS_ACE
18 # include <ace/ACE.h>
19 # include <ace/DLL.h>
20 // In one the ACE headers there is a definition of "main" for WIN32
21 # ifdef main
22 # undef main
23 # endif
24 #else
26 #endif
27 
28 
31 
32 
34 {
35 public:
36  SharedLibraryImpl() = default;
37 
38  inline char* getError()
39  {
40 #ifdef YARP_HAS_ACE
41  if (dll != nullptr) {
42  return dll->error();
43  }
44  return const_cast<char*>("Unknown error");
45 #else
46  return yarp::os::impl::dlerror();
47 #endif
48  }
49 
50 #ifdef YARP_HAS_ACE
51  ACE_DLL* dll{nullptr};
52 #else
53  void* dll;
54 #endif
55  std::string error;
56 };
57 
58 
59 SharedLibrary::SharedLibrary() :
61 {
62  yAssert(implementation != nullptr);
63 }
64 
65 SharedLibrary::SharedLibrary(const char* filename) :
67 {
68  yAssert(implementation != nullptr);
69  open(filename);
70 }
71 
73 {
74  yAssert(implementation != nullptr);
75  close();
76  delete implementation;
77 }
78 
79 bool SharedLibrary::open(const char* filename)
80 {
81  close();
82 #ifdef YARP_HAS_ACE
83  implementation->dll = new ACE_DLL();
84  yAssert(implementation->dll);
85  int result = implementation->dll->open(filename);
86  if (result != 0) {
87  // Save error since close might overwrite it
88  std::string error(implementation->getError());
89  close();
90  implementation->error = error;
91  return false;
92  }
93  return true;
94 #else
95  implementation->dll = yarp::os::impl::dlopen(filename, RTLD_LAZY);
96  if (!implementation->dll) {
97  implementation->error = implementation->getError();
98  return false;
99  }
100  return true;
101 #endif
102 }
103 
105 {
106  int result = 0;
107  if (implementation->dll != nullptr) {
108 #ifdef YARP_HAS_ACE
109  result = implementation->dll->close();
110  delete implementation->dll;
111 #else
112  result = yarp::os::impl::dlclose(implementation->dll);
113 #endif
114  implementation->dll = nullptr;
115  }
116 
117  if (result != 0) {
118  implementation->error = implementation->getError();
119  }
120 
121  return (result == 0);
122 }
123 
124 std::string SharedLibrary::error()
125 {
126  return SharedLibrary::implementation->error;
127 }
128 
129 void* SharedLibrary::getSymbol(const char* symbolName)
130 {
131  if (implementation->dll == nullptr) {
132  implementation->error = "Library is not open";
133  return nullptr;
134  }
135 
136 #ifdef YARP_HAS_ACE
137  void* result = implementation->dll->symbol(symbolName);
138 #else
139  void* result = yarp::os::impl::dlsym(implementation->dll, symbolName);
140 #endif
141  if (result == nullptr) {
142  implementation->error = implementation->getError();
143  }
144 
145  return result;
146 }
147 
149 {
150  return implementation->dll != nullptr;
151 }
yarp::os::SharedLibrary::~SharedLibrary
virtual ~SharedLibrary()
Destructor.
Definition: SharedLibrary.cpp:72
SharedLibrary.h
yarp::os::SharedLibrary::open
bool open(const char *filename)
Load the named shared library / DLL.
Definition: SharedLibrary.cpp:79
Log.h
yarp::os::impl::SharedLibraryImpl::dll
void * dll
Definition: SharedLibrary.cpp:53
yarp::os::SharedLibrary
Low-level wrapper for loading shared libraries (DLLs) and accessing symbols within it.
Definition: SharedLibrary.h:34
yarp::os::impl::SharedLibraryImpl
Definition: SharedLibrary.cpp:34
yarp::os::SharedLibrary::getSymbol
void * getSymbol(const char *symbolName)
Look up a symbol in the shared library.
Definition: SharedLibrary.cpp:129
yarp::os::impl::SharedLibraryImpl::getError
char * getError()
Definition: SharedLibrary.cpp:38
yarp::os::SharedLibrary::error
std::string error()
Returns a human-readable string describing the most recent error that occurred from a call to one of ...
Definition: SharedLibrary.cpp:124
system.h
yarp::os::SharedLibrary::SharedLibrary
SharedLibrary()
Initialize, without opening a shared library yet.
Definition: SharedLibrary.cpp:59
PlatformDlfcn.h
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::os::impl::SharedLibraryImpl::error
std::string error
Definition: SharedLibrary.cpp:55
yarp::os::impl::SharedLibraryImpl::SharedLibraryImpl
SharedLibraryImpl()=default
yAssert
#define yAssert(x)
Definition: Log.h:297
yarp::os::SharedLibrary::isValid
bool isValid() const
Check if the shared library is valid.
Definition: SharedLibrary.cpp:148
yarp::os::SharedLibrary::close
bool close()
Shared library no longer needed, unload if not in use elsewhere.
Definition: SharedLibrary.cpp:104