AudioManager  7.6.6
Native Application Runtime Environment
TAmPluginTemplate.h
Go to the documentation of this file.
1 
24 #ifndef PLUGINTEMPLATE_H_
25 #define PLUGINTEMPLATE_H_
26 
27 #include <dlfcn.h>
28 #include <libgen.h>
29 #include "CAmDltWrapper.h"
30 
31 namespace am
32 {
33 
40 template<class T> T* getCreateFunction(const std::string& libname, void*& libraryHandle)
41 {
42 
43  logInfo("getCreateFunction : Trying to load library with name: ",libname);
44 
45  // cut off directories
46  char* fileWithPath = const_cast<char*>(libname.c_str());
47  std::string libFileName = basename(fileWithPath);
48 
49  // cut off "lib" in front and cut off .so end"
50  std::string createFunctionName = libFileName.substr(3, libFileName.length() - 6) + "Factory";
51  // open library
52  dlerror(); // Clear any existing error
53  libraryHandle = dlopen(libname.c_str(), RTLD_LAZY );
54  const char* dlopen_error = dlerror();
55  if (!libraryHandle || dlopen_error)
56  {
57  logError("getCreateFunction : dlopen failed",dlopen_error);
58  return (0);
59  }
60 
61  // get entry point from shared lib
62  dlerror(); // Clear any existing error
63 
64  union
65  {
66  void* voidPointer;
67  T* typedPointer;
68  } functionPointer;
69 
70  // Note: direct cast is not allowed by ISO C++. e.g.
71  // T* createFunction = reinterpret_cast<T*>(dlsym(libraryHandle, createFunctionName.c_str()));
72  // compiler warning: "forbids casting between pointer-to-function and pointer-to-object"
73 
74  functionPointer.voidPointer = dlsym(libraryHandle, createFunctionName.c_str());
75  T* createFunction = functionPointer.typedPointer;
76 
77  const char* dlsym_error = dlerror();
78  if (!createFunction || dlsym_error)
79  {
80  logError("getCreateFunction: Failed to load shared lib entry point",dlsym_error);
81  }
82  else
83  {
84  logInfo("getCreateFunction : loaded successfully plugin", createFunctionName);
85  }
86  return (createFunction);
87 }
88 
94 template<class T> T* getDestroyFunction(const std::string& libname,void* libraryHandle)
95 {
96  logInfo("destroy : Trying to destroy : ",libname);
97 
98  // cut off directories
99  char* fileWithPath = const_cast<char*>(libname.c_str());
100  std::string libFileName = basename(fileWithPath);
101 
102  // cut off "lib" in front and cut off .so end"
103  std::string destroyFunctionName = "destroy" + libFileName.substr(3, libFileName.length() - 6);
104 
105  // get entry point from shared lib
106  dlerror(); // Clear any existing error
107  union
108  {
109  void* voidPointer;
110  T* typedPointer;
111  } functionPointer;
112 
113  functionPointer.voidPointer = dlsym(libraryHandle, destroyFunctionName.c_str());
114  T* destroyFunction = functionPointer.typedPointer;
115 
116  const char* dlsym_error = dlerror();
117  if (!destroyFunction || dlsym_error)
118  {
119  logError("getDestroyFunction: Failed to load shared lib entry point function name=",
120  destroyFunctionName, "error=",dlsym_error);
121  }
122  else
123  {
124  logInfo("getDestroyFunction: loaded successfully plugin", destroyFunctionName);
125  }
126  return (destroyFunction);
127 }
128 
129 }
130 
131 #endif /* PLUGINTEMPLATE_H_ */
A Common-API wrapper class, which loads the common-api runtime and instantiates all necessary objects...
void logInfo(T value, TArgs...args)
logs given values with infolevel with the default context
SPDX license identifier: MPL-2.0.
T * getCreateFunction(const std::string &libname, void *&libraryHandle)
void logError(T value, TArgs...args)
logs given values with errorlevel with the default context
T * getDestroyFunction(const std::string &libname, void *libraryHandle)