File: mem_lifetime.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (118 lines) | stat: -rw-r--r-- 2,793 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
/*
 * Copyright (C) 2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once

#include <memory>

template <typename T>
using ExtUniquePtrT = std::unique_ptr<T, void (*)(T *)>;

template <typename T>
void cloneExt(ExtUniquePtrT<T> &dst, const T &src);

template <typename T>
void allocateExt(ExtUniquePtrT<T> &dst);

template <typename T>
void destroyExt(T *dst);

namespace Impl {

template <typename ParentT>
struct UniquePtrWrapperOps {
    auto operator*() const noexcept(noexcept(std::declval<decltype(ParentT::ptr)>().operator*())) {
        return *static_cast<const ParentT *>(this)->ptr;
    }

    auto operator->() const noexcept {
        return static_cast<const ParentT *>(this)->ptr.operator->();
    }

    explicit operator bool() const noexcept {
        return static_cast<bool>(static_cast<const ParentT *>(this)->ptr);
    }

    template <typename Rhs>
    friend bool operator==(const ParentT &lhs, const Rhs &rhs) {
        return lhs.ptr == rhs;
    }

    template <typename Lhs>
    friend bool operator==(const Lhs &lhs, const ParentT &rhs) {
        return lhs == rhs.ptr;
    }

    friend bool operator==(const ParentT &lhs, const ParentT &rhs) {
        return lhs.ptr == rhs.ptr;
    }
};

} // namespace Impl

template <typename T, bool allocateAtInit = false>
struct Ext : Impl::UniquePtrWrapperOps<Ext<T>> {
    Ext(T *ptr) : ptr(ptr, destroyExt) {}
    Ext() {
        if constexpr (allocateAtInit) {
            allocateExt(ptr);
        }
    }
    Ext(const Ext &rhs) {
        if (rhs.ptr.get()) {
            cloneExt(this->ptr, *rhs.ptr.get());
        }
    }

    Ext &operator=(const Ext &rhs) {
        if (this == &rhs) {
            return *this;
        }
        if (rhs.ptr.get()) {
            cloneExt(this->ptr, *rhs.ptr.get());
        } else {
            ptr.reset();
        }
        return *this;
    }

    ~Ext() = default;
    Ext(Ext &&rhs) noexcept = default;
    Ext &operator=(Ext &&rhs) noexcept = default;

    ExtUniquePtrT<T> ptr{nullptr, destroyExt};
};

template <typename T>
struct Clonable : Impl::UniquePtrWrapperOps<Clonable<Ext<T>>> {
    Clonable(T *ptr) : ptr(ptr) {}
    Clonable() = default;
    Clonable(const Clonable &rhs) {
        if (rhs.ptr != nullptr) {
            this->ptr = std::make_unique<T>(*rhs.ptr);
        }
    }

    Clonable &operator=(const Clonable &rhs) {
        if (this == &rhs) {
            return *this;
        }
        if (rhs.ptr == nullptr) {
            ptr.reset();
            return *this;
        }

        this->ptr = std::make_unique<T>(*rhs.ptr);
        return *this;
    }

    ~Clonable() = default;
    Clonable(Clonable &&rhs) noexcept = default;
    Clonable &operator=(Clonable &&rhs) noexcept = default;

    std::unique_ptr<T> ptr;
};