20#ifndef LIBREPCB_CORE_SERIALIZABLEOBJECTLIST_H
21#define LIBREPCB_CORE_SERIALIZABLEOBJECTLIST_H
26#include "../exceptions.h"
27#include "../types/uuid.h"
28#include "../utils/signalslot.h"
95template <
typename T,
typename P,
typename... OnEditedArgs>
101 template <
typename I,
typename O>
116 std::shared_ptr<O>
ptr() noexcept {
117 return std::const_pointer_cast<O>(*
it);
132 const std::shared_ptr<const T>&,
Event>
135 const std::shared_ptr<const T>&,
Event>
138 const std::shared_ptr<const T>&, OnEditedArgs...>
141 const std::shared_ptr<const T>&, OnEditedArgs...>
170 while (!other.isEmpty()) {
175 std::initializer_list<std::shared_ptr<T>> elements) noexcept
182 for (
const std::shared_ptr<T>& obj : elements) {
202 std::vector<Uuid> uuids;
204 for (
const std::shared_ptr<T>& obj :
mObjects) {
205 uuids.push_back(obj->getUuid());
212 for (
const std::shared_ptr<T>& obj :
mObjects) {
213 uuids.insert(obj->getUuid());
220 for (
int i = 0; i <
count(); ++i) {
228 for (
int i = 0; i <
count(); ++i) {
229 if (
mObjects[i]->getUuid() == key) {
236 Qt::CaseSensitivity cs = Qt::CaseSensitive)
const noexcept {
237 for (
int i = 0; i <
count(); ++i) {
238 if (QString::compare(
asStr(
mObjects[i]->getName()), name, cs) == 0) {
245 return index >= 0 && index <
mObjects.count();
249 bool contains(
const QString& name)
const noexcept {
254 std::shared_ptr<T>
value(
int index)
noexcept {
return mObjects.value(index); }
255 std::shared_ptr<const T>
value(
int index)
const noexcept {
256 return std::const_pointer_cast<const T>(
mObjects.value(index));
259 std::shared_ptr<T>
find(
const Uuid& key)
noexcept {
262 std::shared_ptr<const T>
find(
const Uuid& key)
const noexcept {
265 std::shared_ptr<T>
find(
const QString& name,
266 Qt::CaseSensitivity cs = Qt::CaseSensitive)
noexcept {
271 Qt::CaseSensitivity cs = Qt::CaseSensitive)
const noexcept {
276 std::shared_ptr<const T>
at(
int index)
const noexcept {
277 return std::const_pointer_cast<const T>(
mObjects.at(index));
280 std::shared_ptr<const T>
first() const noexcept {
return mObjects.first(); }
282 std::shared_ptr<const T>
last() const noexcept {
return mObjects.last(); }
283 std::shared_ptr<T>
get(
const T* obj) {
284 std::shared_ptr<T> ptr =
find(obj);
285 if (!ptr)
throw LogicError(__FILE__, __LINE__);
289 std::shared_ptr<T> ptr =
find(key);
293 std::shared_ptr<const T>
get(
const Uuid& key)
const {
294 std::shared_ptr<const T> ptr =
find(key);
298 std::shared_ptr<T>
get(
const QString& name) {
299 std::shared_ptr<T> ptr =
find(name);
303 std::shared_ptr<const T>
get(
const QString& name)
const {
304 std::shared_ptr<const T> ptr =
find(name);
321 append(std::make_shared<T>(*child));
325 void swap(
int i,
int j)
noexcept {
327 qBound(0, i,
count() - 1);
328 qBound(0, j,
count() - 1);
330 if (i > j) qSwap(i, j);
331 std::shared_ptr<T> oj =
take(j);
332 std::shared_ptr<T> oi =
take(i);
336 int insert(
int index,
const std::shared_ptr<T>& obj)
noexcept {
338 qBound(0, index,
count());
342 int append(
const std::shared_ptr<T>& obj)
noexcept {
347 for (
const std::shared_ptr<T>& ptr : list.mObjects) {
351 std::shared_ptr<T>
take(
int index)
noexcept {
356 std::shared_ptr<T>
take(
const Uuid& uuid)
noexcept {
359 std::shared_ptr<T>
take(
const QString& name)
noexcept {
368 for (
int i =
count() - 1; i >= 0; --i) {
380 for (
const std::shared_ptr<T>& ptr :
mObjects) {
388 template <
typename Compare>
390 Compare lessThan)
const noexcept {
393 std::sort(copiedList.mObjects.begin(), copiedList.mObjects.end(),
394 [&lessThan](
const std::shared_ptr<T>& ptr1,
395 const std::shared_ptr<T>& ptr2) {
396 return lessThan(*ptr1, *ptr2);
401 return sorted([](
const T& lhs,
const T& rhs) {
402 return lhs.getUuid() < rhs.getUuid();
417 if (rhs.mObjects.count() !=
mObjects.count())
return false;
418 for (
int i = 0; i <
mObjects.count(); ++i) {
419 if (*rhs.mObjects[i] != *
mObjects[i])
return false;
425 return !(*
this == rhs);
431 for (
const std::shared_ptr<T>& ptr : rhs.mObjects) {
433 *ptr,
typename std::is_nothrow_copy_constructible<T>::type()));
441 for (
const std::shared_ptr<T>& ptr : rhs.mObjects) {
455 std::shared_ptr<T> obj =
mObjects.takeAt(index);
466 qCritical() <<
"Received notification from unknown list element!";
474 "no element of type \"%1\" with the UUID \"%2\" in the list."))
483 "no element of type \"%1\" with the name \"%2\" in the list."))
490 std::true_type copyConstructable)
noexcept {
491 Q_UNUSED(copyConstructable);
492 return std::make_shared<T>(other);
495 std::false_type copyConstructable)
noexcept {
496 Q_UNUSED(copyConstructable);
497 return other.cloneShared();
499 template <
typename TStr>
500 inline const QString&
asStr(
const TStr& obj)
const noexcept {
503 inline const QString&
asStr(
const QString& obj)
const noexcept {
return obj; }
519template <
typename T,
typename P,
typename... OnEditedArgs>
520class QForeachContainer<
521 librepcb::SerializableObjectList<T, P, OnEditedArgs...>> {
525template <
typename T,
typename P,
typename... OnEditedArgs>
526class QForeachContainer<
527 const
librepcb::SerializableObjectList<T, P, OnEditedArgs...>> {
~QForeachContainer()=delete
~QForeachContainer()=delete
The LogicError class.
Definition exceptions.h:181
The RuntimeError class.
Definition exceptions.h:218
The SExpression class.
Definition sexpression.h:69
SExpression & appendList(const QString &name)
Definition sexpression.cpp:212
QList< SExpression * > getChildren(Type type) noexcept
Definition sexpression.cpp:94
void ensureLineBreak()
Definition sexpression.cpp:206
Definition serializableobjectlist.h:102
O * operator->()
Definition serializableobjectlist.h:115
~Iterator()
Definition serializableobjectlist.h:119
O & operator*()
Definition serializableobjectlist.h:114
Iterator(const I &it) noexcept
Definition serializableobjectlist.h:108
Iterator(const Iterator &other) noexcept
Definition serializableobjectlist.h:107
Iterator & operator++()
Definition serializableobjectlist.h:110
bool operator!=(const Iterator &rhs) const noexcept
Definition serializableobjectlist.h:109
I it
Definition serializableobjectlist.h:103
std::shared_ptr< O > ptr() noexcept
Definition serializableobjectlist.h:116
The SerializableObjectList class implements a list of serializable objects.
Definition serializableobjectlist.h:96
QVector< std::shared_ptr< T > > mObjects
Definition serializableobjectlist.h:506
void clear() noexcept
Definition serializableobjectlist.h:366
std::shared_ptr< const T > find(const QString &name, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition serializableobjectlist.h:269
int loadFromSExpression(const SExpression &node)
Definition serializableobjectlist.h:318
int append(const std::shared_ptr< T > &obj) noexcept
Definition serializableobjectlist.h:342
std::shared_ptr< const T > at(int index) const noexcept
Definition serializableobjectlist.h:276
bool contains(const QString &name) const noexcept
Definition serializableobjectlist.h:249
std::shared_ptr< const T > last() const noexcept
Definition serializableobjectlist.h:282
std::vector< Uuid > getUuids() const noexcept
Definition serializableobjectlist.h:201
std::shared_ptr< T > take(const Uuid &uuid) noexcept
Definition serializableobjectlist.h:356
int count() const noexcept
Definition serializableobjectlist.h:199
const QString & asStr(const QString &obj) const noexcept
Definition serializableobjectlist.h:503
std::shared_ptr< T > & last() noexcept
Definition serializableobjectlist.h:281
const_iterator begin() const noexcept
Definition serializableobjectlist.h:310
Signal< SerializableObjectList< T, P, OnEditedArgs... >, int, const std::shared_ptr< const T > &, Event > onEdited
Definition serializableobjectlist.h:133
int indexOf(const Uuid &key) const noexcept
Definition serializableobjectlist.h:227
bool operator!=(const SerializableObjectList< T, P, OnEditedArgs... > &rhs) const noexcept
Definition serializableobjectlist.h:423
std::shared_ptr< T > find(const Uuid &key) noexcept
Definition serializableobjectlist.h:259
Slot< SerializableObjectList< T, P, OnEditedArgs... >, int, const std::shared_ptr< const T > &, Event > OnEditedSlot
Definition serializableobjectlist.h:136
std::shared_ptr< T > & first() noexcept
Definition serializableobjectlist.h:279
virtual ~SerializableObjectList() noexcept
Definition serializableobjectlist.h:195
void throwNameNotFoundException(const QString &name) const
Definition serializableobjectlist.h:478
Signal< SerializableObjectList< T, P, OnEditedArgs... >, int, const std::shared_ptr< const T > &, OnEditedArgs... > onElementEdited
Definition serializableobjectlist.h:139
std::shared_ptr< T > get(const Uuid &key)
Definition serializableobjectlist.h:288
std::shared_ptr< const T > get(const Uuid &key) const
Definition serializableobjectlist.h:293
std::shared_ptr< T > value(int index) noexcept
Definition serializableobjectlist.h:254
const_iterator cend() noexcept
Definition serializableobjectlist.h:313
const QString & asStr(const TStr &obj) const noexcept
Definition serializableobjectlist.h:500
std::shared_ptr< T > find(const QString &name, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Definition serializableobjectlist.h:265
std::shared_ptr< T > copyObject(const T &other, std::false_type copyConstructable) noexcept
Definition serializableobjectlist.h:494
std::shared_ptr< T > copyObject(const T &other, std::true_type copyConstructable) noexcept
Definition serializableobjectlist.h:489
SerializableObjectList(SerializableObjectList< T, P, OnEditedArgs... > &&other) noexcept
Definition serializableobjectlist.h:162
Event
Definition serializableobjectlist.h:126
std::shared_ptr< T > get(const QString &name)
Definition serializableobjectlist.h:298
const_iterator end() const noexcept
Definition serializableobjectlist.h:311
std::shared_ptr< const T > find(const Uuid &key) const noexcept
Definition serializableobjectlist.h:262
const QVector< std::shared_ptr< T > > & values() noexcept
Definition serializableobjectlist.h:200
bool contains(int index) const noexcept
Definition serializableobjectlist.h:244
SerializableObjectList(std::initializer_list< std::shared_ptr< T > > elements) noexcept
Definition serializableobjectlist.h:174
void remove(const QString &name) noexcept
Definition serializableobjectlist.h:365
std::shared_ptr< T > operator[](int i) noexcept
Definition serializableobjectlist.h:407
SerializableObjectList< T, P, OnEditedArgs... > sorted(Compare lessThan) const noexcept
Definition serializableobjectlist.h:389
QSet< Uuid > getUuidSet() const noexcept
Definition serializableobjectlist.h:209
int indexOf(const QString &name, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition serializableobjectlist.h:235
std::shared_ptr< const T > operator[](int i) const noexcept
Definition serializableobjectlist.h:411
bool contains(const Uuid &key) const noexcept
Definition serializableobjectlist.h:248
SerializableObjectList< T, P, OnEditedArgs... > & operator=(SerializableObjectList< T, P, OnEditedArgs... > &&rhs) noexcept
Definition serializableobjectlist.h:437
void throwKeyNotFoundException(const Uuid &key) const
Definition serializableobjectlist.h:469
void remove(const Uuid &uuid) noexcept
Definition serializableobjectlist.h:364
std::shared_ptr< T > find(const T *obj) noexcept
Definition serializableobjectlist.h:258
iterator begin() noexcept
Definition serializableobjectlist.h:314
void remove(int index) noexcept
Definition serializableobjectlist.h:362
Slot< SerializableObjectList< T, P, OnEditedArgs... >, int, const std::shared_ptr< const T > &, OnEditedArgs... > OnElementEditedSlot
Definition serializableobjectlist.h:142
SerializableObjectList< T, P, OnEditedArgs... > sortedByUuid() const noexcept
Definition serializableobjectlist.h:400
std::shared_ptr< T > take(int index) noexcept
Definition serializableobjectlist.h:351
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition serializableobjectlist.h:379
void elementEditedHandler(const T &obj, OnEditedArgs... args) noexcept
Definition serializableobjectlist.h:460
void swap(int i, int j) noexcept
Definition serializableobjectlist.h:325
SerializableObjectList() noexcept
Definition serializableobjectlist.h:145
std::shared_ptr< const T > value(int index) const noexcept
Definition serializableobjectlist.h:255
SerializableObjectList(const SExpression &node)
Definition serializableobjectlist.h:186
SerializableObjectList< T, P, OnEditedArgs... > & operator=(const SerializableObjectList< T, P, OnEditedArgs... > &rhs) noexcept
Definition serializableobjectlist.h:427
bool isEmpty() const noexcept
Definition serializableobjectlist.h:198
std::shared_ptr< const T > first() const noexcept
Definition serializableobjectlist.h:280
int insert(int index, const std::shared_ptr< T > &obj) noexcept
Definition serializableobjectlist.h:336
int indexOf(const T *obj) const noexcept
Definition serializableobjectlist.h:219
bool contains(const T *obj) const noexcept
Definition serializableobjectlist.h:247
void remove(const T *obj) noexcept
Definition serializableobjectlist.h:363
void append(SerializableObjectList &list) noexcept
Definition serializableobjectlist.h:345
std::shared_ptr< T > take(const QString &name) noexcept
Definition serializableobjectlist.h:359
std::shared_ptr< T > takeElement(int index) noexcept
Definition serializableobjectlist.h:454
const_iterator cbegin() noexcept
Definition serializableobjectlist.h:312
Iterator< typename QVector< std::shared_ptr< T > >::iterator, T > iterator
Definition serializableobjectlist.h:121
Slot< T, OnEditedArgs... > mOnEditedSlot
Definition serializableobjectlist.h:507
Iterator< typename QVector< std::shared_ptr< T > >::const_iterator, const T > const_iterator
Definition serializableobjectlist.h:123
SerializableObjectList(const SerializableObjectList< T, P, OnEditedArgs... > &other) noexcept
Definition serializableobjectlist.h:152
bool operator==(const SerializableObjectList< T, P, OnEditedArgs... > &rhs) const noexcept
Definition serializableobjectlist.h:415
std::shared_ptr< T > take(const T *obj) noexcept
Definition serializableobjectlist.h:355
void insertElement(int index, const std::shared_ptr< T > &obj) noexcept
Definition serializableobjectlist.h:449
iterator end() noexcept
Definition serializableobjectlist.h:315
std::shared_ptr< T > get(const T *obj)
Definition serializableobjectlist.h:283
std::shared_ptr< const T > get(const QString &name) const
Definition serializableobjectlist.h:303
The Signal class is used to emit signals on non-QObject derived classes.
Definition signalslot.h:65
The Slot class is used to receive signals from non-QObject derived classes.
Definition signalslot.h:170
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition uuid.h:56
QString toStr() const noexcept
Get the UUID as a string (without braces)
Definition uuid.h:91
Definition serializableobjectlist.h:517
Definition occmodel.cpp:76