autoPtr.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2013 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::autoPtr
26 
27 Description
28  An auto-pointer similar to the STL auto_ptr but with automatic casting
29  to a reference to the type and with pointer allocation checking on access.
30 
31 SourceFiles
32  autoPtrI.H
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef autoPtr_H
37 #define autoPtr_H
38 
39 #include <cstddef>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class autoPtr Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 template<class T>
51 class autoPtr
52 {
53  // Public data
54 
55  //- Pointer to object
56  mutable T* ptr_;
57 
58 
59 public:
60 
61  // Constructors
62 
63  //- Store object pointer
64  inline explicit autoPtr(T* = 0);
65 
66  //- Construct as copy by transferring pointer to this autoPtr and
67  // setting the arguments pointer to NULL
68  inline autoPtr(const autoPtr<T>&);
69 
70  //- Construct either by transferring pointer or cloning. Should
71  // only be called with type that supports cloning.
72  inline autoPtr(const autoPtr<T>&, const bool reUse);
73 
74 
75  //- Destructor, delete object if pointer is not NULL
76  inline ~autoPtr();
77 
78 
79  // Member Functions
80 
81  // Check
82 
83  //- Return true if the autoPtr is empty (ie, no pointer set).
84  inline bool empty() const;
85 
86  //- Return true if the autoPtr valid (ie, the pointer is set).
87  inline bool valid() const;
88 
89 
90  // Edit
91 
92  //- Return object pointer for reuse
93  inline T* ptr();
94 
95  //- Set pointer to that given.
96  // If object pointer already set issue a FatalError.
97  inline void set(T*);
98 
99  //- If object pointer already set, delete object and set to given
100  // pointer
101  inline void reset(T* = 0);
102 
103  //- Delete object (if the pointer is valid) and set pointer to NULL.
104  inline void clear();
105 
106 
107  // Member operators
108 
109  //- Return reference to the object data
110  inline T& operator()();
111 
112  //- Return const reference to the object data
113  inline const T& operator()() const;
114 
115  //- Const cast to the underlying type reference
116  inline operator const T&() const;
117 
118  //- Return object pointer
119  inline T* operator->();
120 
121  //- Return const object pointer
122  inline const T* operator->() const;
123 
124  //- Take over the object pointer from parameter
125  inline void operator=(const autoPtr<T>&);
126 };
127 
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 } // End namespace Foam
132 
133 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 
135 #include "autoPtrI.H"
136 
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 
139 #endif
140 
141 // ************************************************************************* //
Foam::autoPtr::ptr_
T * ptr_
Pointer to object.
Definition: autoPtr.H:55
Foam::autoPtr::operator->
T * operator->()
Return object pointer.
Definition: autoPtrI.H:172
Foam::autoPtr::ptr
T * ptr()
Return object pointer for reuse.
Definition: autoPtrI.H:90
Foam::autoPtr::empty
bool empty() const
Return true if the autoPtr is empty (ie, no pointer set).
Definition: autoPtrI.H:76
autoPtrI.H
Foam::autoPtr::operator()
T & operator()()
Return reference to the object data.
Definition: autoPtrI.H:135
Foam::autoPtr::operator=
void operator=(const autoPtr< T > &)
Take over the object pointer from parameter.
Definition: autoPtrI.H:194
Foam::autoPtr::~autoPtr
~autoPtr()
Destructor, delete object if pointer is not NULL.
Definition: autoPtrI.H:67
Foam::autoPtr::autoPtr
autoPtr(T *=0)
Store object pointer.
Foam::autoPtr::set
void set(T *)
Set pointer to that given.
Definition: autoPtrI.H:99
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::autoPtr::valid
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set).
Definition: autoPtrI.H:83
Foam::autoPtr::clear
void clear()
Delete object (if the pointer is valid) and set pointer to NULL.
Definition: autoPtrI.H:126
Foam::autoPtr::reset
void reset(T *=0)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114