casacore
DynLib.h
Go to the documentation of this file.
1 //# DynLib.h: Class to handle loadig of dynamic libraries
2 //# Copyright (C) 2009
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_DYNLIB_H
29 #define CASA_DYNLIB_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <string>
34 
35 namespace casacore { //# NAMESPACE CASACORE - BEGIN
36 
37  // <summary>
38  // Class to handle loading of dynamic libraries
39  // </summary>
40  // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
41  // </reviewed>
42 
43  // <use visibility=export>
44 
45  // <prerequisite>
46  // <li> Basic knowledge of the dlopen function family
47  // </prerequisite>
48 
49  // <synopsis>
50  // This class makes it possible to load a dynamic library and execute an
51  // initialization function. Furthermore, one can get a pointer to any function
52  // in the dynamic library and close the library.
53  //
54  // The search path of the shared library is as follows:
55  // <ul>
56  // <li> If the environment library CASACORE_LDPATH is defined, it is tried to
57  // find the library using that path.
58  // <li> If not defined or not found, the system's (DY)LD_LIBRARY_PATH is used.
59  // <li> The library looked for has the name 'prefix'libname'suffix'.
60  // <br>As prefix first "lib" is used, thereafter the given one
61  // (e.g., "libcasa_").
62  // <br>As suffix first ".so" is used, thereafter ".dylib" (for OS-X).
63  // </ul>
64  //
65  // It is a wrapper around functions dlopen, dlsym, and dlclose.
66  // If dlopen and so are not supported on a platform, the class acts as if
67  // the shared library could not be found.
68  // </synopsis>
69 
70  // <example>
71  // <srcblock>
72  // DynLib dl("derivedmscal", "libcasa_", "register_derivedmscal");
73  // AlwaysAssert (dl.getHandle());
74  // </srcblock>
75  // Using this
76  // loads the shared library <src>libcasa_derivedmscal.so</src> and
77  // executes the given register initialization function.
78  // </example>
79 
80  // <motivation>
81  // dlopen is a standard UNIX system call, but some operating systems
82  // do not support it or have different function names (notably Windows).
83  // In this way use of dynamic libraries is centralized and can easily b
84  // tailored as needed.
85  // </motivation>
86 
87  class DynLib
88  {
89  public:
90 
91  // Load the dynamic library. It is tried with prefixes "lib" and <src>prefix</src>
92  // and suffixes ".so" and ".dylib".
93  // If not loaded successfully, the internal handle is NULL.
94  // <br>If a non-empty funcName is given, that function is looked up and
95  // executed. Its signature must be <src>void func()</src>. Note that the
96  // function name should not be mangled, thus declared <src>extern "C"</src>.
97  // An exception is thrown if the library is loaded successfully, but
98  // <src>funcName</src> could not be found.
99  // <br>If <src>closeOnDestruction=True</src>, the dynamic library is closed
100  // on destruction of the DynLib object.
101  DynLib (const std::string& library,
102  const std::string& prefix=std::string(),
103  const std::string& funcName=std::string(),
104  bool closeOnDestruction=True);
105 
106  // Load the dynamic library with the given name, prefix, and suffix.
107  // If not loaded successfully, the internal handle is NULL.
108  // <br>If <src>closeOnDestruction=True</src>, the dynamic library is closed
109  // when the DynLib object is destructed.
110  DynLib (const std::string& library,
111  Bool closeOnDestruction,
112  const std::string& prefix="lib",
113 #ifdef __APPLE__
114  const std::string& suffix=".dylib");
115 #else
116  const std::string& suffix=".so");
117 #endif
118 
119  // Close the dynamic library if told so in the constructor.
120  ~DynLib();
121 
122  // Get a pointer to a function in the dynamic library.
123  // The pointer has to be casted with a reinterpret_cast to a function
124  // pointer with the correct signature. When compiling with -pedantic the
125  // compiler will give a warning for such a cast, because on some systems
126  // (in particular some micro-controllers) a data pointer differs from a
127  // function pointer. However, that problem cannot be solved.
128  // For example:
129  // <srcblock>
130  // typedef Int MyFunc(Int, Int);
131  // void* initfunc = DynLib::getFunc (mod, ("register_"+name).c_str());
132  // if (initFunc) {
133  // MyFunc* func = reinterpret_cast<MyFunc*>(initfunc);
134  // Int result = func(1,2);
135  // }
136  // </srcblock>
137  // casts to a function returning Int and taking two Ints.
138  // <br>A null pointer is returned if the function could not be found.
139  void* getFunc (const std::string& funcName);
140 
141  // Get the dynamic library handle.
142  void* getHandle() const
143  { return itsHandle; }
144 
145  // Get the possible error.
146  const std::string& getError() const
147  { return itsError; }
148 
149  private:
150  // Open (load)the dynamic library.
151  void open (const std::string& name);
152 
153  // Close (unload) the dynamic library (if opened).
154  void close();
155 
156  // Try if the library can be opened using CASACORE_LDPATH.
157  std::string tryCasacorePath (const std::string& library,
158  const std::string& prefix);
159 
160  //# Handle to dynamic library; note that the pointer is not owned, so the
161  //# generated copy ctor and assignment are fine.
162  void* itsHandle;
164  std::string itsError;
165  };
166 
167 } //# NAMESPACE CASACORE - END
168 
169 #endif
void * itsHandle
Definition: DynLib.h:162
void open(const std::string &name)
Open (load)the dynamic library.
DynLib(const std::string &library, const std::string &prefix=std::string(), const std::string &funcName=std::string(), bool closeOnDestruction=True)
Load the dynamic library.
const std::string & getError() const
Get the possible error.
Definition: DynLib.h:146
Class to handle loading of dynamic libraries.
Definition: DynLib.h:87
std::string tryCasacorePath(const std::string &library, const std::string &prefix)
Try if the library can be opened using CASACORE_LDPATH.
void * getFunc(const std::string &funcName)
Get a pointer to a function in the dynamic library.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
void close()
Close (unload) the dynamic library (if opened).
std::string itsError
Definition: DynLib.h:164
void * getHandle() const
Get the dynamic library handle.
Definition: DynLib.h:142
Bool itsDoClose
Definition: DynLib.h:163
const Bool True
Definition: aipstype.h:40
~DynLib()
Close the dynamic library if told so in the constructor.
this file contains all the compiler specific defines
Definition: mainpage.dox:28