Arcane  v4.1.2.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
Property.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* Property.h (C) 2000-2025 */
9/* */
10/* Gestion des propriétés. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_COMMON_INTERNAL_PROPERTY_H
13#define ARCCORE_COMMON_INTERNAL_PROPERTY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arccore/base/String.h"
18#include "arccore/base/Ref.h"
19#include "arccore/common/internal/PropertyDeclarations.h"
20
21#include <iosfwd>
22#include <functional>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane::properties
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
33template <typename T, typename DataType>
34class PropertySetting;
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39template <typename T>
42
43/*---------------------------------------------------------------------------*/
44/*---------------------------------------------------------------------------*/
45
46template <>
47class ARCCORE_COMMON_EXPORT PropertySettingTraits<String>
48{
49 public:
50
51 typedef StringView InputType;
52 typedef String OutputType;
53 static InputType fromJSON(const JSONValue& jv);
54 static InputType fromString(const String& v);
55 static void print(std::ostream& o, InputType v);
56 static const char* typeName() { return "String"; }
57};
58
59template <>
60class ARCCORE_COMMON_EXPORT PropertySettingTraits<StringList>
61{
62 public:
63
64 typedef StringList InputType;
65 typedef Collection<String> OutputType;
66 static InputType fromJSON(const JSONValue& jv);
67 static InputType fromString(const String& v);
68 static void print(std::ostream& o, Collection<String> v);
69 static const char* typeName() { return "StringList"; }
70};
71
72template <>
73class ARCCORE_COMMON_EXPORT PropertySettingTraits<bool>
74{
75 public:
76
77 typedef bool InputType;
78 typedef bool OutputType;
79 static InputType fromJSON(const JSONValue& jv);
80 static InputType fromString(const String& v);
81 static void print(std::ostream& o, InputType v);
82 static const char* typeName() { return "Bool"; }
83};
84
85template <>
86class ARCCORE_COMMON_EXPORT PropertySettingTraits<Int32>
87{
88 public:
89
90 typedef Int32 InputType;
91 typedef Int32 OutputType;
92 static InputType fromJSON(const JSONValue& jv);
93 static InputType fromString(const String& v);
94 static void print(std::ostream& o, InputType v);
95 static const char* typeName() { return "Int32"; }
96};
97
98template <>
99class ARCCORE_COMMON_EXPORT PropertySettingTraits<Int64>
100{
101 public:
102
103 typedef Int64 InputType;
104 typedef Int64 OutputType;
105 static InputType fromJSON(const JSONValue& jv);
106 static InputType fromString(const String& v);
107 static void print(std::ostream& o, InputType v);
108 static const char* typeName() { return "Int64"; }
109};
110
111/*---------------------------------------------------------------------------*/
112/*---------------------------------------------------------------------------*/
116class ARCCORE_COMMON_EXPORT IPropertySetting
117{
118 public:
119
120 virtual ~IPropertySetting() = default;
121
122 public:
123
125 virtual String name() const = 0;
127 virtual String typeName() const = 0;
129 virtual String commandLineArgument() const = 0;
131 virtual String description() const = 0;
132};
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
139template <typename T>
141{
142 public:
143
144 virtual IPropertySetting* setting() = 0;
145 virtual const IPropertySetting* setting() const = 0;
146 virtual void setFromJSON(const JSONValue& v, T& instance) const = 0;
147 virtual void setFromString(const String& v, T& instance) const = 0;
148 virtual void print(std::ostream& o, const T& instance) const = 0;
149};
150
151/*---------------------------------------------------------------------------*/
152/*---------------------------------------------------------------------------*/
153
154template <typename T>
156{
157 public:
158
160 {
161 return PropertySetting<T, String>(name);
162 }
164 {
166 }
168 {
169 return PropertySetting<T, bool>(name);
170 }
172 {
173 return PropertySetting<T, Int64>(name);
174 }
176 {
177 return PropertySetting<T, Int32>(name);
178 }
179};
180
181/*---------------------------------------------------------------------------*/
182/*---------------------------------------------------------------------------*/
186class ARCCORE_COMMON_EXPORT IPropertyVisitor
187{
188 public:
189
190 virtual ~IPropertyVisitor() = default;
191 virtual void visit(const IPropertySetting* ps) = 0;
192};
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
199template <typename T>
201{
202 public:
203
204 virtual void visit(const PropertySettingBase<T>&) = 0;
205
206 public:
207
209 {
211 }
212};
213
214/*---------------------------------------------------------------------------*/
215/*---------------------------------------------------------------------------*/
216
217template <typename T>
218class GenericPropertyVisitorWrapper
219: public PropertyVisitor<T>
220{
221 public:
222
223 GenericPropertyVisitorWrapper(IPropertyVisitor* pv)
224 : m_visitor(pv)
225 {}
226
227 public:
228
229 void visit(const PropertySettingBase<T>& p) override
230 {
231 m_visitor->visit(p.setting());
232 }
233
234 private:
235
236 IPropertyVisitor* m_visitor;
237};
238
239/*---------------------------------------------------------------------------*/
240/*---------------------------------------------------------------------------*/
241
242template <typename T, typename DataType>
243class PropertySetting
244: public PropertySettingBase<T>
245, public IPropertySetting
246{
247 public:
248
249 typedef PropertySetting<T, DataType> ThatClass;
250 typedef PropertySettingTraits<DataType> SettingsTraits;
251 typedef typename SettingsTraits::InputType InputType;
252 typedef typename SettingsTraits::OutputType OutputType;
253
254 public:
255
256 class SetterArg
257 {
258 public:
259
260 SetterArg(T& ax, InputType av)
261 : x(ax)
262 , v(av)
263 {}
264 T& x;
265 InputType v;
266 };
267 class GetterArg
268 {
269 public:
270
271 GetterArg(const T& ax)
272 : x(ax)
273 {}
274 const T& x;
275 };
276
277 public:
278
279 typedef std::function<void(SetterArg a)> SetterType;
280 typedef std::function<OutputType(GetterArg a)> GetterType;
281
282 public:
283
284 PropertySetting(StringView name, GetterType getter, SetterType setter)
285 : m_name(name)
286 , m_getter(getter)
287 , m_setter(setter)
288 {}
290 : m_name(name)
291 , m_getter(nullptr)
292 , m_setter(nullptr)
293 {}
294
295 public:
296
297 IPropertySetting* setting() final
298 {
299 return this;
300 }
301 const IPropertySetting* setting() const final
302 {
303 return this;
304 }
305 String name() const final
306 {
307 return m_name;
308 }
310 {
311 return m_command_line_argument;
312 }
313 String description() const final
314 {
315 return m_description;
316 }
317 String typeName() const final
318 {
319 return SettingsTraits::typeName();
320 }
321 ThatClass& addSetter(const SetterType& setter)
322 {
323 m_setter = setter;
324 return (*this);
325 }
326 ThatClass& addGetter(const GetterType& getter)
327 {
328 m_getter = getter;
329 return (*this);
330 }
331 ThatClass& addCommandLineArgument(const String& arg)
332 {
333 m_command_line_argument = arg;
334 return (*this);
335 }
336 ThatClass& addDescription(const String& arg)
337 {
338 m_description = arg;
339 return (*this);
340 }
341 void setInstanceValue(T& instance, InputType value) const
342 {
343 m_setter(SetterArg(instance, value));
344 }
345 void setFromJSON(const JSONValue& v, T& instance) const override
346 {
347 InputType x1 = SettingsTraits::fromJSON(v);
348 setInstanceValue(instance, x1);
349 }
350 void setFromString(const String& v, T& instance) const override
351 {
352 InputType x1 = SettingsTraits::fromString(v);
353 setInstanceValue(instance, x1);
354 }
355 void print(std::ostream& o, const T& instance) const override
356 {
357 o << "PROP: name=" << m_name << " V=";
358 if (m_getter)
359 SettingsTraits::print(o, m_getter(GetterArg(instance)));
360 else
361 o << "?";
362 o << "\n";
363 }
364 friend PropertyVisitor<T>& operator<<(PropertyVisitor<T>& o, const ThatClass& me)
365 {
366 o.visit(me);
367 return o;
368 }
369
370 private:
371
372 String m_name;
373 GetterType m_getter;
374 SetterType m_setter;
375 String m_command_line_argument;
376 String m_description;
377};
378
379/*---------------------------------------------------------------------------*/
380/*---------------------------------------------------------------------------*/
381
382class ARCCORE_COMMON_EXPORT PropertySettingsBuildInfo
383{
384};
385
386class ARCCORE_COMMON_EXPORT IPropertySettingsInfo
387{
388 public:
389
390 virtual ~IPropertySettingsInfo() = default;
391
392 public:
393
394 virtual void applyVisitor(IPropertyVisitor* v) = 0;
395};
396
397/*---------------------------------------------------------------------------*/
398/*---------------------------------------------------------------------------*/
399
400template <typename T> class PropertySettingsInfo
402{
403 public:
404
405 explicit PropertySettingsInfo([[maybe_unused]] const PropertySettingsBuildInfo& sbi)
406 {
407 }
408
409 public:
410
411 static PropertySettingsInfo*
412 create(const PropertySettingsBuildInfo& sbi, [[maybe_unused]] const char* filename,
413 [[maybe_unused]] int line)
414 {
415 auto x = new PropertySettingsInfo<T>(sbi);
416 return x;
417 }
418
419 public:
420
421 void applyVisitor(IPropertyVisitor* pv) override
422 {
423 T ::applyPropertyVisitor(pv);
424 }
425};
426
427/*---------------------------------------------------------------------------*/
428/*---------------------------------------------------------------------------*/
432class ARCCORE_COMMON_EXPORT PropertySettingsRegisterer
433{
434 public:
435
436 typedef IPropertySettingsInfo* (*CreateFunc)(const PropertySettingsBuildInfo& sbi);
437 typedef PropertySettingsBuildInfo (*CreateBuildInfoFunc)();
438
439 public:
440
441 PropertySettingsRegisterer(CreateFunc func, CreateBuildInfoFunc build_info_func,
442 const char* name) ARCCORE_NOEXCEPT;
443
444 public:
445
447 static PropertySettingsRegisterer* firstRegisterer();
448
450 static Integer nbRegisterer();
451
453 PropertySettingsRegisterer* previousRegisterer() const { return m_previous; }
454
456 PropertySettingsRegisterer* nextRegisterer() const { return m_next; }
457
458 public:
459
461 const char* name() const { return m_name; }
462
463 Ref<IPropertySettingsInfo> createSettingsInfoRef() const;
464
465 private:
466
468 void _setPreviousRegisterer(PropertySettingsRegisterer* s) { m_previous = s; }
469
471 void _setNextRegisterer(PropertySettingsRegisterer* s) { m_next = s; }
472
473 private:
474
476 PropertySettingsRegisterer* m_previous = nullptr;
478 PropertySettingsRegisterer* m_next = nullptr;
479
480 private:
481
483 const char* m_name = nullptr;
485 CreateFunc m_create_func = nullptr;
486
487 private:
488
489 void _init();
490};
491
492/*---------------------------------------------------------------------------*/
493/*---------------------------------------------------------------------------*/
498extern "C++" ARCCORE_COMMON_EXPORT void
499visitAllRegisteredProperties(IPropertyVisitor* visitor);
500
501/*---------------------------------------------------------------------------*/
502/*---------------------------------------------------------------------------*/
503
504} // namespace Arcane::properties
505
506/*---------------------------------------------------------------------------*/
507/*---------------------------------------------------------------------------*/
508
509#endif
Gestion des références à une classe C++.
Classe de base d'une collection fortement typée.
Référence à une instance.
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
Chaîne de caractères unicode.
Interface d'un paramètre de propriété.
Definition Property.h:117
virtual String description() const =0
Description de la propriété
virtual String typeName() const =0
Type de la propriété
virtual String name() const =0
Nom de la propriété
virtual String commandLineArgument() const =0
Nom de l'argument de la ligne de commande (nul si aucun)
Interface d'un visiteur sur une propriété.
Definition Property.h:187
Classe de base d'une proriété typée par une classe.
Definition Property.h:141
String typeName() const final
Type de la propriété
Definition Property.h:317
String name() const final
Nom de la propriété
Definition Property.h:305
String commandLineArgument() const final
Nom de l'argument de la ligne de commande (nul si aucun)
Definition Property.h:309
String description() const final
Description de la propriété
Definition Property.h:313
PropertySettingsRegisterer * previousRegisterer() const
Enregistreur précédent (nullptr si le premier)
Definition Property.h:453
PropertySettingsRegisterer * m_previous
Enregistreur précédent.
Definition Property.h:476
const char * m_name
Nom de l'enregistreur.
Definition Property.h:483
static PropertySettingsRegisterer * firstRegisterer()
Accès au premier élément de la chaine d'enregistreur.
void _setPreviousRegisterer(PropertySettingsRegisterer *s)
Positionne l'enregistreur précédent.
Definition Property.h:468
PropertySettingsRegisterer * m_next
Enregistreur suivant.
Definition Property.h:478
const char * name() const
Nom de classe associée.
Definition Property.h:461
static Integer nbRegisterer()
Nombre d'enregisteur dans la chaîne.
PropertySettingsRegisterer * nextRegisterer() const
Enregistreur suivant (nullptr si le dernier)
Definition Property.h:456
CreateFunc m_create_func
Fonction de création.
Definition Property.h:485
void _setNextRegisterer(PropertySettingsRegisterer *s)
Positionne l'enregistreur suivant.
Definition Property.h:471
Classe de base d'un visiteur typé sur une propriété.
Definition Property.h:201
Espace de nom pour l'utilisation des accélérateurs.
std::int64_t Int64
Type entier signé sur 64 bits.
Int32 Integer
Type représentant un entier.
List< String > StringList
Tableau de chaînes de caractères unicode.
Definition UtilsTypes.h:509
std::int32_t Int32
Type entier signé sur 32 bits.