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