Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
Property.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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/* Property management. */
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/*---------------------------------------------------------------------------*/
113
117class ARCCORE_COMMON_EXPORT IPropertySetting
118{
119 public:
120
121 virtual ~IPropertySetting() = default;
122
123 public:
124
126 virtual String name() const = 0;
128 virtual String typeName() const = 0;
130 virtual String commandLineArgument() const = 0;
132 virtual String description() const = 0;
133};
134
135/*---------------------------------------------------------------------------*/
136/*---------------------------------------------------------------------------*/
137
141template <typename T>
143{
144 public:
145
146 virtual IPropertySetting* setting() = 0;
147 virtual const IPropertySetting* setting() const = 0;
148 virtual void setFromJSON(const JSONValue& v, T& instance) const = 0;
149 virtual void setFromString(const String& v, T& instance) const = 0;
150 virtual void print(std::ostream& o, const T& instance) const = 0;
151};
152
153/*---------------------------------------------------------------------------*/
154/*---------------------------------------------------------------------------*/
155
156template <typename T>
158{
159 public:
160
162 {
163 return PropertySetting<T, String>(name);
164 }
166 {
168 }
170 {
171 return PropertySetting<T, bool>(name);
172 }
174 {
175 return PropertySetting<T, Int64>(name);
176 }
178 {
179 return PropertySetting<T, Int32>(name);
180 }
181};
182
183/*---------------------------------------------------------------------------*/
184/*---------------------------------------------------------------------------*/
185
189class ARCCORE_COMMON_EXPORT IPropertyVisitor
190{
191 public:
192
193 virtual ~IPropertyVisitor() = default;
194 virtual void visit(const IPropertySetting* ps) = 0;
195};
196
197/*---------------------------------------------------------------------------*/
198/*---------------------------------------------------------------------------*/
199
203template <typename T>
205{
206 public:
207
208 virtual void visit(const PropertySettingBase<T>&) = 0;
209
210 public:
211
213 {
215 }
216};
217
218/*---------------------------------------------------------------------------*/
219/*---------------------------------------------------------------------------*/
220
221template <typename T>
222class GenericPropertyVisitorWrapper
223: public PropertyVisitor<T>
224{
225 public:
226
227 GenericPropertyVisitorWrapper(IPropertyVisitor* pv)
228 : m_visitor(pv)
229 {}
230
231 public:
232
233 void visit(const PropertySettingBase<T>& p) override
234 {
235 m_visitor->visit(p.setting());
236 }
237
238 private:
239
240 IPropertyVisitor* m_visitor;
241};
242
243/*---------------------------------------------------------------------------*/
244/*---------------------------------------------------------------------------*/
245
246template <typename T, typename DataType>
247class PropertySetting
248: public PropertySettingBase<T>
249, public IPropertySetting
250{
251 public:
252
253 typedef PropertySetting<T, DataType> ThatClass;
254 typedef PropertySettingTraits<DataType> SettingsTraits;
255 typedef typename SettingsTraits::InputType InputType;
256 typedef typename SettingsTraits::OutputType OutputType;
257
258 public:
259
260 class SetterArg
261 {
262 public:
263
264 SetterArg(T& ax, InputType av)
265 : x(ax)
266 , v(av)
267 {}
268 T& x;
269 InputType v;
270 };
271 class GetterArg
272 {
273 public:
274
275 GetterArg(const T& ax)
276 : x(ax)
277 {}
278 const T& x;
279 };
280
281 public:
282
283 typedef std::function<void(SetterArg a)> SetterType;
284 typedef std::function<OutputType(GetterArg a)> GetterType;
285
286 public:
287
288 PropertySetting(StringView name, GetterType getter, SetterType setter)
289 : m_name(name)
290 , m_getter(getter)
291 , m_setter(setter)
292 {}
294 : m_name(name)
295 , m_getter(nullptr)
296 , m_setter(nullptr)
297 {}
298
299 public:
300
301 IPropertySetting* setting() final
302 {
303 return this;
304 }
305 const IPropertySetting* setting() const final
306 {
307 return this;
308 }
309 String name() const final
310 {
311 return m_name;
312 }
314 {
315 return m_command_line_argument;
316 }
317 String description() const final
318 {
319 return m_description;
320 }
321 String typeName() const final
322 {
323 return SettingsTraits::typeName();
324 }
325 ThatClass& addSetter(const SetterType& setter)
326 {
327 m_setter = setter;
328 return (*this);
329 }
330 ThatClass& addGetter(const GetterType& getter)
331 {
332 m_getter = getter;
333 return (*this);
334 }
335 ThatClass& addCommandLineArgument(const String& arg)
336 {
337 m_command_line_argument = arg;
338 return (*this);
339 }
340 ThatClass& addDescription(const String& arg)
341 {
342 m_description = arg;
343 return (*this);
344 }
345 void setInstanceValue(T& instance, InputType value) const
346 {
347 m_setter(SetterArg(instance, value));
348 }
349 void setFromJSON(const JSONValue& v, T& instance) const override
350 {
351 InputType x1 = SettingsTraits::fromJSON(v);
352 setInstanceValue(instance, x1);
353 }
354 void setFromString(const String& v, T& instance) const override
355 {
356 InputType x1 = SettingsTraits::fromString(v);
357 setInstanceValue(instance, x1);
358 }
359 void print(std::ostream& o, const T& instance) const override
360 {
361 o << "PROP: name=" << m_name << " V=";
362 if (m_getter)
363 SettingsTraits::print(o, m_getter(GetterArg(instance)));
364 else
365 o << "?";
366 o << "\n";
367 }
368 friend PropertyVisitor<T>& operator<<(PropertyVisitor<T>& o, const ThatClass& me)
369 {
370 o.visit(me);
371 return o;
372 }
373
374 private:
375
376 String m_name;
377 GetterType m_getter;
378 SetterType m_setter;
379 String m_command_line_argument;
380 String m_description;
381};
382
383/*---------------------------------------------------------------------------*/
384/*---------------------------------------------------------------------------*/
385
386class ARCCORE_COMMON_EXPORT PropertySettingsBuildInfo
387{
388};
389
390class ARCCORE_COMMON_EXPORT IPropertySettingsInfo
391{
392 public:
393
394 virtual ~IPropertySettingsInfo() = default;
395
396 public:
397
398 virtual void applyVisitor(IPropertyVisitor* v) = 0;
399};
400
401/*---------------------------------------------------------------------------*/
402/*---------------------------------------------------------------------------*/
403
404template <typename T> class PropertySettingsInfo
406{
407 public:
408
409 explicit PropertySettingsInfo([[maybe_unused]] const PropertySettingsBuildInfo& sbi)
410 {
411 }
412
413 public:
414
415 static PropertySettingsInfo*
416 create(const PropertySettingsBuildInfo& sbi, [[maybe_unused]] const char* filename,
417 [[maybe_unused]] int line)
418 {
419 auto x = new PropertySettingsInfo<T>(sbi);
420 return x;
421 }
422
423 public:
424
425 void applyVisitor(IPropertyVisitor* pv) override
426 {
427 T ::applyPropertyVisitor(pv);
428 }
429};
430
431/*---------------------------------------------------------------------------*/
432/*---------------------------------------------------------------------------*/
433
437class ARCCORE_COMMON_EXPORT PropertySettingsRegisterer
438{
439 public:
440
441 typedef IPropertySettingsInfo* (*CreateFunc)(const PropertySettingsBuildInfo& sbi);
442 typedef PropertySettingsBuildInfo (*CreateBuildInfoFunc)();
443
444 public:
445
446 PropertySettingsRegisterer(CreateFunc func, CreateBuildInfoFunc build_info_func,
447 const char* name) ARCCORE_NOEXCEPT;
448
449 public:
450
452 static PropertySettingsRegisterer* firstRegisterer();
453
455 static Integer nbRegisterer();
456
458 PropertySettingsRegisterer* previousRegisterer() const { return m_previous; }
459
461 PropertySettingsRegisterer* nextRegisterer() const { return m_next; }
462
463 public:
464
466 const char* name() const { return m_name; }
467
468 Ref<IPropertySettingsInfo> createSettingsInfoRef() const;
469
470 private:
471
473 void _setPreviousRegisterer(PropertySettingsRegisterer* s) { m_previous = s; }
474
476 void _setNextRegisterer(PropertySettingsRegisterer* s) { m_next = s; }
477
478 private:
479
481 PropertySettingsRegisterer* m_previous = nullptr;
483 PropertySettingsRegisterer* m_next = nullptr;
484
485 private:
486
488 const char* m_name = nullptr;
490 CreateFunc m_create_func = nullptr;
491
492 private:
493
494 void _init();
495};
496
497/*---------------------------------------------------------------------------*/
498/*---------------------------------------------------------------------------*/
499
504extern "C++" ARCCORE_COMMON_EXPORT void
505visitAllRegisteredProperties(IPropertyVisitor* visitor);
506
507/*---------------------------------------------------------------------------*/
508/*---------------------------------------------------------------------------*/
509
510} // namespace Arcane::properties
511
512/*---------------------------------------------------------------------------*/
513/*---------------------------------------------------------------------------*/
514
515#endif
Management of references to a C++ class.
Base class for a strongly typed collection.
Reference to an instance.
View of a UTF-8 character string.
Definition StringView.h:44
Property parameter interface.
Definition Property.h:118
virtual String description() const =0
Property description.
virtual String typeName() const =0
Property type.
virtual String name() const =0
Property name.
virtual String commandLineArgument() const =0
Command line argument name (null if none).
Property visitor interface.
Definition Property.h:190
Base class of a property typed by a class.
Definition Property.h:143
String typeName() const final
Property type.
Definition Property.h:321
String name() const final
Property name.
Definition Property.h:309
String commandLineArgument() const final
Command line argument name (null if none).
Definition Property.h:313
String description() const final
Property description.
Definition Property.h:317
PropertySettingsRegisterer * previousRegisterer() const
Previous registrar (nullptr if the first).
Definition Property.h:458
PropertySettingsRegisterer * m_previous
Previous registrar.
Definition Property.h:481
static PropertySettingsRegisterer * firstRegisterer()
Access to the first element of the registrar chain.
void _setPreviousRegisterer(PropertySettingsRegisterer *s)
Positions the previous registrar.
Definition Property.h:473
PropertySettingsRegisterer * m_next
Next registrar.
Definition Property.h:483
const char * name() const
Associated class name.
Definition Property.h:466
static Integer nbRegisterer()
Number of registrars in the chain.
PropertySettingsRegisterer * nextRegisterer() const
Next registrar (nullptr if the last).
Definition Property.h:461
CreateFunc m_create_func
Creation function.
Definition Property.h:490
void _setNextRegisterer(PropertySettingsRegisterer *s)
Positions the next registrar.
Definition Property.h:476
Base class of a typed visitor on a property.
Definition Property.h:205
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
List< String > StringList
Unicode string list.
Definition UtilsTypes.h:509
std::int32_t Int32
Signed integer type of 32 bits.