Loading...
Searching...
No Matches
piercedSync.hpp
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#ifndef __BITPIT_PIERCED_SYNC_HPP__
26#define __BITPIT_PIERCED_SYNC_HPP__
27
28#include <array>
29#include <memory>
30#include <unordered_map>
31#include <vector>
32
33namespace bitpit {
34
41
42public:
47 TYPE_UNDEFINED = -1,
48 TYPE_NOOP,
49 TYPE_CLEAR,
50 TYPE_RESERVE,
51 TYPE_RESIZE,
52 TYPE_SHRINK_TO_FIT,
53 TYPE_REORDER,
54 TYPE_APPEND,
55 TYPE_INSERT,
56 TYPE_OVERWRITE,
57 TYPE_OVERWRITE_MULTIPLE,
58 TYPE_MOVE_APPEND,
59 TYPE_MOVE_INSERT,
60 TYPE_MOVE_OVERWRITE,
61 TYPE_SWAP,
62 TYPE_PIERCE,
63 TYPE_PIERCE_MULTIPLE
64 };
65
70 INFO_POS = 0,
71 INFO_POS_FIRST = 0,
72 INFO_POS_SECOND = 1,
73 INFO_POS_NEXT = 1,
74 INFO_SIZE = 1,
75 INFO_COUNT = 2
76 };
77
78 PiercedSyncAction(ActionType _type = TYPE_UNDEFINED);
80
83
84 void swap(PiercedSyncAction &other) noexcept;
85
86 void importData(std::vector<std::size_t> &&values);
87 void importData(const std::vector<std::size_t> &values);
88
89 // Dump and restore
90 void restore(std::istream &stream);
91 void dump(std::ostream &stream) const;
92
93 // Data
94 ActionType type;
95 std::array<std::size_t, INFO_COUNT> info;
96 std::unique_ptr<std::vector<std::size_t>> data;
97
98};
99
107
108friend class PiercedSyncMaster;
109
110public:
111 virtual ~PiercedSyncSlave() = default;
112
113protected:
115
116 void swap(PiercedSyncSlave &other) noexcept;
117
118 virtual void commitSyncAction(const PiercedSyncAction &action) = 0;
119
120};
121
129
130public:
134 typedef std::vector<PiercedSyncSlave *> SyncGroup;
135
139 enum SyncMode {
140 SYNC_MODE_CONCURRENT,
141 SYNC_MODE_JOURNALED,
142 SYNC_MODE_DISABLED,
143 SYNC_MODE_ITR_COUNT = SYNC_MODE_DISABLED + 1,
144 SYNC_MODE_ITR_BEGIN = 0,
145 SYNC_MODE_ITR_END = SYNC_MODE_ITR_BEGIN + SYNC_MODE_ITR_COUNT
146 };
147
148protected:
153 {
154 template <typename T>
155 std::size_t operator()(T t) const
156 {
157 return static_cast<std::size_t>(t);
158 }
159 };
160
164 mutable std::unordered_map<PiercedSyncSlave *, SyncMode> m_slaves;
165
167
169 void unregisterSlave(const PiercedSyncSlave *slave) const;
170 bool isSlaveRegistered(const PiercedSyncSlave *slave) const;
172 bool isSlaveSynced(const PiercedSyncSlave *slave) const;
173
174 void setSyncEnabled(bool enabled) const;
175 bool isSyncEnabled() const;
176
177 void sync();
178 bool isSynced() const;
179
180 void swap(PiercedSyncMaster &other) noexcept;
181
182 void processSyncAction(const PiercedSyncAction &action);
183
184 // Dump and restore
185 void restore(std::istream &stream);
186 void dump(std::ostream &stream) const;
187
188private:
192 mutable std::unordered_map<SyncMode, SyncGroup, SyncModeHasher> m_syncGroups;
193
197 std::vector<PiercedSyncAction> m_syncJournal;
198
202 mutable bool m_syncEnabled;
203
204 void commitSyncAction(PiercedSyncSlave *slave, const PiercedSyncAction &action) const;
205 void journalSyncAction(const PiercedSyncAction &action);
206
207};
208
209}
210
211#endif
Action for pierced synchronization.
PiercedSyncAction(ActionType _type=TYPE_UNDEFINED)
void restore(std::istream &stream)
void swap(PiercedSyncAction &other) noexcept
void importData(std::vector< std::size_t > &&values)
void dump(std::ostream &stream) const
PiercedSyncAction & operator=(const PiercedSyncAction &other)
Base class for defining an object that acts like a master in pierced synchronization.
void restore(std::istream &stream)
PiercedSyncMaster::SyncMode getSlaveSyncMode(const PiercedSyncSlave *slave) const
void setSyncEnabled(bool enabled) const
std::vector< PiercedSyncSlave * > SyncGroup
void unregisterSlave(const PiercedSyncSlave *slave) const
void swap(PiercedSyncMaster &other) noexcept
bool isSlaveRegistered(const PiercedSyncSlave *slave) const
void registerSlave(PiercedSyncSlave *slave, PiercedSyncMaster::SyncMode syncMode) const
void processSyncAction(const PiercedSyncAction &action)
bool isSlaveSynced(const PiercedSyncSlave *slave) const
void dump(std::ostream &stream) const
std::unordered_map< PiercedSyncSlave *, SyncMode > m_slaves
Base class for defining an object that acts like a slave in pierced synchronization.
void swap(PiercedSyncSlave &other) noexcept
--- layout: doxygen_footer ---