Tramway SDK
resource.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef TRAM_SDK_FRAMEWORK_RESOURCE_H
5#define TRAM_SDK_FRAMEWORK_RESOURCE_H
6
7#include <framework/uid.h>
9
10namespace tram {
11
12class Resource {
13public:
14 enum Status {
15 UNLOADED, //< Resource not in memory. Can't be used.
16 LOADED, //< Resource loaded in memory, but still needs some processing (like uploading to video memory).
17 READY, //< Resource is ready for sus.
18 };
19
21
22 inline Resource::Status GetStatus() const { return status; }
23 inline name_t GetName() const { return name; }
24 inline size_t GetReferences() const { return references; }
25 inline bool IsLoadFailed() const { return load_fail; }
26
27 inline void AddReference() { references++; }
28 inline void RemoveReference() { references--; }
29
30 virtual void LoadFromDisk() = 0;
31 virtual void LoadFromMemory() = 0;
32
34
35 virtual void Unload() = 0;
36protected:
39 bool load_fail = false;
40 size_t references = 0;
41};
42
43
44namespace Async { void RequestResource (EntityComponent* requester, Resource* requested_resource); }
45
46template <typename T>
48public:
50 ~ResourceProxy() { set(nullptr); }
51 void set(Resource* new_res){
52 if (resource) {
55 }
56
57 resource = new_res;
58
59 if (resource) {
61
65 }
66 }
67 }
68 T* get() { return (T*)resource; }
69 T* operator= (Resource* res) { set(res); return get(); }
70 T* operator->() { return (T*)resource; }
71 T& operator*() { return *((T*)resource); }
72 explicit operator bool() { return resource != nullptr; }
73protected:
75 Resource* resource = nullptr;
76};
77
78}
79
80#endif // TRAM_SDK_FRAMEWORK_RESOURCE_H
Definition: entitycomponent.h:18
size_t resources_waiting
Definition: entitycomponent.h:37
Definition: resource.h:12
virtual void Unload()=0
name_t name
Definition: resource.h:38
void Load()
Definition: resource.h:33
name_t GetName() const
Definition: resource.h:23
void RemoveReference()
Definition: resource.h:28
Resource::Status GetStatus() const
Definition: resource.h:22
virtual void LoadFromDisk()=0
Resource(name_t name)
Definition: resource.h:20
Status
Definition: resource.h:14
@ LOADED
Definition: resource.h:16
@ READY
Definition: resource.h:17
@ UNLOADED
Definition: resource.h:15
bool IsLoadFailed() const
Definition: resource.h:25
size_t references
Definition: resource.h:40
virtual void LoadFromMemory()=0
Status status
Definition: resource.h:37
size_t GetReferences() const
Definition: resource.h:24
void AddReference()
Definition: resource.h:27
bool load_fail
Definition: resource.h:39
Definition: resource.h:47
T * operator->()
Definition: resource.h:70
Resource * resource
Definition: resource.h:75
void set(Resource *new_res)
Definition: resource.h:51
T & operator*()
Definition: resource.h:71
T * get()
Definition: resource.h:68
ResourceProxy(EntityComponent *parent)
Definition: resource.h:49
EntityComponent * parent
Definition: resource.h:74
~ResourceProxy()
Definition: resource.h:50
T * operator=(Resource *res)
Definition: resource.h:69
void CancelRequest(EntityComponent *requester, Resource *resource)
Cancels a resource load request.
Definition: async.cpp:53
void RequestResource(EntityComponent *requester, Resource *resource)
Adds a resource to the loading queue.
Definition: async.cpp:39
Definition: api.h:9
Definition: uid.h:11