Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
openfoam
openfoam
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 331
    • Issues 331
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 6
    • Merge Requests 6
  • Operations
    • Operations
    • Incidents
  • Analytics
    • Analytics
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Commits
  • Issue Boards
  • Development
  • openfoamopenfoam
  • Wiki
    • Coding
    • Patterns
  • dictionary

Last edited by Mark Olesen Jul 09, 2020
Page history

dictionary

home code

We are happy to incorporate content from volunteers!!

Coding Patterns - Dictionary

  • Coding Patterns - Dictionary
    • Dictionary on/off flags.
    • Reading dictionary entries
      • Get dictionary values with additional checks
    • Writing dictionary entries
    • Writing sub-dictionary entries

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

const scalar value(dict.get<scalar>("value"));

const word modelType(dict.get<word>("type"));

const vector dirn(dict.get<vector>("direction"));
  • These retrieve values with input checking on type and excess tokens.

Anti-pattern: using dictionary lookup() returns a stream, which needs additional handling for primitives and does not incorporate input checking:

const scalar value(readScalar(dict.lookup("value"));

const word modelType(dict.lookup("type"));

const vector dirn(dict.lookup("direction"));

\since 1812

When reading into existing entries:

dict.readEntry("value", value);

dict.readIfPresent("value", value);

// or
if (dict.readIfPresent("value", value))
{
   ... more actions
}

Anti-pattern: using raw dictionary lookup() or doing the conditionals manually:

dict.lookup("value") >> value;

if (dict.found("value"))
{
    dict.lookup("value") >> value;

   ... more actions
}

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

os.writeEntry("key", value);

Anti-pattern: doing everything yourself is messy and repetitive:

os.writeKeyword("key") << value << token::END_STATEMENT << nl;

Writing sub-dictionary entries

\since 1606

os.beginBlock("entries");

os.writeEntry("key", value);
...

os.endBlock();

Anti-pattern: doing everything yourself:

os.indent();
os.write("entries") << nl;
os.indent();
os << token::BEGIN_BLOCK << nl;
os.incrIndent();

os.writeKeyword("key") << value << token::END_STATEMENT << nl;
...

os.decrIndent();
os.indent();
os << token::END_BLOCK << nl;

Copyright (C) 2019-2020 OpenCFD Ltd.

Clone repository
  • Repository migration
  • Submitting issues
  • building
  • building
    • cross compile mingw
  • coding
    • git workflow
    • patterns
      • HashTable
      • dictionary
      • memory
      • patterns
      • precision
      • selectors
      • strings
    • style
      • style
  • Home
  • icons
    • info
View All Pages