sandbox
Loading...
Searching...
No Matches
loader_factory.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_IO_LOADER_FACTORY_HPP_
3#define LIBSBX_IO_LOADER_FACTORY_HPP_
4
5#include <filesystem>
6#include <functional>
7#include <string>
8#include <unordered_map>
9
10namespace sbx::io {
11
12template<typename Type, typename Result>
14
15 using load_function_type = std::function<Result(const std::filesystem::path&)>;
16 using unload_function_type = std::function<void(Result&)>;
17
18 struct function_handle_type {
19 load_function_type load_function;
20 unload_function_type unload_function;
21 }; // struct function_handle_type
22
23 using loader_container_type = std::unordered_map<std::string, function_handle_type>;
24
25public:
26
27 template<typename Derived>
28 class loader {
29
30 public:
31
32 using result_type = Result;
33
34 protected:
35
36 template<typename... Extensions>
37 static auto register_extensions(Extensions&&... extensions) -> bool {
38 ((loader_factory<Type, Result>::_loaders()[extensions] = function_handle_type{
39 .load_function = &Derived::load,
40 .unload_function = &Derived::unload_wrapper
41 }), ...);
42
43 return true;
44 }
45
46 static auto unload_wrapper(Result& result) -> void {
47 Derived::unload(result);
48 }
49
50 static auto unload(Result& result) -> void {
51 static_cast<void>(result);
52 }
53
54 }; // struct loader
55
56protected:
57
58 static auto _loaders() -> loader_container_type& {
59 static auto loaders = loader_container_type{};
60 return loaders;
61 }
62
63}; // class loader_factory
64
65} // namespace sbx::io
66
67#endif // LIBSBX_IO_LOADER_FACTORY_HPP_
Definition: loader_factory.hpp:28
Definition: loader_factory.hpp:13