tmpI.H 9.87 KB
Newer Older
Henry Weller's avatar
Henry Weller committed
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) 2018-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
// * * * * * * * * * * * * * Private Member Operators  * * * * * * * * * * * //

template<class T>
35
inline void Foam::tmp<T>::incrCount()
36 37 38 39 40 41 42
{
    ptr_->operator++();

    if (ptr_->count() > 1)
    {
        FatalErrorInFunction
            << "Attempt to create more than 2 tmp's referring to"
43 44
               " the same object of type "
            << tmp<T>::typeName()
45 46 47 48 49
            << abort(FatalError);
    }
}


50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

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


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


68 69 70 71 72 73 74
template<class T>
inline Foam::word Foam::tmp<T>::typeName()
{
    return "tmp<" + word(typeid(T).name()) + '>';
}


75 76 77
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<class T>
78
inline constexpr Foam::tmp<T>::tmp() noexcept
79
:
80
    ptr_(nullptr),
81 82 83 84 85 86 87 88 89
    type_(PTR)
{}


template<class T>
inline constexpr Foam::tmp<T>::tmp(std::nullptr_t) noexcept
:
    ptr_(nullptr),
    type_(PTR)
90 91 92 93 94 95
{}


template<class T>
inline Foam::tmp<T>::tmp(T* p)
:
96
    ptr_(p),
97
    type_(PTR)
98
{
99
    if (p && !p->unique())
100 101
    {
        FatalErrorInFunction
102 103
            << "Attempted construction of a "
            << this->typeName()
104 105 106 107
            << " from non-unique pointer"
            << abort(FatalError);
    }
}
108 109


110
template<class T>
111
inline Foam::tmp<T>::tmp(const T& obj) noexcept
112
:
113 114
    ptr_(const_cast<T*>(&obj)),
    type_(CREF)
115 116 117
{}


118
template<class T>
119
inline Foam::tmp<T>::tmp(tmp<T>&& rhs) noexcept
120
:
121 122
    ptr_(rhs.ptr_),
    type_(rhs.type_)
123
{
124 125
    rhs.ptr_ = nullptr;
    rhs.type_ = PTR;
126 127 128 129
}


template<class T>
130
inline Foam::tmp<T>::tmp(const tmp<T>&& rhs) noexcept
131
:
132 133
    ptr_(rhs.ptr_),
    type_(rhs.type_)
134
{
135 136
    rhs.ptr_ = nullptr;
    rhs.type_ = PTR;
137 138 139
}


140
template<class T>
141
inline Foam::tmp<T>::tmp(const tmp<T>& rhs)
142
:
143 144
    ptr_(rhs.ptr_),
    type_(rhs.type_)
145
{
146
    if (type_ == PTR)
147 148 149
    {
        if (ptr_)
        {
150
            this->incrCount();
151 152 153
        }
        else
        {
154
            FatalErrorInFunction
155 156
                << "Attempted copy of a deallocated "
                << this->typeName()
157 158 159 160 161 162
                << abort(FatalError);
        }
    }
}


163
template<class T>
164
inline Foam::tmp<T>::tmp(const tmp<T>& rhs, bool reuse)
165
:
166 167
    ptr_(rhs.ptr_),
    type_(rhs.type_)
168
{
169
    if (type_ == PTR)
170
    {
171
        if (ptr_)
172
        {
173
            if (reuse)
174
            {
175 176
                rhs.ptr_ = nullptr;
                // Note: rhs.type_ already set as PTR
177 178 179
            }
            else
            {
180
                this->incrCount();
181 182
            }
        }
183 184 185
        else
        {
            FatalErrorInFunction
186 187
                << "Attempted copy of a deallocated "
                << this->typeName()
188 189
                << abort(FatalError);
        }
190 191 192 193
    }
}


194
template<class T>
Mark Olesen's avatar
Mark Olesen committed
195
inline Foam::tmp<T>::~tmp()
196
{
197
    clear();
198 199 200 201 202
}


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

203
template<class T>
204
inline bool Foam::tmp<T>::movable() const noexcept
205 206
{
    return (type_ == PTR && ptr_ && ptr_->unique());
207 208 209 210
}


template<class T>
211
inline const T& Foam::tmp<T>::cref() const
212
{
213
    if (type_ == PTR)
214 215 216 217
    {
        if (!ptr_)
        {
            FatalErrorInFunction
218
                << this->typeName() << " deallocated"
219 220
                << abort(FatalError);
        }
221
    }
222

223 224 225 226 227 228 229
    return *ptr_; // const reference
}


template<class T>
inline T& Foam::tmp<T>::ref() const
{
230
    if (type_ == PTR)
231 232
    {
        if (!ptr_)
233 234
        {
            FatalErrorInFunction
235
                << this->typeName() << " deallocated"
236 237
                << abort(FatalError);
        }
238
    }
239
    else if (type_ == CREF)
240 241 242
    {
        FatalErrorInFunction
            << "Attempted non-const reference to const object from a "
243
            << this->typeName()
244 245
            << abort(FatalError);
    }
246

247 248
    return *ptr_; // non-const reference
}
249

250 251 252 253

template<class T>
inline T& Foam::tmp<T>::constCast() const
{
254
    return const_cast<T&>(cref());
255 256 257 258
}


template<class T>
259
inline T* Foam::tmp<T>::ptr() const
260
{
261 262 263
    if (!ptr_)
    {
        FatalErrorInFunction
264
            << this->typeName() << " deallocated"
265 266 267
            << abort(FatalError);
    }

268
    if (type_ == PTR)
269
    {
270
        if (!ptr_->unique())
271 272 273
        {
            FatalErrorInFunction
                << "Attempt to acquire pointer to object referred to"
274 275
                << " by multiple temporaries of type "
                << this->typeName()
276 277 278
                << abort(FatalError);
        }

279
        // Release pointer
280
        T* p = ptr_;
281 282
        ptr_ = nullptr;

283
        return p;
284
    }
285

286
    return ptr_->clone().ptr();
287 288 289 290
}


template<class T>
291
inline void Foam::tmp<T>::clear() const noexcept
292
{
293
    if (type_ == PTR && ptr_)
294
    {
295 296 297 298 299 300 301 302
        if (ptr_->unique())
        {
            delete ptr_;
        }
        else
        {
            ptr_->operator--();
        }
303
        ptr_ = nullptr;
304 305 306 307
    }
}


308
template<class T>
309
inline void Foam::tmp<T>::reset(T* p) noexcept
310 311 312 313 314 315 316
{
    clear();
    ptr_ = p;
    type_ = PTR;
}


317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
template<class T>
inline void Foam::tmp<T>::reset(tmp<T>&& other) noexcept
{
    if (&other == this)
    {
        return;  // Self-assignment is a no-op
    }

    clear();
    ptr_ = other.ptr_;
    type_ = other.type_;

    other.ptr_ = nullptr;
    other.type_ = PTR;
}


334 335 336 337 338 339 340 341 342
template<class T>
inline void Foam::tmp<T>::cref(const T& obj) noexcept
{
    clear();
    ptr_ = const_cast<T*>(&obj);
    type_ = CREF;
}


343 344 345 346 347 348 349 350 351
template<class T>
inline void Foam::tmp<T>::ref(T& obj) noexcept
{
    clear();
    ptr_ = &obj;
    type_ = REF;
}


352 353 354
template<class T>
inline void Foam::tmp<T>::swap(tmp<T>& other) noexcept
{
355 356 357 358 359
    if (&other == this)
    {
        return;  // Self-swap is a no-op
    }

360
    // Swap is just copy/assign for pointer and enum types
361 362 363 364 365 366 367 368 369 370
    T* p = ptr_;
    ptr_ = other.ptr_;
    other.ptr_ = p;

    refType t = type_;
    type_ = other.type_;
    other.type_ = t;
}


371 372 373
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

template<class T>
Mark Olesen's avatar
Mark Olesen committed
374
inline const T& Foam::tmp<T>::operator()() const
375
{
376
    return cref();
377 378 379 380
}


template<class T>
Mark Olesen's avatar
Mark Olesen committed
381
inline Foam::tmp<T>::operator const T&() const
382
{
383 384 385 386 387 388 389
    return cref();
}


template<class T>
inline const T* Foam::tmp<T>::operator->() const
{
390
    if (type_ == PTR)
391
    {
392 393 394 395 396 397
        if (!ptr_)
        {
            FatalErrorInFunction
                << this->typeName() << " deallocated"
                << abort(FatalError);
        }
398 399 400
    }

    return ptr_;
401 402 403 404
}


template<class T>
Mark Olesen's avatar
Mark Olesen committed
405
inline T* Foam::tmp<T>::operator->()
406
{
407
    if (type_ == PTR)
408
    {
409 410 411
        if (!ptr_)
        {
            FatalErrorInFunction
412
                << this->typeName() << " deallocated"
413 414
                << abort(FatalError);
        }
415
    }
416
    else if (type_ == CREF)
417
    {
418
        FatalErrorInFunction
419 420
            << "Attempt to cast const object to non-const for a "
            << this->typeName()
421
            << abort(FatalError);
422
    }
423 424

    return ptr_;
425 426 427
}


428
template<class T>
429
inline void Foam::tmp<T>::operator=(const tmp<T>& other)
430
{
431
    if (&other == this)
432 433 434 435
    {
        return;  // Self-assignment is a no-op
    }

436
    clear();
437

438
    if (other.type_ == PTR)
439
    {
440
        ptr_ = other.ptr_;
441
        type_ = PTR;
442
        other.ptr_ = nullptr;
443

444
        if (!ptr_)
445
        {
446
            FatalErrorInFunction
447 448
                << "Attempted assignment of a deallocated "
                << this->typeName()
449 450 451 452 453
                << abort(FatalError);
        }
    }
    else
    {
454
        FatalErrorInFunction
455 456
            << "Attempted assignment of an object reference of type "
            << typeid(T).name()
457 458 459 460 461
            << abort(FatalError);
    }
}


462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
template<class T>
inline void Foam::tmp<T>::operator=(tmp<T>&& other) noexcept
{
    if (&other == this)
    {
        return;  // Self-assignment is a no-op
    }

    clear();
    ptr_ = other.ptr_;
    type_ = other.type_;

    other.ptr_ = nullptr;
    other.type_ = PTR;
}


479 480 481 482 483 484
template<class T>
inline void Foam::tmp<T>::operator=(T* p)
{
    if (!p)
    {
        FatalErrorInFunction
485 486
            << "Attempted copy of a deallocated "
            << this->typeName()
487 488 489 490 491
            << abort(FatalError);
    }
    else if (!p->unique())
    {
        FatalErrorInFunction
492 493
            << "Attempted assignment of a "
            << this->typeName()
494 495 496 497
            << " to non-unique pointer"
            << abort(FatalError);
    }

498 499 500 501 502 503 504 505
    reset(p);
}


template<class T>
inline void Foam::tmp<T>::operator=(std::nullptr_t) noexcept
{
    reset(nullptr);
506 507 508
}


509
// ************************************************************************* //