C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
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.
 
 
 
 
 

90 lines
2.1 KiB

  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. #define CPPDB_SOURCE
  19. #include <cppdb/shared_object.h>
  20. #include <string>
  21. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
  22. # include <windows.h>
  23. # define RTLD_LAZY 0
  24. namespace cppdb {
  25. namespace {
  26. void *dlopen(char const *name,int /*unused*/)
  27. {
  28. return LoadLibrary(name);
  29. }
  30. void dlclose(void *h)
  31. {
  32. HMODULE m=(HMODULE)(h);
  33. FreeLibrary(m);
  34. }
  35. void *dlsym(void *h,char const *sym)
  36. {
  37. HMODULE m=(HMODULE)(h);
  38. return (void *)GetProcAddress(m,sym);
  39. }
  40. }
  41. }
  42. #else
  43. # include <dlfcn.h>
  44. #endif
  45. namespace cppdb {
  46. shared_object::shared_object(std::string name,void *h) :
  47. dlname_(name),
  48. handle_(h)
  49. {
  50. }
  51. shared_object::~shared_object()
  52. {
  53. dlclose(handle_);
  54. }
  55. ref_ptr<shared_object> shared_object::open(std::string const &name)
  56. {
  57. ref_ptr<shared_object> dl;
  58. void *h=dlopen(name.c_str(),RTLD_LAZY);
  59. if(!h) {
  60. return dl;
  61. }
  62. try {
  63. dl.reset(new shared_object(name,h));
  64. h=0;
  65. return dl;
  66. }
  67. catch(...) {
  68. if(h) {
  69. dlclose(h);
  70. }
  71. throw;
  72. }
  73. }
  74. void *shared_object::sym(std::string const &s)
  75. {
  76. return dlsym(handle_,s.c_str());
  77. }
  78. void *shared_object::safe_sym(std::string const &s)
  79. {
  80. void *p=sym(s);
  81. if(!p) {
  82. throw cppdb_error("cppdb::shared_object::failed to resolve symbol [" + s +"] in " + dlname_);
  83. }
  84. return p;
  85. }
  86. } // cppdb