dart
SharedLibrary.hpp
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_COMMON_SHAREDLIBRARY_HPP_
34 #define DART_COMMON_SHAREDLIBRARY_HPP_
35 
36 #include <memory>
37 #include <string>
38 
39 #include <boost/filesystem.hpp>
40 
41 #include "dart/common/Deprecated.hpp"
42 #include "dart/common/Platform.hpp"
43 
44 #if DART_OS_LINUX
45 
46 # define DYNLIB_HANDLE void*
47 
48 #elif DART_OS_MACOS
49 
50 # define DYNLIB_HANDLE void*
51 
52 #elif DART_OS_WINDOWS
53 
54 # ifdef NOMINMAX
55 # include <windows.h>
56 # else
57 # define NOMINMAX
58 # include <windows.h>
59 # undef NOMINMAX
60 # endif
61 using hInstance = HINSTANCE__*;
62 # define DYNLIB_HANDLE hInstance
63 
64 #endif
65 
66 #if DART_OS_LINUX
67 static constexpr const char* DART_SHARED_LIB_EXTENSION = "so";
68 #elif DART_OS_MACOS
69 static constexpr const char* DART_SHARED_LIB_EXTENSION = "dylib";
70 #elif DART_OS_WINDOWS
71 static constexpr const char* DART_SHARED_LIB_EXTENSION = "dll";
72 #else
73 # error Unhandled platform
74 #endif
75 
76 #if DART_OS_LINUX
77 static constexpr const char* DART_SHARED_LIB_PREFIX = "lib";
78 #elif DART_OS_MACOS
79 static constexpr const char* DART_SHARED_LIB_PREFIX = "lib";
80 #elif DART_OS_WINDOWS
81 static constexpr const char* DART_SHARED_LIB_PREFIX = "";
82 #else
83 # error Unhandled platform
84 #endif
85 
86 namespace dart {
87 namespace common {
88 
89 namespace detail {
90 class SharedLibraryManager;
91 } // namespace detail
92 
95 {
96 protected:
97  enum ProtectedConstructionTag
98  {
99  ProtectedConstruction
100  };
101 
102 public:
116  DART_DEPRECATED(6.10)
117  static std::shared_ptr<SharedLibrary> create(
118  const boost::filesystem::path& path);
119 
131  static std::shared_ptr<SharedLibrary> create(const std::string& path);
132 
146  DART_DEPRECATED(6.10)
147  explicit SharedLibrary(
148  ProtectedConstructionTag, const boost::filesystem::path& path);
149 
161  explicit SharedLibrary(ProtectedConstructionTag, const std::string& path);
162 
164  virtual ~SharedLibrary();
165 
167  const boost::filesystem::path& getCanonicalPath() const;
168 
170  const std::string& path() const;
171 
173  bool isValid() const;
174 
180  void* getSymbol(const std::string& symbolName) const;
181 
182 protected:
183  friend class detail::SharedLibraryManager;
184 
188  boost::filesystem::path mCanonicalPath;
189  // TODO(JS): Remove in DART 7.
190 
194  std::string mPath;
195 
197  DYNLIB_HANDLE mInstance;
198 
199 private:
201  std::string getLastError() const;
202 };
203 
204 } // namespace common
205 } // namespace dart
206 
207 #endif // DART_COMMON_SHAREDLIBRARY_HPP_
DYNLIB_HANDLE mInstance
Handle to the loaded library.
Definition: SharedLibrary.hpp:197
std::string mPath
Canonical path to the shared library where a canonical path is an absolute path that has no elements ...
Definition: SharedLibrary.hpp:194
Definition: Aspect.cpp:40
boost::filesystem::path mCanonicalPath
Canonical path to the shared library where a canonical path is an absolute path that has no elements ...
Definition: SharedLibrary.hpp:188
Definition: SharedLibraryManager.hpp:66
SharedLibrary is a RAII object wrapping a shared library.
Definition: SharedLibrary.hpp:94