CSEngine
Loading...
Searching...
No Matches
SResource.h
1//
2// Created by ounols on 19. 6. 10.
3//
4
5#pragma once
6
7#include <string>
8#include <utility>
9#include "../Manager/AssetMgr.h"
10#include "../SObject.h"
11
12namespace CSE {
13
14 class SResource : public SObject {
15 public:
16 SResource();
17 explicit SResource(bool isRegister);
18 SResource(const SResource* resource, bool isRegister);
19
20 ~SResource() override;
21
22 void SetName(std::string name);
23
24 void SetAbsoluteID(std::string id);
25
26 std::string GetName() const {
27 return m_name;
28 }
29
30 std::string GetAbsoluteID() const {
31 return m_absoluteId;
32 }
33
34 AssetMgr::AssetReference* GetAssetReference(std::string hash = "") const;
35
36 void LinkResource(AssetMgr::AssetReference* asset) {
37 SetResource(asset, false);
38 }
39
40 void LinkResource(std::string name) {
41 SetResource(std::move(name), false);
42 }
43
44 template <class T>
45 static T* Create(const std::string& name) {
46 {
47 SResource* res = GetResource(name);
48 if (res != nullptr) return static_cast<T*>(res);
49 }
50 T* object = new T();
51 SResource* res = object;
52
53 res->SetResource(name);
54 return object;
55 }
56
57 template <class T>
58 static T* Create(const AssetMgr::AssetReference* asset) {
59 if (asset == nullptr) return nullptr;
60 {
61 SResource* res = GetResource(asset->name);
62 if (res != nullptr) return static_cast<T*>(res);
63 }
64 T* object = new T();
65 SResource* res = object;
66
67 res->SetResource(asset);
68 return object;
69 }
70
71 template <class T>
72 static T* Get(std::string name) {
73 SResource* res = GetResource(std::move(name));
74 if (res != nullptr) return static_cast<T*>(res);
75 return nullptr;
76 }
77
78 void SetHash(std::string& hash) override;
79
80 protected:
81 virtual void Init(const AssetMgr::AssetReference* asset) = 0;
82
83 private:
84 void SetResource(std::string name, bool isInit = true);
85
86 void SetResource(const AssetMgr::AssetReference* asset, bool isInit = true);
87
88 static SResource* GetResource(std::string name);
89
90 private:
91 std::string m_name;
92 std::string m_absoluteId;
93 bool m_isInited = false;
94
95 };
96}