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 
103  };
104 
105 
106 private:
107 
108  // Private Data
109 
110  //- Read option
111  readOption readOpt_;
112 
113  //- Write option
114  writeOption writeOpt_;
115 
116  //- Should created objects be registered?
117  bool registerObject_;
118 
119  //- Is object same for all processors?
120  bool globalObject_;
121 
122 
123 public:
124 
125  // Constructors
126 
127  //- Default construct (NO_READ, NO_WRITE, register, non-global)
128  //- or construct with specified options
129  constexpr IOobjectOption
130  (
131  readOption rOpt = readOption::NO_READ,
132  writeOption wOpt = writeOption::NO_WRITE,
133  registerOption registerObject = registerOption::REGISTER,
134  bool globalObject = false
135  ) noexcept
136  :
137  readOpt_(rOpt),
138  writeOpt_(wOpt),
139  registerObject_(registerObject),
140  globalObject_(globalObject)
141  {}
142 
143  //- Construct NO_WRITE with specified read/register options
144  constexpr IOobjectOption
145  (
146  readOption rOpt,
147  registerOption registerObject = registerOption::REGISTER,
148  bool globalObject = false
149  ) noexcept
150  :
151  readOpt_(rOpt),
152  writeOpt_(writeOption::NO_WRITE),
153  registerObject_(registerObject),
154  globalObject_(globalObject)
155  {}
156 
157  //- Construct NO_READ with specified write/register options
158  constexpr IOobjectOption
159  (
160  writeOption wOpt,
161  registerOption registerObject = registerOption::REGISTER,
162  bool globalObject = false
163  ) noexcept
164  :
165  readOpt_(readOption::NO_READ),
166  writeOpt_(wOpt),
167  registerObject_(registerObject),
168  globalObject_(globalObject)
169  {}
170 
171  //- Construct (NO_READ, NO_WRITE) with specified register option
172  constexpr IOobjectOption
173  (
175  bool globalObject = false
176  ) noexcept
177  :
178  readOpt_(readOption::NO_READ),
179  writeOpt_(writeOption::NO_WRITE),
180  registerObject_(registerObject),
181  globalObject_(globalObject)
182  {}
183 
184  //- Construct from components
185  //- with specified register option as bool
186  constexpr IOobjectOption
187  (
188  readOption rOpt,
190  bool registerObject,
191  bool globalObject = false
192  ) noexcept
193  :
194  readOpt_(rOpt),
195  writeOpt_(wOpt),
196  registerObject_(registerObject ? REGISTER : NO_REGISTER),
197  globalObject_(globalObject)
198  {}
199 
200  //- Construct (NO_READ, NO_WRITE)
201  //- with specified register option as bool
202  explicit constexpr IOobjectOption
203  (
204  bool registerObject,
205  bool globalObject = false
206  ) noexcept
207  :
208  readOpt_(readOption::NO_READ),
209  writeOpt_(writeOption::NO_WRITE),
210  registerObject_(registerObject ? REGISTER : NO_REGISTER),
211  globalObject_(globalObject)
212  {}
213 
214 
215  // Member Functions
216 
217  //- Get the read option
218  readOption readOpt() const noexcept { return readOpt_; }
219 
220  //- Set the read option \return the previous value
222  {
223  readOption old(readOpt_);
224  readOpt_ = opt;
225  return old;
226  }
227 
228  //- Get the write option
229  writeOption writeOpt() const noexcept { return writeOpt_; }
230 
231  //- Set the write option \return the previous value
233  {
234  writeOption old(writeOpt_);
235  writeOpt_ = opt;
236  return old;
237  }
238 
239  //- Should objects created with this IOobject be registered?
240  bool registerObject() const noexcept { return registerObject_; }
241 
242  //- Change registration preference \return previous value
243  bool registerObject(bool on) noexcept
244  {
245  bool old(registerObject_);
246  registerObject_ = on;
247  return old;
248  }
249 
250  //- True if object is treated the same for all processors
251  bool globalObject() const noexcept { return globalObject_; }
252 
253  //- Change global-object status \return previous value
254  bool globalObject(bool on) noexcept
255  {
256  bool old(globalObject_);
257  globalObject_ = on;
258  return old;
259  }
261 
262  // Checks
263 
264  //- True if any reading may be required (ie, != NO_READ)
265  static bool isAnyRead(readOption opt) noexcept
266  {
267  return (opt != readOption::NO_READ);
268  }
269 
270  //- True if any reading may be required(ie, != NO_READ)
271  bool isAnyRead() const noexcept
272  {
273  return (readOpt_ != readOption::NO_READ);
274  }
276  //- True if (MUST_READ | READ_MODIFIED) bits are set
277  static bool isReadRequired(readOption opt) noexcept
278  {
279  return (opt & readOption::MUST_READ);
280  }
281 
282  //- True if (MUST_READ | READ_MODIFIED) bits are set
283  bool isReadRequired() const noexcept
284  {
285  return (readOpt_ & readOption::MUST_READ);
286  }
287 
288  //- True if (LAZY_READ) bits are set [same as READ_IF_PRESENT]
289  static bool isReadOptional(readOption opt) noexcept
290  {
291  return (opt == readOption::LAZY_READ);
292  }
293 
294  //- True if (LAZY_READ) bits are set [same as READ_IF_PRESENT]
295  bool isReadOptional() const noexcept
296  {
297  return (readOpt_ == readOption::LAZY_READ);
298  }
299 
300  //- Downgrade readOption optional (LAZY_READ), leaves NO_READ intact.
302  {
303  return (opt == readOption::NO_READ ? opt : readOption::LAZY_READ);
304  }
305 
306 
307  // Housekeeping
308 
309  //- Access to the read option
310  // \deprecated(2021-03) - use readOpt(readOption)
311  readOption& readOpt() noexcept { return readOpt_; }
312 
313  //- Access to the write option
314  // \deprecated(2021-03) - use writeOpt(writeOption)
315  writeOption& writeOpt() noexcept { return writeOpt_; }
316 
317  //- Access to the register object option
318  // \deprecated(2021-03) - use registerObject(bool)
319  bool& registerObject() noexcept { return registerObject_; }
320 
321  //- Access to the global object option
322  // \deprecated(2021-03) - use globalObject(bool)
323  bool& globalObject() noexcept { return globalObject_; }
324 };
325 
326 
327 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328 
329 } // End namespace Foam
330 
331 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
332 
333 #endif
334 
335 // ************************************************************************* //
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.
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.