sptk2 logo
SPTK Home Page
MemoryDS.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 <list>
30#include <mutex>
31#include <sptk5/DataSource.h>
32#include <sptk5/Exception.h>
33#include <sptk5/FieldList.h>
34#include <sptk5/sptk.h>
35
36namespace sptk {
37
50class SP_EXPORT MemoryDS
51 : public DataSource
52{
53public:
57 MemoryDS() = default;
58
63 MemoryDS(const MemoryDS& other) = delete;
64
69 MemoryDS(MemoryDS&& other) noexcept
70 : m_list(std::move(other.m_list))
71 , m_current(std::move(other.m_current))
72 {
73 }
74
75 ~MemoryDS() override = default;
76
81 MemoryDS& operator=(const MemoryDS& other) = delete;
82
87 MemoryDS& operator=(MemoryDS&& other) noexcept
88 {
89 std::scoped_lock lock(m_mutex, other.m_mutex);
90 m_list = std::move(other.m_list);
91 m_current = std::move(other.m_current);
92 return *this;
93 }
94
98 virtual void clear();
99
105 {
106 std::lock_guard lock(m_mutex);
107 return *m_current;
108 }
109
115 Field& operator[](size_t fieldIndex) override;
116
122 Field& operator[](const String& fieldName) override;
123
128 size_t fieldCount() const override;
129
134 size_t recordCount() const override;
135
141 bool readField(const char* fieldName, Variant& fieldValue) override;
142
148 bool writeField(const char* fieldName, const Variant& fieldValue) override;
149
153 bool open() override;
154
158 bool close() override;
159
163 bool first() override;
164
168 bool next() override;
169
173 bool prior() override;
174
178 bool last() override;
179
183 bool find(const String& fieldName, const Variant& position) override;
184
188 bool eof() const override
189 {
190 std::scoped_lock lock(m_mutex);
191 return m_current == m_list.end();
192 }
193
194 bool empty() const;
195
196 std::list<FieldList>& rows()
197 {
198 std::scoped_lock lock(m_mutex);
199 return m_list;
200 }
201
202 const std::list<FieldList>& rows() const
203 {
204 std::scoped_lock lock(m_mutex);
205 return m_list;
206 }
207
213 void push_back(FieldList&& fieldList);
214
215private:
216 mutable std::mutex m_mutex;
217 std::list<FieldList> m_list; // List of the dataset records
218 std::list<FieldList>::iterator m_current; // DS iterator
219};
223} // namespace sptk
Definition: DataSource.h:49
Definition: FieldList.h:50
Definition: Field.h:54
Base (memory) datasource.
Definition: MemoryDS.h:52
bool last() override
MemoryDS & operator=(const MemoryDS &other)=delete
bool next() override
Field & operator[](const String &fieldName) override
MemoryDS(MemoryDS &&other) noexcept
Definition: MemoryDS.h:69
virtual void clear()
size_t fieldCount() const override
bool prior() override
virtual FieldList & current()
Definition: MemoryDS.h:104
bool writeField(const char *fieldName, const Variant &fieldValue) override
MemoryDS & operator=(MemoryDS &&other) noexcept
Definition: MemoryDS.h:87
void push_back(FieldList &&fieldList)
bool close() override
MemoryDS()=default
bool open() override
bool find(const String &fieldName, const Variant &position) override
Field & operator[](size_t fieldIndex) override
bool eof() const override
Definition: MemoryDS.h:188
MemoryDS(const MemoryDS &other)=delete
bool readField(const char *fieldName, Variant &fieldValue) override
bool first() override
size_t recordCount() const override
Definition: String.h:49
Definition: Variant.h:372

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