BsDynLibManager.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsDynLibManager.h"
  4. #include "BsDynLib.h"
  5. namespace BansheeEngine
  6. {
  7. DynLibManager::DynLibManager()
  8. {
  9. }
  10. DynLib* DynLibManager::load(const String& name)
  11. {
  12. String filename = name;
  13. #if BS_PLATFORM == BS_PLATFORM_LINUX
  14. if (name.substr(name.length() - 3, 3) != ".so")
  15. name += ".so";
  16. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  17. if (name.substr(name.length() - 6, 6) != ".dylib")
  18. name += ".dylib";
  19. #elif BS_PLATFORM == BS_PLATFORM_WIN32
  20. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  21. // if you include a relative path then it does not. So, add it to be sure.
  22. if (filename.substr(filename.length() - 4, 4) != ".dll")
  23. filename += ".dll";
  24. #endif
  25. auto iterFind = mLoadedLibraries.find(filename);
  26. if (iterFind != mLoadedLibraries.end())
  27. {
  28. return iterFind->second;
  29. }
  30. else
  31. {
  32. DynLib* newLib = new (bs_alloc<DynLib>()) DynLib(filename);
  33. mLoadedLibraries[filename] = newLib;
  34. return newLib;
  35. }
  36. }
  37. void DynLibManager::unload(DynLib* lib)
  38. {
  39. auto iterFind = mLoadedLibraries.find(lib->getName());
  40. if (iterFind != mLoadedLibraries.end())
  41. {
  42. mLoadedLibraries.erase(iterFind);
  43. }
  44. lib->unload();
  45. bs_delete(lib);
  46. }
  47. DynLibManager::~DynLibManager()
  48. {
  49. // Unload & delete resources in turn
  50. for(auto& entry : mLoadedLibraries)
  51. {
  52. entry.second->unload();
  53. bs_delete(entry.second);
  54. }
  55. // Empty the list
  56. mLoadedLibraries.clear();
  57. }
  58. DynLibManager& gDynLibManager()
  59. {
  60. return DynLibManager::instance();
  61. }
  62. }