tmpI.H 7.73 KB
Newer Older
1
/*--------------------------------------------------------------------r-------*\
Henry's avatar
Henry committed
2
3
4
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
Henry's avatar
Henry committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    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.

    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
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

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

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

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// * * * * * * * * * * * * * Private Member Operators  * * * * * * * * * * * //

template<class T>
inline void Foam::tmp<T>::operator++()
{
    ptr_->operator++();

    if (ptr_->count() > 1)
    {
        FatalErrorInFunction
            << "Attempt to create more than 2 tmp's referring to"
               " the same object of type " << typeName()
            << abort(FatalError);
    }
}


Henry's avatar
Henry committed
46
47
48
49
50
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<class T>
inline Foam::tmp<T>::tmp(T* tPtr)
:
51
52
    type_(TMP),
    ptr_(tPtr)
53
54
55
56
57
58
59
60
61
{
    if (tPtr && !tPtr->unique())
    {
        FatalErrorInFunction
            << "Attempted construction of a " << typeName()
            << " from non-unique pointer"
            << abort(FatalError);
    }
}
62
63


Henry's avatar
Henry committed
64
65
66
template<class T>
inline Foam::tmp<T>::tmp(const T& tRef)
:
67
68
    type_(CONST_REF),
    ptr_(const_cast<T*>(&tRef))
Henry's avatar
Henry committed
69
70
71
72
73
74
{}


template<class T>
inline Foam::tmp<T>::tmp(const tmp<T>& t)
:
75
76
    type_(t.type_),
    ptr_(t.ptr_)
Henry's avatar
Henry committed
77
{
78
    if (isTmp())
Henry's avatar
Henry committed
79
80
81
    {
        if (ptr_)
        {
82
            operator++();
Henry's avatar
Henry committed
83
84
85
        }
        else
        {
86
            FatalErrorInFunction
87
                << "Attempted copy of a deallocated " << typeName()
Henry's avatar
Henry committed
88
89
90
91
92
93
94
95
96
                << abort(FatalError);
        }
    }
}


template<class T>
inline Foam::tmp<T>::tmp(const tmp<T>& t, bool allowTransfer)
:
97
98
    type_(t.type_),
    ptr_(t.ptr_)
Henry's avatar
Henry committed
99
{
100
    if (isTmp())
Henry's avatar
Henry committed
101
    {
102
        if (ptr_)
Henry's avatar
Henry committed
103
        {
104
            if (allowTransfer)
Henry's avatar
Henry committed
105
            {
106
                t.ptr_ = 0;
Henry's avatar
Henry committed
107
108
109
            }
            else
            {
110
                operator++();
Henry's avatar
Henry committed
111
112
            }
        }
113
114
115
116
117
118
        else
        {
            FatalErrorInFunction
                << "Attempted copy of a deallocated " << typeName()
                << abort(FatalError);
        }
Henry's avatar
Henry committed
119
120
121
122
123
124
125
    }
}


template<class T>
inline Foam::tmp<T>::~tmp()
{
126
    clear();
Henry's avatar
Henry committed
127
128
129
130
131
132
133
134
}


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

template<class T>
inline bool Foam::tmp<T>::isTmp() const
{
135
    return type_ == TMP;
Henry's avatar
Henry committed
136
137
138
139
140
141
}


template<class T>
inline bool Foam::tmp<T>::empty() const
{
142
    return (isTmp() && !ptr_);
Henry's avatar
Henry committed
143
144
145
146
147
148
}


template<class T>
inline bool Foam::tmp<T>::valid() const
{
149
150
151
152
    return (!isTmp() || (isTmp() && ptr_));
}


153
154
155
156
157
158
159
template<class T>
inline Foam::word Foam::tmp<T>::typeName() const
{
    return "tmp<" + word(typeid(T).name()) + '>';
}


160
161
162
template<class T>
inline T& Foam::tmp<T>::ref()
{
163
    if (isTmp())
164
165
166
167
    {
        if (!ptr_)
        {
            FatalErrorInFunction
168
                << typeName() << " deallocated"
169
170
171
172
173
                << abort(FatalError);
        }
    }
    else
    {
174
175
        FatalErrorInFunction
            << "Attempt to acquire non-const reference to const object"
176
            << " from a " << typeName()
177
178
            << abort(FatalError);
    }
179
180

    return *ptr_;
Henry's avatar
Henry committed
181
182
183
184
185
186
}


template<class T>
inline T* Foam::tmp<T>::ptr() const
{
187
    if (isTmp())
Henry's avatar
Henry committed
188
    {
189
190
191
        if (!ptr_)
        {
            FatalErrorInFunction
192
                << typeName() << " deallocated"
193
194
                << abort(FatalError);
        }
Henry's avatar
Henry committed
195

196
197
198
199
        if (!ptr_->unique())
        {
            FatalErrorInFunction
                << "Attempt to acquire pointer to object referred to"
200
                << " by multiple temporaries of type " << typeName()
201
202
                << abort(FatalError);
        }
Henry's avatar
Henry committed
203

204
205
        T* ptr = ptr_;
        ptr_ = 0;
Henry's avatar
Henry committed
206

207
        return ptr;
Henry's avatar
Henry committed
208
209
210
    }
    else
    {
211
        return new T(*ptr_);
Henry's avatar
Henry committed
212
213
214
215
216
217
218
    }
}


template<class T>
inline void Foam::tmp<T>::clear() const
{
219
    if (isTmp() && ptr_)
Henry's avatar
Henry committed
220
    {
221
222
223
224
225
226
227
228
229
230
        if (ptr_->unique())
        {
            delete ptr_;
            ptr_ = 0;
        }
        else
        {
            ptr_->operator--();
            ptr_ = 0;
        }
Henry's avatar
Henry committed
231
232
233
234
235
236
    }
}


// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

237
#ifdef NON_CONST_TMP
Henry's avatar
Henry committed
238
239
240
template<class T>
inline T& Foam::tmp<T>::operator()()
{
241
    if (isTmp())
Henry's avatar
Henry committed
242
243
244
    {
        if (!ptr_)
        {
245
            FatalErrorInFunction
246
                << typeName() << " deallocated"
Henry's avatar
Henry committed
247
248
249
                << abort(FatalError);
        }
    }
250
251
252
253

    // Const-ness is automatically cast-away which is why this operator is
    // deprecated.  Use ref() where non-const access is required.
    return *ptr_;
Henry's avatar
Henry committed
254
}
255
#endif
Henry's avatar
Henry committed
256
257
258
259
260


template<class T>
inline const T& Foam::tmp<T>::operator()() const
{
261
    if (isTmp())
Henry's avatar
Henry committed
262
263
264
    {
        if (!ptr_)
        {
265
            FatalErrorInFunction
266
                << typeName() << " deallocated"
Henry's avatar
Henry committed
267
268
269
                << abort(FatalError);
        }
    }
270
271
272

    // Return const reference
    return *ptr_;
Henry's avatar
Henry committed
273
274
275
276
277
278
279
280
281
282
283
284
285
}


template<class T>
inline Foam::tmp<T>::operator const T&() const
{
    return operator()();
}


template<class T>
inline T* Foam::tmp<T>::operator->()
{
286
    if (isTmp())
Henry's avatar
Henry committed
287
    {
288
289
290
291
292
293
        if (!ptr_)
        {
            FatalErrorInFunction
                << typeName() << " deallocated"
                << abort(FatalError);
        }
Henry's avatar
Henry committed
294
295
296
    }
    else
    {
297
298
        FatalErrorInFunction
            << "Attempt to cast const object to non-const for a " << typeName()
299
            << abort(FatalError);
Henry's avatar
Henry committed
300
    }
301
302

    return ptr_;
Henry's avatar
Henry committed
303
304
305
306
307
308
}


template<class T>
inline const T* Foam::tmp<T>::operator->() const
{
309
    if (isTmp() && !ptr_)
310
    {
311
312
313
        FatalErrorInFunction
            << typeName() << " deallocated"
            << abort(FatalError);
314
    }
315
316

    return ptr_;
Henry's avatar
Henry committed
317
318
319
320
}


template<class T>
321
inline void Foam::tmp<T>::operator=(T* tPtr)
Henry's avatar
Henry committed
322
{
323
324
325
    clear();

    if (!tPtr)
Henry's avatar
Henry committed
326
    {
327
328
329
        FatalErrorInFunction
            << "Attempted copy of a deallocated " << typeName()
            << abort(FatalError);
Henry's avatar
Henry committed
330
331
    }

332
    if (tPtr && !tPtr->unique())
Henry's avatar
Henry committed
333
    {
334
        FatalErrorInFunction
335
336
            << "Attempted assignment of a " << typeName()
            << " to non-unique pointer"
337
338
            << abort(FatalError);
    }
Henry's avatar
Henry committed
339

340
    type_ = TMP;
341
342
343
344
345
346
347
    ptr_ = tPtr;
}


template<class T>
inline void Foam::tmp<T>::operator=(const tmp<T>& t)
{
348
    clear();
349

350
    if (t.isTmp())
351
    {
352
        type_ = TMP;
353
354

        if (!t.ptr_)
Henry's avatar
Henry committed
355
        {
356
            FatalErrorInFunction
357
                << "Attempted assignment to a deallocated " << typeName()
Henry's avatar
Henry committed
358
359
                << abort(FatalError);
        }
360
361

        ptr_ = t.ptr_;
362
        t.ptr_ = 0;
Henry's avatar
Henry committed
363
364
365
    }
    else
    {
366
        FatalErrorInFunction
367
            << "Attempted assignment to a const reference to an object"
Henry's avatar
Henry committed
368
369
370
371
372
373
            << " of type " << typeid(T).name()
            << abort(FatalError);
    }
}


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//- Return the const reference of the non-const reference argument
template<typename T>
inline const T& Const(T& t)
{
    return t;
}


//- Return the const reference of the non-const rvalue reference argument
template<typename T>
inline const T& Const(T&& t)
{
    return t;
}


Henry's avatar
Henry committed
390
// ************************************************************************* //