1 #ifndef QTDATASYNC_DATASTORE_H 2 #define QTDATASYNC_DATASTORE_H 6 #include <QtCore/qobject.h> 7 #include <QtCore/qscopedpointer.h> 8 #include <QtCore/qvariant.h> 10 #include "QtDataSync/qtdatasync_global.h" 11 #include "QtDataSync/objectkey.h" 12 #include "QtDataSync/exception.h" 13 #include "QtDataSync/qtdatasync_helpertypes.h" 19 class DataStorePrivate;
48 qint64 count(
int metaTypeId)
const;
52 QVariantList loadAll(
int metaTypeId)
const;
54 bool contains(
int metaTypeId,
const QString &key)
const;
57 return contains(metaTypeId, key.
toString());
63 return load(metaTypeId, key.
toString());
66 void save(
int metaTypeId,
QVariant value);
68 bool remove(
int metaTypeId,
const QString &key);
70 inline bool remove(
int metaTypeId,
const QVariant &key) {
71 return remove(metaTypeId, key.toString());
74 void update(
int metaTypeId,
QObject *
object)
const;
76 QVariantList search(
int metaTypeId,
const QString &query, SearchMode mode = RegexpMode)
const;
78 void iterate(
int metaTypeId,
79 const std::function<
bool(
QVariant)> &iterator)
const;
81 void iterate(
int metaTypeId,
82 const std::function<
bool(
QVariant)> &iterator,
83 bool skipBroken)
const;
85 void clear(
int metaTypeId);
89 quint64 count()
const;
98 template<
typename T,
typename K>
105 bool contains(
const QString &key)
const;
107 template<
typename T,
typename K>
108 bool contains(
const K &key)
const;
111 T load(
const QString &key)
const;
113 template<
typename T,
typename K>
114 T load(
const K &key)
const;
117 void save(
const T &value);
120 bool remove(
const QString &key);
122 template<
typename T,
typename K>
123 bool remove(
const K &key);
126 void update(T
object)
const;
129 QList<T> search(
const QString &query, SearchMode mode = RegexpMode)
const;
132 void iterate(
const std::function<
bool(T)> &iterator,
bool skipBroken =
false)
const;
139 void dataChanged(
int metaTypeId,
const QString &key,
bool deleted, QPrivateSignal);
141 Q_DECL_DEPRECATED
void dataCleared(
int metaTypeId, QPrivateSignal);
143 void dataResetted(QPrivateSignal);
149 void initStore(
const QString &setupName);
179 QByteArray className()
const noexcept
override;
180 QString qWhat()
const override;
181 void raise()
const override;
204 QByteArray className()
const noexcept
override;
205 QString qWhat()
const override;
206 void raise()
const override;
227 QByteArray className()
const noexcept
override;
228 QString qWhat()
const override;
229 void raise()
const override;
245 QTDATASYNC_STORE_ASSERT(T);
246 return count(qMetaTypeId<T>());
252 QTDATASYNC_STORE_ASSERT(T);
253 return keys(qMetaTypeId<T>());
256 template<
typename T,
typename K>
259 QTDATASYNC_STORE_ASSERT(T);
261 for(
auto k : keys<T>())
269 QTDATASYNC_STORE_ASSERT(T);
271 for(
auto v :
loadAll(qMetaTypeId<T>()))
272 rList.
append(v.template value<T>());
279 QTDATASYNC_STORE_ASSERT(T);
280 return contains(qMetaTypeId<T>(), key);
283 template<
typename T,
typename K>
286 QTDATASYNC_STORE_ASSERT(T);
293 QTDATASYNC_STORE_ASSERT(T);
294 return load(qMetaTypeId<T>(), key).template value<T>();
297 template<
typename T,
typename K>
300 QTDATASYNC_STORE_ASSERT(T);
307 QTDATASYNC_STORE_ASSERT(T);
314 QTDATASYNC_STORE_ASSERT(T);
315 return remove(qMetaTypeId<T>(), key);
318 template<
typename T,
typename K>
321 QTDATASYNC_STORE_ASSERT(T);
328 static_assert(__helpertypes::is_object<T>::value,
"loadInto can only be used for pointers to QObject extending classes");
329 update(qMetaTypeId<T>(),
object);
335 QTDATASYNC_STORE_ASSERT(T);
337 for(
auto v :
search(qMetaTypeId<T>(), query, mode))
338 rList.
append(v.template value<T>());
345 QTDATASYNC_STORE_ASSERT(T);
347 return iterator(v.template value<T>());
354 QTDATASYNC_STORE_ASSERT(T);
355 clear(qMetaTypeId<T>());
360 #endif // QTDATASYNC_DATASTORE_H void update(int metaTypeId, QObject *object) const
Loads the dataset with the given key for the given type into the existing object by updating it's pro...
bool contains(int metaTypeId, const QVariant &key) const
Checks if a dataset exists in the store for the given key.
The data key must contain the search string.
Main store to generically access all stored data synchronously.
QStringList keys() const
Returns all saved keys for the given type.
The base class for all exceptions of QtDataSync.
QVariant load(int metaTypeId, const QString &key) const
Loads the dataset with the given key for the given type.
Exception that is thrown in case no data can be found for a key.
Interpret the search string as a wildcard string (with * and ?)
void append(const T &value)
void clear()
Removes all datasets of the given type from the store.
bool remove(int metaTypeId, const QString &key)
Removes the dataset with the given key for the given type.
QVariantList search(int metaTypeId, const QString &query, SearchMode mode=RegexpMode) const
Searches the store for datasets of the given type where the key matches the query.
void save(int metaTypeId, QVariant value)
Saves the given dataset in the store.
Exception that is thrown when an internal (critical) error occurs.
Defines a unique key to identify a dataset globally.
The primary namespace of the QtDataSync library.
Exception that is thrown from DataStore operations in case of an error.
quint64 count() const
Counts the number of datasets for the given type.
bool contains(int metaTypeId, const QString &key) const
Checks if a dataset exists in the store for the given key.
QVariant fromValue(const T &value)
void iterate(int metaTypeId, const std::function< bool(QVariant)> &iterator) const
Iterates over all existing datasets of the given types.
QVariant load(int metaTypeId, const QVariant &key) const
Loads the dataset with the given key for the given type.
SearchMode
Possible pattern modes for the search mechanism.
The data key must start with the search string.
A helper class to get defaults per datasync instance (threadsafe)
QList< T > loadAll() const
Loads all existing datasets for the given type.
Interpret the search string as a regular expression. See QRegularExpression.
Exception that is thrown when unsaveable/unloadable data is passed to the store.
QString toString() const const
A passive item model for a datasync data store.