* AmMapHanlderTest - added tests for the callbacks in CAmDatabaseObserver.
[profile/ivi/genivi/genivi-audio-manager.git] / AudioManagerDaemon / include / TAmPluginTemplate.h
1 /**
2  * Copyright (C) 2012, BMW AG
3  *
4  * This file is part of GENIVI Project AudioManager.
5  *
6  * Contributions are licensed to the GENIVI Alliance under one or more
7  * Contribution License Agreements.
8  *
9  * \copyright
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/.
13  *
14  *
15  * \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
16  *
17  * \file TAmPluginTemplate.h
18  * For further information see http://www.genivi.org/.
19  *
20  */
21
22 #ifndef PLUGINTEMPLATE_H_
23 #define PLUGINTEMPLATE_H_
24
25 #include <dlfcn.h>
26 #include <libgen.h>
27 #include "shared/CAmDltWrapper.h"
28
29 namespace am
30 {
31
32 /**
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
37  */
38 template<class T> T* getCreateFunction(const std::string& libname, void*& libraryHandle)
39 {
40
41     logInfo("getCreateFunction : Trying to load library with name: ",libname);
42
43     // cut off directories
44     char* fileWithPath = const_cast<char*>(libname.c_str());
45     std::string libFileName = basename(fileWithPath);
46
47     // cut off "lib" in front and cut off .so end"
48     std::string createFunctionName = libFileName.substr(3, libFileName.length() - 6) + "Factory";
49     // open library
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)
54     {
55         logError("getCreateFunction : dlopen failed",dlopen_error);
56         return (0);
57     }
58
59     // get entry point from shared lib
60     dlerror(); // Clear any existing error
61
62     union
63     {
64         void* voidPointer;
65         T* typedPointer;
66     } functionPointer;
67
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"
71
72     functionPointer.voidPointer = dlsym(libraryHandle, createFunctionName.c_str());
73     T* createFunction = functionPointer.typedPointer;
74
75     const char* dlsym_error = dlerror();
76     if (!createFunction || dlsym_error)
77     {
78         logError("getCreateFunction: Failed to load shared lib entry point",dlsym_error);
79     }
80     else
81     {
82         logInfo("getCreateFunction : loaded successfully plugin", createFunctionName);
83     }
84     return (createFunction);
85 }
86
87 }
88
89 #endif /* PLUGINTEMPLATE_H_ */