NANDRAD Data Model Library  Version 2.0
NANDRAD
NANDRAD_KeywordList.h
Go to the documentation of this file.
1 /* The NANDRAD data model library.
2 
3  Copyright (c) 2012-today, Institut für Bauklimatik, TU Dresden, Germany
4 
5  Primary authors:
6  Andreas Nicolai <andreas.nicolai -[at]- tu-dresden.de>
7  Anne Paepcke <anne.paepcke -[at]- tu-dresden.de>
8 
9  This library is part of SIM-VICUS (https://github.com/ghorwin/SIM-VICUS)
10 
11  This library is free software: you can redistribute it and/or modify
12  it 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  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 */
21 
22 #ifndef NANDRAD_KeywordListH
23 #define NANDRAD_KeywordListH
24 
25 #include <string>
26 #include <typeinfo>
27 
28 #include <IBK_Parameter.h>
29 #include <IBK_IntPara.h>
30 
31 namespace NANDRAD {
32 
33 /*! The class KeywordList provides conversion functionality between keyword strings
34  and their respective enumeration values.
35  Keyword strings are used in the NANDRAD project files as identifier. In the code
36  we use the enumeration values. You can obtain a keyword for a given enumeration value
37  with the static function Keyword(). The corresponding enumeration value can be
38  obtained with Enumeration(), as shown in the example below.
39  \code
40  // to get the keyword string use
41  string kw = KeywordList::Keyword("Assignment::type_t", Assignment::ST_MATERIAL);
42  // and to get the enumeration value
43  Assignment::type_t t = KeywordList::Enumeration("Assignment::type_t}", kw);
44  // the string "Assignment::type_t" is the full enumeration type and formed from the class
45  // name and the enumeration type name
46  \endcode
47  Remember to pass the correct and full enumeration type as first argument to Keyword.
48  \note Instead of overloading the Keyword() function for different types
49  in the current implementation we use a generic form in order to avoid
50  adding compilation dependencies for
51  all CPP files in the project that include the keyword list.
52 */
53 class KeywordList {
54 public:
55  /*!Returns a keyword for an enum value t of type enumtype. */
56  static const char * Keyword(const char * const enumtype, int t);
57 
58  /*! Returns a description for an enum value t of type enumtype.
59  This function throws an exception if the enumeration type is invalid or unknown.
60  If no descrption is given, the keyword itself is returned.
61  \param enumtype The full enumeration type including the class name.
62  \param t The enumeration type cast in an int.
63  \param no_description The optional argument is set to true, if there was no description
64  for this keyword, otherwise to false.
65  */
66  static const char * Description(const char * const enumtype, int t, bool * no_description = nullptr);
67 
68  /*! Returns a default unit for an enum value t of type enumtype.
69  This function throws an exception if the enumeration type is invalid or unknown.
70  Returns an empty string if no default unit was specified.
71  \param enumtype The full enumeration type including the class name.
72  \param t The enumeration type cast in an int.
73  */
74  static const char * Unit(const char * const enumtype, int t);
75 
76  /*! Returns a color string for an enum value t of type enumtype.
77  This function throws an exception if the enumeration type is invalid or unknown.
78  Returns the color code for 'white' if no color value was specified.
79  \param enumtype The full enumeration type including the class name.
80  \param t The enumeration type cast in an int.
81  */
82  static const char * Color(const char * const enumtype, int t);
83 
84  /*! Returns a default value for an enum value t of type enumtype.
85  This function throws an exception if the enumeration type is invalid or unknown.
86  Returns an nan if no default value was specified.
87  \param enumtype The full enumeration type including the class name.
88  \param t The enumeration type cast in an int.
89  */
90  static double DefaultValue(const char * const enumtype, int t);
91 
92  /*! Returns an enumeration value for a given keyword kw of type enumtype.
93  This function throws an exception if the keyword or the enumeration type is invalid or unknown.
94  \param enumtype The full enumeration type including the class name.
95  \param kw The keyword string.
96  \param deprecated The optional argument is set the true if the keyword kw is deprecated.
97  */
98  static int Enumeration(const char * const enumtype, const std::string & kw, bool * deprecated = nullptr);
99 
100 
101  /*! Returns the maximum index for entries of a category in the keyword list.
102  This function throws an exception if the enumeration type is invalid or unknown.
103  \param enumtype The full enumeration type including the class name.
104  */
105  static int MaxIndex(const char * const enumtype);
106 
107 
108  /*! Returns the number of keywords in this category.
109  This function throws an exception if the enumeration type is invalid or unknown.
110  \param enumtype The full enumeration type including the class name.
111  */
112  static unsigned int Count(const char * const enumtype);
113 
114  /*! Checks whether a keyword exists in the enumeration of type enumtype.
115  \return Returns true if the keyword is valid, otherwise false.
116  */
117  static bool KeywordExists(const char * const enumtype, const std::string & kw);
118  /*! Checks whether a category of type enumtype exists.
119  \return Returns true if the category/enum type exists, otherwise false.
120  */
121  static bool CategoryExists(const char * const enumtype);
122  /*! Convenience function to set an IBK::Parameter inside a static C-array of IBK::Parameters with
123  correct name, value and unit.
124  \code
125  // Example: in declaration of class 'MyClass'
126 
127  enum MyParameters {
128  MP_Temperature, // Keyword: Temperature [C] 'Some temperatures'
129  MP_Mass, // Keyword: Mass [kg] 'Some mass'
130  NUM_MP
131  };
132 
133  // static array with parameter values
134  IBK::Parameter m_para[NUM_MP];
135 
136 
137 
138  // setting the parameter in code
139 
140  NANDRAD::setParameter(m_para, "MyClass::MyParameters", MP_Temperature, 23.5);
141  NANDRAD::setParameter(m_para, "MyClass::MyParameters", MP_Mass, 1500);
142 
143  \endcode
144 
145  \param para Pointer to begin of array with IBK::Parameters
146  \param enumtype The enumeration type (class::enumtypename)
147  \param n Enumeration value (which parameter to set)
148  \param val The value (given in the unit specified for the parameter; requires a unit to be given)
149  */
150  static void setParameter(IBK::Parameter para[], const char * const enumtype, int n, const double &val);
151 
152  /*! As setParameter(), but for IBK::IntPara. */
153  static void setIntPara(IBK::IntPara para[], const char * const enumtype, int n, const int &val);
154 
155 };
156 
157 } // namespace NANDRAD
158 
159 /*!
160  \file NANDRAD_KeywordList.h
161  \brief Contains the declaration of class KeywordList.
162 */
163 
164 #endif // NANDRAD_KeywordListH
static bool CategoryExists(const char *const enumtype)
Checks whether a category of type enumtype exists.
static const char * Keyword(const char *const enumtype, int t)
Returns a keyword for an enum value t of type enumtype.
static unsigned int Count(const char *const enumtype)
Returns the number of keywords in this category.
static int MaxIndex(const char *const enumtype)
Returns the maximum index for entries of a category in the keyword list.
static int Enumeration(const char *const enumtype, const std::string &kw, bool *deprecated=nullptr)
Returns an enumeration value for a given keyword kw of type enumtype.
static const char * Color(const char *const enumtype, int t)
Returns a color string for an enum value t of type enumtype.
static const char * Unit(const char *const enumtype, int t)
Returns a default unit for an enum value t of type enumtype.
static void setIntPara(IBK::IntPara para[], const char *const enumtype, int n, const int &val)
As setParameter(), but for IBK::IntPara.
static void setParameter(IBK::Parameter para[], const char *const enumtype, int n, const double &val)
Convenience function to set an IBK::Parameter inside a static C-array of IBK::Parameters with correct...
static double DefaultValue(const char *const enumtype, int t)
Returns a default value for an enum value t of type enumtype.
static bool KeywordExists(const char *const enumtype, const std::string &kw)
Checks whether a keyword exists in the enumeration of type enumtype.
The namespace NANDRAD contains the data model classes that make up the NANDRAD solver input data...
static const char * Description(const char *const enumtype, int t, bool *no_description=nullptr)
Returns a description for an enum value t of type enumtype.
The class KeywordList provides conversion functionality between keyword strings and their respective ...