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#include "Base/ReflectionObject.h"
12#include "../Util/VariableBinder.h"
13#include "../Util/ResourceDef.h"
14
15namespace CSE {
16
17 class SResource : public SObject, public VariableBinder, public ReflectionObject {
18 public:
19 explicit SResource(std::string classType);
20
21 SResource(const SResource* resource, bool isRegister);
22
23 ~SResource() override;
24
25 void SetName(std::string name);
26
27 void SetAbsoluteID(std::string id);
28
29 std::string GetName() const {
30 return m_name;
31 }
32
33 std::string GetAbsoluteID() const {
34 return m_absoluteId;
35 }
36
37 AssetMgr::AssetReference* GetAssetReference(std::string hash = "") const;
38
39 void LinkResource(AssetMgr::AssetReference* asset) {
40 SetResource(asset, false);
41 }
42
43 void LinkResource(std::string name) {
44 SetResource(std::move(name), false);
45 }
46
47 template <class T>
48 static T* Create(const std::string& name) {
49 {
50 SResource* res = GetResource(name);
51 if (res != nullptr) return static_cast<T*>(res);
52 }
53 T* object = new T();
54 SResource* res = object;
55
56 res->SetResource(name);
57 return object;
58 }
59
60 static SResource* Create(const std::string& name, const std::string& classType);
61
62 template <class T>
63 static T* Create(const AssetMgr::AssetReference* asset) {
64 if (asset == nullptr) return nullptr;
65 {
66 SResource* res = GetResource(asset->hash);
67 if (res != nullptr) return static_cast<T*>(res);
68 }
69 T* object = new T();
70 SResource* res = object;
71
72 res->SetResource(const_cast<AssetMgr::AssetReference*>(asset));
73 return object;
74 }
75
76 static SResource* Create(const AssetMgr::AssetReference* asset, const std::string& classType);
77
78 template <class T>
79 static T* Get(std::string name) {
80 SResource* res = GetResource(std::move(name));
81 if (res != nullptr) return static_cast<T*>(res);
82 return nullptr;
83 }
84
85 static SResource* Get(std::string& name);
86
87 void SetHash(std::string& hash) override;
88
89 protected:
90 virtual void Init(const AssetMgr::AssetReference* asset) = 0;
91
92 private:
93 void SetResource(std::string name, bool isInit = true);
94
95 void SetResource(AssetMgr::AssetReference* asset, bool isInit = true);
96
97 static SResource* GetResource(std::string name);
98
99 private:
100 std::string m_name;
101 std::string m_absoluteId;
102 bool m_isInited = false;
103
104 };
105}