TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Param.h
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Param_included
17#define Param_included
18
19#include <TRUSTTabs_forward.h>
20#include <Objet_a_lire.h>
21
22class Param;
23class Entree;
24class Motcle;
25class ptrParam; // Defined below in this file
26
27/*!
28 * @brief Helper class to factorize the `readOn` method of `Objet_U` classes.
29 *
30 * `Param` provides a declarative, keyword-based way to parse a block of parameters from an
31 * input stream (typically a `.data` file enclosed in `{ ... }`). Instead of manually reading
32 * tokens in a `readOn` method, the developer registers each expected keyword along with a
33 * pointer to the variable that should receive its value, then calls
34 * `lire_avec_accolades_depuis()` to perform the actual parsing.
35 *
36 * Supported registrations include:
37 * - simple scalar types: `int`, `double`, `std::string`, `bool` (flag)
38 * - `Objet_U` references (standard and non-standard via `lire_motcle_non_standard`)
39 * - fixed-size arrays: `ArrOfInt`, `ArrOfDouble` (size must be set before reading)
40 * - STL containers: `std::vector<T>` and `std::map<std::string, T>` with
41 * `T` being `int`, `double`, `std::string`, or `TRUST_Deriv<U>` (see below
42 * for the exact syntaxes supported)
43 * - nested `Param` blocks (see `ajouter_param`)
44 * - dictionaries mapping string options to integer values (see `dictionnaire`)
45 * - logical post-conditions evaluated after the read (see `ajouter_condition`)
46 *
47 * Each registered keyword can be marked as `OPTIONAL` (default) or `REQUIRED`. Missing
48 * required keywords cause the read to fail in `check()`.
49 *
50 * Synonyms (translations) for a keyword can be declared by separating alternative spellings
51 * with a pipe character. For example:
52 * @code
53 * param.ajouter("keyword|french_keyword|japan_keyword", &my_var);
54 * @endcode
55 * Any of the three spellings will be accepted in the input file. See `Schema_Temps_base.cpp`
56 * for a real-world example.
57 *
58 * @par Typical usage
59 * @code
60 * // Inside some Objet_U::readOn(Entree& is)
61 * Param param(que_suis_je());
62 * int a = 0, b = 0;
63 * param.ajouter("a", &a, Param::REQUIRED);
64 * param.ajouter("b", &b); // optional
65 * param.lire_avec_accolades_depuis(is); // expects { a 1 b 2 }
66 * @endcode
67 *
68 * @par Vector and map syntaxes
69 * Every `ajouter` overload that registers a `std::vector<T>` or a
70 * `std::map<std::string, T>` (with `T` being `int`, `double`, `std::string` or
71 * `TRUST_Deriv<U>`) accepts two interchangeable syntaxes. Spaces are always
72 * required between all tokens.
73 *
74 * - **Historical TRUST syntax**
75 * - Vectors declare the element count first, followed by the elements:
76 * @code
77 * <keyword> 2 v1 v2
78 * @endcode
79 * - Maps are enclosed in `{}` with alternating key/value pairs:
80 * @code
81 * <keyword> { key1 val1 key2 val2 }
82 * @endcode
83 *
84 * - **JSON/Python-like syntax**
85 * - Vectors are enclosed in brackets with comma-separated elements:
86 * @code
87 * <keyword> [ v1 , v2 , ... ]
88 * @endcode
89 * - Maps use `key : value` pairs separated by commas (a trailing comma is
90 * allowed):
91 * @code
92 * <keyword> { key1 : val1 , key2 : val2 }
93 * @endcode
94 *
95 * For `TRUST_Deriv<U>` (OWN_PTR-like) element types, each individual object must
96 * specify its concrete type first, followed by whatever content its `readOn`
97 * expects. In maps of such objects, each value is automatically named using its
98 * map key. For example, given a `std::map<std::string, TRUST_Deriv<Nom>>`:
99 * @code
100 * my_map { nom1 : Nom foo , nom2 : Nom bar , nom3 : Motcle foobar }
101 * @endcode
102 * will produce a map where `my_map.at("nom1")` is named `"nom1"` and
103 * `my_map.at("nom3")` can be `ref_cast` to a `Motcle`.
104 *
105 * Duplicate map keys are rejected with a fatal error, and for vectors whose
106 * expected size has been set via the `size` argument, a mismatch between the
107 * declared and the read length is also fatal.
108 *
109 * For detailed examples, see the unit tests in `unit_params.cpp`.
110 */
111class Param
112{
113public:
114 /*! @brief Whether a registered parameter must be present in the input or may be omitted. */
115 enum Nature { OPTIONAL = 0, REQUIRED = 1 };
116
117 /*!
118 * @brief Build a `Param` that will parse the parameters of an object named @p owner_name.
119 *
120 * @param owner_name Human-readable name of the owning object, used to produce informative
121 * error messages when parsing fails.
122 */
123 Param(const char * owner_name);
124
125 // ---------------------------------------------------------------------------
126 // Scalar overloads of ajouter(...)
127 // ---------------------------------------------------------------------------
128
129 /*!
130 * @brief Register an integer parameter.
131 *
132 * Expected syntax in the input file:
133 * @code
134 * <keyword> <integer_value>
135 * @endcode
136 * For example, if the keyword is `nb_pas_dt_max`, the input would contain:
137 * @code
138 * nb_pas_dt_max 100
139 * @endcode
140 *
141 * @param keyword Name of the keyword (optionally with synonyms separated by `|`).
142 * @param value Pointer to the `int` that will be written when the keyword is read.
143 * The pointer must remain valid until the read operation completes.
144 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
145 */
146 void ajouter(const char * keyword, const int* value, Param::Nature nat = Param::OPTIONAL);
147
148#if INT_is_64_ == 2
149 /*!
150 * @brief Register a `trustIdType` (64-bit id) parameter.
151 *
152 * Only available when TRUST is built in mixed 32/64-bit mode (`INT_is_64_ == 2`).
153 *
154 * Expected syntax:
155 * @code
156 * <keyword> <integer_value>
157 * @endcode
158 *
159 * @param keyword Name of the keyword.
160 * @param value Pointer to the `trustIdType` that will be written when the keyword is read.
161 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
162 */
163 void ajouter(const char * keyword, const trustIdType* value, Param::Nature nat = Param::OPTIONAL);
164#endif
165
166 /*!
167 * @brief Register a floating-point parameter.
168 *
169 * Expected syntax:
170 * @code
171 * <keyword> <double_value>
172 * @endcode
173 * For example:
174 * @code
175 * tmax 1.5e-3
176 * @endcode
177 *
178 * @param keyword Name of the keyword.
179 * @param value Pointer to the `double` that will be written when the keyword is read.
180 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
181 */
182 void ajouter(const char * keyword, const double* value, Param::Nature nat = Param::OPTIONAL);
183
184 /*!
185 * @brief Register a string parameter.
186 *
187 * Expected syntax:
188 * @code
189 * <keyword> <single_token>
190 * @endcode
191 * The value is read as a single whitespace-delimited token.
192 *
193 * @param keyword Name of the keyword.
194 * @param value Pointer to the `std::string` that will be written when the keyword is read.
195 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
196 */
197 void ajouter(const char * keyword, const std::string* value, Param::Nature nat = Param::OPTIONAL);
198
199 /*!
200 * @brief Register an `Objet_U` parameter read with its standard `readOn`.
201 *
202 * The content that follows the keyword is forwarded to the object's `operator>>`
203 * (i.e. its `readOn` method). The exact expected syntax therefore depends on the
204 * concrete type of the object.
205 *
206 * Expected syntax:
207 * @code
208 * <keyword> <whatever readOn expects for this object>
209 * @endcode
210 *
211 * @param keyword Name of the keyword.
212 * @param value Pointer to the `Objet_U` subclass that will be filled when the keyword
213 * is read. Must remain valid until the read completes.
214 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
215 */
216 void ajouter(const char * keyword, const Objet_U* value, Param::Nature nat = Param::OPTIONAL);
217
218 /*!
219 * @brief Register an `ArrOfInt` whose size has already been fixed.
220 *
221 * The array must have been resized to its expected number of elements @em before
222 * the read takes place. Exactly that many integers will be consumed from the stream.
223 *
224 * Expected syntax (for an array of size N):
225 * @code
226 * <keyword> <v_1> <v_2> ... <v_N>
227 * @endcode
228 *
229 * @param keyword Name of the keyword.
230 * @param value Pointer to the `ArrOfInt`; its size must be set before calling
231 * `lire_avec_accolades_depuis`.
232 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
233 */
234 void ajouter_arr_size_predefinie(const char * keyword, const ArrOfInt* value, Param::Nature nat = Param::OPTIONAL);
235
236 /*!
237 * @brief Register an `ArrOfDouble` whose size has already been fixed.
238 *
239 * Same contract as `ajouter_arr_size_predefinie(const char*, const ArrOfInt*, ...)`
240 * but for doubles.
241 *
242 * Expected syntax (for an array of size N):
243 * @code
244 * <keyword> <v_1> <v_2> ... <v_N>
245 * @endcode
246 *
247 * @param keyword Name of the keyword.
248 * @param value Pointer to the `ArrOfDouble`; its size must be set before calling
249 * `lire_avec_accolades_depuis`.
250 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
251 */
252 void ajouter_arr_size_predefinie(const char * keyword, const ArrOfDouble* value, Param::Nature nat = Param::OPTIONAL);
253
254 // ---------------------------------------------------------------------------
255 // Vector overloads of ajouter(...)
256 //
257 // See the class-level documentation for the two supported syntaxes.
258 // ---------------------------------------------------------------------------
259
260 /*!
261 * @brief Register a vector of integers.
262 *
263 * Expected syntax (either of the two below):
264 * @code
265 * <keyword> <N> <v_1> <v_2> ... <v_N> // historical TRUST syntax
266 * <keyword> [ <v_1> , <v_2> , ... , <v_N> ] // JSON/Python-like syntax
267 * @endcode
268 *
269 * @param keyword Name of the keyword.
270 * @param value Pointer to the `std::vector<int>` that will be filled.
271 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
272 * @param size If non-negative, the expected number of elements. A read whose
273 * length differs from this value is treated as an error. Use `-1`
274 * (the default) to accept any length.
275 */
276 void ajouter(const char * keyword, const std::vector<int>* value, Param::Nature nat = Param::OPTIONAL, int size = -1);
277
278 /*!
279 * @brief Register a vector of doubles.
280 *
281 * Expected syntax (either of the two below):
282 * @code
283 * <keyword> <N> <v_1> <v_2> ... <v_N> // historical TRUST syntax
284 * <keyword> [ <v_1> , <v_2> , ... , <v_N> ] // JSON/Python-like syntax
285 * @endcode
286 *
287 * @param keyword Name of the keyword.
288 * @param value Pointer to the `std::vector<double>` that will be filled.
289 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
290 * @param size If non-negative, the expected number of elements. Use `-1` (the
291 * default) to accept any length.
292 */
293 void ajouter(const char * keyword, const std::vector<double>* value, Param::Nature nat = Param::OPTIONAL, int size = -1);
294
295 /*!
296 * @brief Register a vector of strings.
297 *
298 * Each string element is read as a single whitespace-delimited token.
299 *
300 * Expected syntax (either of the two below):
301 * @code
302 * <keyword> <N> <s_1> <s_2> ... <s_N> // historical TRUST syntax
303 * <keyword> [ <s_1> , <s_2> , ... , <s_N> ] // JSON/Python-like syntax
304 * @endcode
305 *
306 * @param keyword Name of the keyword.
307 * @param value Pointer to the `std::vector<std::string>` that will be filled.
308 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
309 * @param size If non-negative, the expected number of elements. Use `-1` (the
310 * default) to accept any length.
311 */
312 void ajouter(const char * keyword, const std::vector<std::string>* value, Param::Nature nat = Param::OPTIONAL, int size = -1);
313
314 /*!
315 * @brief Register a vector of `OWN_PTR`-like objects (`TRUST_Deriv<T>`).
316 *
317 * Each element must specify its concrete type first, followed by whatever content
318 * its `readOn` expects. At read time, each entry is type-checked: a value whose
319 * concrete type is not a subtype of `T` triggers a fatal error.
320 *
321 * Expected syntax (either of the two below):
322 * @code
323 * // Historical TRUST syntax
324 * <keyword> <N> <Type_1> <readOn content_1> <Type_2> <readOn content_2> ...
325 *
326 * // JSON/Python-like syntax
327 * <keyword> [ <Type_1> <readOn content_1> , <Type_2> <readOn content_2> , ... ]
328 * @endcode
329 *
330 * @tparam T Base type of the polymorphic objects stored in the vector; each
331 * read element must be a subtype of `T`.
332 * @param keyword Name of the keyword.
333 * @param value Pointer to the `std::vector<TRUST_Deriv<T>>` that will be filled.
334 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
335 * @param size If non-negative, the expected number of elements. Use `-1` (the
336 * default) to accept any length.
337 */
338 template<typename T>
339 void ajouter(const char * keyword, const std::vector<TRUST_Deriv<T>>* value, Param::Nature nat = Param::OPTIONAL, int size = -1);
340
341 // ---------------------------------------------------------------------------
342 // Map overloads of ajouter(...)
343 //
344 // See the class-level documentation for the two supported syntaxes.
345 // ---------------------------------------------------------------------------
346
347 /*!
348 * @brief Register a map from string keys to integer values.
349 *
350 * Expected syntax (either of the two below):
351 * @code
352 * <keyword> { <key_1> <v_1> <key_2> <v_2> ... } // historical TRUST syntax
353 * <keyword> { <key_1> : <v_1> , <key_2> : <v_2> , ... } // JSON/Python-like syntax
354 * @endcode
355 *
356 * @param keyword Name of the keyword.
357 * @param value Pointer to the `std::map<std::string, int>` that will be filled.
358 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
359 */
360 void ajouter(const char * keyword, const std::map<std::string, int>* value, Param::Nature nat = Param::OPTIONAL);
361
362 /*!
363 * @brief Register a map from string keys to double values.
364 *
365 * Expected syntax (either of the two below):
366 * @code
367 * <keyword> { <key_1> <v_1> <key_2> <v_2> ... } // historical TRUST syntax
368 * <keyword> { <key_1> : <v_1> , <key_2> : <v_2> , ... } // JSON/Python-like syntax
369 * @endcode
370 *
371 * @param keyword Name of the keyword.
372 * @param value Pointer to the `std::map<std::string, double>` that will be filled.
373 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
374 */
375 void ajouter(const char * keyword, const std::map<std::string, double>* value, Param::Nature nat = Param::OPTIONAL);
376
377 /*!
378 * @brief Register a map from string keys to string values.
379 *
380 * Each string value is read as a single whitespace-delimited token.
381 *
382 * Expected syntax (either of the two below):
383 * @code
384 * <keyword> { <key_1> <s_1> <key_2> <s_2> ... } // historical TRUST syntax
385 * <keyword> { <key_1> : <s_1> , <key_2> : <s_2> , ... } // JSON/Python-like syntax
386 * @endcode
387 *
388 * @param keyword Name of the keyword.
389 * @param value Pointer to the `std::map<std::string, std::string>` that will be filled.
390 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
391 */
392 void ajouter(const char * keyword, const std::map<std::string, std::string>* value, Param::Nature nat = Param::OPTIONAL);
393
394 /*!
395 * @brief Register a map from string keys to `OWN_PTR`-like objects (`TRUST_Deriv<T>`).
396 *
397 * Each value must specify its concrete type first, followed by whatever content
398 * its `readOn` expects. After reading, each object is named using the key it is
399 * registered under (via `nommer`), and each value is type-checked against `T`:
400 * a value whose concrete type is not a subtype of `T` triggers a fatal error.
401 *
402 * Expected syntax (either of the two below):
403 * @code
404 * // Historical TRUST syntax
405 * <keyword> { <key_1> <Type_1> <readOn content_1> <key_2> <Type_2> <readOn content_2> ... }
406 *
407 * // JSON/Python-like syntax
408 * <keyword> { <key_1> : <Type_1> <readOn content_1> , <key_2> : <Type_2> <readOn content_2> , ... }
409 * @endcode
410 *
411 * See the class-level documentation for a concrete example.
412 *
413 * @tparam T Base type of the polymorphic objects stored as map values; each
414 * read value must be a subtype of `T`.
415 * @param keyword Name of the keyword.
416 * @param value Pointer to the `std::map<std::string, TRUST_Deriv<T>>` that will
417 * be filled.
418 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
419 */
420 template<typename T>
421 void ajouter(const char * keyword, const std::map<std::string, TRUST_Deriv<T>>* value, Param::Nature nat = Param::OPTIONAL);
422
423 // ---------------------------------------------------------------------------
424 // Other registrations
425 // ---------------------------------------------------------------------------
426
427 /*!
428 * @brief Register a boolean flag whose mere presence switches it to `true`.
429 *
430 * The flag must hold `false` when it is registered (otherwise a fatal error is
431 * raised): flags are toggled on only when the keyword is encountered in the
432 * input. Flags are always optional.
433 *
434 * Expected syntax (the keyword appears alone, no associated value):
435 * @code
436 * <keyword>
437 * @endcode
438 *
439 * @param keyword Name of the flag.
440 * @param value Pointer to the `bool` that will be set to `true` if the keyword
441 * is present. Must be `false` at the moment of registration.
442 */
443 void ajouter_flag(const char * keyword, const bool* value);
444
445 /*!
446 * @brief Register a nested `Param` block and return a reference to it so it can
447 * be populated in turn.
448 *
449 * The nested block has its own `{ ... }` scope in the input file.
450 *
451 * @param keyword Name of the nested block.
452 * @param nat Whether the block is @ref REQUIRED or @ref OPTIONAL (default).
453 * @return A reference to the newly-created nested `Param`, on which
454 * `ajouter(...)` can be called to declare its own sub-parameters.
455 */
456 Param& ajouter_param(const char * keyword, Param::Nature nat = Param::OPTIONAL);
457
458 /*!
459 * @brief Register a keyword handled by `Objet_U::lire_motcle_non_standard`.
460 *
461 * Use this when the content that follows the keyword cannot be described by any
462 * of the standard overloads of `ajouter` and requires ad hoc parsing in the
463 * object itself.
464 *
465 * @param keyword Name of the keyword.
466 * @param value Pointer to the `Objet_U` whose `lire_motcle_non_standard` will
467 * be called with the keyword and the input stream.
468 * @param nat Whether the keyword is @ref REQUIRED or @ref OPTIONAL (default).
469 */
470 void ajouter_non_std(const char * keyword, const Objet_U* value, Param::Nature nat = Param::OPTIONAL);
471
472 /*!
473 * @brief Declare a post-read logical condition that must hold on the parameter values.
474 *
475 * The condition is a formula expression, evaluated after the whole block has been
476 * read. Registered parameters contribute two kinds of variables to the expression:
477 * - `is_read_<keyword>`: `1` if the keyword was read, `0` otherwise
478 * - `value_of_<keyword>`: the numerical value, for simple-type parameters
479 * (`int`, `double`, `bool` flag) only
480 *
481 * If the condition evaluates to false, the read fails and @p message is reported.
482 *
483 * @param condition Formula string to evaluate.
484 * @param message Human-readable explanation displayed when the condition fails.
485 * @param name Optional identifier for the condition, used by
486 * `supprimer_condition`. If `nullptr`, a name is generated.
487 */
488 void ajouter_condition(const char* condition, const char* message, const char* name = 0);
489
490 /*!
491 * @brief Remove a previously-registered keyword from this `Param`.
492 *
493 * @param keyword Name of the keyword to remove. Must have been registered earlier.
494 */
495 void supprimer(const char * keyword);
496
497 /*!
498 * @brief Remove a previously-registered condition from this `Param`.
499 *
500 * @param name Name of the condition, as passed to `ajouter_condition`.
501 */
502 void supprimer_condition(const char* name);
503
504 /*!
505 * @brief Add an (option name, integer value) entry to the dictionary attached
506 * to a previously registered integer parameter.
507 *
508 * Once a dictionary has been attached to an integer parameter, the input file
509 * must provide one of the option names (instead of a raw integer); the
510 * registered `int` receives the associated value.
511 *
512 * @param option_name Textual option name as it will appear in the input file.
513 * @param value Integer value assigned to the parameter when @p option_name
514 * is encountered.
515 */
516 void dictionnaire(const char * option_name, int value);
517
518 /*!
519 * @brief Same as `dictionnaire`, but also attaches a nested `Param` block that
520 * will be read when this option is selected.
521 *
522 * @param option_name Textual option name.
523 * @param value Integer value assigned to the parameter when @p option_name
524 * is encountered.
525 * @return A reference to the nested `Param` associated with this option.
526 */
527 Param& dictionnaire_param(const char * option_name, int value);
528
529 // --- Aliases kept for ELI (script generation) -----------------------------
530
531 /*! @brief Alias of `ajouter(const char*, const int*, Nature)`. */
532 inline void ajouter_int(const char * c, const int* val, Param::Nature nat = Param::OPTIONAL) { ajouter(c,val,nat); }
533 /*! @brief Alias of `ajouter(const char*, const double*, Nature)`. */
534 inline void ajouter_double(const char * c, const double* val, Param::Nature nat = Param::OPTIONAL) { ajouter(c,val,nat); }
535 /*! @brief Alias of `ajouter(const char*, const Objet_U*, Nature)`. */
536 inline void ajouter_objet(const char *c, const Objet_U* obj, Param::Nature nat = Param::OPTIONAL) { ajouter(c,obj,nat); }
537
538 // ---------------------------------------------------------------------------
539 // Read / print / query
540 // ---------------------------------------------------------------------------
541
542 /*!
543 * @brief Parse the parameter block `{ ... }` from @p is.
544 *
545 * Expects an opening brace, then alternating keywords and values (or blocks),
546 * then a closing brace. Required keywords that are missing, unknown keywords,
547 * and condition failures all trigger a fatal error.
548 *
549 * @par Example
550 * To read `{ a 1 b 2 }`:
551 * @code
552 * Param param(que_suis_je());
553 * int a, b;
554 * param.ajouter("a", &a);
555 * param.ajouter("b", &b);
556 * param.lire_avec_accolades_depuis(is);
557 * @endcode
558 *
559 * @param is Input stream positioned just before the opening brace.
560 * @return `1` on success; fatal on error.
561 */
563
564 /*!
565 * @brief Read all required keywords in the order they were registered, without
566 * enclosing braces.
567 *
568 * Only works when every registered parameter is @ref REQUIRED. Optional
569 * parameters are not supported in this mode.
570 *
571 * @param is Input stream.
572 * @return `1` on success; fatal on error.
573 */
574 int lire_sans_accolade(Entree& is);
575
576 /*! @brief Alias of @ref lire_avec_accolades_depuis. */
578
579 /*!
580 * @brief Low-level entry point used by `lire_avec_accolades_depuis`.
581 *
582 * @param is Input stream.
583 * @param with_acco Must be `1` (no other mode currently supported).
584 * @return `1` on success; fatal on error.
585 */
586 int read(Entree& is, int with_acco = 1);
587
588 /*!
589 * @brief Print the current state of the registered parameters as a `{ ... }` block.
590 *
591 * Not implemented for every parameter type; falls back to a fatal error for
592 * unsupported types.
593 *
594 * @param s Output stream to print into.
595 */
596 void print(Sortie& s) const;
597
598 /*! @brief List of keywords that have actually been read from the input. */
599 inline const LIST(Nom)& get_list_mots_lus() const { return list_parametre_lu_; }
600
601 /*!
602 * @brief Retrieve the value of a simple-type parameter by its `value_of_<keyword>` name.
603 *
604 * Only works for parameters of simple types (`int`, `double`, `bool` flag).
605 *
606 * @param mot_lu Name in the form `value_of_<keyword>`.
607 * @return The parameter value as a `double`; fatal if the parameter is unknown.
608 */
609 double get_value(const Nom& mot_lu) const;
610
611 /*!
612 * @brief Validate that every required keyword was read and every condition holds.
613 *
614 * Called automatically by `lire_avec_accolades_depuis` and `lire_sans_accolade`.
615 *
616 * @return `1` if every check succeeded, `0` otherwise.
617 */
618 int check();
619
620protected:
621 /*! @brief Default-constructed `Param` with no owner; only for derived classes. */
623
624 /*!
625 * @brief Look up (or create) the `Objet_a_lire` associated with a keyword string.
626 *
627 * The keyword string may contain synonyms separated by `|`. If any of the spellings
628 * is already used, a fatal error is raised.
629 *
630 * @param keyword Keyword string, possibly containing synonyms.
631 * @return A reference to the underlying `Objet_a_lire` to be configured by the caller.
632 */
633 Objet_a_lire& create_or_get_objet_a_lire(const char * keyword);
634
635private:
636 LIST(Objet_a_lire) list_parametre_a_lire_; ///< All registered parameters.
637 Nom proprietaire_; ///< Name of the owning object, used for error messages.
638 LIST(Nom) list_parametre_lu_; ///< Keywords actually read from the input.
639 LIST(Nom) list_conditions_; ///< Registered post-conditions (formulas).
640 LIST(Nom) list_message_erreur_conditions_; ///< Error messages associated with conditions.
641 LIST(Nom) list_nom_conditions_; ///< Identifiers of the conditions.
642};
643
644
645template<typename T>
646void Param::ajouter(const char * mot, const std::vector<TRUST_Deriv<T>>* quoi, Param::Nature nat ,int size)
647{
648
650
652 obj.set_nature(natc);
653 obj.set_vec_expected_size(size);
654 auto ptr = const_cast<std::vector<TRUST_Deriv<T>>*>(quoi);
655 // captured by copy for error msg
656 std::string attr_name = mot;
657 std::string prop = proprietaire_.getString();
658
659
660 // lambda that will set the values of objects in the map
661 auto vec_initializer = [ptr, attr_name, prop](std::vector<DerObjU>& vec)
662 {
663 for (const auto& ref: vec)
664 {
665
666 if (sub_type(T, ref.valeur()))
667 {
668 const T& cast_obj = ref_cast(T, ref.valeur());
669 ptr->push_back(cast_obj);
670 }
671 else
672 {
673 Cerr <<"When reading '" << prop << "'" << finl;
674 Cerr <<"In keyword '" << attr_name << "', wrong type in vector:" << finl;
675 Cerr <<ref.valeur().le_type() << " is not a subtype of " << T::info_obj.name() << finl;
677 }
678
679 }
680 };
681 obj.set_vec_obj_initializer(vec_initializer);
682}
683
684template<typename T>
685void Param::ajouter(const char * mot, const std::map<std::string, TRUST_Deriv<T>>* quoi ,Param::Nature nat)
686{
687
688 Objet_a_lire& obj_a_lire = create_or_get_objet_a_lire(mot);
689
691 obj_a_lire.set_nature(natc);
692 auto ptr = const_cast<std::map<std::string, TRUST_Deriv<T>>*>(quoi);
693
694 // captured by copy for error msg
695 std::string attr_name = mot;
696 std::string prop = proprietaire_.getString();
697
698 // lambda that will set the values of objects in the map
699 auto map_initializer = [ptr, attr_name, prop](std::map<std::string, DerObjU>& map)
700 {
701 for (const auto& key_to_objU: map)
702 {
703 const auto& key = key_to_objU.first;
704 const auto& objU = key_to_objU.second;
705 if (sub_type(T, objU.valeur()))
706 {
707 const T& cast_obj = ref_cast(T, objU.valeur());
708 (*ptr)[key] = cast_obj;
709 // name the object with the map key
710 (*ptr)[key]->nommer(key);
711 }
712 else
713 {
714 Cerr <<"When reading '" << prop << "'" << finl;
715 Cerr <<"In keyword '" << attr_name << "', wrong type at key " << key << finl;
716 Cerr <<objU.valeur().le_type() << " is not a subtype of " << T::info_obj.name() << finl;
718 }
719
720 }
721 };
722 obj_a_lire.set_map_obj_initializer(map_initializer);
723}
724
725#endif
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Une chaine de caractere (Nom) en majuscules.
Definition Motcle.h:26
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
classe Objet_U Cette classe est la classe de base des Objets de TRUST
Definition Objet_U.h:73
void set_map_obj_initializer(map_obj_initializer_t)
void set_vec_obj_initializer(vec_obj_initializer_t)
void set_nature(Objet_a_lire::Nature n)
void set_vec_expected_size(int s)
Helper class to factorize the readOn method of Objet_U classes.
Definition Param.h:112
void supprimer_condition(const char *name)
Remove a previously-registered condition from this Param.
Definition Param.cpp:510
void ajouter_double(const char *c, const double *val, Param::Nature nat=Param::OPTIONAL)
Alias of ajouter(const char*, const double*, Nature).
Definition Param.h:534
Param(const char *owner_name)
Build a Param that will parse the parameters of an object named owner_name.
Definition Param.cpp:20
const LIST(Nom) &get_list_mots_lus() const
List of keywords that have actually been read from the input.
Definition Param.h:599
void ajouter_flag(const char *keyword, const bool *value)
Register a boolean flag whose mere presence switches it to true.
Definition Param.cpp:474
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 se...
Definition Param.cpp:300
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.
Definition Param.cpp:314
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.
Definition Param.cpp:411
void print(Sortie &s) const
Print the current state of the registered parameters as a { ... } block.
Definition Param.cpp:135
void dictionnaire(const char *option_name, int value)
Add an (option name, integer value) entry to the dictionary attached to a previously registered integ...
Definition Param.cpp:293
int lire_sans_accolade(Entree &is)
Read all required keywords in the order they were registered, without enclosing braces.
Definition Param.cpp:41
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.
Definition Param.cpp:496
int read(Entree &is, int with_acco=1)
Low-level entry point used by lire_avec_accolades_depuis.
Definition Param.cpp:62
void ajouter(const char *keyword, const int *value, Param::Nature nat=Param::OPTIONAL)
Register an integer parameter.
Definition Param.cpp:364
int check()
Validate that every required keyword was read and every condition holds.
Definition Param.cpp:184
void ajouter_objet(const char *c, const Objet_U *obj, Param::Nature nat=Param::OPTIONAL)
Alias of ajouter(const char*, const Objet_U*, Nature).
Definition Param.h:536
void ajouter_int(const char *c, const int *val, Param::Nature nat=Param::OPTIONAL)
Alias of ajouter(const char*, const int*, Nature).
Definition Param.h:532
Nature
Whether a registered parameter must be present in the input or may be omitted.
Definition Param.h:115
@ OPTIONAL
Definition Param.h:115
@ REQUIRED
Definition Param.h:115
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.
Definition Param.cpp:489
int lire_avec_accolades_depuis(Entree &is)
Parse the parameter block { ... } from is.
Definition Param.cpp:32
void supprimer(const char *keyword)
Remove a previously-registered keyword from this Param.
Definition Param.cpp:339
Param()
Default-constructed Param with no owner; only for derived classes.
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.
Definition Param.cpp:401
int lire_avec_accolades(Entree &is)
Alias of lire_avec_accolades_depuis.
Definition Param.h:577
double get_value(const Nom &mot_lu) const
Retrieve the value of a simple-type parameter by its value_of_<keyword> name.
Definition Param.cpp:525
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52
class Objet_a_lire : contient un nom, et une reference vers un int,double,flag,un Objet_U a lire,...
Definition ptrParam.h:27