LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
serializableobject.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_SERIALIZABLEOBJECT_H
21 #define LIBREPCB_SERIALIZABLEOBJECT_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "sexpression.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class SerializableObject
37  ******************************************************************************/
38 
47 public:
48  SerializableObject() noexcept {}
49  virtual ~SerializableObject() noexcept {}
50 
66  SExpression serializeToDomElement(const QString& name) const {
68  serialize(root);
69  return root;
70  }
71 
87  virtual void serialize(SExpression& root) const = 0;
88 
89  template <typename T>
90  static void serializeObjectContainer(SExpression& root, const T& container,
91  const QString& itemName) {
92  for (const auto& object : container) {
93  root.appendChild(object.serializeToDomElement(itemName),
94  true); // can throw
95  }
96  }
97 
98  template <typename T>
99  static void serializePointerContainer(SExpression& root, const T& container,
100  const QString& itemName) {
101  for (const auto& pointer : container) {
102  root.appendChild(pointer->serializeToDomElement(itemName),
103  true); // can throw
104  }
105  }
106 
107  template <typename T>
109  const T& container,
110  const QString& itemName) {
111  T copy = container;
112  std::sort(
113  copy.begin(), copy.end(),
114  [](const typename T::value_type& a, const typename T::value_type& b) {
115  return a->getUuid() < b->getUuid();
116  });
117  serializePointerContainer(root, copy, itemName);
118  }
119 };
120 
121 // Make sure that the SerializableObject class does not contain any data (except
122 // the vptr). Otherwise it could introduce issues when using multiple
123 // inheritance.
124 static_assert(sizeof(SerializableObject) == sizeof(void*),
125  "SerializableObject must not contain any data!");
126 
127 /*******************************************************************************
128  * End of File
129  ******************************************************************************/
130 
131 } // namespace librepcb
132 
133 #endif // LIBREPCB_SERIALIZABLEOBJECT_H
static void serializePointerContainer(SExpression &root, const T &container, const QString &itemName)
Definition: serializableobject.h:99
SExpression & appendChild(const SExpression &child, bool linebreak)
Definition: sexpression.cpp:173
virtual void serialize(SExpression &root) const =0
Serialize the object into an existing S-Expression node.
static void serializePointerContainerUuidSorted(SExpression &root, const T &container, const QString &itemName)
Definition: serializableobject.h:108
static SExpression createList(const QString &name)
Definition: sexpression.cpp:276
The SerializableObject class is the base class for all classes which need to be serializable/deserial...
Definition: serializableobject.h:46
static void serializeObjectContainer(SExpression &root, const T &container, const QString &itemName)
Definition: serializableobject.h:90
SerializableObject() noexcept
Definition: serializableobject.h:48
SExpression serializeToDomElement(const QString &name) const
Serialize the object to a new S-Expression node.
Definition: serializableobject.h:66
virtual ~SerializableObject() noexcept
Definition: serializableobject.h:49
The SExpression class.
Definition: sexpression.h:60