sharedRegIOobjectI.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) 2025 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 \*---------------------------------------------------------------------------*/
25 
26 #include "sharedRegIOobject.H"
27 #include "objectRegistry.H"
28 
29 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30 
31 template<class Type>
33 :
34  ptr_(nullptr)
35 {}
36 
37 
38 template<class Type>
39 template<class ... Args>
41 (
42  const IOobject& io,
43  const Args& ... args
44 )
45 :
46  ptr_(nullptr)
47 {
48  set(io, args ...);
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
53 
54 template<class Type>
56 {}
57 
58 
59 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
60 
61 template<class Type>
63 {
64  return ptr_;
65 }
66 
67 
68 template<class Type>
69 template<class ... Args>
71 (
72  const IOobject& io,
73  const Args& ... args
74 )
75 {
76  clear();
77 
78  if (!io.db().found(io.name()))
79  {
80  ptr_ = new Type(io, args ...);
81 
82  ptr_->checkIn();
83  }
84  else
85  {
86  ptr_ = &io.db().lookupObjectRef<Type>(io.name());
87 
88  ptr_->operator++();
89  }
90 }
91 
92 
93 template<class Type>
95 {
96  if (!valid())
97  {
99  << "Attempted access of invalid object"
100  << exit(FatalError);
101  }
102 
103  return *ptr_;
104 }
105 
106 
107 template<class Type>
109 {
110  if (valid())
111  {
112  if (ptr_->unique())
113  {
114  ptr_->checkOut();
115  }
116  else
117  {
118  ptr_->operator--();
119  }
120 
121  ptr_ = nullptr;
122  }
123 }
124 
125 
126 template<class Type>
128 {
129  if (!valid())
130  {
132  << "Attempted access of invalid object"
133  << exit(FatalError);
134  }
135 
136  return *ptr_;
137 }
138 
139 
140 // ************************************************************************* //
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:138
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:309
const word & name() const
Return name.
Definition: IOobject.H:307
Type & lookupObjectRef(const word &name) const
Lookup and return the object reference of the given Type.
void set(const IOobject &io, const Args &... args)
Set the object.
sharedRegIOobject()
Default construct.
bool valid() const
Is this object set?
const Type & operator()() const
Return a reference to the object.
void clear()
Clear the object.
Type & ref() const
Return a reference to the object.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
tUEqn clear()
bool valid(const PtrList< ModelType > &l)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
Foam::argList args(argc, argv)