Skylicht Engine
Loading...
Searching...
No Matches
CObjectSerializable.h
1/*
2!@
3MIT License
4
5Copyright (c) 2020 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "CValueProperty.h"
28#include "CValuePropertyTemplate.h"
29
30#include "Utils/CSingleton.h"
31#include "Utils/CActivator.h"
32
33namespace Skylicht
34{
35 class SKYLICHT_API CObjectSerializable : public CValueProperty
36 {
37 friend class CValueProperty;
38
39 protected:
40 std::vector<CValueProperty*> m_value;
41 std::vector<CValueProperty*> m_autoRelease;
42 std::string m_savePath;
43
44 public:
45
46 void addProperty(CValueProperty* p)
47 {
48 p->setOwner(this);
49 m_value.push_back(p);
50 }
51
52 void autoRelease(CValueProperty* p)
53 {
54 m_autoRelease.push_back(p);
55 }
56
57 public:
58 CObjectSerializable(const char* name);
59
60 CObjectSerializable(const char* name, CObjectSerializable* parent);
61
62 virtual ~CObjectSerializable();
63
64 void remove(CValueProperty* value);
65
66 inline u32 getNumProperty()
67 {
68 return (u32)m_value.size();
69 }
70
71 inline CValueProperty* getPropertyID(int i)
72 {
73 return m_value[i];
74 }
75
76 CValueProperty* getProperty(const char* name);
77
78 template<class T>
79 T* getProperty(const char* name)
80 {
81 CValueProperty* p = getProperty(name);
82 if (p == NULL)
83 return NULL;
84
85 return dynamic_cast<T*>(p);
86 }
87
88 template<class T>
89 T get(const char* name, T defaultValue)
90 {
91 CValueProperty* p = getProperty(name);
92 if (p == NULL)
93 return defaultValue;
94
96 if (t == NULL)
97 return defaultValue;
98
99 return t->get();
100 }
101
102 template<class T>
103 T get(CValueProperty* p, T defaultValue)
104 {
106 if (t == NULL)
107 return defaultValue;
108
109 return t->get();
110 }
111
112 virtual void serialize(io::IAttributes* io);
113
114 virtual void deserialize(io::IAttributes* io);
115
116 virtual bool save(const char* file);
117
118 virtual bool saveToFile();
119
120 virtual bool load(const char* file);
121
122 virtual void save(io::IXMLWriter* writer);
123
124 virtual void load(io::IXMLReader* reader);
125
126 virtual void parseSerializable(io::IXMLReader* reader);
127
128 virtual CObjectSerializable* clone();
129
130 virtual void copyTo(CObjectSerializable* object);
131
132 inline void setSavePath(const char* path)
133 {
134 m_savePath = path;
135 }
136 };
137
138
139
140#define SERIALIZABLE_REGISTER(type) \
141 CObjectSerializable* type##CreateFunc() { return new type(); } \
142 bool type##_activator = CSerializableActivator::createGetInstance()->registerType(#type, &type##CreateFunc)
143
144 typedef CObjectSerializable* (*SerializableCreateInstance)();
145
146 class SKYLICHT_API CSerializableActivator
147 {
148 public:
150
151 protected:
152 std::map<std::string, int> m_factoryName;
153
154 std::vector<SerializableCreateInstance> m_factoryFunc;
155
156 public:
157 bool registerType(const char* type, SerializableCreateInstance func);
158
159 CObjectSerializable* createInstance(const char* type);
160 };
161}
Definition CObjectSerializable.h:36
Definition CObjectSerializable.h:147
Definition CValuePropertyTemplate.h:33
#define DECLARE_SINGLETON(className)
Declare the standard singleton accessors for a class.
Definition CSingleton.h:36
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29