Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
ResourceSaver.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_RESOURCESAVER_HPP
8 #define NAZARA_RESOURCESAVER_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 FormatQuerier = bool (*)(const String& format);
32  using FileSaver = bool (*)(const Type& resource, const String& filePath, const Parameters& parameters);
33  using StreamSaver = bool (*)(const Type& resource, const String& format, Stream& stream, const Parameters& parameters);
34 
35  ResourceSaver() = delete;
36  ~ResourceSaver() = delete;
37 
38  static bool IsFormatSupported(const String& extension);
39 
40  static bool SaveToFile(const Type& resource, const String& filePath, const Parameters& parameters = Parameters());
41  static bool SaveToStream(const Type& resource, Stream& stream, const String& format, const Parameters& parameters = Parameters());
42 
43  static void RegisterSaver(FormatQuerier formatQuerier, StreamSaver streamSaver, FileSaver fileSaver = nullptr);
44  static void UnregisterSaver(FormatQuerier formatQuerier, StreamSaver streamSaver, FileSaver fileSaver = nullptr);
45 
46  private:
47  using Saver = std::tuple<FormatQuerier, StreamSaver, FileSaver>;
48  using SaverList = std::list<Saver>;
49  };
50 }
51 
52 #include <Nazara/Core/ResourceSaver.inl>
53 
54 #endif // NAZARA_RESOURCESAVER_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Core class that represents a string.
Definition: String.hpp:22
static bool SaveToFile(const Type &resource, const String &filePath, const Parameters &parameters=Parameters())
Saves a resource to a file.
Definition: ResourceSaver.inl:54
Core class that represents a list of saver functions for a specific resource type.
Definition: ResourceSaver.hpp:23
static void UnregisterSaver(FormatQuerier formatQuerier, StreamSaver streamSaver, FileSaver fileSaver=nullptr)
Unregisters a saver.
Definition: ResourceSaver.inl:184
static void RegisterSaver(FormatQuerier formatQuerier, StreamSaver streamSaver, FileSaver fileSaver=nullptr)
Registers a saver.
Definition: ResourceSaver.inl:166
static bool IsFormatSupported(const String &extension)
Checks whether the extension of the file is supported.
Definition: ResourceSaver.inl:27
static bool SaveToStream(const Type &resource, Stream &stream, const String &format, const Parameters &parameters=Parameters())
Saves a resource to a stream.
Definition: ResourceSaver.inl:120
Core class that represents a stream.
Definition: Stream.hpp:19