Coverage Report

Created: 2026-10-19 14:15

src/zserio/AnyHolder.h
Line
Count
Source
1
#ifndef ZSERIO_ANY_HOLDER_H_INC
2
#define ZSERIO_ANY_HOLDER_H_INC
3
4
#include <type_traits>
5
6
#include "zserio/AllocatorHolder.h"
7
#include "zserio/CppRuntimeException.h"
8
#include "zserio/NoInit.h"
9
#include "zserio/OptionalHolder.h"
10
#include "zserio/RebindAlloc.h"
11
#include "zserio/Traits.h"
12
#include "zserio/Types.h"
13
14
namespace zserio
15
{
16
17
namespace detail
18
{
19
20
class TypeIdHolder
21
{
22
public:
23
    using type_id = const int*;
24
25
    template <typename T>
26
    static type_id get()
27
150k
    {
28
150k
        static int currentTypeId;
29
30
150k
        return &currentTypeId;
31
150k
    }
32
};
33
34
// Interface for object holders
35
template <typename ALLOC>
36
class IHolder
37
{
38
public:
39
66.3k
    virtual ~IHolder() = default;
40
    virtual IHolder* clone(const ALLOC& allocator) const = 0;
41
    virtual IHolder* clone(NoInitT, const ALLOC& allocator) const = 0;
42
    virtual IHolder* clone(void* storage) const = 0;
43
    virtual IHolder* clone(NoInitT, void* storage) const = 0;
44
    virtual IHolder* move(const ALLOC& allocator) = 0;
45
    virtual IHolder* move(NoInitT, const ALLOC& allocator) = 0;
46
    virtual IHolder* move(void* storage) = 0;
47
    virtual IHolder* move(NoInitT, void* storage) = 0;
48
    virtual void destroy(const ALLOC& allocator) = 0;
49
    virtual bool isType(detail::TypeIdHolder::type_id typeId) const = 0;
50
};
51
52
// Base of object holders, holds a value in the inplace_optional_holder
53
template <typename T, typename ALLOC>
54
class HolderBase : public IHolder<ALLOC>
55
{
56
public:
57
    template <typename U>
58
    void set(U&& value)
59
50.1k
    {
60
50.1k
        m_typedHolder = std::forward<U>(value);
61
50.1k
    }
62
63
    template <typename U, typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
64
    void set(NoInitT, U&& value)
65
4
    {
66
        // inplace_optional_holder constructor to prevent it's implicit constructor
67
4
        m_typedHolder.assign(NoInit, inplace_optional_holder<T>(NoInit, std::forward<U>(value)));
68
4
    }
69
70
    template <typename U, typename std::enable_if<!std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
71
    void set(NoInitT, U&& value)
72
14
    {
73
14
        m_typedHolder = std::forward<U>(value);
74
14
    }
75
76
    void setHolder(const inplace_optional_holder<T>& value)
77
29
    {
78
29
        m_typedHolder = value;
79
29
    }
80
81
    void setHolder(inplace_optional_holder<T>&& value)
82
16.2k
    {
83
16.2k
        m_typedHolder = std::move(value);
84
16.2k
    }
85
86
    template <typename U, typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
87
    void setHolder(NoInitT, const inplace_optional_holder<U>& value)
88
6
    {
89
6
        m_typedHolder.assign(NoInit, value);
90
6
    }
91
92
    template <typename U, typename std::enable_if<std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
93
    void setHolder(NoInitT, inplace_optional_holder<U>&& value)
94
3
    {
95
3
        m_typedHolder.assign(NoInit, std::move(value));
96
3
    }
97
98
    template <typename U, typename std::enable_if<!std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
99
    void setHolder(NoInitT, const inplace_optional_holder<U>& value)
100
19
    {
101
19
        m_typedHolder = value;
102
19
    }
103
104
    template <typename U, typename std::enable_if<!std::is_constructible<U, NoInitT, U>::value, int>::type = 0>
105
    void setHolder(NoInitT, inplace_optional_holder<U>&& value)
106
19
    {
107
19
        m_typedHolder = std::move(value);
108
19
    }
109
110
    T& get()
111
740
    {
112
740
        return m_typedHolder.value();
113
740
    }
114
115
    const T& get() const
116
48.7k
    {
117
48.7k
        return m_typedHolder.value();
118
48.7k
    }
119
120
    bool isType(detail::TypeIdHolder::type_id typeId) const override
121
75.3k
    {
122
75.3k
        return detail::TypeIdHolder::get<T>() == typeId;
123
75.3k
    }
124
125
protected:
126
    inplace_optional_holder<T>& getHolder()
127
16.3k
    {
128
16.3k
        return m_typedHolder;
129
16.3k
    }
130
131
    const inplace_optional_holder<T>& getHolder() const
132
54
    {
133
54
        return m_typedHolder;
134
54
    }
135
136
private:
137
    inplace_optional_holder<T> m_typedHolder;
138
};
139
140
// Holder allocated on heap
141
template <typename T, typename ALLOC>
142
class HeapHolder : public HolderBase<T, ALLOC>
143
{
144
private:
145
    struct ConstructTag
146
    {};
147
148
public:
149
    using this_type = HeapHolder<T, ALLOC>;
150
151
    explicit HeapHolder(ConstructTag) noexcept
152
16.6k
    {}
153
154
    static this_type* create(const ALLOC& allocator)
155
16.6k
    {
156
16.6k
        using AllocType = RebindAlloc<ALLOC, this_type>;
157
16.6k
        using AllocTraits = std::allocator_traits<AllocType>;
158
159
16.6k
        AllocType typedAlloc = allocator;
160
16.6k
        typename AllocTraits::pointer ptr = AllocTraits::allocate(typedAlloc, 1);
161
        // this never throws because HeapHolder constructor never throws
162
16.6k
        AllocTraits::construct(typedAlloc, std::addressof(*ptr), ConstructTag{});
163
16.6k
        return ptr;
164
16.6k
    }
165
166
    IHolder<ALLOC>* clone(const ALLOC& allocator) const override
167
12
    {
168
12
        this_type* holder = create(allocator);
169
12
        holder->setHolder(this->getHolder());
170
12
        return holder;
171
12
    }
172
173
    IHolder<ALLOC>* clone(NoInitT, const ALLOC& allocator) const override
174
12
    {
175
12
        this_type* holder = create(allocator);
176
12
        holder->setHolder(NoInit, this->getHolder());
177
12
        return holder;
178
12
    }
179
180
    IHolder<ALLOC>* clone(void*) const override
181
1
    {
182
1
        throw CppRuntimeException("AnyHolder: Unexpected clone call.");
183
1
    }
184
185
    IHolder<ALLOC>* clone(NoInitT, void*) const override
186
1
    {
187
1
        throw CppRuntimeException("AnyHolder: Unexpected clone call.");
188
1
    }
189
190
    IHolder<ALLOC>* move(const ALLOC& allocator) override
191
6
    {
192
6
        this_type* holder = create(allocator);
193
6
        holder->setHolder(std::move(this->getHolder()));
194
6
        return holder;
195
6
    }
196
197
    IHolder<ALLOC>* move(NoInitT, const ALLOC& allocator) override
198
6
    {
199
6
        this_type* holder = create(allocator);
200
6
        holder->setHolder(NoInit, std::move(this->getHolder()));
201
6
        return holder;
202
6
    }
203
204
    IHolder<ALLOC>* move(void*) override
205
1
    {
206
1
        throw CppRuntimeException("AnyHolder: Unexpected move call.");
207
1
    }
208
209
    IHolder<ALLOC>* move(NoInitT, void*) override
210
1
    {
211
1
        throw CppRuntimeException("AnyHolder: Unexpected move call.");
212
1
    }
213
214
    void destroy(const ALLOC& allocator) override
215
16.6k
    {
216
16.6k
        using AllocType = RebindAlloc<ALLOC, this_type>;
217
16.6k
        using AllocTraits = std::allocator_traits<AllocType>;
218
219
16.6k
        AllocType typedAlloc = allocator;
220
16.6k
        AllocTraits::destroy(typedAlloc, this);
221
16.6k
        AllocTraits::deallocate(typedAlloc, this, 1);
222
16.6k
    }
223
};
224
225
// Holder allocated in the in-place storage
226
template <typename T, typename ALLOC>
227
class NonHeapHolder : public HolderBase<T, ALLOC>
228
{
229
public:
230
    using this_type = NonHeapHolder<T, ALLOC>;
231
232
    static this_type* create(void* storage)
233
33.3k
    {
234
33.3k
        return new (storage) this_type();
235
33.3k
    }
236
237
    IHolder<ALLOC>* clone(const ALLOC&) const override
238
1
    {
239
1
        throw CppRuntimeException("AnyHolder: Unexpected clone call.");
240
1
    }
241
242
    IHolder<ALLOC>* clone(NoInitT, const ALLOC&) const override
243
1
    {
244
1
        throw CppRuntimeException("AnyHolder: Unexpected clone call.");
245
1
    }
246
247
    IHolder<ALLOC>* clone(void* storage) const override
248
17
    {
249
17
        NonHeapHolder* holder = new (storage) NonHeapHolder();
250
17
        holder->setHolder(this->getHolder());
251
17
        return holder;
252
17
    }
253
254
    IHolder<ALLOC>* clone(NoInitT, void* storage) const override
255
13
    {
256
13
        NonHeapHolder* holder = new (storage) NonHeapHolder();
257
13
        holder->setHolder(NoInit, this->getHolder());
258
13
        return holder;
259
13
    }
260
261
    IHolder<ALLOC>* move(const ALLOC&) override
262
1
    {
263
1
        throw CppRuntimeException("AnyHolder: Unexpected move call.");
264
1
    }
265
266
    IHolder<ALLOC>* move(NoInitT, const ALLOC&) override
267
1
    {
268
1
        throw CppRuntimeException("AnyHolder: Unexpected move call.");
269
1
    }
270
271
    IHolder<ALLOC>* move(void* storage) override
272
16.2k
    {
273
16.2k
        NonHeapHolder* holder = new (storage) NonHeapHolder();
274
16.2k
        holder->setHolder(std::move(this->getHolder()));
275
16.2k
        return holder;
276
16.2k
    }
277
278
    IHolder<ALLOC>* move(NoInitT, void* storage) override
279
16
    {
280
16
        NonHeapHolder* holder = new (storage) NonHeapHolder();
281
16
        holder->setHolder(NoInit, std::move(this->getHolder()));
282
16
        return holder;
283
16
    }
284
285
    void destroy(const ALLOC&) override
286
49.6k
    {
287
49.6k
        this->~NonHeapHolder();
288
49.6k
    }
289
290
private:
291
49.6k
    NonHeapHolder() = default;
292
};
293
294
template <typename ALLOC>
295
union UntypedHolder
296
{
297
    // 2 * sizeof(void*) for T + sizeof(void*) for Holder's vptr
298
    using MaxInPlaceType = std::aligned_storage<3 * sizeof(void*), alignof(void*)>::type;
299
300
    detail::IHolder<ALLOC>* heap;
301
    MaxInPlaceType inPlace;
302
};
303
304
template <typename T, typename ALLOC>
305
using has_non_heap_holder = std::integral_constant<bool,
306
        sizeof(NonHeapHolder<T, ALLOC>) <= sizeof(typename UntypedHolder<ALLOC>::MaxInPlaceType) &&
307
                std::is_nothrow_move_constructible<T>::value &&
308
                alignof(T) <= alignof(typename UntypedHolder<ALLOC>::MaxInPlaceType)>;
309
310
} // namespace detail
311
312
/**
313
 * Type safe container for single values of any type which doesn't need RTTI.
314
 */
315
template <typename ALLOC = std::allocator<uint8_t>>
316
class AnyHolder : public AllocatorHolder<ALLOC>
317
{
318
    using AllocTraits = std::allocator_traits<ALLOC>;
319
    using AllocatorHolder<ALLOC>::get_allocator_ref;
320
    using AllocatorHolder<ALLOC>::set_allocator;
321
322
public:
323
    using AllocatorHolder<ALLOC>::get_allocator;
324
    using allocator_type = ALLOC;
325
326
    /**
327
     * Empty constructor.
328
     */
329
    AnyHolder() :
330
            AnyHolder(ALLOC())
331
375
    {}
332
333
    /**
334
     * Constructor from given allocator
335
     */
336
    explicit AnyHolder(const ALLOC& allocator) :
337
            AllocatorHolder<ALLOC>(allocator)
338
950
    {
339
950
        m_untypedHolder.heap = nullptr;
340
950
    }
341
342
    /**
343
     * Constructor from any value.
344
     *
345
     * \param value Value of any type to hold. Supports move semantic.
346
     */
347
    template <typename T,
348
            typename std::enable_if<!std::is_same<typename std::decay<T>::type, AnyHolder>::value &&
349
                            !std::is_same<typename std::decay<T>::type, ALLOC>::value,
350
                    int>::type = 0>
351
    explicit AnyHolder(T&& value, const ALLOC& allocator = ALLOC()) :
352
            AllocatorHolder<ALLOC>(allocator)
353
25.1k
    {
354
25.1k
        m_untypedHolder.heap = nullptr;
355
25.1k
        set(std::forward<T>(value));
356
25.1k
    }
357
358
    /**
359
     * Constructor from any value which prevents initialization.
360
     *
361
     * \param value Value of any type to hold. Supports move semantic.
362
     */
363
    template <typename T,
364
            typename std::enable_if<!std::is_same<typename std::decay<T>::type, AnyHolder>::value, int>::type =
365
                    0>
366
    explicit AnyHolder(NoInitT, T&& value, const ALLOC& allocator = ALLOC()) :
367
            AllocatorHolder<ALLOC>(allocator)
368
18
    {
369
18
        m_untypedHolder.heap = nullptr;
370
18
        set(NoInit, std::forward<T>(value));
371
18
    }
372
373
    /**
374
     * Destructor.
375
     */
376
    ~AnyHolder()
377
26.3k
    {
378
26.3k
        clearHolder();
379
26.3k
    }
380
381
    /**
382
     * Copy constructor.
383
     *
384
     * \param other Any holder to copy.
385
     */
386
    AnyHolder(const AnyHolder& other) :
387
            AllocatorHolder<ALLOC>(
388
                    AllocTraits::select_on_container_copy_construction(other.get_allocator_ref()))
389
29
    {
390
29
        copy(other);
391
29
    }
392
393
    /**
394
     * Copy constructor which prevents initialization.
395
     *
396
     * \param other Any holder to copy.
397
     */
398
    AnyHolder(NoInitT, const AnyHolder& other) :
399
            AllocatorHolder<ALLOC>(
400
                    AllocTraits::select_on_container_copy_construction(other.get_allocator_ref()))
401
24
    {
402
24
        copy(NoInit, other);
403
24
    }
404
405
    /**
406
     * Allocator-extended copy constructor.
407
     *
408
     * \param other Any holder to copy.
409
     * \param allocator Allocator to be used for dynamic memory allocations.
410
     */
411
    AnyHolder(const AnyHolder& other, const ALLOC& allocator) :
412
            AllocatorHolder<ALLOC>(allocator)
413
8
    {
414
8
        copy(other);
415
8
    }
416
417
    /**
418
     * Allocator-extended copy constructor which prevents initialization.
419
     *
420
     * \param other Any holder to copy.
421
     * \param allocator Allocator to be used for dynamic memory allocations.
422
     */
423
    AnyHolder(NoInitT, const AnyHolder& other, const ALLOC& allocator) :
424
            AllocatorHolder<ALLOC>(allocator)
425
8
    {
426
8
        copy(NoInit, other);
427
8
    }
428
429
    /**
430
     * Copy assignment operator.
431
     *
432
     * \param other Any holder to copy.
433
     *
434
     * \return Reference to this.
435
     */
436
    AnyHolder& operator=(const AnyHolder& other)
437
24
    {
438
24
        if (this != &other)
439
16
        {
440
            // TODO: do not dealloc unless necessary
441
16
            clearHolder();
442
16
            if (AllocTraits::propagate_on_container_copy_assignment::value)
443
8
            {
444
8
                set_allocator(other.get_allocator_ref());
445
8
            }
446
16
            copy(other);
447
16
        }
448
449
24
        return *this;
450
24
    }
451
452
    /**
453
     * Copy assignment operator which prevents initialization.
454
     *
455
     * \param other Any holder to copy.
456
     *
457
     * \return Reference to this.
458
     */
459
    AnyHolder& assign(NoInitT, const AnyHolder& other)
460
37
    {
461
37
        if (this != &other)
462
29
        {
463
            // TODO: do not dealloc unless necessary
464
29
            clearHolder();
465
29
            if (AllocTraits::propagate_on_container_copy_assignment::value)
466
8
            {
467
8
                set_allocator(other.get_allocator_ref());
468
8
            }
469
29
            copy(NoInit, other);
470
29
        }
471
472
37
        return *this;
473
37
    }
474
475
    /**
476
     * Move constructor.
477
     *
478
     * \param other Any holder to move from.
479
     */
480
    AnyHolder(AnyHolder&& other) noexcept :
481
            AllocatorHolder<ALLOC>(std::move(other.get_allocator_ref()))
482
45
    {
483
45
        move(std::move(other));
484
45
    }
485
486
    /**
487
     * Move constructor which prevents initialization.
488
     *
489
     * \param other Any holder to move from.
490
     */
491
    AnyHolder(NoInitT, AnyHolder&& other) noexcept :
492
            AllocatorHolder<ALLOC>(std::move(other.get_allocator_ref()))
493
29
    {
494
29
        move(NoInit, std::move(other));
495
29
    }
496
497
    /**
498
     * Allocator-extended move constructor.
499
     *
500
     * \param other Any holder to move from.
501
     * \param allocator Allocator to be used for dynamic memory allocations.
502
     */
503
    AnyHolder(AnyHolder&& other, const ALLOC& allocator) :
504
            AllocatorHolder<ALLOC>(allocator)
505
24
    {
506
24
        move(std::move(other));
507
24
    }
508
509
    /**
510
     * Allocator-extended move constructor which prevents initialization.
511
     *
512
     * \param other Any holder to move from.
513
     * \param allocator Allocator to be used for dynamic memory allocations.
514
     */
515
    AnyHolder(NoInitT, AnyHolder&& other, const ALLOC& allocator) :
516
            AllocatorHolder<ALLOC>(allocator)
517
24
    {
518
24
        move(NoInit, std::move(other));
519
24
    }
520
521
    /**
522
     * Move assignment operator.
523
     *
524
     * \param other Any holder to move from.
525
     *
526
     * \return Reference to this.
527
     */
528
    AnyHolder& operator=(AnyHolder&& other)
529
48.9k
    {
530
48.9k
        if (this != &other)
531
48.9k
        {
532
48.9k
            clearHolder();
533
48.9k
            if (AllocTraits::propagate_on_container_move_assignment::value)
534
48.9k
            {
535
48.9k
                set_allocator(std::move(other.get_allocator_ref()));
536
48.9k
            }
537
48.9k
            move(std::move(other));
538
48.9k
        }
539
540
48.9k
        return *this;
541
48.9k
    }
542
543
    /**
544
     * Move assignment operator which prevents initialization.
545
     *
546
     * \param other Any holder to move from.
547
     *
548
     * \return Reference to this.
549
     */
550
    AnyHolder& assign(NoInitT, AnyHolder&& other)
551
27
    {
552
27
        if (this != &other)
553
19
        {
554
19
            clearHolder();
555
19
            if (AllocTraits::propagate_on_container_move_assignment::value)
556
11
            {
557
11
                set_allocator(std::move(other.get_allocator_ref()));
558
11
            }
559
19
            move(NoInit, std::move(other));
560
19
        }
561
562
27
        return *this;
563
27
    }
564
565
    /**
566
     * Value assignment operator.
567
     *
568
     * \param value Any value to assign. Supports move semantic.
569
     *
570
     * \return Reference to this.
571
     */
572
    template <typename T,
573
            typename std::enable_if<!std::is_same<typename std::decay<T>::type, AnyHolder>::value, int>::type =
574
                    0>
575
    AnyHolder& operator=(T&& value)
576
72
    {
577
72
        set(std::forward<T>(value));
578
579
72
        return *this;
580
72
    }
581
582
    /**
583
     * Resets the holder.
584
     */
585
    void reset()
586
68
    {
587
68
        clearHolder();
588
68
    }
589
590
    /**
591
     * Sets any value to the holder.
592
     *
593
     * \param value Any value to set. Supports move semantic.
594
     */
595
    template <typename T>
596
    void set(T&& value)
597
50.1k
    {
598
50.1k
        createHolder<typename std::decay<T>::type>()->set(std::forward<T>(value));
599
50.1k
    }
600
601
    /**
602
     * Sets any value to the holder and prevent initialization.
603
     *
604
     * \param value Any value to set. Supports move semantic.
605
     */
606
    template <typename T>
607
    void set(NoInitT, T&& value)
608
18
    {
609
18
        createHolder<typename std::decay<T>::type>()->set(NoInit, std::forward<T>(value));
610
18
    }
611
612
    /**
613
     * Gets value of the given type.
614
     *
615
     * \return Reference to value of the requested type if the type match to the stored value.
616
     *
617
     * \throw CppRuntimeException if the requested type doesn't match to the stored value.
618
     */
619
    template <typename T>
620
    T& get()
621
772
    {
622
772
        checkType<T>();
623
772
        return getHolder<T>(detail::has_non_heap_holder<T, ALLOC>())->get();
624
772
    }
625
626
    /**
627
     * Gets value of the given type.
628
     *
629
     * \return Value of the requested type if the type match to the stored value.
630
     *
631
     * \throw CppRuntimeException if the requested type doesn't match to the stored value.
632
     */
633
    template <typename T>
634
    const T& get() const
635
48.7k
    {
636
48.7k
        checkType<T>();
637
48.7k
        return getHolder<T>(detail::has_non_heap_holder<T, ALLOC>())->get();
638
48.7k
    }
639
640
    /**
641
     * Check whether the holder holds the given type.
642
     *
643
     * \return True if the stored value is of the given type, false otherwise.
644
     */
645
    template <typename T>
646
    bool isType() const
647
50.6k
    {
648
50.6k
        return hasHolder() && 
getUntypedHolder()->isType(detail::TypeIdHolder::get<T>())50.6k
;
649
50.6k
    }
650
651
    /**
652
     * Checks whether the holder has any value.
653
     *
654
     * \return True if the holder has assigned any value, false otherwise.
655
     */
656
    bool hasValue() const
657
24.7k
    {
658
24.7k
        return hasHolder();
659
24.7k
    }
660
661
private:
662
    void copy(const AnyHolder& other)
663
53
    {
664
53
        if (other.m_isInPlace)
665
17
        {
666
17
            other.getUntypedHolder()->clone(&m_untypedHolder.inPlace);
667
17
            m_isInPlace = true;
668
17
        }
669
36
        else if (other.m_untypedHolder.heap != nullptr)
670
12
        {
671
12
            m_untypedHolder.heap = other.getUntypedHolder()->clone(get_allocator_ref());
672
12
        }
673
24
        else
674
24
        {
675
24
            m_untypedHolder.heap = nullptr;
676
24
        }
677
53
    }
678
679
    void copy(NoInitT, const AnyHolder& other)
680
61
    {
681
61
        if (other.m_isInPlace)
682
13
        {
683
13
            other.getUntypedHolder()->clone(NoInit, &m_untypedHolder.inPlace);
684
13
            m_isInPlace = true;
685
13
        }
686
48
        else if (other.m_untypedHolder.heap != nullptr)
687
12
        {
688
12
            m_untypedHolder.heap = other.getUntypedHolder()->clone(NoInit, get_allocator_ref());
689
12
        }
690
36
        else
691
36
        {
692
36
            m_untypedHolder.heap = nullptr;
693
36
        }
694
61
    }
695
696
    void move(AnyHolder&& other)
697
49.0k
    {
698
49.0k
        if (other.m_isInPlace)
699
16.2k
        {
700
16.2k
            other.getUntypedHolder()->move(&m_untypedHolder.inPlace);
701
16.2k
            m_isInPlace = true;
702
16.2k
            other.clearHolder();
703
16.2k
        }
704
32.7k
        else if (other.m_untypedHolder.heap != nullptr)
705
32.6k
        {
706
32.6k
            if (get_allocator_ref() == other.get_allocator_ref())
707
32.6k
            {
708
                // take over the other's storage
709
32.6k
                m_untypedHolder.heap = other.m_untypedHolder.heap;
710
32.6k
                other.m_untypedHolder.heap = nullptr;
711
32.6k
            }
712
6
            else
713
6
            {
714
                // cannot steal the storage, allocate our own and move the holder
715
6
                m_untypedHolder.heap = other.getUntypedHolder()->move(get_allocator_ref());
716
6
                other.clearHolder();
717
6
            }
718
32.6k
        }
719
36
        else
720
36
        {
721
36
            m_untypedHolder.heap = nullptr;
722
36
        }
723
49.0k
    }
724
725
    void move(NoInitT, AnyHolder&& other)
726
72
    {
727
72
        if (other.m_isInPlace)
728
16
        {
729
16
            other.getUntypedHolder()->move(NoInit, &m_untypedHolder.inPlace);
730
16
            m_isInPlace = true;
731
16
            other.clearHolder();
732
16
        }
733
56
        else if (other.m_untypedHolder.heap != nullptr)
734
17
        {
735
17
            if (get_allocator_ref() == other.get_allocator_ref())
736
11
            {
737
                // take over the other's storage
738
11
                m_untypedHolder.heap = other.m_untypedHolder.heap;
739
11
                other.m_untypedHolder.heap = nullptr;
740
11
            }
741
6
            else
742
6
            {
743
                // cannot steal the storage, allocate our own and move the holder
744
6
                m_untypedHolder.heap = other.getUntypedHolder()->move(NoInit, get_allocator_ref());
745
6
                other.clearHolder();
746
6
            }
747
17
        }
748
39
        else
749
39
        {
750
39
            m_untypedHolder.heap = nullptr;
751
39
        }
752
72
    }
753
754
    void clearHolder()
755
116k
    {
756
116k
        if (hasHolder())
757
66.3k
        {
758
66.3k
            getUntypedHolder()->destroy(get_allocator_ref());
759
66.3k
            m_isInPlace = false;
760
66.3k
            m_untypedHolder.heap = nullptr;
761
66.3k
        }
762
116k
    }
763
764
    bool hasHolder() const
765
241k
    {
766
241k
        return (m_isInPlace || 
m_untypedHolder.heap != nullptr142k
);
767
241k
    }
768
769
    template <typename T>
770
    detail::HolderBase<T, ALLOC>* createHolder()
771
50.1k
    {
772
50.1k
        if (hasHolder())
773
24.6k
        {
774
24.6k
            if (getUntypedHolder()->isType(detail::TypeIdHolder::get<T>()))
775
211
            {
776
211
                return getHolder<T>(detail::has_non_heap_holder<T, ALLOC>());
777
211
            }
778
779
24.4k
            clearHolder();
780
24.4k
        }
781
782
49.9k
        return createHolderImpl<T>(detail::has_non_heap_holder<T, ALLOC>());
783
50.1k
    }
784
785
    template <typename T>
786
    detail::HolderBase<T, ALLOC>* createHolderImpl(std::true_type)
787
33.3k
    {
788
33.3k
        detail::NonHeapHolder<T, ALLOC>* holder =
789
33.3k
                detail::NonHeapHolder<T, ALLOC>::create(&m_untypedHolder.inPlace);
790
33.3k
        m_isInPlace = true;
791
33.3k
        return holder;
792
33.3k
    }
793
794
    template <typename T>
795
    detail::HolderBase<T, ALLOC>* createHolderImpl(std::false_type)
796
16.6k
    {
797
16.6k
        detail::HeapHolder<T, ALLOC>* holder = detail::HeapHolder<T, ALLOC>::create(get_allocator_ref());
798
16.6k
        m_untypedHolder.heap = holder;
799
16.6k
        return holder;
800
16.6k
    }
801
802
    template <typename T>
803
    void checkType() const
804
49.4k
    {
805
49.4k
        if (!isType<T>())
806
33
        {
807
33
            throwBadType();
808
33
        }
809
49.4k
    }
810
811
    /** Optimization which increases changes to inline checkType(). */
812
    void throwBadType() const
813
33
    {
814
33
        throw CppRuntimeException("Bad type in AnyHolder");
815
33
    }
816
817
    template <typename T>
818
    detail::HeapHolder<T, ALLOC>* getHeapHolder()
819
168
    {
820
168
        return static_cast<detail::HeapHolder<T, ALLOC>*>(m_untypedHolder.heap);
821
168
    }
822
823
    template <typename T>
824
    const detail::HeapHolder<T, ALLOC>* getHeapHolder() const
825
16.4k
    {
826
16.4k
        return static_cast<detail::HeapHolder<T, ALLOC>*>(m_untypedHolder.heap);
827
16.4k
    }
828
829
    template <typename T>
830
    detail::NonHeapHolder<T, ALLOC>* getInplaceHolder()
831
783
    {
832
783
        return reinterpret_cast<detail::NonHeapHolder<T, ALLOC>*>(&m_untypedHolder.inPlace);
833
783
    }
834
835
    template <typename T>
836
    const detail::NonHeapHolder<T, ALLOC>* getInplaceHolder() const
837
32.2k
    {
838
32.2k
        return reinterpret_cast<const detail::NonHeapHolder<T, ALLOC>*>(&m_untypedHolder.inPlace);
839
32.2k
    }
840
841
    template <typename T>
842
    detail::HolderBase<T, ALLOC>* getHolder(std::true_type)
843
783
    {
844
783
        return static_cast<detail::HolderBase<T, ALLOC>*>(getInplaceHolder<T>());
845
783
    }
846
847
    template <typename T>
848
    detail::HolderBase<T, ALLOC>* getHolder(std::false_type)
849
168
    {
850
168
        return static_cast<detail::HolderBase<T, ALLOC>*>(getHeapHolder<T>());
851
168
    }
852
853
    template <typename T>
854
    const detail::HolderBase<T, ALLOC>* getHolder(std::true_type) const
855
32.2k
    {
856
32.2k
        return static_cast<const detail::HolderBase<T, ALLOC>*>(getInplaceHolder<T>());
857
32.2k
    }
858
859
    template <typename T>
860
    const detail::HolderBase<T, ALLOC>* getHolder(std::false_type) const
861
16.4k
    {
862
16.4k
        return static_cast<const detail::HolderBase<T, ALLOC>*>(getHeapHolder<T>());
863
16.4k
    }
864
865
    detail::IHolder<ALLOC>* getUntypedHolder()
866
107k
    {
867
107k
        return (m_isInPlace)
868
107k
                ? 
reinterpret_cast<detail::IHolder<ALLOC>*>(&m_untypedHolder.inPlace)74.3k
869
107k
                : 
m_untypedHolder.heap32.9k
;
870
107k
    }
871
872
    const detail::IHolder<ALLOC>* getUntypedHolder() const
873
50.7k
    {
874
50.7k
        return (m_isInPlace)
875
50.7k
                ? 
reinterpret_cast<const detail::IHolder<ALLOC>*>(&m_untypedHolder.inPlace)33.4k
876
50.7k
                : 
m_untypedHolder.heap17.3k
;
877
50.7k
    }
878
879
    detail::UntypedHolder<ALLOC> m_untypedHolder;
880
    bool m_isInPlace = false;
881
};
882
883
} // namespace zserio
884
885
#endif // ifndef ZSERIO_ANY_HOLDER_H_INC