3DiVi Face SDK  3.24.0
 Указатель Классы Пространства имен Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Свойства Группы
DllHandle.h
1 #ifndef __PBIO_DLL_HANDLE_H_989601403fee4b0d83e85ee9b5292036
2 #define __PBIO_DLL_HANDLE_H_989601403fee4b0d83e85ee9b5292036
3 
5 
6 #include <string>
7 #include <sstream>
8 #include <iomanip>
9 
10 #include "c_api_functions_list_macro.h"
11 
12 #include "Error.h"
13 #include "SmartPtr.h"
14 
15 
16 #ifndef __STATIC_LIBFACEREC_BUILD__
17  #ifdef _WIN32
18  #include <windows.h>
19  #else
20  #include <dlfcn.h>
21  #endif
22 #endif
23 
24 
25 
26 #ifdef __STATIC_LIBFACEREC_BUILD__
27 // with __STATIC_LIBFACEREC_BUILD__
29 
30  #define __META_STATIC_DECL(rtype, name, typed_args, args, return) \
31  extern "C" rtype name typed_args;
32 
33  __TDV_METASDK_FLIST(__META_STATIC_DECL)
34 
35  #define __583e_STATIC_DECL(rtype, name, typed_args, args, return) \
36  rtype _583e_ADD_NAMESPACE(name) typed_args;
37 
38  __583e_FLIST(__583e_STATIC_DECL)
39  __TDV_FLIST(__583e_STATIC_DECL)
40 
41 // with __STATIC_LIBFACEREC_BUILD__
43 #endif
44 
45 namespace pbio {
46 namespace import {
47 
48 class DllHandle
49 {
50 
51 private:
52  int32_t refcounter4light_shared_ptr;
53 
54  friend class object_with_ref_counter<DllHandle>;
55 
56 
57 #ifdef __STATIC_LIBFACEREC_BUILD__
58 // with __STATIC_LIBFACEREC_BUILD__
60 
61 private:
62  const int _placeholder;
63 
64 public:
65  #define __583e_STATIC_F_INIT(rtype, name, typed_args, args, return) \
66  , name( _583e_ADD_NAMESPACE(name) )
67  #define __META_STATIC_F_INIT(rtype, name, typed_args, args, return) \
68  , name( ::name )
69 
70  DllHandle() :
71  _placeholder(0)
72  __583e_FLIST(__583e_STATIC_F_INIT)
73  __TDV_FLIST(__583e_STATIC_F_INIT)
74  __TDV_METASDK_FLIST(__META_STATIC_F_INIT)
75  {
76  // nothing else
77  }
78 
79 // with __STATIC_LIBFACEREC_BUILD__
81 #else
82 // without __STATIC_LIBFACEREC_BUILD__
84 
85 public:
86 
87  #define __583e_SHARED_F_INIT(rtype, name, typed_args, args, return) \
88  , name( (FuncType_##name) getSymbol( _583e_STR_ADD_NAMESPACE(name) ) )
89 
90  #define __TDV_SHARED_F_INIT(rtype, name, typed_args, args, return) \
91  , name( (FuncType_##name) getSymbol( _583e_STRINGISE2(name) ) )
92 
93  DllHandle(const char* const dll_path) :
94  _dll_path(dll_path),
95 #ifdef _WIN32
96  _dll_sense4( loadDll(make_sense4_dll_path(dll_path).c_str()) ),
97 #endif
98  _dll( loadDll(dll_path) )
99  __583e_FLIST(__583e_SHARED_F_INIT)
100  __TDV_FLIST(__TDV_SHARED_F_INIT)
101  __TDV_METASDK_FLIST(__TDV_SHARED_F_INIT)
102  {
103  // nothig else
104  }
105 
106  ~DllHandle()
107  {
108  #if defined(_WIN32)
109  FreeLibrary(_dll);
110  FreeLibrary(_dll_sense4);
111  #else
112  dlclose(_dll);
113  #endif
114  }
115 
116 private:
117 
118  #ifndef _WIN32
119  typedef void* HINSTANCE;
120  #endif
121 
122  #ifdef _WIN32
123  static
124  std::string make_sense4_dll_path(char const* const dll_path)
125  {
126  int pos = -1;
127  for(int i = 0; dll_path[i]; ++i)
128  if(dll_path[i] == '/' || dll_path[i] == '\\')
129  pos = i;
130 
131  return std::string(dll_path, dll_path + (pos + 1)) + "sense4.dll";
132  }
133  #endif
134 
135  static
136  HINSTANCE loadDll(char const* const dll_path)
137  {
138  #if defined(_WIN32)
139  HINSTANCE const result = LoadLibraryA(dll_path);
140  #else
141  HINSTANCE const result = dlopen( dll_path, RTLD_NOW|RTLD_LOCAL);
142  #endif
143 
144  if(!result)
145  {
146  std::string error;
147  #if defined(_WIN32)
148  {
149  char error_buffer[1024];
150 
151  const DWORD err_code = GetLastError();
152 
153  std::ostringstream oss;
154 
155  oss << " last error: " << std::hex << err_code;
156 
157  if(FormatMessageA(
158  /* dwFlags = */ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
159  /* lpSource = */ NULL,
160  /* dwMessageId = */ err_code,
161  /* dwLanguageId = */ 0,
162  /* lpBuffer = */ error_buffer,
163  /* nSize = */ sizeof(error_buffer) - 1,
164  /* Arguments = */ NULL ) > 0 )
165  {
166  oss << " : '" << error_buffer << "'";
167  }
168 
169  error = oss.str();
170  }
171  #else
172  {
173  char const* const error_c_str = dlerror();
174  if(error_c_str)
175  error = " dlerror: '" + std::string(error_c_str) + "'";
176  }
177  #endif
178 
179  throw pbio::Error(0xbd2483c0, "Error in pbio::FacerecService::createService"
180  " can't open dll file'" + std::string(dll_path) + "', error code: 0xbd2483c0" + error);
181  }
182 
183  return result;
184  }
185 
186  void* getSymbol(char const* const name) const
187  {
188  #ifdef _WIN32
189  void* const result = reinterpret_cast<void*>( GetProcAddress(_dll, name) );
190  #else
191  void* const result = dlsym(_dll, name);
192  #endif
193 
194  if(!result)
195  {
196  throw pbio::Error(0x5146c155, "Error in pbio::FacerecService::createService"
197  " can't fynd symbol '" + std::string(name) + "' in dll '" + _dll_path + "', error code: 0x5146c155");
198  }
199 
200  return result;
201  }
202 
203  const std::string _dll_path;
204 
205 #ifdef _WIN32
206  HINSTANCE const _dll_sense4;
207 #endif
208 
209  HINSTANCE const _dll;
210 
211 // without __STATIC_LIBFACEREC_BUILD__
213 #endif
214 
215 
216 public:
217 
218  #define __583e_F_FIELD_DECL(rtype, name, typed_args, args, return) \
219  typedef rtype (*FuncType_##name) typed_args; \
220  const FuncType_##name name;
221 
222  #define __TDV_F_FIELD_DECL(rtype, name, typed_args, args, return) \
223  typedef rtype (*FuncType_##name) typed_args; \
224  const FuncType_##name name;
225 
226  __583e_FLIST(__583e_F_FIELD_DECL)
227  __TDV_FLIST(__TDV_F_FIELD_DECL)
228  __TDV_METASDK_FLIST(__583e_F_FIELD_DECL)
229 
230 };
231 
232 } // pbio namespace
233 } // import namespace
234 
235 
237 
238 #endif // __PBIO_DLL_HANDLE_H_989601403fee4b0d83e85ee9b5292036
Error - класс исключений, выбрасываемых при возникновении ошибок.
Класс исключений, выбрасываемых при возникновении ошибок.
Definition: Error.h:26
SmartPtr.