Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ResourceLoader.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_RESOURCELOADER_HPP
8 #define NAZARA_RESOURCELOADER_HPP
9 
10 #include <Nazara/Core/Enums.hpp>
11 #include <Nazara/Core/Resource.hpp>
12 #include <Nazara/Core/ResourceParameters.hpp>
13 #include <Nazara/Core/String.hpp>
14 #include <list>
15 #include <tuple>
16 #include <type_traits>
17 
18 namespace Nz
19 {
20  class Stream;
21 
22  template<typename Type, typename Parameters>
24  {
25  static_assert(std::is_base_of<ResourceParameters, Parameters>::value, "ResourceParameters must be a base of Parameters");
26 
27  friend Type;
28 
29  public:
30  using ExtensionGetter = bool (*)(const String& extension);
31  using FileLoader = bool (*)(Type* resource, const String& filePath, const Parameters& parameters);
32  using MemoryLoader = bool (*)(Type* resource, const void* data, std::size_t size, const Parameters& parameters);
33  using StreamChecker = Ternary (*)(Stream& stream, const Parameters& parameters);
34  using StreamLoader = bool (*)(Type* resource, Stream& stream, const Parameters& parameters);
35 
36  ResourceLoader() = delete;
37  ~ResourceLoader() = delete;
38 
39  static bool IsExtensionSupported(const String& extension);
40 
41  static bool LoadFromFile(Type* resource, const String& filePath, const Parameters& parameters = Parameters());
42  static bool LoadFromMemory(Type* resource, const void* data, std::size_t size, const Parameters& parameters = Parameters());
43  static bool LoadFromStream(Type* resource, Stream& stream, const Parameters& parameters = Parameters());
44 
45  static void RegisterLoader(ExtensionGetter extensionGetter, StreamChecker checkFunc, StreamLoader streamLoader, FileLoader fileLoader = nullptr, MemoryLoader memoryLoader = nullptr);
46  static void UnregisterLoader(ExtensionGetter extensionGetter, StreamChecker checkFunc, StreamLoader streamLoader, FileLoader fileLoader = nullptr, MemoryLoader memoryLoader = nullptr);
47 
48  private:
49  using Loader = std::tuple<ExtensionGetter, StreamChecker, StreamLoader, FileLoader, MemoryLoader>;
50  using LoaderList = std::list<Loader>;
51  };
52 }
53 
54 #include <Nazara/Core/ResourceLoader.inl>
55 
56 #endif // NAZARA_RESOURCELOADER_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
static bool LoadFromMemory(Type *resource, const void *data, std::size_t size, const Parameters &parameters=Parameters())
Loads a resource from a raw memory, a size and parameters.
Definition: ResourceLoader.inl:163
static void UnregisterLoader(ExtensionGetter extensionGetter, StreamChecker checkFunc, StreamLoader streamLoader, FileLoader fileLoader=nullptr, MemoryLoader memoryLoader=nullptr)
Unregisters the loader.
Definition: ResourceLoader.inl:313
Core class that represents a string.
Definition: String.hpp:22
static bool LoadFromFile(Type *resource, const String &filePath, const Parameters &parameters=Parameters())
Loads a resource from a file.
Definition: ResourceLoader.inl:56
Core class that represents a loader of resources.
Definition: ResourceLoader.hpp:23
static void RegisterLoader(ExtensionGetter extensionGetter, StreamChecker checkFunc, StreamLoader streamLoader, FileLoader fileLoader=nullptr, MemoryLoader memoryLoader=nullptr)
Registers the loader.
Definition: ResourceLoader.inl:295
Core class that represents a stream.
Definition: Stream.hpp:19
static bool IsExtensionSupported(const String &extension)
Checks whether the extension of the file is supported.
Definition: ResourceLoader.inl:27
static bool LoadFromStream(Type *resource, Stream &stream, const Parameters &parameters=Parameters())
Loads a resource from a stream and parameters.
Definition: ResourceLoader.inl:244