Debian Codegraph @

Storage/StorageHandler.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <vector>
24 #include <map>
25
26 #include "DataModel/DataModel.h"
27 #include "DataTypes.h"
28 #include "Hardware/HardwareParams.h"
29 #include "Storage/Sqlite.h"
30 #include "Storage/StorageInterface.h"
31 #include "Storage/TransactionGuard.h"
32 #include "Storage/StorageParams.h"
33
34 namespace Storage
35 {
36 class StorageHandler
37 {
38 public:
39 inline StorageHandler(Manager* manager, const StorageParams* params)
40 : manager(manager),
41 sqlite(params)
42 {
43 }
44
45 inline ~StorageHandler()
46 {
47 }
48
49 inline void AllHardwareParams(std::map<ControlID,Hardware::HardwareParams*>& hardwareParams)
50 {
51 sqlite.AllHardwareParams(hardwareParams);
52 }
53
54 inline void DeleteHardwareParams(const ControlID controlID)
55 {
56 sqlite.DeleteHardwareParams(controlID);
57 }
58
59 void AllLocos(std::map<LocoID,DataModel::Loco*>& locos);
60 void DeleteLoco(LocoID locoID);
61 void AllMultipleUnits(std::map<LocoID,DataModel::MultipleUnit*>& multipleUnits);
62 void DeleteMultipleUnit(MultipleUnitID multipleUnitID);
63 void AllAccessories(std::map<AccessoryID,DataModel::Accessory*>& accessories);
64
65 inline void DeleteAccessory(AccessoryID accessoryID)
66 {
67 sqlite.DeleteObject(ObjectTypeAccessory, accessoryID);
68 }
69
70 void AllFeedbacks(std::map<FeedbackID,DataModel::Feedback*>& feedbacks);
71
72 inline void DeleteFeedback(FeedbackID feedbackID)
73 {
74 sqlite.DeleteObject(ObjectTypeFeedback, feedbackID);
75 }
76
77 void AllTracks(std::map<TrackID,DataModel::Track*>& tracks);
78 void DeleteTrack(TrackID trackID);
79 void AllSwitches(std::map<SwitchID,DataModel::Switch*>& switches);
80
81 inline void DeleteSwitch(SwitchID switchID)
82 {
83 sqlite.DeleteObject(ObjectTypeSwitch, switchID);
84 }
85
86 void AllRoutes(std::map<RouteID,DataModel::Route*>& routes);
87 void DeleteRoute(RouteID routeID);
88 void AllLayers(std::map<LayerID,DataModel::Layer*>& layers);
89
90 inline void DeleteLayer(LayerID layerID)
91 {
92 sqlite.DeleteObject(ObjectTypeLayer, layerID);
93 }
94
95 void AllSignals(std::map<SignalID,DataModel::Signal*>& signals);
96 void DeleteSignal(SignalID signalID);
97 void AllClusters(std::map<ClusterID,DataModel::Cluster*>& clusters);
98
99 inline void DeleteCluster(ClusterID clusterID)
100 {
101 sqlite.DeleteObject(ObjectTypeCluster, clusterID);
102 }
103
104 void AllTexts(std::map<TextID,DataModel::Text*>& texts);
105
106 inline void DeleteText(TextID textID)
107 {
108 sqlite.DeleteObject(ObjectTypeText, textID);
109 }
110
•111 void AllCounters(std::map<CounterID,DataModel::Counter*>& counters);
112
113 inline void DeleteCounter(CounterID counterID)
114 {
115 sqlite.DeleteObject(ObjectTypeCounter, counterID);
116 }
117
118 inline void Save(const Hardware::HardwareParams& hardwareParams)
119 {
120 sqlite.SaveHardwareParams(hardwareParams);
121 }
122
123 void Save(const DataModel::Route& route);
124 void Save(const DataModel::Loco& loco);
125 void Save(const DataModel::MultipleUnit& multipleUnit);
126 void Save(const DataModel::Cluster& cluster);
127 void Save(const DataModel::Track& track);
128
129 template<class T> void Save(const T& t)
130 {
131 const std::string serialized = t.Serialize();
132 sqlite.SaveObject(t.GetObjectType(), t.GetID(), t.GetName(), serialized);
133 }
134
135 template <class T> static void Save(StorageHandler* storageHandler, const T* t)
136 {
137 storageHandler->Save(*t);
138 }
139
140 inline void SaveSetting(const std::string& key, const std::string& value)
141 {
142 sqlite.SaveSetting(key, value);
143 }
144
145
146 inline std::string GetSetting(const std::string& key)
147 {
148 return sqlite.GetSetting(key);
149 }
150
151 inline void StartTransaction()
152 {
153 transactionMutex.lock();
154 sqlite.StartTransaction();
155 }
156
157 inline void CommitTransaction()
158 {
159 sqlite.CommitTransaction();
160 transactionMutex.unlock();
161 }
162
163 private:
164 void SaveRelations(const std::vector<DataModel::Relation*> relations);
165 std::vector<DataModel::Relation*> RelationsFrom(const DataModel::Relation::RelationType type, const ObjectID objectID);
166
167 Manager* manager;
168 Storage::SQLite sqlite;
169 std::mutex transactionMutex;
170 };
171 } // namespace Storage
172