portability/dynaload.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef STLPLUS_DYNALOAD
#define STLPLUS_DYNALOAD
////////////////////////////////////////////////////////////////////////////////
 
//   Author:    Andy Rushton
//   Copyright: (c) Southampton University 1999-2004
//              (c) Andy Rushton           2004 onwards
//   License:   BSD License, see ../docs/license.html
 
//   A portable interface to the dynamic loader - i.e. the system for loading
//   dynamic libraries or shared libraries during the running of a program,
//   rather than by linking
 
////////////////////////////////////////////////////////////////////////////////
#include "portability_fixes.hpp"
#include <string>
 
namespace stlplus
{
 
  //////////////////////////////////////////////////////////////////////////////
  // dynaload class manages a dynamic loadable library and unloads it on destruction
 
  class dynaload
  {
  public:
 
    ////////////////////////////////////////////////////////////////////////////
    // library management
 
    // construct the object but do not load
    dynaload(void);
 
    // construct and load
    dynaload(const std::string& library, const std::string& path = std::string());
 
    // unload if loaded and then destroy
    ~dynaload(void);
 
    // load the library - return success or fail
    bool load(const std::string& library, const std::string& path = std::string());
 
    // unload the library if loaded
    bool unload(void);
 
    // test whether the library is loaded
    bool loaded(void) const;
 
    ////////////////////////////////////////////////////////////////////////////
    // symbol management
 
    // test whether a function is exported by the library
    bool present(const std::string& name);
 
    // get the function as a generic pointer
    void* symbol(const std::string& name);
 
    ////////////////////////////////////////////////////////////////////////////
    // error management
 
    // enum values to indicate type of error
    enum error_t {no_error, load_error, unload_error, symbol_error};
 
    // test whether there has been an error
    bool error(void) const;
 
    // clear an error once it has been handled (or ignored)
    void clear_error(void);
 
    // get the type of the error as indicated by the enum error_t
    error_t error_type(void) const;
 
    // get the text of the error as provided by the OS
    std::string error_text(void) const;
 
    ////////////////////////////////////////////////////////////////////////////
 
  private:
    void* m_handle;
    error_t m_error;
    std::string m_text;
  };
 
}
 
#endif