Arcane  v3.14.10.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-2022 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-2021 */
9/* */
10/* Gestion des propriétés. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_PROPERTY_H
13#define ARCANE_UTILS_PROPERTY_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16/*
17 * NOTE: Les classes de ce fichier sont en cours de mise au point.
18 * NOTE: L'API peut changer à tout moment. Ne pas utiliser en dehors de Arcane.
19 */
20
22#include "arcane/utils/String.h"
23#include "arcane/utils/PropertyDeclarations.h"
24
25#include <iosfwd>
26#include <functional>
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Arcane::properties
32{
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37template <typename T,typename DataType>
38class PropertySetting;
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43template <typename T>
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49template <>
50class ARCANE_UTILS_EXPORT PropertySettingTraits<String>
51{
52 public:
53 typedef StringView InputType;
54 typedef String OutputType;
55 static InputType fromJSON(const JSONValue& jv);
56 static InputType fromString(const String& v);
57 static void print(std::ostream& o,InputType v);
58 static const char* typeName() { return "String"; }
59};
60
61template <>
62class ARCANE_UTILS_EXPORT PropertySettingTraits<StringList>
63{
64 public:
65 typedef StringList InputType;
67 static InputType fromJSON(const JSONValue& jv);
68 static InputType fromString(const String& v);
69 static void print(std::ostream& o,StringCollection v);
70 static const char* typeName() { return "StringList"; }
71};
72
73template <>
74class ARCANE_UTILS_EXPORT PropertySettingTraits<bool>
75{
76 public:
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 ARCANE_UTILS_EXPORT PropertySettingTraits<Int32>
87{
88 public:
89 typedef Int32 InputType;
90 typedef Int32 OutputType;
91 static InputType fromJSON(const JSONValue& jv);
92 static InputType fromString(const String& v);
93 static void print(std::ostream& o,InputType v);
94 static const char* typeName() { return "Int32"; }
95};
96
97template <>
98class ARCANE_UTILS_EXPORT PropertySettingTraits<Int64>
99{
100 public:
101 typedef Int64 InputType;
102 typedef Int64 OutputType;
103 static InputType fromJSON(const JSONValue& jv);
104 static InputType fromString(const String& v);
105 static void print(std::ostream& o,InputType v);
106 static const char* typeName() { return "Int64"; }
107};
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
114class ARCANE_UTILS_EXPORT IPropertySetting
115{
116 public:
117 virtual ~IPropertySetting() = default;
118 public:
120 virtual String name() const =0;
122 virtual String typeName() const =0;
124 virtual String commandLineArgument() const =0;
126 virtual String description() const =0;
127};
128
129/*---------------------------------------------------------------------------*/
130/*---------------------------------------------------------------------------*/
134template <typename T>
136{
137 public:
138 virtual IPropertySetting* setting() =0;
139 virtual const IPropertySetting* setting() const =0;
140 virtual void setFromJSON(const JSONValue& v,T& instance) const =0;
141 virtual void setFromString(const String& v,T& instance) const =0;
142 virtual void print(std::ostream& o, const T& instance) const =0;
143};
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
148template<typename T>
150{
151 public:
153 {
154 return PropertySetting<T,String>(name);
155 }
156 PropertySetting<T,StringList> addStringList(StringView name)
157 {
159 }
161 {
162 return PropertySetting<T,bool>(name);
163 }
165 {
166 return PropertySetting<T,Int64>(name);
167 }
169 {
170 return PropertySetting<T,Int32>(name);
171 }
172};
173
174/*---------------------------------------------------------------------------*/
175/*---------------------------------------------------------------------------*/
179class ARCANE_UTILS_EXPORT IPropertyVisitor
180{
181 public:
182 virtual ~IPropertyVisitor() = default;
183 virtual void visit(const IPropertySetting* ps) =0;
184};
185
186/*---------------------------------------------------------------------------*/
187/*---------------------------------------------------------------------------*/
191template<typename T>
193{
194 public:
195 virtual void visit(const PropertySettingBase<T>&) =0;
196 public:
198 {
200 }
201};
202
203
204/*---------------------------------------------------------------------------*/
205/*---------------------------------------------------------------------------*/
206
207template<typename T>
209: public PropertyVisitor<T>
210{
211 public:
213 public:
214 void visit(const PropertySettingBase<T>& p) override
215 {
216 m_visitor->visit(p.setting());
217 }
218 private:
219 IPropertyVisitor* m_visitor;
220};
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224
225template <typename T,typename DataType>
227: public PropertySettingBase<T>
228, public IPropertySetting
229{
230 public:
233 typedef typename SettingsTraits::InputType InputType;
234 typedef typename SettingsTraits::OutputType OutputType;
235 public:
237 {
238 public:
239 SetterArg(T& ax,InputType av) : x(ax), v(av){}
240 T& x;
241 InputType v;
242 };
244 {
245 public:
246 GetterArg(const T& ax) : x(ax){}
247 const T& x;
248 };
249 public:
250 typedef std::function<void(SetterArg a)> SetterType;
251 typedef std::function<OutputType(GetterArg a)> GetterType;
252 public:
253 PropertySetting(StringView name,GetterType getter,SetterType setter)
254 : m_name(name), m_getter(getter), m_setter(setter) {}
256 : m_name(name), m_getter(nullptr), m_setter(nullptr) {}
257 public:
258 IPropertySetting* setting() final
259 {
260 return this;
261 }
262 const IPropertySetting* setting() const final
263 {
264 return this;
265 }
267 {
268 return m_name;
269 }
271 {
272 return m_command_line_argument;
273 }
275 {
276 return m_description;
277 }
279 {
280 return SettingsTraits::typeName();
281 }
282 ThatClass& addSetter(const SetterType& setter)
283 {
284 m_setter = setter;
285 return (*this);
286 }
287 ThatClass& addGetter(const GetterType& getter)
288 {
289 m_getter = getter;
290 return (*this);
291 }
292 ThatClass& addCommandLineArgument(const String& arg)
293 {
294 m_command_line_argument = arg;
295 return (*this);
296 }
297 ThatClass& addDescription(const String& arg)
298 {
299 m_description = arg;
300 return (*this);
301 }
302 void setInstanceValue(T& instance,InputType value) const
303 {
304 m_setter(SetterArg(instance,value));
305 }
306 void setFromJSON(const JSONValue& v,T& instance) const override
307 {
308 InputType x1 = SettingsTraits::fromJSON(v);
309 setInstanceValue(instance,x1);
310 }
311 void setFromString(const String& v,T& instance) const override
312 {
313 InputType x1 = SettingsTraits::fromString(v);
314 setInstanceValue(instance,x1);
315 }
316 void print(std::ostream& o, const T& instance) const override
317 {
318 o << "PROP: name=" << m_name << " V=";
319 if (m_getter)
320 SettingsTraits::print(o,m_getter(GetterArg(instance)));
321 else
322 o << "?";
323 o << "\n";
324 }
325 friend PropertyVisitor<T>& operator<<(PropertyVisitor<T>& o, const ThatClass& me)
326 {
327 o.visit(me);
328 return o;
329 }
330 private:
331 String m_name;
332 GetterType m_getter;
333 SetterType m_setter;
334 String m_command_line_argument;
335 String m_description;
336};
337
338/*---------------------------------------------------------------------------*/
339/*---------------------------------------------------------------------------*/
340
341class ARCANE_UTILS_EXPORT PropertySettingsBuildInfo
342{
343};
344
345class ARCANE_UTILS_EXPORT IPropertySettingsInfo
346{
347 public:
348 virtual ~IPropertySettingsInfo() = default;
349 public:
350 virtual void applyVisitor(IPropertyVisitor* v) =0;
351};
352
353/*---------------------------------------------------------------------------*/
354/*---------------------------------------------------------------------------*/
355
356template<typename T> class PropertySettingsInfo
358{
359 public:
361 {
362 ARCANE_UNUSED(sbi);
363 }
364 public:
366 create(const PropertySettingsBuildInfo& sbi,const char* filename,int line)
367 {
368 ARCANE_UNUSED(filename);
369 ARCANE_UNUSED(line);
370 auto x = new PropertySettingsInfo<T>(sbi);
371 return x;
372 }
373 public:
374 void applyVisitor(IPropertyVisitor* pv) override
375 {
376 T :: applyPropertyVisitor(pv);
377 }
378};
379
380/*---------------------------------------------------------------------------*/
381/*---------------------------------------------------------------------------*/
385class ARCANE_UTILS_EXPORT PropertySettingsRegisterer
386{
387 public:
388
389 typedef IPropertySettingsInfo* (*CreateFunc)(const PropertySettingsBuildInfo& sbi);
390 typedef PropertySettingsBuildInfo (*CreateBuildInfoFunc)();
391
392 public:
393
394 PropertySettingsRegisterer(CreateFunc func,CreateBuildInfoFunc build_info_func,
395 const char* name) ARCANE_NOEXCEPT;
396
397 public:
398
400 static PropertySettingsRegisterer* firstRegisterer();
401
403 static Integer nbRegisterer();
404
406 PropertySettingsRegisterer* previousRegisterer() const { return m_previous; }
407
409 PropertySettingsRegisterer* nextRegisterer() const { return m_next; }
410
411 public:
412
414 const char* name() const { return m_name; }
415
416 Ref<IPropertySettingsInfo> createSettingsInfoRef() const;
417
418 private:
419
422
425
426 private:
427
429 PropertySettingsRegisterer* m_previous = nullptr;
431 PropertySettingsRegisterer* m_next = nullptr;
432
433 private:
434
436 const char* m_name;
438 CreateFunc m_create_func;
439
440 private:
441
442 void _init();
443};
444
445/*---------------------------------------------------------------------------*/
446/*---------------------------------------------------------------------------*/
451extern "C++" ARCANE_UTILS_EXPORT void
452visitAllRegisteredProperties(IPropertyVisitor* visitor);
453
454/*---------------------------------------------------------------------------*/
455/*---------------------------------------------------------------------------*/
456
457} // End namespace Arcane::properties
458
459/*---------------------------------------------------------------------------*/
460/*---------------------------------------------------------------------------*/
461
462#endif
Déclarations des types utilisés dans Arcane.
Représente une valeur JSON.
Definition JSONReader.h:43
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Interface d'un paramètre de propriété.
Definition Property.h:115
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:180
Classe de base d'une proriété typée par une classe.
Definition Property.h:136
String typeName() const final
Type de la propriété
Definition Property.h:278
String name() const final
Nom de la propriété
Definition Property.h:266
String commandLineArgument() const final
Nom de l'argument de la ligne de commande (nul si aucun)
Definition Property.h:270
String description() const final
Description de la propriété
Definition Property.h:274
Enregistreur de paramètres de propriétés.
Definition Property.h:386
PropertySettingsRegisterer * previousRegisterer() const
Enregistreur précédent (nullptr si le premier)
Definition Property.h:406
const char * m_name
Nom de l'enregistreur.
Definition Property.h:436
void _setPreviousRegisterer(PropertySettingsRegisterer *s)
Positionne l'enregistreur précédent.
Definition Property.h:421
const char * name() const
Nom de classe associée.
Definition Property.h:414
PropertySettingsRegisterer * nextRegisterer() const
Enregistreur suivant (nullptr si le dernier)
Definition Property.h:409
CreateFunc m_create_func
Fonction de création.
Definition Property.h:438
void _setNextRegisterer(PropertySettingsRegisterer *s)
Positionne l'enregistreur suivant.
Definition Property.h:424
Classe de base d'un visiteur typé sur une propriété.
Definition Property.h:193
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
Chaîne de caractères unicode.
Espace de nom pour l'utilisation des accélérateurs.