tmp.H 9.08 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-2016 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 30

Class
    Foam::tmp

Description
31 32 33 34 35 36
    A class for managing temporary objects.

    This is a combination of std::shared_ptr (with intrusive ref-counting)
    and a shared_ptr without ref-counting and null deleter.
    This allows the tmp to double as a pointer management and an indirect
    pointer to externally allocated objects.
37 38 39 40

SourceFiles
    tmpI.H

41
See also
42
    Foam::autoPtr
43
    Foam::refPtr
44
    Foam::refCount
45

46 47 48 49 50 51
\*---------------------------------------------------------------------------*/

#ifndef tmp_H
#define tmp_H

#include "refCount.H"
52
#include "word.H"
53
#include "stdFoam.H"
54 55 56 57 58 59 60

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{

/*---------------------------------------------------------------------------*\
61
                             Class tmp Declaration
62 63 64 65 66
\*---------------------------------------------------------------------------*/

template<class T>
class tmp
{
67
    // Private Data
68

69
        //- Object types
70
        enum refType
71
        {
72
            PTR,    //!< Managing a pointer (ref-counted)
73 74
            CREF,   //!< Using (const) reference to an object
            REF     //!< Using (non-const) reference to an object
75 76
        };

77
        //- The managed pointer or address of the object (reference)
78 79
        mutable T* ptr_;

80
        //- The type (managed pointer | object reference)
81
        mutable refType type_;
82

83

84
    // Private Member Operators
85

86
        //- Increment the ref-count for a managed pointer
87 88
        //- and check that it is not oversubscribed
        inline void incrCount();
89 90


91 92
public:

93 94 95 96 97 98 99 100 101 102
    // STL type definitions

        //- Type of object being managed or referenced
        typedef T element_type;

        //- Pointer to type of object being managed or referenced
        typedef T* pointer;


    //- Reference counter class
103 104 105
    typedef Foam::refCount refCount;


106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    // Factory Methods

        //- Construct tmp of T with forwarding arguments
        //  \param args list of arguments with which an instance of T
        //      will be constructed.
        //
        //  \note Similar to std::make_shared, but the overload for
        //      array types is not disabled.
        template<class... Args>
        inline static tmp<T> New(Args&&... args);

        //- Construct tmp from derived type with forwarding arguments
        //  \param args list of arguments with which an instance of U
        //      will be constructed.
        //
        //  \note Similar to New but for derived types
        template<class U, class... Args>
        inline static tmp<T> NewFrom(Args&&... args);


126 127
    // Constructors

128
        //- Default construct, no managed pointer.
129 130 131 132
        inline constexpr tmp() noexcept;

        //- Construct with no managed pointer.
        inline constexpr tmp(std::nullptr_t) noexcept;
133

134
        //- Construct, taking ownership of the pointer.
135
        inline explicit tmp(T* p);
136

137 138
        //- Construct for a const reference to an object.
        inline tmp(const T& obj) noexcept;
139

140 141
        //- Move construct, transferring ownership.
        //  Does not affect ref-count
142
        inline tmp(tmp<T>&& rhs) noexcept;
143

144 145 146
        //- Move construct, transferring ownership.
        //  Does not affect ref-count
        //  \note Non-standard definition - should be non-const
147
        inline tmp(const tmp<T>&& rhs) noexcept;
148

149 150
        //- Copy construct, incrementing ref-count of managed pointer.
        //  \note Non-standard definition - should be non-const
151
        inline tmp(const tmp<T>& rhs);
152

153 154
        //- Copy/move construct. Optionally reusing ref-counted pointer.
        inline tmp(const tmp<T>& rhs, bool reuse);
155

156 157

    //- Destructor: deletes managed pointer when the ref-count is 0
158
    inline ~tmp();
159 160 161 162


    // Member Functions

163 164 165 166
        //- The type-name, constructed from type-name of T
        inline static word typeName();


167 168
    // Query

169 170 171 172
        //- Deprecated(2020-07) True if a null managed pointer
        //
        //  \deprecated(2020-07) - use bool operator
        FOAM_DEPRECATED_FOR(2020-07, "bool operator")
173
        bool empty() const noexcept { return !ptr_; }
174

175 176
        //- True for non-null pointer/reference
        bool valid() const noexcept { return ptr_; }
177

178 179 180
        //- True if this is a managed pointer (not a reference)
        bool is_pointer() const noexcept { return type_ == PTR; }

181
        //- Identical to is_pointer()
182
        bool isTmp() const noexcept { return type_ == PTR; }
183

184
        //- True if this is a non-null managed pointer with a unique ref-count
185
        inline bool movable() const noexcept;
186 187


188
    // Access
189

190
        //- Return pointer without nullptr checking.
191
        T* get() noexcept { return ptr_; }
192 193

        //- Return const pointer without nullptr checking.
194
        const T* get() const noexcept { return ptr_; }
195

196 197
        //- Return const reference to the object or to the contents
        //- of a (non-null) managed pointer.
198 199
        //  Fatal for a null managed pointer
        inline const T& cref() const;
200

201 202 203 204
        //- Return non-const reference to the contents of a non-null
        //- managed pointer.
        //  Fatal for a null managed pointer or if the object is const.
        inline T& ref() const;
205

206 207
        //- Return non-const reference to the object or to the contents
        //- of a (non-null) managed pointer, with an additional const_cast.
208
        //  Fatal for a null pointer.
209
        inline T& constCast() const;
210

211

212 213
    // Edit

214
        //- Return managed pointer for reuse, or clone() the object reference.
215 216 217 218
        inline T* ptr() const;

        //- If object pointer points to valid object:
        //- delete object and set pointer to nullptr
219
        inline void clear() const noexcept;
220 221

        //- Delete managed temporary object and set to new given pointer
222
        inline void reset(T* p = nullptr) noexcept;
223

224 225 226
        //- Clear existing and transfer ownership.
        inline void reset(tmp<T>&& other) noexcept;

227
        //- Delete managed temporary object and set to (const) reference
228 229
        inline void cref(const T& obj) noexcept;

230 231 232
        //- Delete managed temporary object and set to (non-const) reference
        inline void ref(T& obj) noexcept;

233
        //- Swaps the managed object with other.
234
        inline void swap(tmp<T>& other) noexcept;
235 236


237
    // Member Operators
238

239
        //- Identical to cref() method.
240 241
        inline const T& operator()() const;

242
        //- Cast to underlying data type, using the cref() method.
243 244
        inline operator const T&() const;

245 246
        //- Dereferences (const) pointer to the managed object.
        //  Fatal for a null managed pointer.
247 248
        inline const T* operator->() const;

249 250 251 252
        //- Dereferences (non-const) pointer to the managed object.
        //  Fatal for a null managed pointer or if the object is const.
        inline T* operator->();

253 254
        //- Non-null pointer/reference : valid()
        explicit operator bool() const noexcept { return ptr_; }
255

256 257
        //- Transfer ownership of the managed pointer.
        //  Fatal for a null managed pointer or if the object is const.
258
        inline void operator=(const tmp<T>& other);
259

260 261 262
        //- Clear existing and transfer ownership.
        inline void operator=(tmp<T>&& other) noexcept;

263 264 265
        //- Take ownership of the pointer.
        //  Fatal for a null pointer, or when the pointer is non-unique.
        inline void operator=(T* p);
266

267 268
        //- Reset via assignment from literal nullptr
        inline void operator=(std::nullptr_t) noexcept;
269 270 271
};


272 273
// Global Functions

274
//- Specialized Swap algorithm for tmp.
275 276 277 278 279 280 281 282
//  Swaps the pointers and types of lhs and rhs. Calls \c lhs.swap(rhs)
template<class T>
void Swap(tmp<T>& lhs, tmp<T>& rhs)
{
    lhs.swap(rhs);
}


283 284 285 286 287 288 289 290 291 292 293 294 295
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#include "tmpI.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif

// ************************************************************************* //