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 
44 public:
45  SerializableObject() noexcept {}
46  virtual ~SerializableObject() noexcept {}
47 
63  SExpression serializeToDomElement(const QString& name) const {
65  serialize(root);
66  return root;
67  }
68 
84  virtual void serialize(SExpression& root) const = 0;
85 
86  template <typename T>
87  static void serializeObjectContainer(SExpression& root, const T& container,
88  const QString& itemName) {
89  for (const auto& object : container) {
90  root.appendChild(object.serializeToDomElement(itemName),
91  true); // can throw
92  }
93  }
94 
95  template <typename T>
96  static void serializePointerContainer(SExpression& root, const T& container,
97  const QString& itemName) {
98  for (const auto& pointer : container) {
99  root.appendChild(pointer->serializeToDomElement(itemName),
100  true); // can throw
101  }
102  }
103 
104  template <typename T>
106  const T& container,
107  const QString& itemName) {
108  T copy = container;
109  std::sort(
110  copy.begin(), copy.end(),
111  [](const typename T::value_type& a, const typename T::value_type& b) {
112  return a->getUuid() < b->getUuid();
113  });
114  serializePointerContainer(root, copy, itemName);
115  }
116 };
117 
118 // Make sure that the SerializableObject class does not contain any data (except
119 // the vptr). Otherwise it could introduce issues when using multiple
120 // inheritance.
121 static_assert(sizeof(SerializableObject) == sizeof(void*),
122  "SerializableObject must not contain any data!");
123 
124 /*******************************************************************************
125  * End of File
126  ******************************************************************************/
127 
128 } // namespace librepcb
129 
130 #endif // LIBREPCB_SERIALIZABLEOBJECT_H
static void serializePointerContainer(SExpression &root, const T &container, const QString &itemName)
Definition: serializableobject.h:96
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:105
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:43
static void serializeObjectContainer(SExpression &root, const T &container, const QString &itemName)
Definition: serializableobject.h:87
SerializableObject() noexcept
Definition: serializableobject.h:45
SExpression serializeToDomElement(const QString &name) const
Serialize the object to a new S-Expression node.
Definition: serializableobject.h:63
virtual ~SerializableObject() noexcept
Definition: serializableobject.h:46
The SExpression class.
Definition: sexpression.h:57