BodyCustomizerInterface.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * National Institute of Advanced Industrial Science and Technology (AIST)
8  * General Robotix Inc.
9  */
16 #include <iostream>
18 #include "Body.h"
19 #include <cstdlib>
20 #include <map>
21 #include <boost/version.hpp>
22 #include <boost/regex.hpp>
23 #include <boost/tokenizer.hpp>
24 
25 #if (BOOST_VERSION <= 103301)
26 #include <boost/filesystem/path.hpp>
27 #include <boost/filesystem/operations.hpp>
28 #else
29 #include <boost/filesystem.hpp>
30 #endif
31 
32 using namespace hrp;
33 using namespace std;
34 using namespace boost;
35 
36 namespace {
37 
38 #ifdef _WIN32
39 # include <windows.h>
40  const char* DLLSFX = ".dll";
41  const char* PATH_DELIMITER = ";";
42  typedef HINSTANCE DllHandle;
43  inline DllHandle loadDll(const char* filename) { return LoadLibrary(filename); }
44  inline void* resolveDllSymbol(DllHandle handle, const char* symbol) { return GetProcAddress(handle, symbol); }
45  inline void unloadDll(DllHandle handle) { FreeLibrary(handle); }
46 #else
47 # include <dlfcn.h>
48 #ifdef __darwin__
49  const char* DLLSFX = ".dylib";
50 #else
51  const char* DLLSFX = ".so";
52 #endif
53  const char* PATH_DELIMITER = ":";
54  typedef void* DllHandle;
55  inline DllHandle loadDll(const char* filename) { return dlopen(filename, RTLD_LAZY); }
56  inline void* resolveDllSymbol(DllHandle handle, const char* symbol) { return dlsym(handle, symbol); }
57  inline void unloadDll(DllHandle handle) { dlclose(handle); }
58 #endif
59 
60  typedef std::map<std::string, BodyCustomizerInterface*> NameToInterfaceMap;
61  NameToInterfaceMap customizerRepository;
62  bool pluginLoadingFunctionsCalled = false;
63 
64  inline string toNativePathString(const filesystem::path& path) {
65 #if (BOOST_VERSION <= 103301)
66  return path.native_file_string();
67 #elif (BOOST_VERSION < 104600)
68  return path.file_string();
69 #else
70  return path.string();
71 #endif
72  }
73 
74 }
75 
76 
77 static bool checkInterface(BodyCustomizerInterface* customizerInterface)
78 {
79  bool qualified = true
80  && (customizerInterface->version == BODY_CUSTOMIZER_INTERFACE_VERSION)
81  && customizerInterface->getTargetModelNames
82  && customizerInterface->create
83  && customizerInterface->destroy;
84  //&& customizerInterface->initializeAnalyticIk
85  //&& customizerInterface->calcAnalyticIk
86  //&& customizerInterface->setVirtualJointForces;
87 
88  return qualified;
89 }
90 
91 
92 static bool loadCustomizerDll(BodyInterface* bodyInterface, const std::string filename)
93 {
94  BodyCustomizerInterface* customizerInterface = 0;
95 
96  DllHandle dll = loadDll(filename.c_str());
97 
98  if(dll){
99 
100  GetBodyCustomizerInterfaceFunc getCustomizerInterface =
101  (GetBodyCustomizerInterfaceFunc)resolveDllSymbol(dll, "getHrpBodyCustomizerInterface");
102 
103  if(!getCustomizerInterface){
104  unloadDll(dll);
105  } else {
106  customizerInterface = getCustomizerInterface(bodyInterface);
107 
108  if(customizerInterface){
109 
110  if(!checkInterface(customizerInterface)){
111  cerr << "Body customizer \"" << filename << "\" is incomatible and cannot be loaded.";
112  } else {
113  cerr << "Loading body customizer \"" << filename << "\" for ";
114 
115  const char** names = customizerInterface->getTargetModelNames();
116 
117  for(int i=0; names[i]; ++i){
118  if(i > 0){
119  cerr << ", ";
120  }
121  string name(names[i]);
122  if(!name.empty()){
123  customizerRepository[name] = customizerInterface;
124  }
125  cerr << names[i];
126  }
127  cerr << endl;
128  }
129  }
130  }
131  }
132 
133  return (customizerInterface != 0);
134 }
135 
136 
147 int hrp::loadBodyCustomizers(const std::string pathString, BodyInterface* bodyInterface)
148 {
149  pluginLoadingFunctionsCalled = true;
150 
151  int numLoaded = 0;
152 #if (BOOST_VERSION < 104600)
153  filesystem::path pluginPath(pathString, filesystem::native);
154 
155  if(filesystem::exists(pluginPath)){
156 
157  if(!filesystem::is_directory(pluginPath)){
158  if(loadCustomizerDll(bodyInterface, toNativePathString(pluginPath))){
159  numLoaded++;
160  }
161  } else {
162  regex pluginNamePattern(string(".+Customizer") + DLLSFX);
163  filesystem::directory_iterator end;
164 
165  for(filesystem::directory_iterator it(pluginPath); it != end; ++it){
166  const filesystem::path& filepath = *it;
167  if(!filesystem::is_directory(filepath)){
168  if(regex_match(filepath.leaf(), pluginNamePattern)){
169  if(loadCustomizerDll(bodyInterface, toNativePathString(filepath))){
170  numLoaded++;
171  }
172  }
173  }
174  }
175  }
176  }
177 #elif (BOOST_VERSION >= 105000)
178  filesystem::path pluginPath(pathString, (void *)filesystem::native);
179 
180  if(filesystem::exists(pluginPath)){
181 
182  if(!filesystem::is_directory(pluginPath)){
183  if(loadCustomizerDll(bodyInterface, toNativePathString(pluginPath))){
184  numLoaded++;
185  }
186  } else {
187  regex pluginNamePattern(string(".+Customizer") + DLLSFX);
188  filesystem::directory_iterator end;
189 
190  for(filesystem::directory_iterator it(pluginPath); it != end; ++it){
191  const filesystem::path& filepath = *it;
192  if(!filesystem::is_directory(filepath)){
193  if(regex_match(filepath.filename().string(), pluginNamePattern)){
194  if(loadCustomizerDll(bodyInterface, toNativePathString(filepath))){
195  numLoaded++;
196  }
197  }
198  }
199  }
200  }
201  }
202 #else
203  filesystem3::path pluginPath(pathString, (void *)filesystem3::native);
204 
205  if(filesystem3::exists(pluginPath)){
206 
207  if(!filesystem3::is_directory(pluginPath)){
208  if(loadCustomizerDll(bodyInterface, toNativePathString(pluginPath))){
209  numLoaded++;
210  }
211  } else {
212  regex pluginNamePattern(string(".+Customizer") + DLLSFX);
213  filesystem3::directory_iterator end;
214 
215  for(filesystem3::directory_iterator it(pluginPath); it != end; ++it){
216  const filesystem3::path& filepath = *it;
217  if(!filesystem3::is_directory(filepath)){
218  if(regex_match(filepath.filename().string(), pluginNamePattern)){
219  if(loadCustomizerDll(bodyInterface, toNativePathString(filepath))){
220  numLoaded++;
221  }
222  }
223  }
224  }
225  }
226  }
227 #endif
228  return numLoaded;
229 }
230 
231 
232 int hrp::loadBodyCustomizers(const std::string pathString)
233 {
234  return loadBodyCustomizers(pathString, Body::bodyInterface());
235 }
236 
237 
243 {
244  int numLoaded = 0;
245 
246  if(!pluginLoadingFunctionsCalled){
247 
248  pluginLoadingFunctionsCalled = true;
249 
250  char* pathListEnv = getenv("HRPMODEL_CUSTOMIZER_PATH");
251 
252  if(pathListEnv){
253  char_separator<char> sep(PATH_DELIMITER);
254  string pathList(pathListEnv);
255  tokenizer< char_separator<char> > paths(pathList, sep);
256  tokenizer< char_separator<char> >::iterator p;
257  for(p = paths.begin(); p != paths.end(); ++p){
258  numLoaded = loadBodyCustomizers(*p, bodyInterface);
259  }
260  }
261 
262 #ifndef _WIN32
263  Dl_info info;
264  if(dladdr((void*)&hrp::findBodyCustomizer, &info)){
265  filesystem::path customizerPath =
266  filesystem::path(info.dli_fname).branch_path().branch_path() / OPENHRP_RELATIVE_SHARE_DIR / "customizer";
267  numLoaded += loadBodyCustomizers(customizerPath.string(), bodyInterface);
268  }
269 #else
270  string customizerPath(OPENHRP_SHARE_DIR);
271  customizerPath.append("/customizer");
272  numLoaded += loadBodyCustomizers(customizerPath, bodyInterface);
273 #endif
274 
275  }
276 
277  return numLoaded;
278 }
279 
280 
282 {
284 }
285 
286 
288 {
289  BodyCustomizerInterface* customizerInterface = 0;
290 
291  NameToInterfaceMap::iterator p = customizerRepository.find(modelName);
292  if(p != customizerRepository.end()){
293  customizerInterface = p->second;
294  }
295 
296  return customizerInterface;
297 }
static bool checkInterface(BodyCustomizerInterface *customizerInterface)
Modifications controlling boost library behavior.
Definition: ColladaUtil.h:306
char * filename
Definition: cdjpeg.h:133
png_infop png_charpp name
Definition: png.h:2382
static const int BODY_CUSTOMIZER_INTERFACE_VERSION
png_uint_32 i
Definition: png.h:2735
static BodyInterface * bodyInterface()
Definition: Body.cpp:766
static bool loadCustomizerDll(BodyInterface *bodyInterface, const std::string filename)
BodyCustomizerCreateFunc create
static BodyInterface * bodyInterface
BodyCustomizerDestroyFunc destroy
BodyCustomizerInterface *(* GetBodyCustomizerInterfaceFunc)(BodyInterface *bodyInterface)
The definitions of the body customizer interface for increasing binary compatibility.
char * getenv(const char *name)
BodyCustomizerGetTargetModelNamesFunc getTargetModelNames
backing_store_ptr info
Definition: jmemsys.h:181
HRPMODEL_API int loadBodyCustomizers(const std::string pathString, BodyInterface *bodyInterface)
HRPMODEL_API BodyCustomizerInterface * findBodyCustomizer(std::string modelName)


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:19