IOobjectOption.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011 OpenFOAM Foundation
9  Copyright (C) 2016-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  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 Class
28  Foam::IOobjectOption
29 
30 Description
31  A simple container of IOobject preferences.
32  Can also be used for general handling of read/no-read/read-if-present
33  logic outside of an IOobject.
34 
35 Note
36  In the future may include an Enumeration defining register preferences
37  (NO_REGISTER, REGISTER)
38 
39 See also
40  Foam::IOobject
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef Foam_IOobjectOption_H
45 #define Foam_IOobjectOption_H
46 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 /*---------------------------------------------------------------------------*\
53  Class IOobjectOption Declaration
54 \*---------------------------------------------------------------------------*/
55 
56 class IOobjectOption
57 {
58 public:
59 
60  // Public Data Types
61 
62  //- Enumeration defining read preferences
63  // Lowest bit encodes must read
64  enum readOption : unsigned char
65  {
67  NO_READ = 0,
68 
70  MUST_READ = 0x1,
71 
74 
76  READ_IF_PRESENT = 0x4
77  };
78 
79  //- Enumeration defining write preferences
80  enum writeOption : unsigned char
81  {
83  NO_WRITE = 0,
84 
86  AUTO_WRITE = 0x10
87  };
88 
89  //- Enumeration for use with registerObject().
90  //- Values map to bool (false/true)
91  enum registerOption : unsigned char
92  {
95 
98  };
99 
100 
101 private:
102 
103  // Private Data
104 
105  //- Read option
106  readOption readOpt_;
107 
108  //- Write option
109  writeOption writeOpt_;
110 
111  //- Should created objects be registered?
112  bool registerObject_;
113 
114  //- Is object same for all processors?
115  bool globalObject_;
116 
117 
118 public:
119 
120  // Constructors
121 
122  //- Default construct (NO_READ, NO_WRITE, register, non-global)
123  //- or construct with specified options
124  constexpr IOobjectOption
125  (
126  readOption rOpt = readOption::NO_READ,
127  writeOption wOpt = writeOption::NO_WRITE,
128  registerOption registerObject = registerOption::REGISTER,
129  bool globalObject = false
130  ) noexcept
131  :
132  readOpt_(rOpt),
133  writeOpt_(wOpt),
134  registerObject_(registerObject),
135  globalObject_(globalObject)
136  {}
137 
138  //- Construct NO_WRITE with specified read/register options
139  constexpr IOobjectOption
140  (
141  readOption rOpt,
142  registerOption registerObject = registerOption::REGISTER,
143  bool globalObject = false
144  ) noexcept
145  :
146  readOpt_(rOpt),
147  writeOpt_(writeOption::NO_WRITE),
148  registerObject_(registerObject),
149  globalObject_(globalObject)
150  {}
151 
152  //- Construct NO_READ with specified write/register options
153  constexpr IOobjectOption
154  (
155  writeOption wOpt,
156  registerOption registerObject = registerOption::REGISTER,
157  bool globalObject = false
158  ) noexcept
159  :
160  readOpt_(readOption::NO_READ),
161  writeOpt_(wOpt),
162  registerObject_(registerObject),
163  globalObject_(globalObject)
164  {}
165 
166  //- Construct (NO_READ, NO_WRITE) with specified register option
167  constexpr IOobjectOption
168  (
170  bool globalObject = false
171  ) noexcept
172  :
173  readOpt_(readOption::NO_READ),
174  writeOpt_(writeOption::NO_WRITE),
175  registerObject_(registerObject),
176  globalObject_(globalObject)
177  {}
178 
179  //- Construct from components
180  //- with specified register option as bool
181  constexpr IOobjectOption
182  (
183  readOption rOpt,
185  bool registerObject,
186  bool globalObject = false
187  ) noexcept
188  :
189  readOpt_(rOpt),
190  writeOpt_(wOpt),
191  registerObject_(registerObject ? REGISTER : NO_REGISTER),
192  globalObject_(globalObject)
193  {}
194 
195  //- Construct (NO_READ, NO_WRITE)
196  //- with specified register option as bool
197  explicit constexpr IOobjectOption
198  (
199  bool registerObject,
200  bool globalObject = false
201  ) noexcept
202  :
203  readOpt_(readOption::NO_READ),
204  writeOpt_(writeOption::NO_WRITE),
205  registerObject_(registerObject ? REGISTER : NO_REGISTER),
206  globalObject_(globalObject)
207  {}
208 
209 
210  // Member Functions
211 
212  //- Get the read option
213  readOption readOpt() const noexcept { return readOpt_; }
214 
215  //- Set the read option \return the previous value
217  {
218  readOption old(readOpt_);
219  readOpt_ = opt;
220  return old;
221  }
222 
223  //- Get the write option
224  writeOption writeOpt() const noexcept { return writeOpt_; }
225 
226  //- Set the write option \return the previous value
228  {
229  writeOption old(writeOpt_);
230  writeOpt_ = opt;
231  return old;
232  }
233 
234  //- Should objects created with this IOobject be registered?
235  bool registerObject() const noexcept { return registerObject_; }
236 
237  //- Change registration preference \return previous value
238  bool registerObject(bool on) noexcept
239  {
240  bool old(registerObject_);
241  registerObject_ = on;
242  return old;
243  }
244 
245  //- True if object is treated the same for all processors
246  bool globalObject() const noexcept { return globalObject_; }
247 
248  //- Change global-object status \return previous value
249  bool globalObject(bool on) noexcept
250  {
251  bool old(globalObject_);
252  globalObject_ = on;
253  return old;
254  }
256 
257  // Checks
258 
259  //- True if (MUST_READ | MUST_READ_IF_MODIFIED) bits are set
260  static bool isReadRequired(readOption opt) noexcept
261  {
262  return static_cast<bool>(opt & readOption::MUST_READ);
263  }
264 
265  //- True if (MUST_READ | MUST_READ_IF_MODIFIED) bits are set
266  bool isReadRequired() const noexcept
267  {
268  return static_cast<bool>(readOpt_ & readOption::MUST_READ);
269  }
271  //- True if (READ_IF_PRESENT) bits are set
272  static bool isReadOptional(readOption opt) noexcept
273  {
274  return (opt == readOption::READ_IF_PRESENT);
275  }
276 
277  //- True if (READ_IF_PRESENT) bits are set
278  bool isReadOptional() const noexcept
279  {
280  return (readOpt_ == readOption::READ_IF_PRESENT);
281  }
282 
283 
284  // Housekeeping
286  //- Access to the read option
287  // \deprecated(2021-03) - use readOpt(readOption)
288  readOption& readOpt() noexcept { return readOpt_; }
289 
290  //- Access to the write option
291  // \deprecated(2021-03) - use writeOpt(writeOption)
292  writeOption& writeOpt() noexcept { return writeOpt_; }
293 
294  //- Access to the register object option
295  // \deprecated(2021-03) - use registerObject(bool)
296  bool& registerObject() noexcept { return registerObject_; }
297 
298  //- Access to the global object option
299  // \deprecated(2021-03) - use globalObject(bool)
300  bool& globalObject() noexcept { return globalObject_; }
301 };
302 
303 
304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305 
306 } // End namespace Foam
307 
308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 
310 #endif
311 
312 // ************************************************************************* //
constexpr IOobjectOption(readOption rOpt=readOption::NO_READ, writeOption wOpt=writeOption::NO_WRITE, registerOption registerObject=registerOption::REGISTER, bool globalObject=false) noexcept
Default construct (NO_READ, NO_WRITE, register, non-global) or construct with specified options...
writeOption
Enumeration defining write preferences.
readOption readOpt() const noexcept
Get the read option.
Ignore writing from objectRegistry::writeObject()
bool isReadOptional() const noexcept
True if (READ_IF_PRESENT) bits are set.
bool globalObject() const noexcept
True if object is treated the same for all processors.
writeOption writeOpt() const noexcept
Get the write option.
Reading required, file watched for runTime modification.
const direction noexcept
Definition: Scalar.H:258
bool isReadRequired() const noexcept
True if (MUST_READ | MUST_READ_IF_MODIFIED) bits are set.
registerOption
Enumeration for use with registerObject(). Values map to bool (false/true)
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
Nothing to be read.
Automatically write from objectRegistry::writeObject()
bool registerObject() const noexcept
Should objects created with this IOobject be registered?
Request registration (bool: true)
Do not request registration (bool: false)
Namespace for OpenFOAM.
readOption
Enumeration defining read preferences.