Loading...
Searching...
No Matches
patch_manager.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include "patch_kernel.hpp"
26#include "patch_manager.hpp"
27
28namespace bitpit {
29
37const int PatchManager::AUTOMATIC_ID = IndexGenerator<int>::NULL_ID;
38
39/*
40 Initialize logger manager instance.
41*/
42std::unique_ptr<PatchManager> PatchManager::m_manager = nullptr;
43
50{
51 if (!m_manager) {
52 m_manager = std::unique_ptr<PatchManager>(new PatchManager());
53 }
54
55 return *m_manager;
56}
57
64{
65 for (const auto &entry : m_patchIds) {
66 long patchId = entry.second;
67 if (patchId == id) {
68 return entry.first;
69 }
70 }
71
72 return nullptr;
73}
74
82int PatchManager::registerPatch(PatchKernel *patch, int id)
83{
84 if (id >= 0) {
85 if (m_idGenerator.isAssigned(id)) {
86 throw std::runtime_error ("A patch with the same id already exists");
87 }
88
89 m_idGenerator.setAssigned(id);
90 } else {
91 id = m_idGenerator.generate();
92 }
93
94 patch->_setId(id);
95 m_patchIds[patch] = id;
96 m_patchOrder.push_back(patch);
97
98 return id;
99}
100
106void PatchManager::unregisterPatch(PatchKernel *patch)
107{
108 auto iterator = m_patchIds.find(patch);
109 if (iterator == m_patchIds.end()) {
110 throw std::runtime_error ("The patch to be unregistered does not exist");
111 }
112
113 int id = iterator->second;
114 m_idGenerator.trash(id);
115
116 m_patchIds.erase(iterator);
117 for (auto itr = m_patchOrder.begin(); itr != m_patchOrder.end(); ++itr) {
118 if (*itr == patch) {
119 m_patchOrder.erase(itr);
120 break;
121 }
122 }
123}
124
128PatchManager::PatchManager()
129{
130}
131
137void PatchManager::dump(std::ostream &stream)
138{
139 m_idGenerator.dump(stream);
140}
141
147void PatchManager::restore(std::istream &stream)
148{
149 m_idGenerator.restore(stream);
150}
151
158void PatchManager::dumpAll(std::ostream &stream)
159{
160 for (PatchKernel *patch : m_patchOrder) {
161 patch->dump(stream);
162 }
163
164 dump(stream);
165}
166
173void PatchManager::restoreAll(std::istream &stream)
174{
175 m_idGenerator.reset();
176
177 for (PatchKernel *patch : m_patchOrder) {
178 patch->restore(stream);
179 }
180
181 restore(stream);
182}
183
184// Patch manager global functions
185namespace patch {
186
187 // Generic global functions
188
195 {
196 return PatchManager::manager();
197 }
198
199}
200
201}
bool isAssigned(id_type id)
void setAssigned(id_type id)
void restore(std::istream &stream)
void dump(std::ostream &stream) const
The PatchKernel class provides an interface for defining patches.
The PatchManager oversee the handling of the patches.
void dumpAll(std::ostream &stream)
void restoreAll(std::istream &stream)
void dump(std::ostream &stream)
PatchKernel * get(int id)
static PatchManager & manager()
void restore(std::istream &stream)
PatchManager & manager()
--- layout: doxygen_footer ---