Skip to content

Commit 13d8917

Browse files
committed
fix in SharedLibrary and cosmetic changes to the code
1 parent 83580a8 commit 13d8917

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

include/behaviortree_cpp/bt_factory.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,38 @@ inline TreeNodeManifest CreateManifest(const std::string& ID,
4646
return {getType<T>(), ID, portlist, {}};
4747
}
4848

49-
constexpr const char* PLUGIN_SYMBOL = "BT_RegisterNodesFromPlugin";
49+
#ifdef BT_PLUGIN_EXPORT
5050

51-
#ifndef BT_PLUGIN_EXPORT
51+
#if defined(_WIN32)
52+
#define BTCPP_EXPORT extern "C" __declspec(dllexport)
53+
#else
54+
// Unix-like OSes
55+
#define BTCPP_EXPORT extern "C" __attribute__ ((visibility ("default")))
56+
#endif
5257

58+
#else
59+
#define BTCPP_EXPORT
60+
#endif
5361
/* Use this macro to automatically register one or more custom Nodes
54-
into a factory. For instance:
55-
56-
BT_REGISTER_NODES(factory)
57-
{
58-
factory.registerNodeType<MoveBaseAction>("MoveBase");
59-
}
62+
* into a factory. For instance:
63+
*
64+
* BT_REGISTER_NODES(factory)
65+
* {
66+
* factory.registerNodeType<MoveBaseAction>("MoveBase");
67+
* }
68+
*
69+
* IMPORTANT: this function MUST be declared in a cpp file, NOT a header file.
70+
* In your cake, you must add the definition [BT_PLUGIN_EXPORT] with:
71+
*
72+
* target_compile_definitions(my_plugin_target PRIVATE BT_PLUGIN_EXPORT )
6073
61-
IMPORTANT: this function MUST be declared in a cpp file, NOT a header file.
62-
See examples for more information about configuring CMake correctly
74+
* See examples in sample_nodes directory.
6375
*/
64-
#define BT_REGISTER_NODES(factory) \
65-
static void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
66-
67-
#else
68-
69-
#if defined(__linux__) || defined __APPLE__
70-
71-
#define BT_REGISTER_NODES(factory) \
72-
extern "C" void __attribute__((visibility("default"))) \
73-
BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
74-
75-
#elif _WIN32
7676

7777
#define BT_REGISTER_NODES(factory) \
78-
extern "C" void __declspec(dllexport) \
79-
BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
80-
#endif
78+
static BTCPP_EXPORT void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
8179

82-
#endif
80+
constexpr const char* PLUGIN_SYMBOL = "BT_RegisterNodesFromPlugin";
8381

8482
/**
8583
* @brief Struct used to store a tree.

include/behaviortree_cpp/utils/shared_library.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,81 +51,81 @@ class SharedLibrary
5151
public:
5252
enum Flags
5353
{
54-
SHLIB_GLOBAL = 1,
5554
/// On platforms that use dlopen(), use RTLD_GLOBAL. This is the default
5655
/// if no flags are given.
5756
///
5857
/// This flag is ignored on platforms that do not use dlopen().
58+
SHLIB_GLOBAL = 1,
5959

60-
SHLIB_LOCAL = 2
6160
/// On platforms that use dlopen(), use RTLD_LOCAL instead of RTLD_GLOBAL.
6261
///
6362
/// Note that if this flag is specified, RTTI (including dynamic_cast and throw) will
6463
/// not work for types defined in the shared library with GCC and possibly other
6564
/// compilers as well. See http://gcc.gnu.org/faq.html#dso for more information.
6665
///
6766
/// This flag is ignored on platforms that do not use dlopen().
67+
SHLIB_LOCAL = 2
6868
};
6969

70-
SharedLibrary();
7170
/// Creates a SharedLibrary object.
71+
SharedLibrary();
7272

73-
SharedLibrary(const std::string& path, int flags = 0);
7473
/// Creates a SharedLibrary object and loads a library
7574
/// from the given path, using the given flags.
7675
/// See the Flags enumeration for valid values.
76+
SharedLibrary(const std::string& path, int flags = 0);
7777

78-
virtual ~SharedLibrary() = default;
7978
/// Destroys the SharedLibrary. The actual library
8079
/// remains loaded.
80+
virtual ~SharedLibrary() = default;
8181

82-
void load(const std::string& path, int flags = 0);
8382
/// Loads a shared library from the given path,
8483
/// using the given flags. See the Flags enumeration
8584
/// for valid values.
8685
/// Throws a LibraryAlreadyLoadedException if
8786
/// a library has already been loaded.
8887
/// Throws a LibraryLoadException if the library
8988
/// cannot be loaded.
89+
void load(const std::string& path, int flags = 0);
9090

91-
void unload();
9291
/// Unloads a shared library.
92+
void unload();
9393

94-
bool isLoaded() const;
9594
/// Returns true iff a library has been loaded.
95+
bool isLoaded() const;
9696

97-
bool hasSymbol(const std::string& name);
9897
/// Returns true iff the loaded library contains
9998
/// a symbol with the given name.
99+
bool hasSymbol(const std::string& name);
100100

101-
void* getSymbol(const std::string& name);
102101
/// Returns the address of the symbol with
103102
/// the given name. For functions, this
104103
/// is the entry point of the function.
105104
/// Throws a NotFoundException if the symbol
106105
/// does not exist.
106+
void* getSymbol(const std::string& name);
107107

108-
const std::string& getPath() const;
109108
/// Returns the path of the library, as
110109
/// specified in a call to load() or the
111110
/// constructor.
111+
const std::string& getPath() const;
112112

113-
static std::string prefix();
114113
/// Returns the platform-specific filename prefix
115114
/// for shared libraries.
116115
/// Most platforms would return "lib" as prefix, while
117116
/// on Cygwin, the "cyg" prefix will be returned.
117+
static std::string prefix();
118118

119-
static std::string suffix();
120119
/// Returns the platform-specific filename suffix
121120
/// for shared libraries (including the period).
122121
/// In debug mode, the suffix also includes a
123122
/// "d" to specify the debug version of a library.
123+
static std::string suffix();
124124

125-
static std::string getOSName(const std::string& name);
126125
/// Returns the platform-specific filename
127126
/// for shared libraries by prefixing and suffixing name
128127
/// with prefix() and suffix()
128+
static std::string getOSName(const std::string& name);
129129

130130
private:
131131
SharedLibrary(const SharedLibrary&);
@@ -134,7 +134,7 @@ class SharedLibrary
134134
void* findSymbol(const std::string& name);
135135

136136
std::string _path;
137-
void* _handle;
137+
void* _handle = nullptr;
138138
std::mutex _mutex;
139139
};
140140

0 commit comments

Comments
 (0)