1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
---|---|
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "Utility/BsDynLibManager.h" |
4 | #include "Utility/BsDynLib.h" |
5 | |
6 | namespace bs |
7 | { |
8 | static bool operator<(const UPtr<DynLib>& lhs, const String& rhs) |
9 | { |
10 | return lhs->getName() < rhs; |
11 | } |
12 | |
13 | static bool operator<(const String& lhs, const UPtr<DynLib>& rhs) |
14 | { |
15 | return lhs < rhs->getName(); |
16 | } |
17 | |
18 | static bool operator<(const UPtr<DynLib>& lhs, const UPtr<DynLib>& rhs) |
19 | { |
20 | return lhs->getName() < rhs->getName(); |
21 | } |
22 | |
23 | DynLib* DynLibManager::load(String filename) |
24 | { |
25 | // Add the extension (.dll, .so, ...) if necessary. |
26 | |
27 | // Note: The string comparison here could be slightly more efficent by using a templatized string_concat function |
28 | // for the lower_bound call and/or a custom comparitor that does comparison by parts. |
29 | const String::size_type length = filename.length(); |
30 | const String extension = String(".") + DynLib::EXTENSION; |
31 | const String::size_type extLength = extension.length(); |
32 | if(length <= extLength || filename.substr(length - extLength) != extension) |
33 | filename.append(extension); |
34 | |
35 | if(DynLib::PREFIX != nullptr) |
36 | filename.insert(0, DynLib::PREFIX); |
37 | |
38 | const auto& iterFind = mLoadedLibraries.lower_bound(filename); |
39 | if(iterFind != mLoadedLibraries.end() && (*iterFind)->getName() == filename) |
40 | { |
41 | return iterFind->get(); |
42 | } |
43 | else |
44 | { |
45 | DynLib* newLib = bs_new<DynLib>(std::move(filename)); |
46 | mLoadedLibraries.emplace_hint(iterFind, newLib); |
47 | |
48 | return newLib; |
49 | } |
50 | } |
51 | |
52 | void DynLibManager::unload(DynLib* lib) |
53 | { |
54 | const auto& iterFind = mLoadedLibraries.find(lib->getName()); |
55 | if(iterFind != mLoadedLibraries.end()) |
56 | mLoadedLibraries.erase(iterFind); |
57 | else |
58 | bs_delete(lib); |
59 | } |
60 | |
61 | DynLibManager& gDynLibManager() |
62 | { |
63 | return DynLibManager::instance(); |
64 | } |
65 | } |
66 |