shared_object.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // Distributed under:
  6. //
  7. // the Boost Software License, Version 1.0.
  8. // (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // or (at your opinion) under:
  12. //
  13. // The MIT License
  14. // (See accompanying file MIT.txt or a copy at
  15. // http://www.opensource.org/licenses/mit-license.php)
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef CPPDB_SHARED_OBJECT_H
  19. #define CPPDB_SHARED_OBJECT_H
  20. #include <cppdb/defs.h>
  21. #include <cppdb/ref_ptr.h>
  22. namespace cppdb {
  23. ///
  24. /// \brief This class allows to load and unload shared objects in simple and exception safe way.
  25. ///
  26. class CPPDB_API shared_object : public ref_counted {
  27. shared_object() : handle_(0) {}
  28. shared_object(std::string name,void *h);
  29. shared_object(shared_object const &);
  30. void operator=(shared_object const &);
  31. public:
  32. ~shared_object();
  33. ///
  34. /// Load shared object, returns empty pointer if the object does not exits or not loadable
  35. ///
  36. static ref_ptr<shared_object> open(std::string const &name);
  37. ///
  38. /// Resolve symbol \a name and return pointer on it, throws cppdb_error if the symbol can't be resolved
  39. ///
  40. void *safe_sym(std::string const &name);
  41. ///
  42. /// Resolve symbol \a name and return pointer on it, returns NULL if the symbol can't be resolved
  43. ///
  44. void *sym(std::string const &name);
  45. ///
  46. /// Resolve symbol \a name and assign it to \a v, returns false if the symbol can't be resolved
  47. ///
  48. template<typename T>
  49. bool resolve(std::string const &s,T *&v)
  50. {
  51. void *p=sym(s);
  52. if(!p) {
  53. return false;
  54. }
  55. v=(T*)(p);
  56. return true;
  57. }
  58. ///
  59. /// Resolve symbol \a name and assign it to v, throws cppdb_error if the symbol can't be resolved
  60. ///
  61. template<typename T>
  62. void safe_resolve(std::string const &s,T *&v)
  63. {
  64. v=(T*)(sym(s));
  65. }
  66. private:
  67. std::string dlname_;
  68. void *handle_;
  69. };
  70. }
  71. #endif