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-2023 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 See also
36  Foam::IOobject
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef Foam_IOobjectOption_H
41 #define Foam_IOobjectOption_H
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 /*---------------------------------------------------------------------------*\
49  Class IOobjectOption Declaration
50 \*---------------------------------------------------------------------------*/
51 
52 class IOobjectOption
53 {
54 public:
55 
56  // Public Data Types
57 
58  //- Enumeration defining read preferences
59  // Lowest bit encodes 'must read'.
60  // Possible (future) named variants (none | normal | modified | lazy)
61  enum readOption : unsigned char
62  {
64  NO_READ = 0,
65 
67  MUST_READ = 0x1,
68 
72 
76 
78  LAZY_READ = 0x4,
79 
81  READ_IF_PRESENT = 0x4
82  };
83 
84  //- Enumeration defining write preferences
85  enum writeOption : unsigned char
86  {
88  NO_WRITE = 0,
89 
91  AUTO_WRITE = 0x10
92  };
93 
94  //- Enumeration for use with registerObject().
95  //- Values map to bool (false/true)
96  enum registerOption : unsigned char
97  {
100 
102  REGISTER = 1,
103 
106  };
107 
108 
109 private:
110 
111  // Private Data
112 
113  //- Read option
114  readOption readOpt_;
115 
116  //- Write option
117  writeOption writeOpt_;
118 
119  //- Should created objects be registered?
120  bool registerObject_;
121 
122  //- Is object same for all processors?
123  bool globalObject_;
124 
125 
126 public:
127 
128  // Constructors
129 
130  //- Default construct (NO_READ, NO_WRITE, REGISTER, non-global)
131  //- or construct with specified options
132  constexpr IOobjectOption
133  (
134  readOption rOpt = readOption::NO_READ,
135  writeOption wOpt = writeOption::NO_WRITE,
136  registerOption registerObject = registerOption::REGISTER,
137  bool globalObject = false
138  ) noexcept
139  :
140  readOpt_(rOpt),
141  writeOpt_(wOpt),
142  registerObject_(registerObject),
143  globalObject_(globalObject)
144  {}
145 
146  //- Construct NO_WRITE with specified read/register options
147  constexpr IOobjectOption
148  (
149  readOption rOpt,
150  registerOption registerObject = registerOption::REGISTER,
151  bool globalObject = false
152  ) noexcept
153  :
154  readOpt_(rOpt),
155  writeOpt_(writeOption::NO_WRITE),
156  registerObject_(registerObject),
157  globalObject_(globalObject)
158  {}
159 
160  //- Construct NO_READ with specified write/register options
161  constexpr IOobjectOption
162  (
163  writeOption wOpt,
164  registerOption registerObject = registerOption::REGISTER,
165  bool globalObject = false
166  ) noexcept
167  :
168  readOpt_(readOption::NO_READ),
169  writeOpt_(wOpt),
170  registerObject_(registerObject),
171  globalObject_(globalObject)
172  {}
173 
174  //- Construct (NO_READ, NO_WRITE) with specified register option
175  constexpr IOobjectOption
176  (
178  bool globalObject = false
179  ) noexcept
180  :
181  readOpt_(readOption::NO_READ),
182  writeOpt_(writeOption::NO_WRITE),
183  registerObject_(registerObject),
184  globalObject_(globalObject)
185  {}
186 
187  //- Construct from components
188  //- with specified register option as bool
189  constexpr IOobjectOption
190  (
191  readOption rOpt,
193  bool registerObject,
194  bool globalObject = false
195  ) noexcept
196  :
197  readOpt_(rOpt),
198  writeOpt_(wOpt),
199  registerObject_(registerObject ? REGISTER : NO_REGISTER),
200  globalObject_(globalObject)
201  {}
202 
203  //- Construct (NO_READ, NO_WRITE)
204  //- with specified register option as bool
205  explicit constexpr IOobjectOption
206  (
207  bool registerObject,
208  bool globalObject = false
209  ) noexcept
210  :
211  readOpt_(readOption::NO_READ),
212  writeOpt_(writeOption::NO_WRITE),
213  registerObject_(registerObject ? REGISTER : NO_REGISTER),
214  globalObject_(globalObject)
215  {}
216 
217 
218  // Member Functions
219 
220  //- Get the read option
221  readOption readOpt() const noexcept { return readOpt_; }
222 
223  //- Set the read option \return the previous value
225  {
226  readOption old(readOpt_);
227  readOpt_ = opt;
228  return old;
229  }
230 
231  //- Get the write option
232  writeOption writeOpt() const noexcept { return writeOpt_; }
233 
234  //- Set the write option \return the previous value
236  {
237  writeOption old(writeOpt_);
238  writeOpt_ = opt;
239  return old;
240  }
241 
242  //- Should objects created with this IOobject be registered?
243  bool registerObject() const noexcept { return registerObject_; }
244 
245  //- Change registration preference \return previous value
246  bool registerObject(bool on) noexcept
247  {
248  bool old(registerObject_);
249  registerObject_ = on;
250  return old;
251  }
252 
253  //- True if object is treated the same for all processors
254  bool globalObject() const noexcept { return globalObject_; }
255 
256  //- Change global-object status \return previous value
257  bool globalObject(bool on) noexcept
258  {
259  bool old(globalObject_);
260  globalObject_ = on;
261  return old;
262  }
264 
265  // Checks
266 
267  //- True if any reading may be required (ie, != NO_READ)
268  static bool isAnyRead(readOption opt) noexcept
269  {
270  return (opt != readOption::NO_READ);
271  }
272 
273  //- True if any reading may be required (ie, != NO_READ)
274  bool isAnyRead() const noexcept
275  {
276  return (readOpt_ != readOption::NO_READ);
277  }
279  //- True if (MUST_READ | READ_MODIFIED) bits are set
280  static bool isReadRequired(readOption opt) noexcept
281  {
282  return (opt & readOption::MUST_READ);
283  }
284 
285  //- True if (MUST_READ | READ_MODIFIED) bits are set
286  bool isReadRequired() const noexcept
287  {
288  return (readOpt_ & readOption::MUST_READ);
289  }
290 
291  //- True if (LAZY_READ) bits are set [same as READ_IF_PRESENT]
292  static bool isReadOptional(readOption opt) noexcept
293  {
294  return (opt == readOption::LAZY_READ);
295  }
296 
297  //- True if (LAZY_READ) bits are set [same as READ_IF_PRESENT]
298  bool isReadOptional() const noexcept
299  {
300  return (readOpt_ == readOption::LAZY_READ);
301  }
302 
303  //- Downgrade readOption optional (LAZY_READ), leaves NO_READ intact.
305  {
306  return (opt == readOption::NO_READ ? opt : readOption::LAZY_READ);
307  }
308 
309 
310  // Housekeeping
311 
312  //- Access to the read option
313  // \deprecated(2021-03) - use readOpt(readOption)
314  readOption& readOpt() noexcept { return readOpt_; }
315 
316  //- Access to the write option
317  // \deprecated(2021-03) - use writeOpt(writeOption)
318  writeOption& writeOpt() noexcept { return writeOpt_; }
319 
320  //- Access to the register object option
321  // \deprecated(2021-03) - use registerObject(bool)
322  bool& registerObject() noexcept { return registerObject_; }
323 
324  //- Access to the global object option
325  // \deprecated(2021-03) - use globalObject(bool)
326  bool& globalObject() noexcept { return globalObject_; }
327 };
328 
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 
332 } // End namespace Foam
333 
334 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
335 
336 #endif
337 
338 // ************************************************************************* //
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.
Legacy/default registration request (bool: true)
bool isAnyRead() const noexcept
True if any reading may be required (ie, != NO_READ)
Ignore writing from objectRegistry::writeObject()
bool isReadOptional() const noexcept
True if (LAZY_READ) bits are set [same as READ_IF_PRESENT].
bool globalObject() const noexcept
True if object is treated the same for all processors.
writeOption writeOpt() const noexcept
Get the write option.
Reading is optional [identical to LAZY_READ].
const direction noexcept
Definition: Scalar.H:258
bool isReadRequired() const noexcept
True if (MUST_READ | READ_MODIFIED) bits are set.
static readOption lazierRead(readOption opt) noexcept
Downgrade readOption optional (LAZY_READ), leaves NO_READ intact.
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()
Reading is optional [identical to READ_IF_PRESENT].
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.