Loading...
Searching...
No Matches
piercedStorage.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_STORAGE_HPP__
26#define __BITPIT_PIERCED_STORAGE_HPP__
27
28#include <vector>
29
30#include "piercedStorageRange.hpp"
31#include "piercedStorageIterator.hpp"
32#include "piercedKernel.hpp"
33#include "piercedSync.hpp"
34
35#define __PS_REFERENCE__ typename PiercedStorage<value_t, id_t>::reference
36#define __PS_CONST_REFERENCE__ typename PiercedStorage<value_t, id_t>::const_reference
37#define __PS_POINTER__ typename PiercedStorage<value_t, id_t>::pointer
38#define __PS_CONST_POINTER__ typename PiercedStorage<value_t, id_t>::const_pointer
39
40namespace bitpit {
41
49
50public:
51 virtual ~BasePiercedStorage() = default;
52
53protected:
54 BasePiercedStorage() = default;
55
56};
57
65template<typename id_t = long>
67
68public:
69 // Template typedef
70
74 template<typename PK_id_t>
76
77 // Typedefs
78
83
88
89 // Enums
90 enum KernelType {
91 KERNEL_NONE = -1,
92 KERNEL_STATIC = 0,
93 KERNEL_DYNAMIC = 1
94 };
95
96 // Virtual destructor
98
99 // Methods for synchronizing the storage
100 void setStaticKernel(const PiercedKernel<id_t> *kernel);
102 void unsetKernel(bool release = true);
103 const PiercedKernel<id_t> * getKernel() const;
104 KernelType getKernelType() const;
106
107 // Methods that modify the container as a whole
108 void swap(PiercedStorageSyncSlave<id_t> &other) noexcept;
109
110protected:
111 const PiercedKernel<id_t> *m_kernel;
112 KernelType m_kernelType;
113
114 // Constructors and initialization
124
125 // Methods for synchronizing the storage
126 virtual void _postSetStaticKernel();
127 virtual void _postSetDynamicKernel();
128 virtual void _postUnsetKernel(bool release = true);
129 void detachKernel();
130
131};
132
145template<typename value_t, typename id_t = long>
147
148// Friendships
149template<typename PI_value_t, typename PI_id_t, typename PI_value_no_cv_t>
150friend class PiercedStorageIterator;
151
152private:
156 template <typename T>
157 class check_restore
158 {
159
160 private:
161 template<typename class_t>
162 static std::true_type test_restore(decltype(std::declval<class_t>().restore(std::declval<std::istream &>())) *);
163
164 template<typename class_t>
165 static std::false_type test_restore(...);
166
167 template<typename class_t>
168 static constexpr bool has_restore()
169 {
170 return std::is_same<decltype(test_restore<class_t>(nullptr)), std::true_type>();
171 }
172
173 public:
174 static const bool value = has_restore<T>();
175
176 };
177
181 template <typename T>
182 class check_dump
183 {
184
185 private:
186 template<typename class_t>
187 static std::true_type test_dump(decltype(std::declval<class_t>().dump(std::declval<std::ostream &>())) *);
188
189 template<typename class_t>
190 static std::false_type test_dump(...);
191
192 template<typename class_t>
193 static constexpr bool has_dump()
194 {
195 return std::is_same<decltype(test_dump<class_t>(nullptr)), std::true_type>();
196 }
197
198 public:
199 static const bool value = has_dump<T>();
200
201 };
202
206 template <typename T, typename Ret, typename... Args>
207 class check_initialize
208 {
209
210 private:
211 template<typename C>
212 static constexpr auto test_initialize(C *)
213 -> typename std::is_same<decltype(std::declval<C>().initialize(std::declval<Args>()...)), Ret>::type;
214
215 template<typename class_t>
216 static constexpr auto test_initialize(...)
217 -> std::false_type;
218
219 public:
220 static const bool value = std::is_same<decltype(test_initialize<T>(nullptr)), std::true_type>();
221
222 };
223
224public:
225 // Template typedef
226
230 template<typename PK_id_t>
232
233 // Typedefs
234
238 typedef value_t value_type;
239
244
249
253 typedef typename PiercedStorageSyncSlave<id_t>::KernelType KernelType;
254
258 typedef std::vector<value_t> container_t;
259
263 typedef typename container_t::reference reference;
264
268 typedef typename container_t::const_reference const_reference;
269
273 typedef typename container_t::pointer pointer;
274
278 typedef typename container_t::const_pointer const_pointer;
279
284
289
293 typedef typename std::vector<value_t>::iterator raw_iterator;
294
298 typedef typename std::vector<value_t>::const_iterator raw_const_iterator;
299
304
309
313 static constexpr bool has_restore()
314 {
315 return check_restore<value_t>::value;
316 };
317
321 static constexpr bool has_dump()
322 {
323 return check_dump<value_t>::value;
324 };
325
329 template<typename... Args>
330 static constexpr bool has_initialize()
331 {
332 return check_initialize<value_t, void, Args...>::value;
333 }
334
340 template <typename... Args>
341 using EnableIfHasInitialize = typename std::enable_if<PiercedStorage<value_t, id_t>::template has_initialize<Args...>()>::type;
342
343 // Constructors and initialization
345 PiercedStorage(std::size_t nFields);
346 PiercedStorage(std::size_t nFields, const PiercedKernel<id_t> *kernel);
347 PiercedStorage(std::size_t nFields, const PiercedKernel<id_t> *kernel, PiercedSyncMaster::SyncMode syncMode);
354
357
358 // Methods for accessing container properties
359 std::size_t getFieldCount() const;
360
361 // Methods that modify the container as a whole
362 void swap(PiercedStorage &other) noexcept;
363 void fill(const value_t &value);
364
365 // Methos to access data stored in the container
366 __PS_POINTER__ data();
367 __PS_CONST_POINTER__ data() const;
368
369 __PS_POINTER__ data(id_t id, std::size_t offset = 0);
370 __PS_CONST_POINTER__ data(id_t id, std::size_t offset = 0) const;
371
372 __PS_POINTER__ rawData(std::size_t pos, std::size_t offset = 0);
373 __PS_CONST_POINTER__ rawData(std::size_t pos, std::size_t offset = 0) const;
374
375 // Methods for editing the items using their id
376 __PS_REFERENCE__ back(std::size_t k = 0);
377 __PS_CONST_REFERENCE__ back(std::size_t k = 0) const;
378
379 __PS_REFERENCE__ front(std::size_t k = 0);
380 __PS_CONST_REFERENCE__ front(std::size_t k = 0) const;
381
382 __PS_REFERENCE__ at(id_t id, std::size_t k = 0);
383 __PS_CONST_REFERENCE__ at(id_t id, std::size_t k = 0) const;
384
385 __PS_REFERENCE__ operator[](id_t id);
386 __PS_CONST_REFERENCE__ operator[](id_t id) const;
387
388 void copy(id_t id, value_t *values) const;
389 void copy(id_t id, std::size_t nFields, std::size_t offset, value_t *values) const;
390
391 void set(id_t id, const value_t &value);
392 void set(id_t id, std::size_t k, const value_t &value);
393 void set(id_t id, const value_t *values);
394 void set(id_t id, std::size_t nFields, std::size_t offset, const value_t *values);
395
396 // Methods for editing the items using their position
397 __PS_REFERENCE__ rawAt(std::size_t pos, std::size_t offset = 0);
398 __PS_CONST_REFERENCE__ rawAt(std::size_t pos, std::size_t offset = 0) const;
399
400 void rawCopy(std::size_t pos, value_t *values) const;
401 void rawCopy(std::size_t pos, std::size_t nFields, std::size_t offset, value_t *values) const;
402
403 void rawSet(std::size_t pos, const value_t &value);
404 void rawSet(std::size_t pos, std::size_t k, const value_t &value);
405 void rawSet(std::size_t pos, const value_t *values);
406 void rawSet(std::size_t pos, std::size_t nFields, std::size_t offset, const value_t *values);
407
408 // Iterators
409 iterator find(const id_t &id) noexcept;
410 const_iterator find(const id_t &id) const noexcept;
411
412 iterator rawFind(std::size_t pos) noexcept;
413 const_iterator rawFind(std::size_t pos) const noexcept;
414
415 iterator begin() noexcept;
416 iterator end() noexcept;
417 const_iterator begin() const noexcept;
418 const_iterator end() const noexcept;
419 const_iterator cbegin() const noexcept;
420 const_iterator cend() const noexcept;
421
422 raw_iterator rawBegin() noexcept;
423 raw_iterator rawEnd() noexcept;
424 raw_const_iterator rawBegin() const noexcept;
425 raw_const_iterator rawEnd() const noexcept;
426 raw_const_iterator rawCbegin() const noexcept;
427 raw_const_iterator rawCend() const noexcept;
428
429 // Dump and restore
430 template<typename T = value_t, typename std::enable_if<std::is_pod<T>::value || PiercedStorage<T, id_t>::has_restore()>::type * = nullptr>
431 void restore(std::istream &stream);
432
433 template<typename T = value_t, typename std::enable_if<std::is_pod<T>::value || PiercedStorage<T, id_t>::has_dump()>::type * = nullptr>
434 void dump(std::ostream &stream) const;
435
436protected:
437 // Methods for synchronizing the storage
438 void _postSetStaticKernel() override;
439 void _postSetDynamicKernel() override;
440 void _postUnsetKernel(bool release = true) override;
441
442 // Methos for getting information on the storage
443 std::size_t rawSize() const;
444
445 // Methods for synchronizing the storage
446 void commitSyncAction(const PiercedSyncAction &action) override;
447
448 // Methos for updating the storage
449 void rawReserve(std::size_t n);
450 void rawShrinkToFit();
451
452 void rawClear(bool release);
453 void rawErase(std::size_t pos, std::size_t n);
454
455 template<typename T = value_t, typename std::enable_if<!std::is_same<T, bool>::value>::type * = nullptr>
456 void rawSwap(std::size_t pos_first, std::size_t pos_second);
457 template<typename T = value_t, typename std::enable_if<std::is_same<T, bool>::value>::type * = nullptr>
458 void rawSwap(std::size_t pos_first, std::size_t pos_second);
459 void rawReorder(const std::vector<std::size_t> &permutations);
460
461 void rawResize(std::size_t n, const value_t &value = value_t());
462
463 template<typename... Args, typename PiercedStorage<value_t>::template EnableIfHasInitialize<Args...> * = nullptr>
464 void rawInitialize(std::size_t pos, Args&&... args);
465 template<typename... Args, typename PiercedStorage<value_t>::template EnableIfHasInitialize<Args...> * = nullptr>
466 void rawInitialize(std::size_t pos, std::size_t k, Args&&... args);
467
468 void rawInsert(std::size_t pos, std::size_t n, const value_t &value);
469 void rawPushBack(const value_t &value);
470 template<typename T = value_t, typename std::enable_if<!std::is_same<T, bool>::value>::type * = nullptr, typename... Args>
471 void rawEmplace(std::size_t pos, Args&&... args);
472 template<typename T = value_t, typename std::enable_if<std::is_same<T, bool>::value>::type * = nullptr>
473 void rawEmplace(std::size_t pos, bool value = false);
474 template<typename T = value_t, typename std::enable_if<!std::is_same<T, bool>::value>::type * = nullptr, typename... Args>
475 void rawEmplaceBack(Args&&... args);
476 template<typename T = value_t, typename std::enable_if<std::is_same<T, bool>::value>::type * = nullptr>
477 void rawEmplaceBack(bool value = false);
478 template<typename... Args>
479 void rawEmreplace(std::size_t pos, Args&&... args);
480
481private:
482 std::size_t m_nFields;
483 container_t m_fields;
484
485 void restoreField(std::istream &stream, std::vector<bool>::reference value);
486
487 template<typename T = value_t, typename std::enable_if<!PiercedStorage<T, id_t>::has_restore()>::type * = nullptr>
488 void restoreField(std::istream &stream, value_t &value);
489
490 template<typename T = value_t, typename std::enable_if<PiercedStorage<T, id_t>::has_restore()>::type * = nullptr>
491 void restoreField(std::istream &stream, value_t &object);
492
493 void dumpField(std::ostream &stream, std::vector<bool>::const_reference value) const;
494
495 template<typename T = value_t, typename std::enable_if<!PiercedStorage<T, id_t>::has_dump()>::type * = nullptr>
496 void dumpField(std::ostream &stream, const value_t &value) const;
497
498 template<typename T = value_t, typename std::enable_if<PiercedStorage<T, id_t>::has_dump()>::type * = nullptr>
499 void dumpField(std::ostream &stream, const value_t &object) const;
500
501};
502
503}
504
505// Templates
506#include "piercedStorage.tpp"
507
508#endif
Base class for the pierced storages.
Metafunction for generating a pierced kernel.
Iterator for the class PiercedStorage.
The PiercedStorageRange allow to iterate using range-based loops over a PiercedStorage.
Base class for defining storages that acts like a slave in pierced synchronization.
void setStaticKernel(const PiercedKernel< id_t > *kernel)
const PiercedKernel< id_t > * getKernel() const
PiercedSyncMaster::SyncMode getSyncMode() const
void swap(PiercedStorageSyncSlave< id_t > &other) noexcept
virtual void _postUnsetKernel(bool release=true)
void setDynamicKernel(const PiercedKernel< id_t > *kernel, PiercedSyncMaster::SyncMode syncMode)
void unsetKernel(bool release=true)
Kernel< id_t >::id_type id_type
Metafunction for generating a pierced storage.
raw_iterator rawBegin() noexcept
raw_iterator rawEnd() noexcept
void restore(std::istream &stream)
PiercedStorageRange< value_t, id_t > range
std::vector< value_t >::const_iterator raw_const_iterator
const_iterator cend() const noexcept
std::vector< value_t >::iterator raw_iterator
Kernel< id_t >::id_type id_type
void copy(id_t id, value_t *values) const
void rawClear(bool release)
std::size_t getFieldCount() const
void rawReserve(std::size_t n)
typename std::enable_if< PiercedStorage< value_t, id_t >::template has_initialize< Args... >()>::type EnableIfHasInitialize
PiercedStorageIterator< value_t, id_t > iterator
void set(id_t id, const value_t &value)
void rawInitialize(std::size_t pos, Args &&... args)
void swap(PiercedStorage &other) noexcept
std::vector< value_t > container_t
__PS_REFERENCE__ at(id_t id, std::size_t k=0)
void commitSyncAction(const PiercedSyncAction &action) override
void rawSwap(std::size_t pos_first, std::size_t pos_second)
void _postSetDynamicKernel() override
std::size_t rawSize() const
__PS_REFERENCE__ rawAt(std::size_t pos, std::size_t offset=0)
container_t::reference reference
PiercedStorageRange< const value_t, id_t > const_range
iterator end() noexcept
static constexpr bool has_initialize()
static constexpr bool has_dump()
void rawPushBack(const value_t &value)
iterator rawFind(std::size_t pos) noexcept
void rawEmreplace(std::size_t pos, Args &&... args)
void rawCopy(std::size_t pos, value_t *values) const
void rawErase(std::size_t pos, std::size_t n)
typename PiercedStorageSyncSlave< id_t >::template Kernel< PK_id_t > Kernel
container_t::const_reference const_reference
__PS_POINTER__ rawData(std::size_t pos, std::size_t offset=0)
void fill(const value_t &value)
iterator find(const id_t &id) noexcept
static constexpr bool has_restore()
iterator begin() noexcept
void _postSetStaticKernel() override
void _postUnsetKernel(bool release=true) override
raw_const_iterator rawCbegin() const noexcept
void rawEmplaceBack(Args &&... args)
void dump(std::ostream &stream) const
void rawEmplace(std::size_t pos, Args &&... args)
const_iterator cbegin() const noexcept
PiercedStorageIterator< const value_t, id_t > const_iterator
container_t::const_pointer const_pointer
void rawInsert(std::size_t pos, std::size_t n, const value_t &value)
void rawReorder(const std::vector< std::size_t > &permutations)
PiercedStorage & operator=(const PiercedStorage &other)
__PS_REFERENCE__ front(std::size_t k=0)
raw_const_iterator rawCend() const noexcept
void rawSet(std::size_t pos, const value_t &value)
void rawResize(std::size_t n, const value_t &value=value_t())
__PS_REFERENCE__ back(std::size_t k=0)
__PS_REFERENCE__ operator[](id_t id)
container_t::pointer pointer
PiercedStorageSyncSlave< id_t >::KernelType KernelType
Action for pierced synchronization.
Base class for defining an object that acts like a slave in pierced synchronization.
--- layout: doxygen_footer ---