GL Studio API
dynamic_library.h
Go to the documentation of this file.
1 /*! \file
2  \brief A cross-platform class for loading dynamic link libraries and shared objects.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2015 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _DYNAMIC_LIBRARY_H
41 #define _DYNAMIC_LIBRARY_H
42 
43 
44 #include "disti_include.h"
45 #include <string>
46 
47 #if defined(_WIN32)
48 # include <windows.h>
49 #endif
50 
51 namespace disti
52 {
53 
54 typedef void * (*FunctionPointer)(...);
55 
56 /** The DynamicLibrary class. A cross-platform class for loading dynamic link libraries
57 * and shared objects
58 */
60 {
61 public:
62  /** Error Enum */
63  typedef enum
64  {
65  ERROR_NONE,
66  ERROR_VERSION_MISMATCH, ///< Load failed due to incompatible GL Studio or compiler versions
67  ERROR_UNKNOWN ///< Load failed for unknown reason (see DynamicLibrary::ErrorString())
68  } ErrorEnum;
69 
70 #if defined(_WIN32)
71  typedef HMODULE LibHandle_t; /** Windows uses a handle to the Open Dynamic Library */
72 #else
73  typedef void* LibHandle_t; /** UNIX uses a pointer to the Open Dynamic Library */
74 #endif
75 
76  /**
77  * Constructor for dynamic library object. Causes the dynamic library
78  * which is passed in by filename to be opened.
79  * \param lib_name_arg A pointer to a string which is a library name to be opened, may include the path, can not be NULL
80  * \param quiet Whether this class emits error messages or fails silently.
81  * \param searchLibPath whether or not the library path is searched to find the library if it cannot be found
82  * first in its specified directory.
83  * \param tryStandardExtensions If this is true, the file extension, if any, will be stripped off and the appropriate
84  * standard extension for the current OS will be appended. If the changed filename cannot be
85  * found, then the original filename will be used as a last resort.
86  * \param matchVersion
87  */
88  DISTI_EXPORT DynamicLibrary(
89  const char* lib_name_arg,
90  bool quiet = false,
91  bool searchLibPath = false,
92  bool tryStandardExtensions = false,
93  bool matchVersion = false);
94 
95  /** Destructor for dynamic library object. Closes the library so the OS can cleanup. */
96  virtual DISTI_EXPORT ~DynamicLibrary();
97 
98  /**
99  * Dynamically resolves a function from the library. The name of the
100  * function is passed in.
101  * \param function_name A pointer to a string, which represents the Name of a Function to find.
102  * \return Returns a pointer to the function if it is found or NULL otherwise
103  */
104  virtual DISTI_EXPORT FunctionPointer DynamicFunction(const char *function_name);
105 
106  /** \return True if the library can be found somewhere on the system, false otherwise */
107  static DISTI_EXPORT bool Exists(const char* libName, bool tryStandardExtensions = true);
108 
109  /** \return The path to the given file name */
110  static DISTI_EXPORT std::string Find(const std::string& name);
111 
112  /** \return True if the library successfully loaded, false otherwise */
113  virtual DISTI_EXPORT bool Loaded(void) const;
114 
115  /** Call LastError() when Loaded() is false to determine to determine
116  * if load failure occured due to a version mismatch.
117  */
118  DISTI_EXPORT ErrorEnum LastError() const;
119 
120  /** Returns the last error string or NULL if no error has occured.
121  * When tryStandardExtensions == true, this may be a very long string
122  * since it contains the error information for each attempted filename.
123  */
124  DISTI_EXPORT const char* ErrorString() const;
125 
126  /**
127  * Removes the extension, if any, from the specified path
128  */
129  static DISTI_EXPORT void RemoveExtension(std::string& libpath);
130 
131 private:
132 
133  bool _quiet;
134  bool _searchLibPath;
135  bool _tryStandardExtensions;
136  LibHandle_t _dlHandle;
137  ErrorEnum _lastError;
138 
139  char* _errorString;
140 
141  DISTI_EXPORT void SetErrorString(const char*);
142 
143  /**
144  * Attempts to find the specified library file on the system.
145  * If the file doesn't exist as named, any path will be stripped off and
146  * this will try to open the library file, which uses the standard OS search
147  * algorithm to find the file. If the open is successful, the file was
148  * found.
149  *
150  * \returns true if the library is found.
151  */
152  static bool FoundOnSystem(const std::string& libname);
153 
154 
155  /**
156  * Opens the specified library searching the library paths if specified.
157  */
158  static LibHandle_t Open(const std::string& libpath, ErrorEnum& errorCode /*returned*/, bool searchLibPath = true,bool matchVersion = false,bool quiet=false);
159 
160 };
161 
162 } // namespace disti
163 
164 #endif
165 
166 
167 
virtual FunctionPointer DynamicFunction(const char *function_name)
Load failed for unknown reason (see DynamicLibrary::ErrorString())
Definition: dynamic_library.h:67
static bool Exists(const char *libName, bool tryStandardExtensions=true)
const char * ErrorString() const
ErrorEnum LastError() const
static std::string Find(const std::string &name)
A file for all GL Studio files to include.
virtual bool Loaded(void) const
Load failed due to incompatible GL Studio or compiler versions.
Definition: dynamic_library.h:66
Definition: dynamic_library.h:59
DynamicLibrary(const char *lib_name_arg, bool quiet=false, bool searchLibPath=false, bool tryStandardExtensions=false, bool matchVersion=false)
ErrorEnum
Definition: dynamic_library.h:63
Definition: bmpimage.h:46
static void RemoveExtension(std::string &libpath)