Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/benchmark/catch_constructor.hpp
blob: bf6dfec990bc58f395c93b052c3648b7359563e7 (plain)
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
/*
 *  Created by Joachim on 16/04/2019.
 *  Adapted from donated nonius code.
 *
 *  Distributed under the Boost Software License, Version 1.0. (See accompanying
 *  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 */

// Constructor and destructor helpers

#ifndef TWOBLUECUBES_CATCH_CONSTRUCTOR_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CONSTRUCTOR_HPP_INCLUDED

#include <type_traits>

namespace Catch {
    namespace Detail {
        template <typename T, bool Destruct>
        struct ObjectStorage
        {
            using TStorage = typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type;

            ObjectStorage() : data() {}

            ObjectStorage(const ObjectStorage& other)
            {
                new(&data) T(other.stored_object());
            }

            ObjectStorage(ObjectStorage&& other)
            {
                new(&data) T(std::move(other.stored_object()));
            }

            ~ObjectStorage() { destruct_on_exit<T>(); }

            template <typename... Args>
            void construct(Args&&... args)
            {
                new (&data) T(std::forward<Args>(args)...);
            }

            template <bool AllowManualDestruction = !Destruct>
            typename std::enable_if<AllowManualDestruction>::type destruct()
            {
                stored_object().~T();
            }

        private:
            // If this is a constructor benchmark, destruct the underlying object
            template <typename U>
            void destruct_on_exit(typename std::enable_if<Destruct, U>::type* = 0) { destruct<true>(); }
            // Otherwise, don't
            template <typename U>
            void destruct_on_exit(typename std::enable_if<!Destruct, U>::type* = 0) { }

            T& stored_object()
            {
                return *static_cast<T*>(static_cast<void*>(&data));
            }

            TStorage data;
        };
    }

    template <typename T>
    using storage_for = Detail::ObjectStorage<T, true>;

    template <typename T>
    using destructable_object = Detail::ObjectStorage<T, false>;
}

#endif // TWOBLUECUBES_CATCH_CONSTRUCTOR_HPP_INCLUDED