Skylicht Engine
Loading...
Searching...
No Matches
CArraySerializable.h
1/*
2!@
3MIT License
4
5Copyright (c) 2022 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 "CObjectSerializable.h"
28#include <functional>
29
30namespace Skylicht
31{
32 class SKYLICHT_API CArraySerializable : public CObjectSerializable
33 {
34 public:
35 CArraySerializable(const char* name);
36
37 CArraySerializable(const char* name, CObjectSerializable* parent);
38
39 virtual ~CArraySerializable();
40
41 template<class T>
42 T getElementValue(int i, T defaultValue)
43 {
44 T* v = dynamic_cast<T*>(getElement(i));
45 if (v == NULL)
46 return defaultValue;
47 return *v;
48 }
49
50 int getElementCount()
51 {
52 return getNumProperty();
53 }
54
55 CValueProperty* getElement(int i)
56 {
57 return getPropertyID(i);
58 }
59
60 void removeByIndex(int index);
61
62 void clear();
63
64 virtual bool haveCreateElementFunction()
65 {
66 return false;
67 }
68
69 virtual CValueProperty* createElement()
70 {
71 return NULL;
72 }
73
74 bool resize(int count);
75
76 protected:
77
78 void autoName();
79 };
80
81 template <class T>
82 class SKYLICHT_API CArrayTypeSerializable : public CArraySerializable
83 {
84 public:
85 std::function<void(CValueProperty*)> OnCreateElement;
86
87 public:
88 CArrayTypeSerializable(const char* name) :
89 CArraySerializable(name)
90 {
91
92 }
93
94 CArrayTypeSerializable(const char* name, CObjectSerializable* parent) :
95 CArraySerializable(name, parent)
96 {
97
98 }
99
100 virtual bool haveCreateElementFunction()
101 {
102 return true;
103 }
104
105 virtual CValueProperty* createElement()
106 {
107 T* t = new T();
108 CValueProperty* element = dynamic_cast<CValueProperty*>(t);
109 if (element == NULL)
110 {
111 delete t;
112 return NULL;
113 }
114
115 char name[32];
116 sprintf(name, "[%d]", (int)m_value.size());
117
118 addProperty(element);
119 autoRelease(element);
120
121 element->Name = name;
122
123 if (OnCreateElement != nullptr)
124 {
125 OnCreateElement(element);
126 }
127
128 return element;
129 }
130 };
131}
Definition CObjectSerializable.h:36
Definition CValueProperty.h:68
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29