... | ... | @@ -7,6 +7,22 @@ |
|
|
|
|
|
## Coding Patterns
|
|
|
|
|
|
#### Dictionary on/off flags.
|
|
|
|
|
|
In many cases, it can be advantageous to enable/disable code
|
|
|
functionality based on a user flag. Unless pretty output is needed,
|
|
|
a plain `bool` suffices. Eg,
|
|
|
```
|
|
|
const bool flag(dict.getOrDefault("flag", false));
|
|
|
```
|
|
|
|
|
|
If the user flag will also be used for pretty output (eg, on/off,
|
|
|
yes/no), the `Switch` class is used:
|
|
|
```
|
|
|
const Switch flag(dict.getOrDefault<Switch>("flag", false));
|
|
|
```
|
|
|
|
|
|
|
|
|
### Reading dictionary entries
|
|
|
|
|
|
\since 1812
|
... | ... | @@ -62,6 +78,26 @@ if (dict.found("value")) |
|
|
```
|
|
|
|
|
|
|
|
|
#### Get dictionary values with additional checks
|
|
|
|
|
|
\since 1906
|
|
|
|
|
|
It is possible to embed range or other checks into the dictionary
|
|
|
retrieval. The checks take the form of a unary predicate,
|
|
|
and there are many, many ways to express them. Only dictionary input
|
|
|
values are checked, not any default value supplied by the programmer.
|
|
|
|
|
|
```
|
|
|
const scalar relax(dict.getCheck<scalar>("relax", scalarMinMax::zero_one()));
|
|
|
|
|
|
const scalar positive(dict.getCheckOrDefault<scalar>("pos", 1, scalarMinMax::ge(SMALL));
|
|
|
|
|
|
const scalar positive(dict.getCheckOrDefault<scalar>("pos", 1, scalarRange::gt0());
|
|
|
|
|
|
const scalar positive(dict.getCheck<label>("pos", labelMinMax::ge(0));
|
|
|
```
|
|
|
|
|
|
|
|
|
### Writing dictionary entries
|
|
|
|
|
|
\since 1606
|
... | ... | @@ -82,7 +118,7 @@ os.writeKeyword("key") << value << token::END_STATEMENT << nl; |
|
|
```
|
|
|
os.beginBlock("entries");
|
|
|
|
|
|
os.writeKeyword("key", value);
|
|
|
os.writeEntry("key", value);
|
|
|
...
|
|
|
|
|
|
os.endBlock();
|
... | ... | @@ -106,4 +142,4 @@ os << token::END_BLOCK << nl; |
|
|
|
|
|
----
|
|
|
|
|
|
Copyright (C) 2019 OpenCFD Ltd. |
|
|
Copyright (C) 2019-2020 OpenCFD Ltd. |