55304a76a736248997f9557dc851db12b27a789a
[iec.git] / src / type3_AndroidCloud / anbox-master / external / android-emugl / shared / emugl / common / shared_library.h
1 // Copyright (C) 2014 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef EMUGL_COMMON_SHARED_LIBRARY_H
16 #define EMUGL_COMMON_SHARED_LIBRARY_H
17
18 #include <stddef.h>
19
20 #ifdef _WIN32
21 #include <windows.h>
22 #endif
23
24 namespace emugl {
25
26 // A class used to open a platform-specific shared library, and probe
27 // it for symbols. Usage is the following:
28 //
29 //    // Open the library.
30 //    SharedLibrary* library = SharedLibrary::open("libFoo");
31 //    if (!library) {
32 //        ... could not find / open library!
33 //    }
34 //
35 //    //Probe for function symbol.
36 //    FunctionPtr my_func = library->findSymbol("my_func");
37 //
38 //    // Closes library/
39 //    delete library;
40 //
41 class SharedLibrary {
42 public:
43     // Open a given library.  If |libraryName| has no extension, a
44     // platform-appropriate extension is added and that path is opened.
45     // If the |libraryName| has an extension, that form is opened.
46     //
47     // On OSX, some libraries don't include an extension (notably OpenGL)
48     // On OSX we try to open |libraryName| first.  If that doesn't exist,
49     // we try |libraryName|.dylib
50     //
51     // On success, returns a new SharedLibrary instance that must be
52     // deleted by the caller.
53     static SharedLibrary* open(const char* libraryName);
54
55     // A variant of open() that can report a human-readable error if loading
56     // the library fails. |error| is a caller-provided buffer of |errorSize|
57     // bytes that will be filled with snprintf() and always zero terminated.
58     //
59     // On success, return a new SharedLibrary instance, and do not touch
60     // the content of |error|. On failure, return NULL, and sets the content
61     // of |error|.
62     static SharedLibrary* open(const char* libraryName,
63                                char* error,
64                                size_t errorSize);
65
66     // Closes an existing SharedLibrary instance.
67     ~SharedLibrary();
68
69     // Generic function pointer type, for values returned by the
70     // findSymbol() method.
71     typedef void (*FunctionPtr)(void);
72
73     // Probe a given SharedLibrary instance to find a symbol named
74     // |symbolName| in it. Return its address as a FunctionPtr, or
75     // NULL if the symbol is not found.
76     FunctionPtr findSymbol(const char* symbolName) const;
77
78 private:
79 #ifdef _WIN32
80     typedef HMODULE HandleType;
81 #else
82     typedef void* HandleType;
83 #endif
84
85     // Constructor intentionally hidden.
86     SharedLibrary(HandleType);
87
88     HandleType mLib;
89 };
90
91 // Macro to compose emugl shared library name under various OS and bitness
92 // eg.
93 //     on x86_64, EMUGL_LIBNAME("foo") --> "lib64foo"
94
95
96 #  define EMUGL_LIBNAME(name) "lib" name
97
98 }  // namespace emugl
99
100 #endif  // EMUGL_COMMON_SHARED_LIBRARY_H