MLIB
Loading...
Searching...
No Matches
asset.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7
8#pragma once
9
10#ifndef RESFILE
12#define RESFILE 256
13#endif
14
15#ifndef RC_INVOKED //
16
17#if __has_include("defs.h")
18#include "defs.h"
19#endif
20
21#include <string>
22#include <filesystem>
23
24namespace mlib {
25
27class asset
28{
29public:
30 asset (int id_, const std::filesystem::path& name_ = std::filesystem::path (),
31 bool persist = false);
32 ~asset ();
33
35 const void* data ();
36
38 size_t size ();
39
40 bool write (const std::filesystem::path& root_path);
41
43 bool remove ();
44
46 std::filesystem::path name;
47
48private:
49 bool load ();
50 int id;
51 bool written;
52 bool loaded;
53 bool keep; // do not delete asset file in destructor
54 void* ptr;
55 size_t sz;
56 std::filesystem::path fullpath;
57};
58
66inline
67asset::asset (int id_, const std::filesystem::path& name_, bool persist)
68 : name (name_)
69 , id (id_)
70 , written (false)
71 , keep (persist)
72 , loaded (false)
73 , sz (0)
74 , ptr (nullptr)
75{}
76
78inline
80{
81 if (written && !keep)
82 remove ();
83}
84
90inline
92{
93 if (written)
94 {
95 written = false;
96 return std::filesystem::remove (fullpath);
97 }
98 return false;
99}
100
102inline
103size_t asset::size ()
104{
105 if (!loaded)
106 load ();
107 return sz;
108}
109
111inline
112const void* asset::data ()
113{
114 if (!loaded)
115 load ();
116
117 return ptr;
118}
119
120} // namespace mlib
121#endif //RC_INVOKED
~asset()
Destructor. Delete asset file if it exists and is not persistent.
Definition asset.h:79
bool write(const std::filesystem::path &root_path)
Write the asset to a folder.
Definition asset.cpp:60
asset(int id_, const std::filesystem::path &name_=std::filesystem::path(), bool persist=false)
Constructor for an asset object.
Definition asset.h:67
std::filesystem::path name
Relative asset name.
Definition asset.h:46
size_t size()
Return size of asset data or 0 if asset could not be loaded.
Definition asset.h:103
const void * data()
Load asset data and return a pointer to it.
Definition asset.h:112
bool remove()
Delete asset file from disk.
Definition asset.h:91