/xbmc/screensavers/rsxs-0.9/src/dlopen.hh

http://github.com/xbmc/xbmc · C++ Header · 73 lines · 44 code · 7 blank · 22 comment · 4 complexity · 3f666b6bf17ee85f5d5a7c0d609b9c3a MD5 · raw file

  1. /*
  2. * Really Slick XScreenSavers
  3. * Copyright (C) 2002-2006 Michael Chapman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *****************************************************************************
  19. *
  20. * This is a Linux port of the Really Slick Screensavers,
  21. * Copyright (C) 2002 Terence M. Welsh, available from www.reallyslick.com
  22. */
  23. #ifndef _DLOPEN_HH
  24. #define _DLOPEN_HH
  25. #include <common.hh>
  26. #if USE_DLOPEN
  27. #include <ltdl.h>
  28. class Loader : public ResourceManager::Resource<void> {
  29. public:
  30. typedef std::string Exception;
  31. public:
  32. Loader() {
  33. if (lt_dlinit())
  34. throw Exception(lt_dlerror());
  35. }
  36. ~Loader() { lt_dlexit(); }
  37. void operator()() const {}
  38. };
  39. class Library;
  40. class Library : public ResourceManager::Resource<const Library*> {
  41. public:
  42. typedef std::string Exception;
  43. private:
  44. lt_dlhandle _handle;
  45. public:
  46. Library(const std::string& library) {
  47. static bool inited = false;
  48. if (!inited) {
  49. Common::resources->manage(new Loader);
  50. inited = true;
  51. }
  52. _handle = lt_dlopenext(library.c_str());
  53. if (!_handle)
  54. throw Exception(lt_dlerror());
  55. }
  56. ~Library() { lt_dlclose(_handle); }
  57. const Library* operator()() const { return this; }
  58. lt_ptr operator()(const std::string& function) const {
  59. lt_ptr ptr = lt_dlsym(_handle, function.c_str());
  60. if (!ptr)
  61. throw Exception(lt_dlerror());
  62. return ptr;
  63. }
  64. };
  65. #endif // USE_DLOPEN
  66. #endif // _DLOPEN_HH