MLIB
Loading...
Searching...
No Matches
asset.h
Go to the documentation of this file.
1
6#pragma once
7
8#if __has_include("defs.h")
9#include "defs.h"
10#endif
11
12#include <string>
13#include <utf8/utf8.h>
14
15#ifndef RESFILE
17#define RESFILE 256
18#endif
19
20namespace mlib {
21
23class asset
24{
25public:
26 asset (int id_, const std::string& name_ = std::string (), bool persist = false);
27 ~asset ();
28 const void* data ();
29 size_t size ();
30 bool write (const std::string& path);
31 bool remove ();
32
33 const std::string name;
34
35private:
36 void load ();
37 int id;
38 bool written;
39 bool loaded;
40 bool keep; // do not delete asset file in destructor
41 size_t sz;
42 void* ptr;
43 std::string fullpath;
44};
45
53inline asset::asset (int id_, const std::string& name_, bool persist)
54 : name (name_)
55 , id (id_)
56 , written (false)
57 , keep (persist)
58 , loaded (false)
59 , sz (0)
60 , ptr (nullptr)
61{}
62
65{
66 if (written && !keep)
67 remove ();
68}
69
70/*
71 Delete asset file from disk.
72
73 File is deleted even if this is a persistent asset.
74*/
75inline bool asset::remove ()
76{
77 if (written)
78 {
79 written = false;
80 return utf8::remove (fullpath);
81 }
82 return true;
83}
84
86inline size_t asset::size ()
87{
88 return sz;
89}
90} // namespace mlib
Class for storing asset files in Windows resource data.
Definition asset.h:24
~asset()
Destructor. Delete asset file if it exists.
Definition asset.h:64
bool write(const std::string &path)
Write the asset to a folder.
Definition asset.cpp:51
size_t size()
Return size of asset (in bytes)
Definition asset.h:86
const void * data()
Load asset data.
Definition asset.cpp:35
asset(int id_, const std::string &name_=std::string(), bool persist=false)
Constructor for an asset object.
Definition asset.h:53