BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
shared_library.h
1//
2// SharedLibrary.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/SharedLibrary.h#1 $
5//
6// Library: Foundation
7// Package: SharedLibrary
8// Module: SharedLibrary
9//
10// Definition of the SharedLibrary class.
11//
12// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13// and Contributors.
14//
15// Permission is hereby granted, free of charge, to any person or organization
16// obtaining a copy of the software and accompanying documentation covered by
17// this license (the "Software") to use, reproduce, display, distribute,
18// execute, and transmit the Software, and to prepare derivative works of the
19// Software, and to permit third-parties to whom the Software is furnished to
20// do so, all subject to the following:
21//
22// The copyright notices in the Software and this entire statement, including
23// the above license grant, this restriction and the following disclaimer,
24// must be included in all copies of the Software, in whole or in part, and
25// all derivative works of the Software, unless such copies or derivative
26// works are solely in the form of machine-executable object code generated by
27// a source language processor.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35// DEALINGS IN THE SOFTWARE.
36//
37
38#ifndef Foundation_SharedLibrary_INCLUDED
39#define Foundation_SharedLibrary_INCLUDED
40
41#include "platform.hpp"
42
43#include <mutex>
44#include <string>
45
46namespace BT
47{
48class SharedLibrary
49/// The SharedLibrary class dynamically
50/// loads shared libraries at run-time.
51{
52public:
53 enum Flags
54 {
55 /// On platforms that use dlopen(), use RTLD_GLOBAL. This is the default
56 /// if no flags are given.
57 ///
58 /// This flag is ignored on platforms that do not use dlopen().
59 SHLIB_GLOBAL = 1,
60
61 /// On platforms that use dlopen(), use RTLD_LOCAL instead of RTLD_GLOBAL.
62 ///
63 /// Note that if this flag is specified, RTTI (including dynamic_cast and throw) will
64 /// not work for types defined in the shared library with GCC and possibly other
65 /// compilers as well. See http://gcc.gnu.org/faq.html#dso for more information.
66 ///
67 /// This flag is ignored on platforms that do not use dlopen().
68 SHLIB_LOCAL = 2
69 };
70
71 /// Creates a SharedLibrary object.
73
74 /// Creates a SharedLibrary object and loads a library
75 /// from the given path, using the given flags.
76 /// See the Flags enumeration for valid values.
77 SharedLibrary(const std::string& path, int flags = 0);
78
79 /// Destroys the SharedLibrary. The actual library
80 /// remains loaded.
81 virtual ~SharedLibrary() = default;
82
83 /// Loads a shared library from the given path,
84 /// using the given flags. See the Flags enumeration
85 /// for valid values.
86 /// Throws a LibraryAlreadyLoadedException if
87 /// a library has already been loaded.
88 /// Throws a LibraryLoadException if the library
89 /// cannot be loaded.
90 void load(const std::string& path, int flags = 0);
91
92 /// Unloads a shared library.
93 void unload();
94
95 /// Returns true iff a library has been loaded.
96 bool isLoaded() const;
97
98 /// Returns true iff the loaded library contains
99 /// a symbol with the given name.
100 bool hasSymbol(const std::string& name);
101
102 /// Returns the address of the symbol with
103 /// the given name. For functions, this
104 /// is the entry point of the function.
105 /// Throws a NotFoundException if the symbol
106 /// does not exist.
107 void* getSymbol(const std::string& name);
108
109 /// Returns the path of the library, as
110 /// specified in a call to load() or the
111 /// constructor.
112 const std::string& getPath() const;
113
114 /// Returns the platform-specific filename prefix
115 /// for shared libraries.
116 /// Most platforms would return "lib" as prefix, while
117 /// on Cygwin, the "cyg" prefix will be returned.
118 static std::string prefix();
119
120 /// Returns the platform-specific filename suffix
121 /// for shared libraries (including the period).
122 /// In debug mode, the suffix also includes a
123 /// "d" to specify the debug version of a library.
124 static std::string suffix();
125
126 /// Returns the platform-specific filename
127 /// for shared libraries by prefixing and suffixing name
128 /// with prefix() and suffix()
129 static std::string getOSName(const std::string& name);
130
131 SharedLibrary(const SharedLibrary&) = delete;
132 SharedLibrary& operator=(const SharedLibrary&) = delete;
133 SharedLibrary(SharedLibrary&&) = delete;
134 SharedLibrary& operator=(SharedLibrary&&) = delete;
135
136private:
137 void* findSymbol(const std::string& name);
138
139 std::string _path;
140 void* _handle = nullptr;
141 std::mutex _mutex;
142};
143
144} // namespace BT
145
146#endif // Foundation_SharedLibrary_INCLUDED
Definition: shared_library.h:51
bool hasSymbol(const std::string &name)
SharedLibrary(const std::string &path, int flags=0)
void load(const std::string &path, int flags=0)
void * getSymbol(const std::string &name)
bool isLoaded() const
Returns true iff a library has been loaded.
static std::string suffix()
virtual ~SharedLibrary()=default
SharedLibrary()
Creates a SharedLibrary object.
static std::string prefix()
static std::string getOSName(const std::string &name)
const std::string & getPath() const
Flags
Definition: shared_library.h:54
@ SHLIB_GLOBAL
Definition: shared_library.h:59
@ SHLIB_LOCAL
Definition: shared_library.h:68
void unload()
Unloads a shared library.
Definition: action_node.h:24