File: container.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (716 lines) | stat: -rw-r--r-- 20,667 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/************************************************************************
 *
 * Copyright (C) 2022-2025 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/data/config.hpp>
#include "data/object.hpp"

#include <core/com/signal.hpp>
#include <core/compare.hpp>

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index_container.hpp>

namespace sight::data
{

// cspell:ignore crbegin crend

/// sequenced_set alias. It provides sequence like API, while keeping the unicity of the elements.
/// @note Remove me, once there is a STL equivalent.
template<class C>
using sequenced_set = boost::multi_index::multi_index_container<
    C,
    boost::multi_index::indexed_by<
        boost::multi_index::random_access<>,
        boost::multi_index::hashed_unique<
            boost::multi_index::identity<C>
        >
    >
>;

#define USING_CONTAINER(C) \
        using typename C::value_type; \
        using typename C::reference; \
        using typename C::const_reference; \
        using typename C::iterator; \
        using typename C::const_iterator; \
        using typename C::size_type; \
        using typename C::difference_type; \
        using C::operator=; \
        using C::begin; \
        using C::end; \
        using C::cbegin; \
        using C::cend; \
        using C::size; \
        using C::max_size; \
        using C::empty; \
        using C::swap; \
        using typename C::pointer; \
        using typename C::const_pointer

#define USING_ORDERED_CONTAINER(C) \
        using typename C::reverse_iterator; \
        using typename C::const_reverse_iterator; \
        using C::rbegin; \
        using C::rend; \
        using C::crbegin; \
        using C::crend

#define USING_DYNAMIC_CONTAINER(C) \
        using typename C::allocator_type; \
        using C::get_allocator; \
        using C::clear; \
        using C::insert; \
        using C::emplace; \
        using C::erase

#define USING_SEQUENCE_CONTAINER(C) \
        using C::assign; \
        using C::resize; \
        using C::front; \
        using C::back; \
        using C::push_back; \
        using C::pop_back

#define USING_ASSOCIATIVE_CONTAINER(C) \
        using typename C::key_type; \
        using typename C::node_type; \
        using C::emplace_hint; \
        using C::merge; \
        using C::extract; \
        using C::count; \
        using C::find; \
        using C::equal_range; \
        using C::contains

#define USING_ASSOCIATIVE_ORDERED_CONTAINER(C) \
        using typename C::key_compare; \
        using typename C::value_compare; \
        using C::key_comp; \
        using C::value_comp; \
        using C::lower_bound; \
        using C::upper_bound

#define USING_UNORDERED_ASSOCIATIVE_CONTAINER(C) \
        using typename C::hasher; \
        using typename C::key_equal; \
        using typename C::local_iterator; \
        using typename C::const_local_iterator; \
        using C::hash_function; \
        using C::key_eq; \
        using C::bucket_count; \
        using C::max_bucket_count; \
        using C::bucket_size; \
        using C::bucket; \
        using C::load_factor; \
        using C::max_load_factor; \
        using C::rehash; \
        using C::reserve

#define USING_MAP_CONTAINER(C) \
        using typename C::mapped_type; \
        using C::operator[]; \
        using C::at; \
        using C::try_emplace; \
        using C::insert_or_assign; \
        using typename C::insert_return_type

/// Dummy class.
template<class C, typename = void>
class container_wrapper
{
};

/// Array container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_array<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other array named requirements
    /// @{
    using C::fill;
    using C::operator[];
    using C::at;
    using C::data;
    using C::front;
    using C::back;
    /// @}

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Vector container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<
    C,
    typename std::enable_if_t<core::is_vector<C>::value>
>: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other sequence named requirements
    USING_SEQUENCE_CONTAINER(C);

    /// Other vector named requirements
    /// @{
    using C::capacity;
    using C::reserve;
    using C::shrink_to_fit;
    using C::operator[];
    using C::at;
    using C::data;
    using C::emplace_back;
    /// @}

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;

    /// Utility function to remove all matching elements from the container.
    constexpr auto remove(const typename C::value_type& _value)
    {
        return C::erase(std::remove(C::begin(), C::end(), _value), C::end());
    }

    /// Utility function to remove first matching elements from the container.
    constexpr auto remove_one(const typename C::value_type& _value)
    {
        if(const auto& it = std::find(C::cbegin(), C::cend(), _value); it != C::cend())
        {
            return C::erase(it);
        }

        return C::end();
    }
};

/// Deque container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_deque<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other sequence named requirements
    USING_SEQUENCE_CONTAINER(C);

    /// Other deque named requirements
    /// @{
    using C::shrink_to_fit;
    using C::operator[];
    using C::at;
    using C::emplace_front;
    using C::emplace_back;
    using C::push_front;
    using C::pop_front;
    /// @}

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// List container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_list<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other sequence named requirements
    USING_SEQUENCE_CONTAINER(C);

    /// Other list named requirements
    /// @{
    using C::push_front;
    using C::pop_front;
    using C::splice;
    using C::remove;
    using C::unique;
    using C::merge;
    using C::sort;
    using C::reverse;
    /// @}

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Sequenced set container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_sequenced_set<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other sequence named requirements
    USING_SEQUENCE_CONTAINER(C);

    /// Other sequenced_set named requirements
    /// @{
    using typename C::ctor_args;
    using typename C::tag_list;
    using typename C::node_type;
    using typename C::insert_return_type;
    using C::iterator_to;
    using C::capacity;
    using C::reserve;
    using C::shrink_to_fit;
    using C::operator[];
    using C::at;
    using C::emplace_front;
    using C::push_front;
    using C::pop_front;
    using C::emplace_back;
    using C::extract;
    using C::replace;
    using C::modify;
    using C::splice;
    using C::remove;
    using C::remove_if;
    using C::unique;
    using C::merge;
    using C::sort;
    using C::reverse;
    using C::rearrange;
    using C::get;
    /// @}

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;

    /// Utility function to remove first matching elements from the container.
    constexpr auto remove(const typename C::value_type& _value)
    {
        if(const auto& it = std::find(C::cbegin(), C::cend(), _value); it != C::cend())
        {
            return C::erase(it);
        }
        else
        {
            return C::end();
        }
    }
};

/// Map container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_map<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other associative ordered requirements
    USING_ASSOCIATIVE_ORDERED_CONTAINER(C);

    /// Other map named requirements
    USING_MAP_CONTAINER(C);

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Multimap container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_multimap<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other associative ordered requirements
    USING_ASSOCIATIVE_ORDERED_CONTAINER(C);

    /// Other map named requirements
    using typename C::mapped_type;

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Set container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_set<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other associative ordered requirements
    USING_ASSOCIATIVE_ORDERED_CONTAINER(C);

    /// Other set named requirements
    using typename C::insert_return_type;

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Set container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_multiset<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other ordered named requirements
    USING_ORDERED_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other associative ordered requirements
    USING_ASSOCIATIVE_ORDERED_CONTAINER(C);

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Unordered map container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_unordered_map<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other unordered associative named requirements
    USING_UNORDERED_ASSOCIATIVE_CONTAINER(C);

    /// Other map named requirements
    USING_MAP_CONTAINER(C);

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Unordered multimap container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<
    C,
    typename std::enable_if_t<core::is_unordered_multimap<C>::value>
>: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other unordered associative named requirements
    USING_UNORDERED_ASSOCIATIVE_CONTAINER(C);

    /// Missing map named requirements
    using typename C::mapped_type;

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Unordered set container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<C, typename std::enable_if_t<core::is_unordered_set<C>::value> >: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other unordered associative named requirements
    USING_UNORDERED_ASSOCIATIVE_CONTAINER(C);

    /// Missing set named requirements
    using typename C::insert_return_type;

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

/// Unordered set container class.
template<class C>
class SIGHT_DATA_CLASS_API container_wrapper<
    C,
    typename std::enable_if_t<core::is_unordered_multiset<C>::value>
>: private C
{
public:

    /// STL container named requirements
    USING_CONTAINER(C);

    /// Other dynamic named requirements
    USING_DYNAMIC_CONTAINER(C);

    /// Other associative named requirements
    USING_ASSOCIATIVE_CONTAINER(C);

    /// Other unordered associative named requirements
    USING_UNORDERED_ASSOCIATIVE_CONTAINER(C);

    /// Enable all constructors from the base class...
    using C::C;

    /// Default virtual destructor
    /// @note: constexpr destructors are only available with ‘-std=c++20’ or ‘-std=gnu++20’
    constexpr virtual ~container_wrapper() noexcept = default;
};

#undef USING_CONTAINER
#undef USING_ORDERED_CONTAINER
#undef USING_DYNAMIC_CONTAINER
#undef USING_SEQUENCE_CONTAINER
#undef USING_LIST_CONTAINER
#undef USING_ASSOCIATIVE_CONTAINER
#undef USING_ASSOCIATIVE_ORDERED_CONTAINER
#undef USING_UNORDERED_ASSOCIATIVE_CONTAINER
#undef USING_MAP_CONTAINER

/// Generic Sight data container class.
template<class C>
class SIGHT_DATA_CLASS_API container : public object,
                                       public container_wrapper<C>
{
public:

    SIGHT_DECLARE_CLASS(container<C>, object);

    /// Could be usefull to keep the underlying container
    using container_t = C;

    /// Constructors / Destructor / Assignment operators
    /// @{
    constexpr container();
    inline explicit container(const C& _container);
    inline explicit container(C&& _container);
    inline ~container() noexcept override = default;

    /// To allow assignment from STL containers
    using container_wrapper<C>::container_wrapper;
    using container_wrapper<C>::operator=;
    /// @}

    /// Equality comparison operators
    /// @{
    constexpr bool operator==(const container& _other) const noexcept;
    constexpr bool operator!=(const container& _other) const noexcept;
    /// @}

    /// Signals
    /// @{
    /// Type of signal when objects are added
    using added_signal_t = core::com::signal<void (container_t)>;
    inline static const core::com::signals::key_t ADDED_OBJECTS_SIG = "added_objects";

    /// Type of signal when objects are changed (newObjects, oldObjects)
    using changed_signal_t = core::com::signal<void (container_t, container_t)>;
    inline static const core::com::signals::key_t CHANGED_OBJECTS_SIG = "changedObjects";

    /// Type of signal when objects are removed
    using removed_signal_t = core::com::signal<void (container_t)>;
    inline static const core::com::signals::key_t REMOVED_OBJECTS_SIG = "removed_objects";
    /// @}

    /// Returns a copy of the underlying container
    constexpr C get_content() const noexcept;

    struct SIGHT_DATA_CLASS_API scoped_emitter
    {
        constexpr scoped_emitter(const container& _container) noexcept;
        inline ~scoped_emitter() noexcept;

        /// Emits the needed signals
        void emit() noexcept;

        /// Resets the internal state
        constexpr void reset() noexcept;

        /// Resets the internal state
        constexpr void block(const core::com::slot_base::sptr& _slot) noexcept;

        const container& m_container;
        container::container_t m_backup;
        std::vector<core::com::slot_base::sptr> m_blocked_slots;
    };

    [[nodiscard]] constexpr auto scoped_emit() const noexcept;

    /// Defines shallow copy
    /// @throws data::exception if an errors occurs during copy
    /// @param[in] _source the source object to copy
    inline void shallow_copy(const object::csptr& _source) override;

    /// Defines deep copy
    /// @throws data::exception if an errors occurs during copy
    /// @param _source source object to copy
    /// @param _cache cache used to deduplicate pointers
    inline void deep_copy(
        const object::csptr& _source,
        const std::unique_ptr<deep_copy_cache_t>& _cache = std::make_unique<deep_copy_cache_t>()
    ) override;
};

} // namespace sight::data

/// This will also includes the templated definitions, which is required for generated registerServices.cpp or Qt MOC
#include "data/container.hxx"