autoPtrI.H 4.87 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2011-2017 OpenFOAM Foundation
9
    Copyright (C) 2016-2020 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18 19 20 21 22 23 24

    OpenFOAM 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 General Public License
    for more details.

    You should have received a copy of the GNU General Public License
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29

\*---------------------------------------------------------------------------*/

#include "error.H"
30
#include <typeinfo>
31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

template<class T>
template<class... Args>
inline Foam::autoPtr<T> Foam::autoPtr<T>::New(Args&&... args)
{
    return autoPtr<T>(new T(std::forward<Args>(args)...));
}


template<class T>
template<class U, class... Args>
inline Foam::autoPtr<T> Foam::autoPtr<T>::NewFrom(Args&&... args)
{
    return autoPtr<T>(new U(std::forward<Args>(args)...));
}


50 51
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

52
template<class T>
53 54 55 56 57 58 59 60
inline constexpr Foam::autoPtr<T>::autoPtr() noexcept
:
    ptr_(nullptr)
{}


template<class T>
inline constexpr Foam::autoPtr<T>::autoPtr(std::nullptr_t) noexcept
61 62 63 64 65
:
    ptr_(nullptr)
{}


66
template<class T>
67
inline Foam::autoPtr<T>::autoPtr(T* p) noexcept
68
:
Mark Olesen's avatar
Mark Olesen committed
69
    ptr_(p)
70 71 72 73
{}


template<class T>
74
inline Foam::autoPtr<T>::autoPtr(autoPtr<T>&& ap) noexcept
75
:
76 77
    ptr_(ap.release())
{}
78 79


80
template<class T>
81 82 83 84 85 86 87 88 89
template<class U>
inline Foam::autoPtr<T>::autoPtr(autoPtr<U>&& ap)
:
    ptr_(ap.release())
{}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

90
template<class T>
91
inline Foam::autoPtr<T>::~autoPtr() noexcept
92
{
93
    reset(nullptr);
94 95 96 97 98 99
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class T>
100
inline T* Foam::autoPtr<T>::release() noexcept
101
{
102 103 104 105
    T* p = ptr_;
    ptr_ = nullptr;
    return p;
}
106

107

108
template<class T>
109
inline void Foam::autoPtr<T>::reset(T* p) noexcept
110
{
111 112
    if (ptr_) delete ptr_;
    ptr_ = p;
113 114 115 116
}


template<class T>
117
inline void Foam::autoPtr<T>::reset(autoPtr<T>&& ap) noexcept
118
{
119
    reset(ap.release());
120 121 122
}


123
template<class T>
124
inline void Foam::autoPtr<T>::swap(autoPtr<T>& other) noexcept
125
{
126
    // Self-swap is effectively ignored
127 128 129
    T* p = ptr_;
    ptr_ = other.ptr_;
    other.ptr_ = p;
130 131 132
}


133
template<class T>
134 135
template<class... Args>
inline Foam::autoPtr<T> Foam::autoPtr<T>::clone(Args&&... args) const
136
{
137
    if (ptr_)
138
    {
139
        return autoPtr<T>(ptr_->clone(std::forward<Args>(args)...).ptr());
140 141
    }

142
    return autoPtr<T>();
143 144 145
}


146 147
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

148
template<class T>
149
inline T& Foam::autoPtr<T>::operator*()
150 151 152
{
    if (!ptr_)
    {
153
        FatalErrorInFunction
154
            << "unallocated autoPtr of type " << typeid(T).name()
155 156 157 158 159 160 161
            << abort(FatalError);
    }
    return *ptr_;
}


template<class T>
162
inline const T& Foam::autoPtr<T>::operator*() const
163
{
164
    return const_cast<autoPtr<T>*>(this)->operator*();
165 166 167 168 169 170 171 172
}


template<class T>
inline T* Foam::autoPtr<T>::operator->()
{
    if (!ptr_)
    {
173
        FatalErrorInFunction
174
            << "unallocated autoPtr of type " << typeid(T).name()
175 176 177 178 179 180 181 182 183
            << abort(FatalError);
    }
    return ptr_;
}


template<class T>
inline const T* Foam::autoPtr<T>::operator->() const
{
184
    return const_cast<autoPtr<T>*>(this)->operator->();
185 186 187
}


188
template<class T>
189
inline T& Foam::autoPtr<T>::operator()()
190
{
191 192 193 194 195 196 197 198
    return operator*();
}


template<class T>
inline const T& Foam::autoPtr<T>::operator()() const
{
    return operator*();
199 200 201
}


202
template<class T>
203
inline void Foam::autoPtr<T>::operator=(autoPtr<T>&& ap) noexcept
204 205 206
{
    if (this != &ap)
    {
207
        // Ignore self-assignment
208
        reset(ap.release());
209 210 211 212
    }
}


213 214 215 216 217 218
template<class T>
template<class U>
inline void Foam::autoPtr<T>::operator=(autoPtr<U>&& ap) noexcept
{
    if (this != &ap)
    {
219
        // Ignore self-assignment
220 221 222 223 224 225 226 227
        reset(ap.release());
    }
}


template<class T>
inline void Foam::autoPtr<T>::operator=(std::nullptr_t) noexcept
{
228
    reset(nullptr);
229 230 231
}


232
// ************************************************************************* //