tmpNrc.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::tmpNrc
26 
27 Description
28  A class for managing temporary objects without reference counting.
29 
30 SourceFiles
31  tmpNrcI.H
32 
33 See also
34  Foam::autoPtr
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef tmpNrc_H
39 #define tmpNrc_H
40 
41 #include "word.H"
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class tmpNrc Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 template<class T>
53 class tmpNrc
54 {
55  // Private data
56 
57  //- Object types
58  enum type
59  {
60  TMP,
61  CONST_REF
62  };
63 
64  //- Type of object
65  mutable type type_;
66 
67  //- Pointer to object
68  mutable T* ptr_;
69 
70 
71 public:
72 
73  // Null reference counter class
74  class refCount
75  {
76  public:
77 
78  refCount()
79  {}
80  };
81 
82 
83  // Constructors
84 
85  //- Store object pointer
86  inline explicit tmpNrc(T* = 0);
87 
88  //- Store object const reference
89  inline tmpNrc(const T&);
90 
91  //- Construct copy and increment reference count
92  inline tmpNrc(const tmpNrc<T>&);
93 
94  //- Construct copy transferring content of temporary if required
95  inline tmpNrc(const tmpNrc<T>&, bool allowTransfer);
96 
97 
98  //- Destructor: deletes temporary object when the reference count is 0
99  inline ~tmpNrc();
100 
101 
102  // Member Functions
103 
104  // Access
105 
106  //- Return true if this is really a temporary object
107  inline bool isTmp() const;
108 
109  //- Return true if this temporary object empty,
110  // ie, a temporary without allocation
111  inline bool empty() const;
112 
113  //- Is this temporary object valid,
114  // ie, it is a reference or a temporary that has been allocated
115  inline bool valid() const;
116 
117  //- Return the type name of the tmpNrc
118  // constructed from the type name of T
119  inline word typeName() const;
120 
121 
122  // Edit
123 
124  //- Return non-const reference or generate a fatal error
125  // if the object is const.
126  inline T& ref();
127 
128  //- Return tmpNrc pointer for reuse.
129  // Returns a clone if the object is not a temporary
130  inline T* ptr() const;
131 
132  //- If object pointer points to valid object:
133  // delete object and set pointer to nullptr
134  inline void clear() const;
135 
136 
137  // Member operators
138 
139  //- Const dereference operator
140  inline const T& operator()() const;
141 
142  //- Const cast to the underlying type reference
143  inline operator const T&() const;
144 
145  //- Return object pointer
146  inline T* operator->();
147 
148  //- Return const object pointer
149  inline const T* operator->() const;
150 
151  //- Assignment to pointer changing this tmpNrc to a temporary T
152  inline void operator=(T*);
153 
154  //- Assignment transferring the temporary T to this tmpNrc
155  inline void operator=(const tmpNrc<T>&);
156 };
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 } // End namespace Foam
162 
163 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
164 
165 #include "tmpNrcI.H"
166 
167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
168 
169 #endif
170 
171 // ************************************************************************* //
T * ptr() const
Return tmpNrc pointer for reuse.
Definition: tmpNrcI.H:160
~tmpNrc()
Destructor: deletes temporary object when the reference count is 0.
Definition: tmpNrcI.H:99
T & ref()
Return non-const reference or generate a fatal error.
Definition: tmpNrcI.H:136
bool valid() const
Is this temporary object valid,.
Definition: tmpNrcI.H:122
void clear() const
If object pointer points to valid object:
Definition: tmpNrcI.H:184
bool empty() const
Return true if this temporary object empty,.
Definition: tmpNrcI.H:115
A class for handling words, derived from string.
Definition: word.H:59
bool isTmp() const
Return true if this is really a temporary object.
Definition: tmpNrcI.H:108
word typeName() const
Return the type name of the tmpNrc.
Definition: tmpNrcI.H:129
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
A class for managing temporary objects without reference counting.
Definition: tmpNrc.H:52
T * operator->()
Return object pointer.
Definition: tmpNrcI.H:222
void operator=(T *)
Assignment to pointer changing this tmpNrc to a temporary T.
Definition: tmpNrcI.H:259
const T & operator()() const
Const dereference operator.
Definition: tmpNrcI.H:197
Namespace for OpenFOAM.
tmpNrc(T *=0)
Store object pointer.
Definition: tmpNrcI.H:32