raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
FileData.hpp
1#ifndef RAYLIB_CPP_INCLUDE_FILEDATA_HPP_
2#define RAYLIB_CPP_INCLUDE_FILEDATA_HPP_
3
4#include <string>
5#include <utility>
6
7#include "./raylib.hpp"
8#include "./raylib-cpp-utils.hpp"
9
10namespace raylib {
11
12class FileData {
13 public:
14 FileData() = default;
15 FileData(const FileData&) = delete;
16 FileData(FileData&& other) noexcept : data(other.data), bytesRead(other.bytesRead) {
17 other.data = nullptr;
18 other.bytesRead = 0;
19 }
20 FileData& operator=(const FileData&) = delete;
21 FileData& operator=(FileData&& other) noexcept {
22 std::swap(data, other.data);
23 std::swap(bytesRead, other.bytesRead);
24 return *this;
25 }
26 ~FileData() { Unload(); }
27
28 explicit FileData(const std::string_view fileName) {
29 Load(fileName);
30 }
31
32 GETTER(const unsigned char*, Data, data)
33 GETTER(int, BytesRead, bytesRead)
34
35 void Load(const std::string_view fileName) { Load(fileName.data()); }
36 void Load(const char* fileName) {
37 data = ::LoadFileData(fileName, &bytesRead);
38 }
39
40 void Unload() {
41 if (data != nullptr) {
42 ::UnloadFileData(data);
43 data = nullptr;
44 }
45 }
46
47 private:
48 unsigned char* data{nullptr};
49 int bytesRead{0};
50};
51
52} // namespace raylib
53
54using RFileData = raylib::FileData;
55
56#endif // RAYLIB_CPP_INCLUDE_FILEDATA_HPP_