1 // Copyright (C) 2014 The Android Open Source Project
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef EMUGL_COMMON_SHARED_LIBRARY_H
16 #define EMUGL_COMMON_SHARED_LIBRARY_H
26 // A class used to open a platform-specific shared library, and probe
27 // it for symbols. Usage is the following:
29 // // Open the library.
30 // SharedLibrary* library = SharedLibrary::open("libFoo");
32 // ... could not find / open library!
35 // //Probe for function symbol.
36 // FunctionPtr my_func = library->findSymbol("my_func");
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.
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
51 // On success, returns a new SharedLibrary instance that must be
52 // deleted by the caller.
53 static SharedLibrary* open(const char* libraryName);
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.
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
62 static SharedLibrary* open(const char* libraryName,
66 // Closes an existing SharedLibrary instance.
69 // Generic function pointer type, for values returned by the
70 // findSymbol() method.
71 typedef void (*FunctionPtr)(void);
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;
80 typedef HMODULE HandleType;
82 typedef void* HandleType;
85 // Constructor intentionally hidden.
86 SharedLibrary(HandleType);
91 // Macro to compose emugl shared library name under various OS and bitness
93 // on x86_64, EMUGL_LIBNAME("foo") --> "lib64foo"
96 # define EMUGL_LIBNAME(name) "lib" name
100 #endif // EMUGL_COMMON_SHARED_LIBRARY_H