Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SharedLibrary.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright and trademark 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
40 // Plugin architecture example //
41 // //
42 // This code serves as an example to the plugin architecture discussed in //
43 // the article and can be freely used //
45 
49 #ifndef __PLUGIN_SHARED_LIBRARY_H__
50 #define __PLUGIN_SHARED_LIBRARY_H__
51 
52 #include "CoreConfig.h"
53 #include "Logger.h"
54 
55 #include <string>
56 #include <stdexcept>
57 #include <iostream>
58 
59 #if defined( PLUGIN_WIN32 )
60 
61 #define WIN32_LEAN_AND_MEAN
62 #define VC_EXTRALEAN
63 #include <windows.h>
64 
65 #elif defined( PLUGIN_LINUX )
66 
67 #include <dlfcn.h>
68 
69 #else
70 
71 #error Please implement the shared library functions for your system
72 
73 #endif
74 
75 namespace Menge {
76 
77  #if defined( PLUGIN_WIN32 )
78 
82  class SharedLibrary {
83  public:
87  typedef HMODULE HandleType;
88 
96  MENGE_API static HandleType Load( const std::string &path ) {
97  HMODULE moduleHandle = ::LoadLibraryA( path.c_str() );
98  if( moduleHandle == NULL ) {
99  throw std::runtime_error("Could not load DLL");
100  }
101 
102  return moduleHandle;
103  }
104 
111  MENGE_API static void Unload( HandleType sharedLibraryHandle ) {
112  BOOL result = ::FreeLibrary( sharedLibraryHandle );
113  if ( result == FALSE ) {
114  throw std::runtime_error ("Could not unload DLL" );
115  }
116  }
117 
127  template< typename TSignature >
128  static TSignature * GetFunctionPointer( HandleType handle, const std::string & funcName ) {
129  FARPROC functionAddress = ::GetProcAddress( handle, funcName.c_str() );
130  if(functionAddress == NULL) {
131  throw std::runtime_error("Could not find exported function");
132  }
133 
134  return reinterpret_cast<TSignature *>(functionAddress);
135  }
136 
137  };
138 
139  #endif
140 
141  // ----------------------------------------------------------------------- //
142 
143  #if defined( PLUGIN_LINUX )
144 
148  class SharedLibrary {
149  public:
153  typedef void * HandleType;
154 
162  MENGE_API static HandleType Load( const std::string &path ) {
163  void * sharedObject = ::dlopen(path.c_str(), RTLD_NOW);
164  if( sharedObject == NULL) {
165  logger << Logger::ERR_MSG << dlerror() << "\n";
166  throw std::runtime_error( std::string("Could not load '") + path + "'" );
167  }
168 
169  return sharedObject;
170  }
171 
178  MENGE_API static void Unload(HandleType sharedLibraryHandle) {
179  int result = ::dlclose( sharedLibraryHandle );
180  if(result != 0) {
181  throw std::runtime_error( "Could not unload shared object" );
182  }
183  }
184 
194  template< typename TSignature >
195  static TSignature *GetFunctionPointer( HandleType handle, const std::string &funcName ) {
196  ::dlerror(); // clear error value
197 
198  void *functionAddress = ::dlsym( handle, funcName.c_str() );
199 
200  const char *error = ::dlerror(); // check for error
201  if ( error != NULL ) {
202  throw std::runtime_error( "Could not find exported function" );
203  }
204 
205  return reinterpret_cast<TSignature *>(functionAddress);
206  }
207  };
208 
209  #endif
210 } // namespace Menge
211 
212 #endif // __PLUGIN_SHARED_LIBRARY_H__
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
Sets up the proper compiler directives for platform and dll export/import.
The specificaiton of a message logger for menge, such that all messages to the system get properly re...
Logger logger
Globally available Logger.
Definition: Logger.cpp:49
Error encountered but not handled.
Definition: Logger.h:69