replicode
sharedlibrary.h
Go to the documentation of this file.
1 // utils.h
2 //
3 // Author: Eric Nivel, Thor List
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel, Thor List
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel or Thor List nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef SHAREDLIBRARY_H
32 #define SHAREDLIBRARY_H
33 
34 #include "dll.h"
35 
36 #include <mutex>
37 #include <iostream>
38 
39 
40 #if !defined(WIN32) || !defined(WIN64)
41 #include <dlfcn.h>
42 #endif
43 
44 // Wrapping of OS-dependent functions
45 namespace core {
46 
47 #if defined(WIN32) || defined(WIN64)
48 typedef HINSTANCE shared_object;
49 #else
50 typedef void* shared_object;
51 #endif
52 
54 private:
55  shared_object library;
56 public:
57  static SharedLibrary *New(const char *fileName);
58  SharedLibrary();
59  ~SharedLibrary();
60  SharedLibrary *load(const char *fileName);
61  template<typename T> T getFunction(const char *functionName) {
62  T function = NULL;
63  #if defined(WIN32) || defined(WIN64)
64  if (library) {
65 
66  function = (T)GetProcAddress(library, functionName);
67  if (!function) {
68 
69  DWORD error = GetLastError();
70  std::cerr << "GetProcAddress > Error: " << error << std::endl;
71  }
72  }
73  #else
74  if (library) {
75  function = T(dlsym(library, functionName));
76  if (!function) {
77  std::cout << "> Error: unable to find symbol " << functionName << " :" << dlerror() << std::endl;
78  }
79  }
80  #endif
81  return function;
82  }
83 };
84 
85 } // namespace core
86 
87 #endif//SHAREDLIBRARY_H
Definition: base.cpp:35
shared_object library
Definition: sharedlibrary.h:55
Definition: sharedlibrary.h:53
#define core_dll
Definition: dll.h:43
void * shared_object
Definition: sharedlibrary.h:50
T getFunction(const char *functionName)
Definition: sharedlibrary.h:61