ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
4.9 KiB

  1. //
  2. // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #ifndef BOOSTER_SHARED_OBJECT_H
  9. #define BOOSTER_SHARED_OBJECT_H
  10. #include <booster/config.h>
  11. #include <booster/noncopyable.h>
  12. #include <booster/hold_ptr.h>
  13. #include <booster/backtrace.h>
  14. #include <booster/cstdint.h>
  15. #include <string>
  16. namespace booster {
  17. ///
  18. /// \brief Class that allows loading dynamic libraries: shared objects and dlls.
  19. ///
  20. class BOOSTER_API shared_object : public booster::noncopyable {
  21. public:
  22. static const int load_lazy = 1; /// Pass RTLD_LAZY to dlopen
  23. static const int load_now = 2; /// Pass RTLD_NOW to dlopen
  24. static const int load_global = 4; /// Pass RTLD_GLOBAL to dlopen
  25. static const int load_local = 8; /// Pass RTLD_LOCAL to dlopen
  26. ///
  27. /// Create an empty shared object
  28. ///
  29. shared_object();
  30. ///
  31. /// If the shared object was open unloads it, If private copy of DLL was made it is removed from
  32. /// the file system
  33. ///
  34. ~shared_object();
  35. ///
  36. /// Create shared object and load it, \see open(std::string const&,bool);
  37. ///
  38. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  39. /// \params flags - load flags: one of load_lazy, load_now, load_local, load_global - ignored on Windows
  40. ///
  41. /// \throws booster::system::system_error if it is impossible to load it
  42. /// \ver{v1_2}
  43. shared_object(std::string const &file_name,int flags);
  44. ///
  45. /// Create shared object and load it, \see open(std::string const&,bool);
  46. ///
  47. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  48. ///
  49. /// \throws booster::system::system_error if it is impossible to load it
  50. ///
  51. shared_object(std::string const &file_name);
  52. ///
  53. /// Check if the shared object was loaded
  54. ///
  55. bool is_open() const;
  56. ///
  57. /// Load shared object or dll
  58. ///
  59. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  60. /// \return true if the shared object was loaded
  61. ///
  62. bool open(std::string const &file_name);
  63. ///
  64. /// Load shared object or dll
  65. ///
  66. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  67. /// \param error_message - the error message
  68. /// \return true if the shared object was loaded
  69. ///
  70. bool open(std::string const &file_name,std::string &error_message);
  71. ///
  72. /// Load shared object or dll
  73. ///
  74. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  75. /// \params flags - load flags: one of load_lazy, load_now, load_local, load_global - ignored on Windows
  76. /// \return true if the shared object was loaded
  77. ///
  78. /// \ver{v1_2}
  79. bool open(std::string const &file_name,int flags);
  80. ///
  81. /// Load shared object or dll
  82. ///
  83. /// \param file_name - the name of the file, UTF-8 encoded under Windows
  84. /// \params flags - load flags: one of load_lazy, load_now, load_local, load_global - ignored on Windows
  85. /// \param error_message - the error message
  86. /// \return true if the shared object was loaded
  87. ///
  88. /// \ver{v1_2}
  89. bool open(std::string const &file_name,std::string &error_message,int flags);
  90. ///
  91. ///
  92. /// Unload the shared object
  93. ///
  94. void close();
  95. ///
  96. /// Resolve symbol in the shared object dll. If it can't be resolved, NULL is returned
  97. ///
  98. /// If the shared object was not opened, it would throw booster::runtime_error
  99. ///
  100. void *resolve_symbol(std::string const &name) const;
  101. ///
  102. /// Resolve symbol in the shared object dll. If it can't be resolved booster::runtime_error
  103. /// is thrown
  104. ///
  105. template<typename T>
  106. void symbol(T &s,std::string const &name) const
  107. {
  108. void *p = resolve_symbol(name);
  109. if(!p) {
  110. throw booster::runtime_error("booster::shared_object:failed to resolve symbol:" + name);
  111. }
  112. s = reinterpret_cast<T>(reinterpret_cast<size_t>(p));
  113. }
  114. ///
  115. /// Format the OS specific name name of the library according to its name. Uses CMake convensions.
  116. ///
  117. /// For example library "foo" is converted to the name
  118. ///
  119. /// - libfoo.so under Linux, FreeBSD, Solaris
  120. /// - libfoo.dylib under Darwin/Mac OS X
  121. /// - libfoo.dll under Windows using gcc/mingw
  122. /// - foo.dll under Windows using MSVC
  123. /// - cygfoo.dll under Cygwin
  124. ///
  125. static std::string name(std::string const &module);
  126. ///
  127. /// Format the OS specific name name of the library according to its name and its soversion.
  128. /// Uses CMake convensions.
  129. ///
  130. /// For example library "foo" and soversion "1" is converted to the name
  131. ///
  132. /// - libfoo.so.1 under Linux, FreeBSD, Solaris
  133. /// - libfoo.1.dylib under Darwin/Mac OS X
  134. /// - libfoo-1.dll under Windows using gcc/mingw
  135. /// - foo-1.dll under Windows using MSVC
  136. /// - cygfoo-1.dll under Cygwin
  137. ///
  138. static std::string name(std::string const &module,std::string const &soversion);
  139. private:
  140. struct data;
  141. hold_ptr<data> d;
  142. };
  143. }
  144. #endif