sptk2 logo
SPTK Home Page
WSArray.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/Field.h>
30#include <sptk5/wsdl/WSFieldIndex.h>
31#include <sptk5/wsdl/WSType.h>
32#include <sptk5/xdoc/Document.h>
33#include <sptk5/xdoc/Node.h>
34
35namespace sptk {
36
40template<typename T>
41class SP_EXPORT WSArray
42 : public WSType
43{
44public:
45 using value_type = T;
46 using iterator = typename std::vector<T>::iterator;
47 using const_iterator = typename std::vector<T>::const_iterator;
48
53 explicit WSArray(const char* name = "array")
54 : WSType(name)
55 {
56 }
57
61 [[nodiscard]] String className() const override
62 {
63 return "WSArray";
64 }
65
66 [[nodiscard]] bool isNull() const override
67 {
68 return m_array.empty();
69 }
70
71 [[nodiscard]] size_t size() const
72 {
73 return m_array.size();
74 }
75
76 void clear() override
77 {
78 return m_array.clear();
79 }
80
81 bool empty() const
82 {
83 return m_array.empty();
84 }
85
86 T& operator[](size_t index)
87 {
88 return m_array[index];
89 }
90
91 const T& operator[](size_t index) const
92 {
93 return m_array[index];
94 }
95
96 iterator begin()
97 {
98 return m_array.begin();
99 }
100
101 iterator end()
102 {
103 return m_array.end();
104 }
105
106 const_iterator begin() const
107 {
108 return m_array.begin();
109 }
110
111 const_iterator end() const
112 {
113 return m_array.end();
114 }
115
116 T& front()
117 {
118 return m_array.front();
119 }
120
121 const T& front() const
122 {
123 return m_array.front();
124 }
125
126 T& back()
127 {
128 return m_array.back();
129 }
130
131 const T& back() const
132 {
133 return m_array.back();
134 }
135
136 void push_back(const T& value)
137 {
138 m_array.push_back(value);
139 }
140
141 void push_back(T&& value)
142 {
143 m_array.push_back(value);
144 }
145
146 void emplace_back(const T& value)
147 {
148 m_array.emplace_back(value);
149 }
150
151 void emplace_back(T&& value)
152 {
153 m_array.emplace_back(value);
154 }
155
156 void resize(size_t sz)
157 {
158 m_array.resize(sz);
159 }
160
161 auto erase(const iterator& pos)
162 {
163 return m_array.erase(pos);
164 }
165
166 auto erase(const iterator& first, const iterator& last)
167 {
168 return m_array.erase(first, last);
169 }
170
171 void load(const xdoc::SNode& node, bool nullLargeData) override
172 {
173 for (const auto& arrayElement: node->nodes())
174 {
175 T item(name().c_str(), false);
176 item.load(arrayElement, nullLargeData);
177 m_array.push_back(std::move(item));
178 }
179 }
180
184 String asString() const override
185 {
186 xdoc::Document document;
187 Buffer buffer;
188 exportTo(document.root());
189 const auto& arrayNode = document.root()->findFirst(name());
190 arrayNode->exportTo(xdoc::DataFormat::JSON, buffer, false);
191 return (String) buffer;
192 }
193
199 void exportTo(const xdoc::SNode& output, const char* name = nullptr) const override
200 {
201 const char* itemName = name == nullptr ? "item" : name;
202 auto& arrayNode = output->pushNode(this->name(), xdoc::Node::Type::Array);
203 for (const auto& element: m_array)
204 {
205 element.exportTo(arrayNode, itemName);
206 }
207 }
208
209private:
210 std::vector<T> m_array;
211};
212
213} // namespace sptk
Definition: Buffer.h:51
Definition: String.h:49
Definition: WSArray.h:43
void clear() override
Definition: WSArray.h:76
String asString() const override
Definition: WSArray.h:184
String className() const override
Definition: WSArray.h:61
WSArray(const char *name="array")
Definition: WSArray.h:53
void exportTo(const xdoc::SNode &output, const char *name=nullptr) const override
Definition: WSArray.h:199
void load(const xdoc::SNode &node, bool nullLargeData) override
Definition: WSArray.h:171
Definition: WSType.h:42
Definition: Document.h:34

Fri Oct 14 2022 09:58:32: SPTK 5.4.1