Cantera  2.5.1
AnyMap.h
Go to the documentation of this file.
1 //! @file AnyMap.h
2 
3 // This file is part of Cantera. See License.txt in the top-level directory or
4 // at https://cantera.org/license.txt for license and copyright information.
5 
6 #ifndef CT_ANYMAP_H
7 #define CT_ANYMAP_H
8 
9 #include "cantera/base/ct_defs.h"
10 #include "cantera/base/global.h"
11 #include "cantera/base/Units.h"
13 
14 #include <string>
15 #include <vector>
16 #include <memory>
17 #include <unordered_map>
18 #include <functional>
19 
20 namespace boost
21 {
22 class any;
23 }
24 
25 namespace Cantera
26 {
27 
28 //! Base class defining common data possessed by both AnyMap and AnyValue
29 //! objects.
30 class AnyBase {
31 public:
32  AnyBase();
33  virtual ~AnyBase() {};
34 
35  //! For values which are derived from an input file, set the line and column
36  //! of this value in that file. Used for providing context for some error
37  //! messages.
38  void setLoc(int line, int column);
39 
40  //! Get a value from the metadata applicable to the AnyMap tree containing
41  //! this node.
42  const AnyValue& getMetadata(const std::string& key) const;
43 
44 protected:
45  //! Line where this node occurs in the input file
46  int m_line;
47 
48  //! Column where this node occurs in the input file
49  int m_column;
50 
51  //! Metadata relevant to an entire AnyMap tree, such as information about
52  // the input file used to create it
53  shared_ptr<AnyMap> m_metadata;
54 
55  friend class InputFileError;
56 };
57 
58 class AnyMap;
59 
60 //! A wrapper for a variable whose type is determined at runtime
61 /*!
62  * Instances of AnyValue are used as values in an AnyMap. Values are converted
63  * to a concrete type using the templated as() method or convenience methods
64  * such as asString() and asDouble(). See AnyMap for usage examples.
65  *
66  * Elements are set using assignment, and the assignment operator has been
67  * overloaded for specific types so that only those types are allowed to be
68  * used in an AnyValue. The allowed types are:
69  * - AnyMap
70  * - `double`
71  * - `long int`
72  * - `bool`
73  * - `std::string`
74  * - `std::vector` of any of the above
75  */
76 class AnyValue : public AnyBase
77 {
78 public:
79  AnyValue();
80  ~AnyValue();
81  AnyValue(AnyValue const& other);
82  AnyValue(AnyValue&& other);
83  AnyValue& operator=(AnyValue const& other);
84  AnyValue& operator=(AnyValue&& other);
85 
86  bool operator==(const AnyValue& other) const;
87  bool operator!=(const AnyValue& other) const;
88 
89  //! If this AnyValue is an AnyMap, return the value stored in `key`.
90  AnyValue& operator[](const std::string& key);
91  const AnyValue& operator[](const std::string& key) const;
92 
93  //! Returns `true` if this AnyValue is an AnyMap and that map contains
94  //! a key with the given name.
95  bool hasKey(const std::string& key) const;
96 
97  //! Set the name of the key storing this value in an AnyMap. Used for
98  //! providing informative error messages in class InputFileError.
99  void setKey(const std::string& key);
100 
101  //! Propagate metadata to any child elements
102  void propagateMetadata(shared_ptr<AnyMap>& file);
103 
104  //! Get the value of this key as the specified type.
105  template<class T>
106  const T& as() const;
107 
108  template<class T>
109  T& as();
110 
111  //! Returns the type of the held value.
112  const std::type_info& type() const;
113 
114  //! Returns a string specifying the type of the held value.
115  std::string type_str() const;
116 
117  //! Returns `true` if the held value is of the specified type.
118  template<class T>
119  bool is() const;
120 
121  //! Returns `true` if the held value is a scalar type (e.g. `double`, `long
122  //! int`, `string`, or `bool`).
123  bool isScalar() const;
124 
125  explicit AnyValue(const std::string& value);
126  explicit AnyValue(const char* value);
127  AnyValue& operator=(const std::string& value);
128  AnyValue& operator=(const char* value);
129  //! Return the held value, if it is a string
130  const std::string& asString() const;
131  bool operator==(const std::string& other) const;
132  bool operator!=(const std::string& other) const;
133  friend bool operator==(const std::string& lhs, const AnyValue& rhs);
134  friend bool operator!=(const std::string& lhs, const AnyValue& rhs);
135 
136  explicit AnyValue(double value);
137  AnyValue& operator=(double value);
138  //! Return the held value as a `double`, if it is a `double` or a `long
139  //! int`.
140  double& asDouble();
141  const double& asDouble() const;
142  bool operator==(const double& other) const;
143  bool operator!=(const double& other) const;
144  friend bool operator==(const double& lhs, const AnyValue& rhs);
145  friend bool operator!=(const double& lhs, const AnyValue& rhs);
146 
147  explicit AnyValue(bool value);
148  AnyValue& operator=(bool value);
149  //! Return the held value, if it is a `bool`.
150  bool& asBool();
151  const bool& asBool() const;
152 
153  explicit AnyValue(long int value);
154  explicit AnyValue(int value);
155  AnyValue& operator=(long int value);
156  AnyValue& operator=(int value);
157  //! Return the held value, if it is a `long int`.
158  long int& asInt();
159  const long int& asInt() const;
160  bool operator==(const long int& other) const;
161  bool operator!=(const long int& other) const;
162  bool operator==(const int& other) const;
163  bool operator!=(const int& other) const;
164  friend bool operator==(const long int& lhs, const AnyValue& rhs);
165  friend bool operator!=(const long int& lhs, const AnyValue& rhs);
166  friend bool operator==(const int& lhs, const AnyValue& rhs);
167  friend bool operator!=(const int& lhs, const AnyValue& rhs);
168 
169  template<class T>
170  AnyValue& operator=(const std::vector<T>& value);
171  //! Return the held value, if it is a vector of type `T`. If called with one
172  //! argument, requires the vector to be of the specified size. If called
173  //! with two arguments, requires the vector to be within the range specified
174  //! by the two values, inclusive.
175  template<class T>
176  const std::vector<T>& asVector(size_t nMin=npos, size_t nMax=npos) const;
177  template<class T>
178  std::vector<T>& asVector(size_t nMin=npos, size_t nMax=npos);
179 
180  explicit AnyValue(const AnyMap& value);
181  AnyValue& operator=(const AnyMap& value);
182  AnyValue& operator=(AnyMap&& value);
183 
184  template<class T>
185  AnyValue& operator=(const std::unordered_map<std::string, T> items);
186 
187  template<class T>
188  AnyValue& operator=(const std::map<std::string, T> items);
189 
190  //! Return the held `AnyMap` as a `std::map` where all of the values have
191  //! the specified type.
192  template<class T>
193  std::map<std::string, T> asMap() const;
194 
195  //! Access a `vector<AnyMap>` as a mapping using the value of `name` from
196  //! each item as the key in the new mapping.
197  /*!
198  * For example, for the list:
199  * ```
200  * [{name: O2, weight: 32}, {name: CH4, weight: 16}]
201  * ```
202  * calling `asMap("name")` will create a map with keys ``O2`` and ``CH4``.
203  */
204  std::unordered_map<std::string, const AnyMap*> asMap(const std::string& name) const;
205  std::unordered_map<std::string, AnyMap*> asMap(const std::string& name);
206 
207  //! Treating the value as `vector<AnyMap>`, return the item where the given
208  //! key has the specified value.
209  /*!
210  * If value is the empty string, returns the first item in the list.
211  *
212  * If the contained type is just `AnyMap` rather than `vector<AnyMap>`, it
213  * will be treated as a vector of length 1.
214  *
215  * If the value does not exist but the `create` flag is set to true, a new
216  * map with that key and value will be created and returned.
217  */
218  AnyMap& getMapWhere(const std::string& key, const std::string& value, bool create=false);
219  const AnyMap& getMapWhere(const std::string& key, const std::string& value) const;
220 
221  //! Returns `true` when getMapWhere() would succeed
222  bool hasMapWhere(const std::string& key, const std::string& value) const;
223 
224  //! @see AnyMap::applyUnits
225  void applyUnits(const UnitSystem& units);
226 
227 private:
228  std::string demangle(const std::type_info& type) const;
229 
230  template<class T>
231  void checkSize(const std::vector<T>& v, size_t nMin, size_t nMax) const;
232 
233  //! Key of this value in a parent `AnyMap`
234  std::string m_key;
235 
236  //! The held value
237  std::unique_ptr<boost::any> m_value;
238 
239  //! Human-readable names for some common types, for use when
240  //! `boost::demangle` is not available.
241  static std::map<std::string, std::string> s_typenames;
242 
243  typedef bool (*Comparer)(const boost::any&, const boost::any&);
244 
245  //! Equality comparison function used when *lhs* is of type *T*
246  template <typename T>
247  static bool eq_comparer(const boost::any& lhs, const boost::any& rhs);
248 
249  //! Helper function for comparing vectors of different (but comparable)
250  //! types, e.g. `vector<double>` and `vector<long int>`
251  template<class T, class U>
252  static bool vector_eq(const boost::any& lhs, const boost::any& rhs);
253 
254  //! Helper function for comparing nested vectors of different (but
255  //! comparable) types, e.g. `vector<vector<double>>` and
256  //! `vector<vector<long int>>`
257  template<class T, class U>
258  static bool vector2_eq(const boost::any& lhs, const boost::any& rhs);
259 
260  mutable Comparer m_equals;
261 };
262 
263 //! Implicit conversion to vector<AnyValue>
264 template<>
265 const std::vector<AnyValue>& AnyValue::asVector<AnyValue>(size_t nMin, size_t nMax) const;
266 
267 template<>
268 std::vector<AnyValue>& AnyValue::asVector<AnyValue>(size_t nMin, size_t nMax);
269 
270 //! Implicit conversion of long int to double if accessed as a vector<double>
271 template<>
272 const std::vector<double>& AnyValue::asVector<double>(size_t nMin, size_t nMax) const;
273 
274 template<>
275 std::vector<double>& AnyValue::asVector<double>(size_t nMin, size_t nMax);
276 
277 //! Implicit conversion of long int to double if accessed as a vector<vector<double>>
278 template<>
279 const std::vector<vector_fp>& AnyValue::asVector<vector_fp>(size_t nMin, size_t nMax) const;
280 
281 template<>
282 std::vector<vector_fp>& AnyValue::asVector<vector_fp>(size_t nMin, size_t nMax);
283 
284 //! Implicit conversion of AnyMap to a vector<AnyMap> of length 1, or an empty
285 //! vector<AnyValue> an empty vector<AnyMap>
286 template<>
287 const std::vector<AnyMap>& AnyValue::asVector<AnyMap>(size_t nMin, size_t nMax) const;
288 
289 template<>
290 std::vector<AnyMap>& AnyValue::asVector<AnyMap>(size_t nMin, size_t nMax);
291 
292 //! A map of string keys to values whose type can vary at runtime
293 /*!
294  * Values in an AnyMap are held by instances of AnyValue. Instances of AnyMap
295  * can be nested to form a tree.
296  *
297  * ## Setting elements
298  *
299  * ```
300  * AnyMap breakfast;
301  * breakfast["spam"] = 123.4; // Creates a value of type 'double'
302  * breakfast["eggs"] = "scrambled"; // Creates a value of type 'std::string'
303  *
304  * // Create a nested AnyMap named "beans" which has a key named "baked"
305  * // whose value is a vector<double>
306  * std::vector<double> v{3.14, 1.59, 2.65};
307  * breakfast["beans"]["baked"] = v;
308  *
309  * // Create a nested AnyMap with values of the same type
310  * std::map<std::string, double> breads{{"wheat", 4.0}, {"white", 2.5}};
311  * breakfast["toast"] = breads;
312  * // Equivalent to:
313  * breakfast["toast"]["wheat"] = 4.0
314  * breakfast["toast"]["white"] = 2.5
315  * ```
316  *
317  * ## Accessing elements
318  *
319  * ```
320  * double val1 = breakfast["spam"].asDouble();
321  * std::string val2 = breakfast["eggs"].asString();
322  * vector_fp val3 = breakfast["beans"]["baked"].asVector<double>();
323  *
324  * std::map<std::string, double> = breakfast["toast"].asMap<double>();
325  * ```
326  *
327  * ## Checking for elements
328  *
329  * ```
330  * try {
331  * breakfast["waffle"].asDouble();
332  * } except (std::exception& err) {
333  * // Exception will be thrown.
334  * // 'breakfast' will have an empty key named 'waffle' unless 'breakfast'
335  * // is a 'const AnyMap'.
336  * }
337  *
338  * try {
339  * breakfast.at("grits").asDouble();
340  * } except (std::exception& err) {
341  * // Exception will be thrown and no new key will be added
342  * }
343  *
344  * if (breakfast.hasKey("grits")) {
345  * // do something with this entry
346  * }
347  * ```
348  *
349  * ## Checking element types
350  *
351  * ```
352  * if (breakfast["sausage"].is<vector<double>>()) {
353  * // access using asVector<double>
354  * } else if (breakfast["sausage"].type() == typeid(vector<std::string>)) {
355  * // access using asVector<std::string>
356  * }
357  * ```
358  */
359 class AnyMap : public AnyBase
360 {
361 public:
362  AnyMap(): m_units() {};
363 
364  //! Create an AnyMap from a YAML file.
365  /*!
366  * Searches the directory containing the optionally-specified parent file
367  * first, followed by the current working directory and the Cantera include
368  * path.
369  */
370  static AnyMap fromYamlFile(const std::string& name,
371  const std::string& parent_name="");
372 
373  //! Create an AnyMap from a string containing a YAML document
374  static AnyMap fromYamlString(const std::string& yaml);
375 
376  //! Get the value of the item stored in `key`.
377  AnyValue& operator[](const std::string& key);
378  const AnyValue& operator[](const std::string& key) const;
379 
380  //! Get the value of the item stored in `key`. Raises an exception if the
381  //! value does not exist.
382  const AnyValue& at(const std::string& key) const;
383 
384  //! Returns `true` if the map contains an item named `key`.
385  bool hasKey(const std::string& key) const;
386 
387  //! Erase the value held by `key`.
388  void erase(const std::string& key);
389 
390  //! Erase all items in the mapping
391  void clear();
392 
393  //! Return a string listing the keys in this AnyMap, e.g. for use in error
394  //! messages
395  std::string keys_str() const;
396 
397  //! Set a metadata value that applies to this AnyMap and its children.
398  //! Mainly for internal use in reading or writing from files.
399  void setMetadata(const std::string& key, const AnyValue& value);
400 
401  //! Propagate metadata to any child elements
402  void propagateMetadata(shared_ptr<AnyMap>& file);
403 
404  //! If `key` exists, return it as a `bool`, otherwise return `default_`.
405  bool getBool(const std::string& key, bool default_) const;
406 
407  //! If `key` exists, return it as a `long int`, otherwise return `default_`.
408  long int getInt(const std::string& key, long int default_) const;
409 
410  //! If `key` exists, return it as a `double`, otherwise return `default_`.
411  double getDouble(const std::string& key, double default_) const;
412 
413  //! If `key` exists, return it as a `string`, otherwise return `default_`.
414  const std::string& getString(const std::string& key,
415  const std::string& default_) const;
416 
417  //! Convert the item stored by the given `key` to the units specified in
418  //! `units`. If the stored value is a double, convert it using the default
419  //! units. If the input is a string, treat this as a dimensioned value, e.g.
420  //! '988 kg/m^3' and convert from the specified units.
421  double convert(const std::string& key, const std::string& units) const;
422  double convert(const std::string& key, const Units& units) const;
423 
424  //! Convert the item stored by the given `key` to the units specified in
425  //! `units`. If the stored value is a double, convert it using the default
426  //! units. If the input is a string, treat this as a dimensioned value, e.g.
427  //! '988 kg/m^3' and convert from the specified units. If the key is
428  //! missing, the `default_` value is returned.
429  double convert(const std::string& key, const std::string& units,
430  double default_) const;
431 
432  //! Convert a vector of dimensional values
433  /*!
434  * For each item in the vector, if the stored value is a double, convert it
435  * using the default units. If the value is a string, treat it as a
436  * dimensioned value, e.g. '988 kg/m^3', and convert from the specified
437  * units.
438  *
439  * @param key Location of the vector in this AnyMap
440  * @param units Units to convert to
441  * @param nMin Minimum allowed length of the vector. If `nMax` is not
442  * specified, this is also taken to be the maximum length. An exception
443  * is thrown if this condition is not met.
444  * @param nMax Maximum allowed length of the vector. An exception is
445  * thrown if this condition is not met.
446  */
447  vector_fp convertVector(const std::string& key, const std::string& units,
448  size_t nMin=npos, size_t nMax=npos) const;
449 
450  //! Defined to allow use with range-based for loops. Iteration automatically
451  //! skips over keys that start and end with double underscores.
452  class Iterator {
453  public:
454  Iterator(const std::unordered_map<std::string, AnyValue>::const_iterator& start,
455  const std::unordered_map<std::string, AnyValue>::const_iterator& stop);
456 
457  const std::pair<const std::string, AnyValue>& operator*() const {
458  return *m_iter;
459  }
460  const std::pair<const std::string, AnyValue>* operator->() const {
461  return &*m_iter;
462  }
463  bool operator!=(const Iterator& right) const {
464  return m_iter != right.m_iter;
465  }
466  Iterator& operator++();
467 
468  private:
469  std::unordered_map<std::string, AnyValue>::const_iterator m_iter;
470  std::unordered_map<std::string, AnyValue>::const_iterator m_stop;
471  };
472 
473  //! Defined to allow use with range-based for loops
474  Iterator begin() const {
475  return Iterator(m_data.begin(), m_data.end());
476  }
477 
478  //! Defined to allow use with range-based for loops
479  Iterator end() const {
480  return Iterator(m_data.end(), m_data.end());
481  }
482 
483  //! Returns the number of elements in this map
484  size_t size() const {
485  return m_data.size();
486  };
487 
488  bool operator==(const AnyMap& other) const;
489  bool operator!=(const AnyMap& other) const;
490 
491  //! Return the default units that should be used to convert stored values
492  const UnitSystem& units() const { return m_units; }
493 
494  //! Use the supplied UnitSystem to set the default units, and recursively
495  //! process overrides from nodes named `units`.
496  /*!
497  * If a `units` node is present in a map that contains other keys, the
498  * specified units are taken to be the defaults for that map. If the map
499  * contains only a `units` node, and is the first item in a list of maps,
500  * then the specified units are taken to be the defaults for all the maps in
501  * the list.
502  *
503  * After being processed, the `units` nodes are removed, so this function
504  * should be called only once, on the root AnyMap. This function is called
505  * automatically by the fromYamlFile() and fromYamlString() constructors.
506  *
507  * @warning This function is an experimental part of the %Cantera API and
508  * may be changed or removed without notice.
509  */
510  void applyUnits(const UnitSystem& units);
511 
512 private:
513  //! The stored data
514  std::unordered_map<std::string, AnyValue> m_data;
515 
516  //! The default units that are used to convert stored values
518 
519  //! Cache for previously-parsed input (YAML) files. The key is the full path
520  //! to the file, and the second element of the value is the last-modified
521  //! time for the file, which is used to enable change detection.
522  static std::unordered_map<std::string, std::pair<AnyMap, int>> s_cache;
523 
524  friend class AnyValue;
525 };
526 
527 // Define begin() and end() to allow use with range-based for loops
528 AnyMap::Iterator begin(const AnyValue& v);
529 AnyMap::Iterator end(const AnyValue& v);
530 
531 //! Error thrown for problems processing information contained in an AnyMap or
532 //! AnyValue.
533 /*!
534  * This class uses the file, line, and column information stored in an AnyMap
535  * or AnyValue to provide an error message including context lines for the
536  * original user input.
537  */
539 {
540 public:
541  //! Indicate an error occurring in `procedure` while using information from
542  //! `node`. The `message` and `args` are processed as in the CanteraError
543  //! class.
544  template <typename... Args>
545  InputFileError(const std::string& procedure, const AnyBase& node,
546  const std::string& message, const Args&... args)
547  : CanteraError(
548  procedure,
549  formatError(fmt::format(message, args...),
550  node.m_line, node.m_column, node.m_metadata))
551  {
552  }
553 
554  //! Indicate an error occurring in `procedure` while using information from
555  //! `node1` and `node2`. The `message` and `args` are processed as in the
556  //! CanteraError class.
557  template <typename... Args>
558  InputFileError(const std::string& procedure, const AnyBase& node1,
559  const AnyBase& node2, const std::string& message,
560  const Args&... args)
561  : CanteraError(
562  procedure,
563  formatError2(fmt::format(message, args...),
564  node1.m_line, node1.m_column, node1.m_metadata,
565  node2.m_line, node2.m_column, node2.m_metadata))
566  {
567  }
568 
569 
570  virtual std::string getClass() const {
571  return "InputFileError";
572  }
573 protected:
574  static std::string formatError(const std::string& message,
575  int line, int column,
576  const shared_ptr<AnyMap>& metadata);
577  static std::string formatError2(const std::string& message,
578  int line1, int column1, const shared_ptr<AnyMap>& metadata1,
579  int line2, int column2, const shared_ptr<AnyMap>& metadata2);
580 };
581 
582 }
583 
584 #ifndef CANTERA_API_NO_BOOST
585 #include "cantera/base/AnyMap.inl.h"
586 #endif
587 
588 #endif
Header for unit conversion utilities, which are used to translate user input from input files (See In...
Base class defining common data possessed by both AnyMap and AnyValue objects.
Definition: AnyMap.h:30
const AnyValue & getMetadata(const std::string &key) const
Get a value from the metadata applicable to the AnyMap tree containing this node.
Definition: AnyMap.cpp:289
int m_column
Column where this node occurs in the input file.
Definition: AnyMap.h:49
void setLoc(int line, int column)
For values which are derived from an input file, set the line and column of this value in that file.
Definition: AnyMap.cpp:283
int m_line
Line where this node occurs in the input file.
Definition: AnyMap.h:46
shared_ptr< AnyMap > m_metadata
Metadata relevant to an entire AnyMap tree, such as information about.
Definition: AnyMap.h:53
Defined to allow use with range-based for loops.
Definition: AnyMap.h:452
A map of string keys to values whose type can vary at runtime.
Definition: AnyMap.h:360
AnyValue & operator[](const std::string &key)
Get the value of the item stored in key.
Definition: AnyMap.cpp:942
Iterator begin() const
Defined to allow use with range-based for loops.
Definition: AnyMap.h:474
size_t size() const
Returns the number of elements in this map.
Definition: AnyMap.h:484
std::unordered_map< std::string, AnyValue > m_data
The stored data.
Definition: AnyMap.h:514
vector_fp convertVector(const std::string &key, const std::string &units, size_t nMin=npos, size_t nMax=npos) const
Convert a vector of dimensional values.
Definition: AnyMap.cpp:1075
const AnyValue & at(const std::string &key) const
Get the value of the item stored in key.
Definition: AnyMap.cpp:974
double convert(const std::string &key, const std::string &units) const
Convert the item stored by the given key to the units specified in units.
Definition: AnyMap.cpp:1055
UnitSystem m_units
The default units that are used to convert stored values.
Definition: AnyMap.h:517
Iterator end() const
Defined to allow use with range-based for loops.
Definition: AnyMap.h:479
const std::string & getString(const std::string &key, const std::string &default_) const
If key exists, return it as a string, otherwise return default_.
Definition: AnyMap.cpp:1049
bool getBool(const std::string &key, bool default_) const
If key exists, return it as a bool, otherwise return default_.
Definition: AnyMap.cpp:1034
static AnyMap fromYamlFile(const std::string &name, const std::string &parent_name="")
Create an AnyMap from a YAML file.
Definition: AnyMap.cpp:1156
long int getInt(const std::string &key, long int default_) const
If key exists, return it as a long int, otherwise return default_.
Definition: AnyMap.cpp:1044
void erase(const std::string &key)
Erase the value held by key.
Definition: AnyMap.cpp:989
double getDouble(const std::string &key, double default_) const
If key exists, return it as a double, otherwise return default_.
Definition: AnyMap.cpp:1039
void propagateMetadata(shared_ptr< AnyMap > &file)
Propagate metadata to any child elements.
Definition: AnyMap.cpp:1014
std::string keys_str() const
Return a string listing the keys in this AnyMap, e.g.
Definition: AnyMap.cpp:999
static std::unordered_map< std::string, std::pair< AnyMap, int > > s_cache
Cache for previously-parsed input (YAML) files.
Definition: AnyMap.h:522
void clear()
Erase all items in the mapping.
Definition: AnyMap.cpp:994
static AnyMap fromYamlString(const std::string &yaml)
Create an AnyMap from a string containing a YAML document.
Definition: AnyMap.cpp:1140
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
Definition: AnyMap.cpp:984
const UnitSystem & units() const
Return the default units that should be used to convert stored values.
Definition: AnyMap.h:492
void applyUnits(const UnitSystem &units)
Use the supplied UnitSystem to set the default units, and recursively process overrides from nodes na...
Definition: AnyMap.cpp:1128
void setMetadata(const std::string &key, const AnyValue &value)
Set a metadata value that applies to this AnyMap and its children.
Definition: AnyMap.cpp:1022
A wrapper for a variable whose type is determined at runtime.
Definition: AnyMap.h:77
AnyValue & operator[](const std::string &key)
If this AnyValue is an AnyMap, return the value stored in key.
Definition: AnyMap.cpp:356
bool hasMapWhere(const std::string &key, const std::string &value) const
Returns true when getMapWhere() would succeed.
Definition: AnyMap.cpp:725
void setKey(const std::string &key)
Set the name of the key storing this value in an AnyMap.
Definition: AnyMap.cpp:370
std::unique_ptr< boost::any > m_value
The held value.
Definition: AnyMap.h:237
const std::string & asString() const
Return the held value, if it is a string.
Definition: AnyMap.cpp:424
bool & asBool()
Return the held value, if it is a bool.
Definition: AnyMap.cpp:512
const std::vector< T > & asVector(size_t nMin=npos, size_t nMax=npos) const
Return the held value, if it is a vector of type T.
Definition: AnyMap.inl.h:73
static std::map< std::string, std::string > s_typenames
Human-readable names for some common types, for use when boost::demangle is not available.
Definition: AnyMap.h:241
long int & asInt()
Return the held value, if it is a long int.
Definition: AnyMap.cpp:544
const std::type_info & type() const
Returns the type of the held value.
Definition: AnyMap.cpp:372
double & asDouble()
Return the held value as a double, if it is a double or a long int.
Definition: AnyMap.cpp:465
static bool eq_comparer(const boost::any &lhs, const boost::any &rhs)
Equality comparison function used when lhs is of type T
Definition: AnyMap.inl.h:180
bool isScalar() const
Returns true if the held value is a scalar type (e.g.
Definition: AnyMap.cpp:396
std::string type_str() const
Returns a string specifying the type of the held value.
Definition: AnyMap.cpp:392
static bool vector2_eq(const boost::any &lhs, const boost::any &rhs)
Helper function for comparing nested vectors of different (but comparable) types, e....
Definition: AnyMap.inl.h:163
void propagateMetadata(shared_ptr< AnyMap > &file)
Propagate metadata to any child elements.
Definition: AnyMap.cpp:376
static bool vector_eq(const boost::any &lhs, const boost::any &rhs)
Helper function for comparing vectors of different (but comparable) types, e.g.
Definition: AnyMap.inl.h:151
std::string m_key
Key of this value in a parent AnyMap
Definition: AnyMap.h:234
bool is() const
Returns true if the held value is of the specified type.
Definition: AnyMap.inl.h:61
AnyMap & getMapWhere(const std::string &key, const std::string &value, bool create=false)
Treating the value as vector<AnyMap>, return the item where the given key has the specified value.
Definition: AnyMap.cpp:675
bool hasKey(const std::string &key) const
Returns true if this AnyValue is an AnyMap and that map contains a key with the given name.
Definition: AnyMap.cpp:366
std::map< std::string, T > asMap() const
Return the held AnyMap as a std::map where all of the values have the specified type.
Definition: AnyMap.inl.h:126
void applyUnits(const UnitSystem &units)
Definition: AnyMap.cpp:748
const T & as() const
Get the value of this key as the specified type.
Definition: AnyMap.inl.h:17
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:61
Error thrown for problems processing information contained in an AnyMap or AnyValue.
Definition: AnyMap.h:539
virtual std::string getClass() const
Method overridden by derived classes to indicate their type.
Definition: AnyMap.h:570
InputFileError(const std::string &procedure, const AnyBase &node, const std::string &message, const Args &... args)
Indicate an error occurring in procedure while using information from node.
Definition: AnyMap.h:545
InputFileError(const std::string &procedure, const AnyBase &node1, const AnyBase &node2, const std::string &message, const Args &... args)
Indicate an error occurring in procedure while using information from node1 and node2.
Definition: AnyMap.h:558
Unit conversion utility.
Definition: Units.h:101
A representation of the units associated with a dimensional quantity.
Definition: Units.h:30
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles,...
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:188
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:180
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:264
Versions 6.2.0 and 6.2.1 of fmtlib do not include this define before they include windows....
Definition: fmt.h:35