OOFEM 3.0
Loading...
Searching...
No Matches
inputrecord.h
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35#ifndef inputrecord_h
36#define inputrecord_h
37
38#include <vector>
39#include <list>
40#include <string>
41#include <memory>
42#include <exception>
43#include <regex>
44
45#include "logger.h" // for missing __func__ in MSC
46#include "oofemenv.h"
47#include "enum.h"
48
49namespace oofem {
50class IntArray;
51class FloatArray;
52class FloatMatrix;
53class Dictionary;
54class Range;
55class ScalarFunction;
56class DataReader;
57
59typedef const char *InputFieldType;
60
67#define IR_GIVE_FIELD(__ir, __value, __id) (__ir).giveField(__value, __id);
68
75#define IR_GIVE_OPTIONAL_FIELD(__ir, __value, __id) (__ir).giveOptionalField(__value, __id);
76
82#define IR_GIVE_RECORD_KEYWORD_FIELD(__ir, __name, __value) \
83 (__ir).giveRecordKeywordField(__name, __value);
84
85
86// #define _INPUTRECORD_OPTIONAL_OLD
87
97class OOFEM_EXPORT InputRecord: public std::enable_shared_from_this<InputRecord>
98{
99 DataReader* reader = nullptr;
100public:
101
103 InputRecord(DataReader* reader_);
105 virtual ~InputRecord() = default;
106
108 virtual std::shared_ptr<InputRecord> clone() const = 0;
109 std::shared_ptr<InputRecord> ptr();
110
112 virtual std :: string giveRecordAsString() const = 0;
113 virtual std :: string giveRecordInTXTFormat() const = 0;
114 virtual std :: string giveLocation() const = 0;
115
122
123 virtual void giveRecordKeywordField(std :: string &answer, int &value) = 0;
125 virtual void giveRecordKeywordField(std :: string &answer) = 0;
127 virtual void giveField(int &answer, InputFieldType id) = 0;
129 virtual void giveField(double &answer, InputFieldType id) = 0;
131 virtual void giveField(bool &answer, InputFieldType id) = 0;
133 virtual void giveField(std :: string &answer, InputFieldType id) = 0;
135 virtual void giveField(FloatArray &answer, InputFieldType id) = 0;
137 virtual void giveField(IntArray &answer, InputFieldType id) = 0;
139 virtual void giveField(FloatMatrix &answer, InputFieldType id) = 0;
141 virtual void giveField(std :: vector< std :: string > &answer, InputFieldType id) = 0;
143 virtual void giveField(Dictionary &answer, InputFieldType id) = 0;
145 virtual void giveField(std :: list< Range > &answer, InputFieldType id) = 0;
147 virtual void giveField(ScalarFunction &function, InputFieldType id) = 0;
148
149 static std::string error_msg_with_hints(const std::string& val, const std::map<int,std::vector<std::string>>& v2nn);
150 static int giveLevenshteinDist(const std::string& word1, const std::string& word2);
151
153 template<typename AnEnum>
154 void giveField(AnEnum& answer, InputFieldType id){
155 typedef EnumTraits<AnEnum> Traits;
156 std::string s;
157 giveField(s,id);
158 #ifdef _USE_TRACE_FIELDS
159 if(InputRecord::TraceFields::active){
160 traceEnum(Traits::enum_name,Traits::all_values_to_names());
161 traceField(id,(std::string("enum:")+Traits::enum_name).c_str());
162 }
163 #endif
164 if(std::regex_match(s,std::regex("\\s*[0-9]+\\s*"))){
165 int val=std::atoi(s.c_str());
166 auto v=Traits::value(val);
167 if(!v) OOFEM_ERROR("%s: %s (enum %s): invalid index '%d'%s",giveLocation().c_str(),id,Traits::enum_name,val,error_msg_with_hints("",Traits::all_values_to_names()).c_str());
168 answer=v.value();
169 } else {
170 auto v=Traits::value(s.c_str());
171 if(!v){ OOFEM_ERROR("%s: %s (enum %s): unrecognized name '%s'%s",giveLocation().c_str(),id,Traits::enum_name,s.c_str(),error_msg_with_hints(s,Traits::all_values_to_names()).c_str()); }
172 answer=v.value();
173 }
174 }
175 #ifdef _USE_TRACE_FIELDS
176 // field access tracing variables, set at startup from main()
177 struct TraceFields {
178 static bool active;
179 static std::ofstream out;
180 static void write(const std::string& s);
181 };
182 static void traceEnum(const std::string& name, const std::map<int,std::vector<std::string>>& val2names);
183 virtual void traceField(InputFieldType id, const char* type) {};
184 #else
185 void traceField(InputFieldType id, const char* type) const { };
186 #endif
192 template<typename T>
193 void giveOptionalField(T& answer, InputFieldType id){ if(hasField(id)) giveField(answer,id); }
195
199 // return count of nested group; if not optional, the empty group is required
200 virtual int giveGroupCount(InputFieldType id, const std::string& name, bool optional) = 0;
201 // return whether a single child of given type exists
202 virtual bool hasChild(InputFieldType id, const std::string& name, bool optional) = 0;
204#ifdef _INPUTRECORD_OPTIONAL_OLD
206
207 void giveOptionalField(int &answer, InputFieldType id);
209 void giveOptionalField(double &answer, InputFieldType id);
211 void giveOptionalField(bool &answer, InputFieldType id);
213 void giveOptionalField(std :: string &answer, InputFieldType id);
217 void giveOptionalField(IntArray &answer, InputFieldType id);
221 void giveOptionalField(std :: vector< std :: string > &answer, InputFieldType id);
225 void giveOptionalField(std :: list< Range > &answer, InputFieldType id);
229#endif
231 virtual bool hasField(InputFieldType id) = 0;
232
234 virtual void printYourself() = 0;
235
237 virtual void finish(bool wrn = true) = 0;
238
239 DataReader* giveReader() const;
240};
241
242
243class InputException : public std::exception
244{
245public:
246 std::string record;
247 std::string keyword;
249 InputException(const InputRecord &ir, std::string keyword, int number);
250};
251
252
254{
255protected:
256 std::string msg;
257
258public:
259 MissingKeywordInputException(const InputRecord &ir, std::string keyword, int number);
260 const char* what() const noexcept override;
261};
262
263
265{
266protected:
267 std::string msg;
268
269public:
270 BadFormatInputException(const InputRecord &ir, std::string keyword, int number);
271 const char* what() const noexcept override;
272};
273
274
276{
277protected:
278 std::string msg;
279
280public:
281 ValueInputException(const InputRecord &ir, std::string keyword, const std::string &reason);
282 const char* what() const noexcept override;
283};
284
285class ComponentInputException : public std::exception {
286public:
292
293protected:
294 std::string msg;
295public:
296 ComponentInputException(const std::string keyword, ComponentType ct, int number, const std::string &reason);
297 ComponentInputException(ComponentType ct, int number, const std::string &reason);
298
299 const char* what() const noexcept override;
300};
301
302} // end namespace oofem
303#endif // inputrecord_h
BadFormatInputException(const InputRecord &ir, std::string keyword, int number)
ComponentInputException(const std::string keyword, ComponentType ct, int number, const std::string &reason)
InputException(const InputRecord &ir, std::string keyword, int number)
virtual void giveField(ScalarFunction &function, InputFieldType id)=0
Reads the ScalarFunction field value.
virtual bool hasField(InputFieldType id)=0
Returns true if record contains field identified by idString keyword.
virtual void giveRecordKeywordField(std ::string &answer, int &value)=0
Reads the record id field (type of record) and its corresponding number.
void giveOptionalField(T &answer, InputFieldType id)
virtual std::string giveRecordAsString() const =0
Returns string representation of record in OOFEMs text format.
virtual void giveField(std ::string &answer, InputFieldType id)=0
Reads the string field value.
DataReader * reader
Definition inputrecord.h:99
virtual void giveField(int &answer, InputFieldType id)=0
Reads the integer field value.
virtual void finish(bool wrn=true)=0
Terminates the current record session and if the flag is true, warning is printed for unscanned token...
virtual void giveField(FloatMatrix &answer, InputFieldType id)=0
Reads the FloatMatrix field value.
virtual void printYourself()=0
Print input record.
virtual void giveField(std ::list< Range > &answer, InputFieldType id)=0
Reads the std::list<Range> field value.
void traceField(InputFieldType id, const char *type) const
virtual void giveField(IntArray &answer, InputFieldType id)=0
Reads the IntArray field value.
DataReader * giveReader() const
virtual ~InputRecord()=default
Destructor.
virtual std::shared_ptr< InputRecord > clone() const =0
std::shared_ptr< InputRecord > ptr()
virtual std::string giveRecordInTXTFormat() const =0
static int giveLevenshteinDist(const std::string &word1, const std::string &word2)
Definition inputrecord.C:49
virtual int giveGroupCount(InputFieldType id, const std::string &name, bool optional)=0
virtual void giveField(bool &answer, InputFieldType id)=0
Reads the bool field value.
virtual void giveField(std ::vector< std ::string > &answer, InputFieldType id)=0
Reads the vector of strings.
void giveField(AnEnum &answer, InputFieldType id)
Reads enumeration (must be defined via enum-impl.hpp) directly.
virtual void giveField(Dictionary &answer, InputFieldType id)=0
Reads the Dictionary field value.
virtual void giveRecordKeywordField(std ::string &answer)=0
Reads the record id field (type of record).
static std::string error_msg_with_hints(const std::string &val, const std::map< int, std::vector< std::string > > &v2nn)
Definition inputrecord.C:91
virtual void giveField(FloatArray &answer, InputFieldType id)=0
Reads the FloatArray field value.
virtual bool hasChild(InputFieldType id, const std::string &name, bool optional)=0
virtual std::string giveLocation() const =0
virtual void giveField(double &answer, InputFieldType id)=0
Reads the double field value.
MissingKeywordInputException(const InputRecord &ir, std::string keyword, int number)
const char * what() const noexcept override
ValueInputException(const InputRecord &ir, std::string keyword, const std::string &reason)
#define OOFEM_ERROR(...)
Definition error.h:79
const char * InputFieldType
Identifier of fields in input records.
Definition inputrecord.h:59
#define OOFEM_EXPORT
Definition oofemcfg.h:7

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011