2 * Copyright (C) 2012, BMW AG
4 * This file is part of GENIVI Project AudioManager.
6 * Contributions are licensed to the GENIVI Alliance under one or more
7 * Contribution License Agreements.
10 * This Source Code Form is subject to the terms of the
11 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
12 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
17 * \file TAmPluginTemplate.h
18 * For further information see http://www.genivi.org/.
22 #ifndef PLUGINTEMPLATE_H_
23 #define PLUGINTEMPLATE_H_
27 #include "shared/CAmDltWrapper.h"
33 * * This template tries to load a library and cast to a class
34 * @param libname the full path to the library to be loaded
35 * @param libraryHandle the handle to the library that gets returned
36 * @return returns the pointer to the class to be loaded
38 template<class T> T* getCreateFunction(const std::string& libname, void*& libraryHandle)
41 logInfo("getCreateFunction : Trying to load library with name: ",libname);
43 // cut off directories
44 char* fileWithPath = const_cast<char*>(libname.c_str());
45 std::string libFileName = basename(fileWithPath);
47 // cut off "lib" in front and cut off .so end"
48 std::string createFunctionName = libFileName.substr(3, libFileName.length() - 6) + "Factory";
50 dlerror(); // Clear any existing error
51 libraryHandle = dlopen(libname.c_str(), RTLD_LAZY );
52 const char* dlopen_error = dlerror();
53 if (!libraryHandle || dlopen_error)
55 logError("getCreateFunction : dlopen failed",dlopen_error);
59 // get entry point from shared lib
60 dlerror(); // Clear any existing error
68 // Note: direct cast is not allowed by ISO C++. e.g.
69 // T* createFunction = reinterpret_cast<T*>(dlsym(libraryHandle, createFunctionName.c_str()));
70 // compiler warning: "forbids casting between pointer-to-function and pointer-to-object"
72 functionPointer.voidPointer = dlsym(libraryHandle, createFunctionName.c_str());
73 T* createFunction = functionPointer.typedPointer;
75 const char* dlsym_error = dlerror();
76 if (!createFunction || dlsym_error)
78 logError("getCreateFunction: Failed to load shared lib entry point",dlsym_error);
82 logInfo("getCreateFunction : loaded successfully plugin", createFunctionName);
84 return (createFunction);
89 #endif /* PLUGINTEMPLATE_H_ */