Skylicht Engine
Loading...
Searching...
No Matches
CXMLTableData.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 "CXMLSpreadsheet.h"
28#include "Serializable/CObjectSerializable.h"
29
30namespace Skylicht
31{
39 class SKYLICHT_API CXMLTableData
40 {
41 protected:
42 std::vector<std::string> m_column;
43
45
46 public:
52
56 virtual ~CXMLTableData();
57
62 void addColumn(const char* name);
63
69 void insertColumn(const char* name, u32 position);
70
75 void removeColumn(u32 index);
76
82 inline const char* getColumn(u32 index)
83 {
84 return m_column[index].c_str();
85 }
86
92 {
93 return (u32)m_column.size();
94 }
95
101 {
102 return (u32)m_sheet->Rows.size();
103 }
104
113 u32 fetchData(std::function<CObjectSerializable* ()> creatorFunc, std::vector<CObjectSerializable*>& data, u32 fromRow, int count);
114
121
127 template<class T>
128 void freeData(std::vector<T*>& data)
129 {
130 for (T* i : data)
131 delete i;
132 data.clear();
133 }
134
146 template<class T>
147 u32 fetchData(std::vector<T*>& data, u32 fromRow, int count)
148 {
149 std::vector<CXMLSpreadsheet::SRow*>::iterator i = m_sheet->Rows.begin(), end = m_sheet->Rows.end();
150 while (i != end)
151 {
152 CXMLSpreadsheet::SRow* row = (*i);
153 if (row->Index < fromRow)
154 {
155 ++i;
156 continue;
157 }
158
159 if (count > 0)
160 {
161 if (row->Index >= fromRow + (u32)count)
162 break;
163 }
164
165 T* obj = new T();
166 CObjectSerializable* objectSerizable = dynamic_cast<CObjectSerializable*>(obj);
167 if (objectSerizable == NULL)
168 {
169 delete obj;
170 return 0;
171 }
172
173 copyRowToObject(row, objectSerizable);
174
175 data.push_back(obj);
176
177 ++i;
178 }
179
180 return (u32)data.size();
181 }
182 };
183}
Definition CObjectSerializable.h:36
u32 fetchData(std::vector< T * > &data, u32 fromRow, int count)
Create and fill typed serializable objects from sheet rows.
Definition CXMLTableData.h:147
void addColumn(const char *name)
Append a column mapping.
u32 fetchData(std::function< CObjectSerializable *()> creatorFunc, std::vector< CObjectSerializable * > &data, u32 fromRow, int count)
Create and fill serializable objects from sheet rows.
void freeData(std::vector< T * > &data)
Delete all pointers in a fetched data vector and clear it.
Definition CXMLTableData.h:128
virtual ~CXMLTableData()
Destroy the table adapter.
CXMLTableData(CXMLSpreadsheet::SSheet *sheet)
Construct a table adapter for a sheet.
u32 getNumColumn()
Get the number of mapped columns.
Definition CXMLTableData.h:91
const char * getColumn(u32 index)
Get a column mapping name.
Definition CXMLTableData.h:82
void insertColumn(const char *name, u32 position)
Insert a column mapping at a position.
void removeColumn(u32 index)
Remove a column mapping.
u32 getNumRow()
Get the number of rows in the source sheet.
Definition CXMLTableData.h:100
void copyRowToObject(CXMLSpreadsheet::SRow *row, CObjectSerializable *data)
Copy one spreadsheet row into a serializable object.
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
One spreadsheet row that owns its cells.
Definition CXMLSpreadsheet.h:99
u32 Index
Row index from the source document.
Definition CXMLSpreadsheet.h:101
One spreadsheet sheet that owns its rows.
Definition CXMLSpreadsheet.h:122