registerNamedEnum.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::RegisterNamedEnum
26 
27 Description
28  Class and registration macros for info and optimisation named enumerations
29  to support reading from system/controlDict and dynamic update.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #ifndef registerNamedEnum_H
34 #define registerNamedEnum_H
35 
36 #include "simpleRegIOobject.H"
37 #include "macros.H"
38 #include "NamedEnum.H"
39 #include <type_traits>
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class RegisterNamedEnum Declaration
48 \*---------------------------------------------------------------------------*/
49 
50 template<class NamedEnumType>
51 class RegisterNamedEnum;
52 
53 template<class Enum, unsigned int nEnum>
54 class RegisterNamedEnum<NamedEnum<Enum, nEnum>>
55 :
56  public simpleRegIOobject
57 {
58  const NamedEnum<Enum, nEnum>& enumNames_;
59 
60  Enum& enumRef_;
61 
62 public:
63 
65  (
66  void (*registryFn)(const char* name, simpleRegIOobject*),
67  const word& name,
68  const NamedEnum<Enum, nEnum>& enumNames,
69  Enum& enumRef
70  )
71  :
72  ::Foam::simpleRegIOobject(registryFn, name.c_str()),
73  enumNames_(enumNames),
74  enumRef_(enumRef)
75  {}
76 
77  virtual ~RegisterNamedEnum()
78  {}
79 
80  virtual void readData(Istream& is)
81  {
82  enumRef_ = enumNames_.read(is);
83  }
84 
85  virtual void writeData(Ostream& os) const
86  {
87  os << enumNames_[enumRef_];
88  }
89 };
90 
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 } // End namespace Foam
95 
96 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
97 
98 #define registerOptNamedEnum(Name, EnumNames, EnumRef) \
99  static Foam::RegisterNamedEnum<std::remove_cv<decltype(EnumNames)>::type> \
100  FILE_UNIQUE(_addToOpt_) \
101  (Foam::debug::addOptimisationObject, Name, EnumNames, EnumRef)
103 #define registerInfoNamedEnum(Name, EnumNames, EnumRef) \
104  static Foam::RegisterNamedEnum<std::remove_cv<decltype(EnumNames)>::type> \
105  FILE_UNIQUE(_addToOpt_) \
106  (Foam::debug::addInfoObject, Name, EnumNames, EnumRef)
107 
108 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
109 
110 #endif
111 
112 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class and registration macros for info and optimisation named enumerations to support reading from sy...
General C-preprocessor macros.
A class for handling words, derived from string.
Definition: word.H:59
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Namespace for OpenFOAM.