LibrePCB Developers Documentation
workspacesettingsitem_genericvalue.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_CORE_WORKSPACESETTINGSITEM_GENERICVALUE_H
21 #define LIBREPCB_CORE_WORKSPACESETTINGSITEM_GENERICVALUE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "workspacesettingsitem.h"
27 
28 #include <QtCore>
29 
30 /*******************************************************************************
31  * Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  * Class WorkspaceSettingsItem_GenericValue
37  ******************************************************************************/
38 
43 template <typename T>
45 public:
46  // Constructors / Destructor
49  const WorkspaceSettingsItem_GenericValue& other) = delete;
51  const QString& key, const T& defaultValue,
52  QObject* parent = nullptr) noexcept
53  : WorkspaceSettingsItem(key, parent),
54  mDefaultValue(defaultValue),
55  mCurrentValue(defaultValue) {}
57 
63  const T& get() const noexcept { return mCurrentValue; }
64 
70  void set(const T& value) noexcept {
71  if (value != mCurrentValue) {
72  mCurrentValue = value;
73  valueModified();
74  }
75  }
76 
82  const T& getDefault() const noexcept { return mDefaultValue; }
83 
84  // Operator Overloadings
86  const WorkspaceSettingsItem_GenericValue& rhs) = delete;
87 
88 private: // Methods
92  virtual void restoreDefaultImpl() noexcept override { set(mDefaultValue); }
93 
97  void loadImpl(const SExpression& root) override {
98  set(deserialize<T>(root.getChild("@0"))); // can throw
99  }
100 
104  void serializeImpl(SExpression& root) const override {
106  }
107 
108 private:
111 };
112 
113 /*******************************************************************************
114  * End of File
115  ******************************************************************************/
116 
117 } // namespace librepcb
118 
119 #endif
WorkspaceSettingsItem_GenericValue(const QString &key, const T &defaultValue, QObject *parent=nullptr) noexcept
Definition: workspacesettingsitem_genericvalue.h:50
Generic implementation of librepcb::WorkspaceSettingsItem for simple, value-type settings.
Definition: workspacesettingsitem_genericvalue.h:44
const T & getDefault() const noexcept
Get the default value.
Definition: workspacesettingsitem_genericvalue.h:82
virtual void restoreDefaultImpl() noexcept override
Restore default value.
Definition: workspacesettingsitem_genericvalue.h:92
Definition: occmodel.cpp:76
void valueModified() noexcept
Definition: workspacesettingsitem.cpp:48
T mDefaultValue
Initial, default value.
Definition: workspacesettingsitem_genericvalue.h:109
SExpression & getChild(const QString &path)
Get a child by path.
Definition: sexpression.cpp:120
void serializeImpl(SExpression &root) const override
Serialize the value into S-Expression node.
Definition: workspacesettingsitem_genericvalue.h:104
T mCurrentValue
Current value.
Definition: workspacesettingsitem_genericvalue.h:110
SExpression & appendChild(const SExpression &child)
Definition: sexpression.cpp:201
~WorkspaceSettingsItem_GenericValue() noexcept
Definition: workspacesettingsitem_genericvalue.h:56
void loadImpl(const SExpression &root) override
Load value from S-Expression node.
Definition: workspacesettingsitem_genericvalue.h:97
Base class for all workspace settings items.
Definition: workspacesettingsitem.h:49
WorkspaceSettingsItem_GenericValue & operator=(const WorkspaceSettingsItem_GenericValue &rhs)=delete
The SExpression class.
Definition: sexpression.h:66