* make Telnetserver work with maphandler
[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
50     // open library
51     dlerror(); // Clear any existing error
52     libraryHandle = dlopen(libname.c_str(), RTLD_LAZY);
53     const char* dlopen_error = dlerror();
54     if (!libraryHandle || dlopen_error)
55     {
56         logError("getCreateFunction : dlopen failed",dlopen_error);
57         return (0);
58     }
59
60     // get entry point from shared lib
61     dlerror(); // Clear any existing error
62
63     union
64     {
65         void* voidPointer;
66         T* typedPointer;
67     } functionPointer;
68
69     // Note: direct cast is not allowed by ISO C++. e.g.
70     // T* createFunction = reinterpret_cast<T*>(dlsym(libraryHandle, createFunctionName.c_str()));
71     // compiler warning: "forbids casting between pointer-to-function and pointer-to-object"
72
73     functionPointer.voidPointer = dlsym(libraryHandle, createFunctionName.c_str());
74     T* createFunction = functionPointer.typedPointer;
75
76     const char* dlsym_error = dlerror();
77     if (!createFunction || dlsym_error)
78     {
79         logError("getCreateFunction: Failed to load shared lib entry point",dlsym_error);
80     }
81     else
82     {
83         logInfo("getCreateFunction : loaded successfully plugin", createFunctionName);
84     }
85     return (createFunction);
86 }
87
88 }
89
90 #endif /* PLUGINTEMPLATE_H_ */