Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_LibraryImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FBaseRt_LibraryImpl.cpp
19  * @brief       This is the implementation file for the _LibraryImpl class.
20  *
21  */
22
23 #include <dlfcn.h>
24 #include <stdlib.h>
25 #include <wchar.h>
26 #include <FBaseUtilStringUtil.h>
27 #include <FBaseSysLog.h>
28 #include "FBase_StringConverter.h"
29 #include "FBaseRt_LibraryImpl.h"
30
31
32 namespace Tizen { namespace Base { namespace Runtime
33 {
34
35 _LibraryImpl::_LibraryImpl(void)
36         : __pHandle(null)
37 {
38 }
39
40 _LibraryImpl::~_LibraryImpl(void)
41 {
42         if (__pHandle != null)
43         {
44                 dlclose(__pHandle);
45         }
46 }
47
48 result
49 _LibraryImpl::Construct(const Tizen::Base::String& dllPath, unsigned long option)
50 {
51         result r = E_SUCCESS;
52         char* pLibPath = null;
53         int flag = -1;
54
55         SysTryReturnResult(NID_BASE_RT, __pHandle == null, E_INVALID_STATE,
56                                 "Library has already has been loaded.");
57
58         SysTryReturnResult(NID_BASE_RT, !dllPath.IsEmpty(), E_INVALID_ARG, "Specified path is null string.");
59
60         if (option & _LIBRARY_LOAD_OPTION_LAZY)
61         {
62                 flag = RTLD_LAZY;
63         }
64
65         if (option & _LIBRARY_LOAD_OPTION_NOW)
66         {
67                 SysTryReturnResult(NID_BASE_RT, flag != RTLD_LAZY, E_INVALID_ARG,
68                                                   "_LIBRARY_LOAD_OPTION_LAZY and _LIBRARY_LOAD_OPTION_NOW must be included exclusively.");
69
70                 flag = RTLD_NOW;
71         }
72
73         SysTryReturnResult(NID_BASE_RT, flag != -1, E_INVALID_ARG,
74                                           "One of _LIBRARY_LOAD_OPTION_LAZY or _LIBRARY_LOAD_OPTION_NOW must be included.");
75
76         if (option & _LIBRARY_LOAD_OPTION_GLOBAL)
77         {
78                 flag |= RTLD_GLOBAL;
79         }
80
81         if (option & _LIBRARY_LOAD_OPTION_LOCAL)
82         {
83                 SysTryReturnResult(NID_BASE_RT, (option & _LIBRARY_LOAD_OPTION_GLOBAL) == 0, E_INVALID_ARG,
84                                                   "_LIBRARY_LOAD_OPTION_LAZY and _LIBRARY_LOAD_OPTION_NOW must be included exclusively.");
85
86                 flag |= RTLD_LOCAL;
87         }
88
89         if (option & _LIBRARY_LOAD_OPTION_NODELETE)
90         {
91                 flag |= RTLD_NODELETE;
92         }
93
94         pLibPath = _StringConverter::CopyToCharArrayN(dllPath);
95         SysTryReturnResult(NID_BASE_RT, pLibPath != null, E_OUT_OF_MEMORY, "Not enough memroy.");
96
97         __pHandle = dlopen(pLibPath, flag);
98         SysTryCatch(NID_BASE_RT, __pHandle != null, r = E_LIBRARY_NOT_FOUND, E_LIBRARY_NOT_FOUND,
99                            "[E_LIBRARY_NOT_FOUND] Failed to load the library with reason: %s",
100                            dlerror());
101
102         delete[] pLibPath;
103
104         return E_SUCCESS;
105
106 CATCH:
107         delete[] pLibPath;
108
109         return r;
110 }
111
112 void*
113 _LibraryImpl::GetProcAddress(const Tizen::Base::String& symbol)
114 {
115         result r = E_SUCCESS;
116         char* pSymName = null;
117         void* pSymAddr = null;
118
119         SysTryReturn(NID_BASE_RT, __pHandle != null, null, E_INVALID_ARG, "[E_INVALID_ARG] Library is not loaded.");
120         SysTryReturn(NID_BASE_RT, !symbol.IsEmpty(), null, E_INVALID_ARG,
121                                 "[E_INVALID_ARG] Specified path is null.");
122
123         pSymName = _StringConverter::CopyToCharArrayN(symbol);
124         SysTryCatch(NID_BASE_RT, pSymName != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
125
126         pSymAddr = dlsym(__pHandle, pSymName);
127         SysTryCatch(NID_BASE_RT, pSymAddr != null, , E_SYMBOL_NOT_FOUND,
128                            "[E_SYMBOL_NOT_FOUND] Failed to get a symbol address with reason : %s",
129                            dlerror());
130
131         delete[] pSymName;
132
133         return pSymAddr;
134
135 CATCH:
136         SetLastResult(r);
137
138         delete[] pSymName;
139
140         return null;
141 }
142
143 } } } // Tizen::Runtime