FEDEM Model Database  R8.0
Source code of the FEDEM model database
FFaField.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
13 #ifndef FFA_FIELD_H
14 #define FFA_FIELD_H
15 
16 #include <set>
17 #include <sstream>
18 #include "FFaFieldBase.H"
19 
20 using IntDouble = std::pair<int,double>;
21 using IntDoubles = std::vector<IntDouble>;
22 using Doubles = std::pair<double,double>;
23 using DoubleVec = std::vector<double>;
24 using Ints = std::pair<int,int>;
25 using IntVec = std::vector<int>;
26 using BoolVec = std::vector<bool>;
27 using StringSet = std::set<std::string>;
28 
29 
37 template <typename ValueT>
38 class FFaField : public FFaFieldBase
39 {
40 public:
42  FFaField() {}
44  virtual ~FFaField() {}
45 
47  ValueT& operator=(const ValueT& data)
48  {
49  if (this->isDataField())
50  myData = data;
51 
52  return myData;
53  }
54 
56  const ValueT& getValue() const { return myData; }
58  ValueT& getValue() { return myData; }
59 
66  bool setValue(const ValueT& newData, bool checkChanged = true)
67  {
68  if (checkChanged && myData == newData)
69  return false;
70 
71  myData = newData;
72  return true;
73  }
74 
81  bool setDefaultValue(const ValueT& defaultVal, bool checkChanged = true)
82  {
83  if (checkChanged && myDefaultValue == defaultVal)
84  return false;
85 
86  myDefaultValue = defaultVal;
87  return true;
88  }
89 
90  void reset() final { myData = myDefaultValue; }
91  void updateDefault() final { myDefaultValue = myData; }
92  bool isDefault() const final { return myData == myDefaultValue; }
93  bool isDataField() const override { return true; }
94  bool isPrintable() const override { return true; }
95 
96 protected:
97  FFaFieldBase* copy(const FFaFieldBase* field, bool defaultValueToo) override
98  {
99  if (this == field || !this->isDataField())
100  return this;
101 
102  const FFaField<ValueT>* ref = dynamic_cast<const FFaField<ValueT>*>(field);
103  if (ref)
104  {
105  this->myData = ref->myData;
106  if (defaultValueToo)
107  this->myDefaultValue = ref->myDefaultValue;
108  }
109  else
110  std::cerr <<"FFaField<T>::copy(const FFaFieldBase*,bool)"
111  <<" - mismatch in field types" << std::endl;
112 
113  return this;
114  }
115 
116  void write(std::ostream& os) const override { os << myData; }
117  void read(std::istream& is) override { this->read(is,myData); }
118 
119 private:
121  void read(std::istream& is, ValueT& v) const { is >> v; }
123  void read(const std::string& sval, ValueT& v) const
124  {
125  std::stringstream s(sval);
126  this->read(s,v);
127  }
128 
129 protected:
130  ValueT myData;
131  ValueT myDefaultValue;
132 };
133 
134 
139 template <class valueT>
140 class FFaNoPrintField : public FFaField<valueT>
141 {
142 public:
144  bool isPrintable() const final { return false; }
145 
146 protected:
148  void write(std::ostream&) const final {}
150  void read(std::istream&) final {}
151 };
152 
153 
158 template <class valueT>
159 class FFaNoCopyField : public FFaField<valueT>
160 {
161 protected:
163  FFaFieldBase* copy(const FFaFieldBase*, bool) final { return this; }
164 };
165 
166 
171 template <class T>
172 class FFaObsoleteField : public FFaField<T>
173 {
174 public:
176  FFaObsoleteField() : isRead(false) {}
177 
179  bool wasOnFile() const { return isRead; }
180 
182  bool isPrintable() const final { return false; }
183 
184 protected:
185  FFaFieldBase* copy(const FFaFieldBase* field, bool defaultValueToo) final
186  {
187  if (this == field || !this->isDataField())
188  return this;
189 
190  const FFaObsoleteField<T>* ref = dynamic_cast<const FFaObsoleteField<T>*>(field);
191  if (ref)
192  {
193  this->isRead = ref->isRead;
194  this->myData = ref->myData;
195  if (defaultValueToo)
196  this->myDefaultValue = ref->myDefaultValue;
197  }
198  else
199  std::cerr <<"FFaObsoleteField<T>::copy(const FFaFieldBase*,bool)"
200  <<" - mismatch in field type"<< std::endl;
201 
202  return this;
203  }
204 
206  void write(std::ostream&) const final {}
208  void read(std::istream& is) final
209  {
210  this->FFaField<T>::read(is);
211  isRead = true;
212  }
213 
214 private:
215  bool isRead;
216 };
217 
218 
220 
221 template<>
223 {
224  myData = myDefaultValue = 0;
225 }
226 
227 template<>
229 {
230  myData = myDefaultValue = 0;
231 }
232 
233 template<>
235 {
236  myData = myDefaultValue = 0;
237 }
238 
239 template<>
241 {
242  myData = myDefaultValue = 0;
243 }
244 
245 template<>
247 {
248  myData = myDefaultValue = { 0, 0 };
249 }
250 
251 template<>
253 {
254  myData = myDefaultValue = 0.0f;
255 }
256 
257 template<>
259 {
260  myData = myDefaultValue = 0.0;
261 }
262 
263 template<>
265 {
266  myData = myDefaultValue = { 0.0, 0.0 };
267 }
268 
269 template<>
271 {
272  myData = myDefaultValue = false;
273 }
274 
275 
276 template<>
277 inline void FFaField<int>::read(std::istream& is, int& v) const
278 {
279  FFaFieldBase::readInt(is,v);
280 }
281 
282 
283 template<>
284 inline void FFaField<float>::write(std::ostream& os) const
285 {
286  std::streamsize oldPrec = os.precision(7);
287  os << myData;
288  os.precision(oldPrec);
289 }
290 
291 
292 template<>
293 inline void FFaField<Ints>::write(std::ostream& os) const
294 {
295  os << myData.first <<" "<< myData.second;
296 }
297 
298 template<>
299 inline void FFaField<Ints>::read(std::istream& is, Ints& v) const
300 {
301  if (FFaFieldBase::readInt(is,v.first))
302  FFaFieldBase::readInt(is,v.second);
303 }
304 
305 
306 template<>
307 inline bool FFaField<std::string>::isPrintable() const
308 {
309  return !myData.empty();
310 }
311 
312 template<>
313 inline void FFaField<std::string>::write(std::ostream& os) const
314 {
316 }
317 
318 template<>
319 inline void FFaField<std::string>::read(std::istream& is, std::string& v) const
320 {
321  v.clear();
323 }
324 
325 
326 template<>
327 inline bool FFaField<Strings>::isPrintable() const
328 {
329  return !myData.empty();
330 }
331 
332 template<>
333 inline void FFaField<Strings>::write(std::ostream& os) const
334 {
335  FFaFieldBase::writeString(os,myData.front());
336 
337  for (size_t i = 1; i < myData.size(); i++)
338  FFaFieldBase::writeString(os, myData[i], myData[i].empty() ? " " : "\n\t");
339 }
340 
341 template<>
342 inline void FFaField<Strings>::read(std::istream& is, Strings& v) const
343 {
344  v.clear();
346 }
347 
348 
349 template<>
350 inline bool FFaField<StringSet>::isPrintable() const
351 {
352  return !myData.empty();
353 }
354 
355 template<>
356 inline void FFaField<StringSet>::write(std::ostream& os) const
357 {
358  StringSet::const_iterator it = myData.begin();
360 
361  for (++it; it != myData.end(); ++it)
362  FFaFieldBase::writeString(os, *it, it->empty() ? " " : "\n\t");
363 }
364 
365 template<>
366 inline void FFaField<StringSet>::read(std::istream& is, StringSet& v) const
367 {
368  Strings tmp;
370 
371  v.clear();
372  v.insert(tmp.begin(),tmp.end());
373 }
374 
375 
376 template<>
377 inline void FFaField<Doubles>::write(std::ostream& os) const
378 {
379  os << myData.first <<" "<< myData.second;
380 }
381 
382 template<>
383 inline void FFaField<Doubles>::read(std::istream& is, Doubles& v) const
384 {
385  if (is) is >> v.first;
386  if (is) is >> v.second;
387 }
388 
389 
390 template<>
391 inline bool FFaField<DoubleVec>::isPrintable() const
392 {
393  return !myData.empty();
394 }
395 
396 template<>
397 inline void FFaField<DoubleVec>::write(std::ostream& os) const
398 {
399  for (size_t i = 0; i < myData.size(); i++)
400  {
401  // write up to 6 numbers on a row
402  if (i) os << (i%6 ? " " : "\n\t");
403  os << myData[i];
404  }
405 }
406 
407 template<>
408 inline void FFaField<DoubleVec>::read(std::istream& is, DoubleVec& v) const
409 {
410  v.clear();
411 
412  double d;
413  while (is)
414  {
415  is >> d;
416  if (is)
417  v.push_back(d);
418  }
419 
420  is.clear();
421 }
422 
423 
424 template<>
425 inline bool FFaField<IntVec>::isPrintable() const
426 {
427  return !myData.empty();
428 }
429 
430 template<>
431 inline void FFaField<IntVec>::write(std::ostream& os) const
432 {
433  for (size_t i = 0; i < myData.size(); i++)
434  {
435  // write up to 10 numbers on a row
436  if (i) os << (i%10 ? " " : "\n\t");
437  os << myData[i];
438  }
439 }
440 
441 template<>
442 inline void FFaField<IntVec>::read(std::istream& is, IntVec& v) const
443 {
444  v.clear();
445 
446  int i;
447  while (is)
448  if (FFaFieldBase::readInt(is,i,true))
449  v.push_back(i);
450 
451  is.clear();
452 }
453 
454 
455 template<>
456 inline bool FFaField<BoolVec>::isPrintable() const
457 {
458  return !myData.empty();
459 }
460 
461 template<>
462 inline void FFaField<BoolVec>::write(std::ostream& os) const
463 {
464  for (size_t i = 0; i < myData.size(); i++)
465  {
466  // write up to 20 values on a row
467  if (i) os << (i%20 ? " " : "\n\t");
468  os << (myData[i] ? 1 : 0);
469  }
470 }
471 
472 template<>
473 inline void FFaField<BoolVec>::read(std::istream& is, BoolVec& v) const
474 {
475  v.clear();
476 
477  int i;
478  while (is)
479  {
480  is >> i;
481  if (is)
482  v.push_back(i > 0 ? true : false);
483  }
484 
485  is.clear();
486 }
487 
488 
489 template<>
490 inline bool FFaField<IntDoubles>::isPrintable() const
491 {
492  return !myData.empty();
493 }
494 
495 template<>
496 inline void FFaField<IntDoubles>::write(std::ostream& os) const
497 {
498  for (size_t i = 0; i < myData.size(); i++)
499  {
500  // write up to 6 pairs on a row
501  if (i) os << (i%6 ? " " : "\n\t");
502  os << myData[i].first <<" "<< myData[i].second;
503  }
504 }
505 
506 template<>
507 inline void FFaField<IntDoubles>::read(std::istream& is, IntDoubles& v) const
508 {
509  v.clear();
510 
511  int i;
512  double d;
513  while (is)
514  if (FFaFieldBase::readInt(is,i,true))
515  {
516  is >> d;
517  if (is)
518  v.emplace_back(i,d);
519  }
520 
521  is.clear();
522 }
523 
524 
525 template<>
526 inline void FFaField<bool>::write(std::ostream& os) const
527 {
528  os << (myData ? "true" : "false");
529 }
530 
531 template<>
532 inline void FFaField<bool>::read(std::istream& is, bool& v) const
533 {
534  char c;
535 
536  // skip whitespaces
537  while (is.get(c) && isspace(c));
538 
539  // check if we have a numerical bool representation
540  if (isdigit(c))
541  v = (c == '0' ? false : true);
542  else
543  {
544  // we have alpha or end sign
545  std::string tmpStr;
546  while (is && isalpha(c))
547  {
548  tmpStr += toupper(c);
549  is.get(c);
550  }
551  is.putback(c); // putting back end sign, space or whatever
552 
553  v = (tmpStr == "YES" || tmpStr == "TRUE") ? true : false;
554  }
555 }
556 
558 #endif
Base class for all model database fields.
std::vector< std::string > Strings
Convenience type alias.
Definition: FFaFieldBase.H:20
std::set< std::string > StringSet
Convenience type alias.
Definition: FFaField.H:27
std::vector< bool > BoolVec
Convenience type alias.
Definition: FFaField.H:26
std::pair< int, double > IntDouble
Convenience type alias.
Definition: FFaField.H:20
std::vector< int > IntVec
Convenience type alias.
Definition: FFaField.H:25
std::pair< int, int > Ints
Convenience type alias.
Definition: FFaField.H:24
std::vector< IntDouble > IntDoubles
Convenience type alias.
Definition: FFaField.H:21
std::pair< double, double > Doubles
Convenience type alias.
Definition: FFaField.H:22
std::vector< double > DoubleVec
Convenience type alias.
Definition: FFaField.H:23
Base class for all model database fields.
Definition: FFaFieldBase.H:30
static void writeString(std::ostream &os, const std::string &str, const char *pfx=NULL)
Writes the string value str to the output stream os.
Definition: FFaFieldBase.C:37
static void readString(std::istream &is, std::string &str)
Reads a string value from an input stream.
Definition: FFaFieldBase.C:45
static void readStrings(std::istream &os, Strings &str)
Reads a list of string values from an input stream.
Definition: FFaFieldBase.C:60
static bool readInt(std::istream &is, int &val, bool silence=false)
Reads an integer value from an input stream.
Definition: FFaFieldBase.C:96
Template class for field representation in the FEDEM model database.
Definition: FFaField.H:39
FFaFieldBase * copy(const FFaFieldBase *field, bool defaultValueToo) override
Copies another database field into *this.
Definition: FFaField.H:97
bool isDefault() const final
Returns true, if the field value equals its default value.
Definition: FFaField.H:92
FFaField()
Empty default constructor.
Definition: FFaField.H:42
void read(const std::string &sval, ValueT &v) const
Reads the field value from a std::string.
Definition: FFaField.H:123
ValueT myDefaultValue
Default field value.
Definition: FFaField.H:131
bool setValue(const ValueT &newData, bool checkChanged=true)
Assigns a new value to the field.
Definition: FFaField.H:66
ValueT & operator=(const ValueT &data)
Assignment operator.
Definition: FFaField.H:47
ValueT myData
Current field value.
Definition: FFaField.H:130
virtual ~FFaField()
Empty destructor.
Definition: FFaField.H:44
bool isPrintable() const override
Returns true, if this field should be printed.
Definition: FFaField.H:94
void read(std::istream &is) override
Reads the field value from an input stream.
Definition: FFaField.H:117
ValueT & getValue()
Returns a reference to myData (for update).
Definition: FFaField.H:58
bool isDataField() const override
Returns true, if this is a numerical data field.
Definition: FFaField.H:93
bool setDefaultValue(const ValueT &defaultVal, bool checkChanged=true)
Assigns the default value of the field.
Definition: FFaField.H:81
void write(std::ostream &os) const override
Writes the field name and value to an output stream.
Definition: FFaField.H:116
const ValueT & getValue() const
Returns a const reference to myData.
Definition: FFaField.H:56
void reset() final
Resets the field value to its default value.
Definition: FFaField.H:90
void read(std::istream &is, ValueT &v) const
Reads the field value from an input stream.
Definition: FFaField.H:121
void updateDefault() final
Updates the default value of the field.
Definition: FFaField.H:91
Subclass used for fields to be ignored when the model is copied.
Definition: FFaField.H:160
FFaFieldBase * copy(const FFaFieldBase *, bool) final
Overridden to return *this instead of a copy.
Definition: FFaField.H:163
Subclass used for internal fields that should not be printed.
Definition: FFaField.H:141
void write(std::ostream &) const final
Overridden to do nothing.
Definition: FFaField.H:148
void read(std::istream &) final
Overridden to do nothing.
Definition: FFaField.H:150
bool isPrintable() const final
Returns false, since this field should never be printed.
Definition: FFaField.H:144
Subclass used for conversion of obsolete fields in old model files.
Definition: FFaField.H:173
void read(std::istream &is) final
Overridden to set the isRead flag.
Definition: FFaField.H:208
FFaObsoleteField()
Default constructor.
Definition: FFaField.H:176
bool isPrintable() const final
Returns false, since this field should never be printed.
Definition: FFaField.H:182
FFaFieldBase * copy(const FFaFieldBase *field, bool defaultValueToo) final
Copies another database field into *this.
Definition: FFaField.H:185
bool isRead
Flag telling if this field was found in the model file.
Definition: FFaField.H:215
bool wasOnFile() const
Returns true if this field was found in the model field.
Definition: FFaField.H:179
void write(std::ostream &) const final
Overridden to do nothing.
Definition: FFaField.H:206