|
TrioCFD 1.9.8
TrioCFD documentation
|
Helper class to factorize the readOn method of Objet_U classes. More...
#include <Param.h>
Public Types | |
| enum | Nature { OPTIONAL = 0 , REQUIRED = 1 } |
| Whether a registered parameter must be present in the input or may be omitted. More... | |
Public Member Functions | |
| Param (const char *owner_name) | |
Build a Param that will parse the parameters of an object named owner_name. | |
| void | ajouter (const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL) |
| Register an integer parameter. | |
| void | ajouter (const char *keyword, const double *value, Param::Nature nat=Param::OPTIONAL) |
| Register a floating-point parameter. | |
| void | ajouter (const char *keyword, const std::string *value, Param::Nature nat=Param::OPTIONAL) |
| Register a string parameter. | |
| void | ajouter (const char *keyword, const Objet_U *value, Param::Nature nat=Param::OPTIONAL) |
| Register an Objet_U parameter read with its standard readOn. | |
| void | ajouter_arr_size_predefinie (const char *keyword, const ArrOfInt *value, Param::Nature nat=Param::OPTIONAL) |
| Register an ArrOfInt whose size has already been fixed. | |
| void | ajouter_arr_size_predefinie (const char *keyword, const ArrOfDouble *value, Param::Nature nat=Param::OPTIONAL) |
| Register an ArrOfDouble whose size has already been fixed. | |
| void | ajouter (const char *keyword, const std::vector< int > *value, Param::Nature nat=Param::OPTIONAL, int size=-1) |
| Register a vector of integers. | |
| void | ajouter (const char *keyword, const std::vector< double > *value, Param::Nature nat=Param::OPTIONAL, int size=-1) |
| Register a vector of doubles. | |
| void | ajouter (const char *keyword, const std::vector< std::string > *value, Param::Nature nat=Param::OPTIONAL, int size=-1) |
| Register a vector of strings. | |
| template<typename T> | |
| void | ajouter (const char *keyword, const std::vector< TRUST_Deriv< T > > *value, Param::Nature nat=Param::OPTIONAL, int size=-1) |
| Register a vector of OWN_PTR-like objects (TRUST_Deriv<T>). | |
| void | ajouter (const char *keyword, const std::map< std::string, int > *value, Param::Nature nat=Param::OPTIONAL) |
| Register a map from string keys to integer values. | |
| void | ajouter (const char *keyword, const std::map< std::string, double > *value, Param::Nature nat=Param::OPTIONAL) |
| Register a map from string keys to double values. | |
| void | ajouter (const char *keyword, const std::map< std::string, std::string > *value, Param::Nature nat=Param::OPTIONAL) |
| Register a map from string keys to string values. | |
| template<typename T> | |
| void | ajouter (const char *keyword, const std::map< std::string, TRUST_Deriv< T > > *value, Param::Nature nat=Param::OPTIONAL) |
| Register a map from string keys to OWN_PTR-like objects (TRUST_Deriv<T>). | |
| void | ajouter_flag (const char *keyword, const bool *value) |
| Register a boolean flag whose mere presence switches it to true. | |
| Param & | ajouter_param (const char *keyword, Param::Nature nat=Param::OPTIONAL) |
| Register a nested Param block and return a reference to it so it can be populated in turn. | |
| void | ajouter_non_std (const char *keyword, const Objet_U *value, Param::Nature nat=Param::OPTIONAL) |
| Register a keyword handled by Objet_U::lire_motcle_non_standard. | |
| void | ajouter_condition (const char *condition, const char *message, const char *name=0) |
| Declare a post-read logical condition that must hold on the parameter values. | |
| void | supprimer (const char *keyword) |
| Remove a previously-registered keyword from this Param. | |
| void | supprimer_condition (const char *name) |
| Remove a previously-registered condition from this Param. | |
| void | dictionnaire (const char *option_name, int value) |
| Add an (option name, integer value) entry to the dictionary attached to a previously registered integer parameter. | |
| Param & | dictionnaire_param (const char *option_name, int value) |
| Same as dictionnaire, but also attaches a nested Param block that will be read when this option is selected. | |
| void | ajouter_int (const char *c, const int *val, Param::Nature nat=Param::OPTIONAL) |
| Alias of ajouter(const char*, const int*, Nature). | |
| void | ajouter_double (const char *c, const double *val, Param::Nature nat=Param::OPTIONAL) |
| Alias of ajouter(const char*, const double*, Nature). | |
| void | ajouter_objet (const char *c, const Objet_U *obj, Param::Nature nat=Param::OPTIONAL) |
| Alias of ajouter(const char*, const Objet_U*, Nature). | |
| int | lire_avec_accolades_depuis (Entree &is) |
Parse the parameter block { ... } from is. | |
| int | lire_sans_accolade (Entree &is) |
| Read all required keywords in the order they were registered, without enclosing braces. | |
| int | lire_avec_accolades (Entree &is) |
| Alias of lire_avec_accolades_depuis. | |
| int | read (Entree &is, int with_acco=1) |
| Low-level entry point used by lire_avec_accolades_depuis. | |
| void | print (Sortie &s) const |
| Print the current state of the registered parameters as a { ... } block. | |
| const | LIST (Nom) &get_list_mots_lus() const |
| List of keywords that have actually been read from the input. | |
| double | get_value (const Nom &mot_lu) const |
| Retrieve the value of a simple-type parameter by its value_of_<keyword> name. | |
| int | check () |
| Validate that every required keyword was read and every condition holds. | |
Protected Member Functions | |
| Param () | |
| Default-constructed Param with no owner; only for derived classes. | |
| Objet_a_lire & | create_or_get_objet_a_lire (const char *keyword) |
| Look up (or create) the Objet_a_lire associated with a keyword string. | |
Helper class to factorize the readOn method of Objet_U classes.
Param provides a declarative, keyword-based way to parse a block of parameters from an input stream (typically a .data file enclosed in { ... }). Instead of manually reading tokens in a readOn method, the developer registers each expected keyword along with a pointer to the variable that should receive its value, then calls lire_avec_accolades_depuis() to perform the actual parsing.
Supported registrations include:
Each registered keyword can be marked as OPTIONAL (default) or REQUIRED. Missing required keywords cause the read to fail in check().
Synonyms (translations) for a keyword can be declared by separating alternative spellings with a pipe character. For example:
Any of the three spellings will be accepted in the input file. See Schema_Temps_base.cpp for a real-world example.
For TRUST_Deriv<U> (OWN_PTR-like) element types, each individual object must specify its concrete type first, followed by whatever content its readOn expects. In maps of such objects, each value is automatically named using its map key. For example, given a std::map<std::string, TRUST_Deriv<Nom>>:
will produce a map where my_map.at("nom1") is named "nom1" and my_map.at("nom3") can be ref_cast to a Motcle.
Duplicate map keys are rejected with a fatal error, and for vectors whose expected size has been set via the size argument, a mismatch between the declared and the read length is also fatal.
For detailed examples, see the unit tests in unit_params.cpp.
| enum Param::Nature |
| Param::Param | ( | const char * | owner_name | ) |
|
protected |
Default-constructed Param with no owner; only for derived classes.
| void Param::ajouter | ( | const char * | keyword, |
| const double * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
| void Param::ajouter | ( | const char * | keyword, |
| const int * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register an integer parameter.
Expected syntax in the input file:
For example, if the keyword is nb_pas_dt_max, the input would contain:
| void Param::ajouter | ( | const char * | keyword, |
| const Objet_U * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register an Objet_U parameter read with its standard readOn.
The content that follows the keyword is forwarded to the object's operator>> (i.e. its readOn method). The exact expected syntax therefore depends on the concrete type of the object.
Expected syntax:
| void Param::ajouter | ( | const char * | keyword, |
| const std::map< std::string, double > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a map from string keys to double values.
Expected syntax (either of the two below):
| void Param::ajouter | ( | const char * | keyword, |
| const std::map< std::string, int > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a map from string keys to integer values.
Expected syntax (either of the two below):
| void Param::ajouter | ( | const char * | keyword, |
| const std::map< std::string, std::string > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a map from string keys to string values.
Each string value is read as a single whitespace-delimited token.
Expected syntax (either of the two below):
| void Param::ajouter | ( | const char * | keyword, |
| const std::map< std::string, TRUST_Deriv< T > > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a map from string keys to OWN_PTR-like objects (TRUST_Deriv<T>).
Each value must specify its concrete type first, followed by whatever content its readOn expects. After reading, each object is named using the key it is registered under (via nommer), and each value is type-checked against T: a value whose concrete type is not a subtype of T triggers a fatal error.
Expected syntax (either of the two below):
See the class-level documentation for a concrete example.
| T | Base type of the polymorphic objects stored as map values; each read value must be a subtype of T. |
| keyword | Name of the keyword. |
| value | Pointer to the std::map<std::string, TRUST_Deriv<T>> that will be filled. |
| nat | Whether the keyword is REQUIRED or OPTIONAL (default). |
| void Param::ajouter | ( | const char * | keyword, |
| const std::string * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a string parameter.
Expected syntax:
The value is read as a single whitespace-delimited token.
| void Param::ajouter | ( | const char * | keyword, |
| const std::vector< double > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL, | ||
| int | size = -1 ) |
Register a vector of doubles.
Expected syntax (either of the two below):
| void Param::ajouter | ( | const char * | keyword, |
| const std::vector< int > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL, | ||
| int | size = -1 ) |
Register a vector of integers.
Expected syntax (either of the two below):
| keyword | Name of the keyword. |
| value | Pointer to the std::vector<int> that will be filled. |
| nat | Whether the keyword is REQUIRED or OPTIONAL (default). |
| size | If non-negative, the expected number of elements. A read whose length differs from this value is treated as an error. Use -1 (the default) to accept any length. |
| void Param::ajouter | ( | const char * | keyword, |
| const std::vector< std::string > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL, | ||
| int | size = -1 ) |
Register a vector of strings.
Each string element is read as a single whitespace-delimited token.
Expected syntax (either of the two below):
| void Param::ajouter | ( | const char * | keyword, |
| const std::vector< TRUST_Deriv< T > > * | value, | ||
| Param::Nature | nat = Param::OPTIONAL, | ||
| int | size = -1 ) |
Register a vector of OWN_PTR-like objects (TRUST_Deriv<T>).
Each element must specify its concrete type first, followed by whatever content its readOn expects. At read time, each entry is type-checked: a value whose concrete type is not a subtype of T triggers a fatal error.
Expected syntax (either of the two below):
| T | Base type of the polymorphic objects stored in the vector; each read element must be a subtype of T. |
| keyword | Name of the keyword. |
| value | Pointer to the std::vector<TRUST_Deriv<T>> that will be filled. |
| nat | Whether the keyword is REQUIRED or OPTIONAL (default). |
| size | If non-negative, the expected number of elements. Use -1 (the default) to accept any length. |
| void Param::ajouter_arr_size_predefinie | ( | const char * | keyword, |
| const ArrOfDouble * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register an ArrOfDouble whose size has already been fixed.
Same contract as ajouter_arr_size_predefinie(const char*, const ArrOfInt*, ...) but for doubles.
Expected syntax (for an array of size N):
| keyword | Name of the keyword. |
| value | Pointer to the ArrOfDouble; its size must be set before calling lire_avec_accolades_depuis. |
| nat | Whether the keyword is REQUIRED or OPTIONAL (default). |
| void Param::ajouter_arr_size_predefinie | ( | const char * | keyword, |
| const ArrOfInt * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register an ArrOfInt whose size has already been fixed.
The array must have been resized to its expected number of elements before the read takes place. Exactly that many integers will be consumed from the stream.
Expected syntax (for an array of size N):
| keyword | Name of the keyword. |
| value | Pointer to the ArrOfInt; its size must be set before calling lire_avec_accolades_depuis. |
| nat | Whether the keyword is REQUIRED or OPTIONAL (default). |
| void Param::ajouter_condition | ( | const char * | condition, |
| const char * | message, | ||
| const char * | name = 0 ) |
Declare a post-read logical condition that must hold on the parameter values.
The condition is a formula expression, evaluated after the whole block has been read. Registered parameters contribute two kinds of variables to the expression:
If the condition evaluates to false, the read fails and message is reported.
| condition | Formula string to evaluate. |
| message | Human-readable explanation displayed when the condition fails. |
| name | Optional identifier for the condition, used by supprimer_condition. If nullptr, a name is generated. |
|
inline |
| void Param::ajouter_flag | ( | const char * | keyword, |
| const bool * | value ) |
Register a boolean flag whose mere presence switches it to true.
The flag must hold false when it is registered (otherwise a fatal error is raised): flags are toggled on only when the keyword is encountered in the input. Flags are always optional.
Expected syntax (the keyword appears alone, no associated value):
| keyword | Name of the flag. |
| value | Pointer to the bool that will be set to true if the keyword is present. Must be false at the moment of registration. |
|
inline |
Alias of ajouter(const char*, const int*, Nature).
| void Param::ajouter_non_std | ( | const char * | keyword, |
| const Objet_U * | value, | ||
| Param::Nature | nat = Param::OPTIONAL ) |
Register a keyword handled by Objet_U::lire_motcle_non_standard.
Use this when the content that follows the keyword cannot be described by any of the standard overloads of ajouter and requires ad hoc parsing in the object itself.
|
inline |
| Param & Param::ajouter_param | ( | const char * | keyword, |
| Param::Nature | nat = Param::OPTIONAL ) |
| int Param::check | ( | ) |
Validate that every required keyword was read and every condition holds.
Called automatically by lire_avec_accolades_depuis and lire_sans_accolade.
|
protected |
Look up (or create) the Objet_a_lire associated with a keyword string.
renvoit Objet_a_lire correspondant au mot le rajoute au besoin a la liste
The keyword string may contain synonyms separated by |. If any of the spellings is already used, a fatal error is raised.
| keyword | Keyword string, possibly containing synonyms. |
| void Param::dictionnaire | ( | const char * | nom_option, |
| int | valeur ) |
Add an (option name, integer value) entry to the dictionary attached to a previously registered integer parameter.
Si le dernier parametre ajoute est de type "int", associe a ce parametre une ou plusieurs chaines de caracteres et pour chacune une valeur numerique.
Once a dictionary has been attached to an integer parameter, the input file must provide one of the option names (instead of a raw integer); the registered int receives the associated value.
| option_name | Textual option name as it will appear in the input file. |
| value | Integer value assigned to the parameter when option_name is encountered. |
Il faut appeler dictionnaire pour chaque motcle autorise pour ce parametre Le parametre lu doit alors etre un motcle parmi ceux du dictionnaire de noms autorises.
| Param & Param::dictionnaire_param | ( | const char * | option_name, |
| int | value ) |
Same as dictionnaire, but also attaches a nested Param block that will be read when this option is selected.
| option_name | Textual option name. |
| value | Integer value assigned to the parameter when option_name is encountered. |
| double Param::get_value | ( | const Nom & | mot_lu | ) | const |
Retrieve the value of a simple-type parameter by its value_of_<keyword> name.
Only works for parameters of simple types (int, double, bool flag).
| mot_lu | Name in the form value_of_<keyword>. |
|
inline |
Alias of lire_avec_accolades_depuis.
| int Param::lire_avec_accolades_depuis | ( | Entree & | is | ) |
Parse the parameter block { ... } from is.
Read the is input with the parameters (defined with the Param object) inside: { .
Expects an opening brace, then alternating keywords and values (or blocks), then a closing brace. Required keywords that are missing, unknown keywords, and condition failures all trigger a fatal error.
| is | Input stream positioned just before the opening brace. |
.. } Example, to read: { a 1 b 2 } Param param(que_suis_je()); int a,b; param.ajouter("a",&a); param.ajouter("b",&b); param.lire_avec_accolades_depuis(is);
| int Param::lire_sans_accolade | ( | Entree & | is | ) |
Read all required keywords in the order they were registered, without enclosing braces.
Only works when every registered parameter is REQUIRED. Optional parameters are not supported in this mode.
| is | Input stream. |
|
inline |
| void Param::print | ( | Sortie & | s | ) | const |
| int Param::read | ( | Entree & | is, |
| int | with_acco = 1 ) |
Low-level entry point used by lire_avec_accolades_depuis.
| is | Input stream. |
| with_acco | Must be 1 (no other mode currently supported). |
| void Param::supprimer | ( | const char * | keyword | ) |
| void Param::supprimer_condition | ( | const char * | name | ) |
Remove a previously-registered condition from this Param.
| name | Name of the condition, as passed to ajouter_condition. |