14#include "arcane/Properties.h"
16#include "arcane/utils/String.h"
17#include "arcane/utils/Array.h"
18#include "arcane/utils/TraceAccessor.h"
19#include "arcane/utils/TraceInfo.h"
20#include "arcane/utils/ArgumentException.h"
21#include "arcane/utils/FatalErrorException.h"
23#include "arcane/core/datatype/SmallVariant.h"
24#include "arcane/core/datatype/DataTypeTraits.h"
26#include "arcane/core/IPropertyMng.h"
27#include "arcane/core/ISerializer.h"
60 static const int NB_TYPE = 11;
87 PropertyVariant* p =
new PropertyVariant();
89 p->m_type = PV_ArrayInt32;
95 PropertyVariant* p =
new PropertyVariant();
97 p->m_type = PV_ArrayInt64;
103 PropertyVariant* p =
new PropertyVariant();
105 p->m_type = PV_ArrayReal;
111 PropertyVariant* p =
new PropertyVariant();
113 p->m_type = PV_ArrayBool;
119 PropertyVariant* p =
new PropertyVariant();
121 p->m_type = PV_ArrayString;
127 PropertyVariant* p =
new PropertyVariant();
128 p->m_is_scalar =
true;
131 case SmallVariant::TUnknown:
134 case SmallVariant::TReal:
135 p->m_type = PV_ScalarReal;
137 case SmallVariant::TInt32:
138 p->m_type = PV_ScalarInt32;
140 case SmallVariant::TInt64:
141 p->m_type = PV_ScalarInt64;
143 case SmallVariant::TBool:
144 p->m_type = PV_ScalarBool;
146 case SmallVariant::TString:
147 p->m_type = PV_ScalarString;
185 eType type()
const {
return m_type; }
210 virtual void print(std::ostream& o, PropertyVariant* v) = 0;
211 virtual const String& typeName()
const = 0;
212 virtual void serializeReserve(ISerializer* s, PropertyVariant* v) = 0;
213 virtual void serializePut(ISerializer* s, PropertyVariant* v) = 0;
222 template <
typename DataType>
223 void _directPutScalar(
ISerializer* s,
const DataType& value)
228 void _directPutScalar(
ISerializer* s,
const bool& value)
233 template <
typename DataType>
234 void _directReserveScalar(
ISerializer* s,
const DataType&)
244 template <
typename DataType>
253 Int64 n = values.size();
261 Int64 n = values.size();
262 for (
Integer i = 0; i < n; ++i)
263 s->reserve(values[i]);
266 template <
typename DataType>
269 s->putInt64(values.size());
275 Int64 n = values.size();
278 for (
Int64 i = 0; i < n; ++i)
279 bytes[i] = values[i] ? 1 : 0;
286 Int64 n = values.size();
288 for (
Integer i = 0; i < n; ++i)
292 template <
typename DataType>
295 Int64 n = s->getInt64();
303 Int64 n = s->getInt64();
309 for (
Integer i = 0; i < n; ++i)
310 values[i] = (bytes[i] != 0);
315 Int64 n = s->getInt64();
318 for (
Integer i = 0; i < n; ++i)
326template <
typename DataType>
327class ScalarPropertyType
342 DataType d = DataType();
347 const String& typeName()
const override
355 DataType d = DataType();
357 _directReserveScalar(s, d);
363 DataType d = DataType();
365 _directPutScalar(s, d);
376template <
typename DataType>
377class ArrayPropertyType
395 for (
Integer i = 0; i < n; ++i)
396 o <<
',' <<
'[' << i <<
"]=" << x->operator[](i);
400 virtual const String& typeName()
const
438 static const Int32 SERIALIZE_VERSION = 2;
442 typedef std::map<String, PropertyVariant*> MapType;
459 MapType m_property_map;
464 template <
typename DataType>
465 bool getScalarValue(
const String& name, DataType& value)
467 typename MapType::const_iterator v = m_property_map.find(name);
468 if (v == m_property_map.end()) {
472 SmallVariant* x = v->second->getScalar();
474 throw ArgumentException(A_FUNCINFO,
"Bad data dimension for property (expecting scalar but property is array)");
479 template <
typename DataType>
480 void _setScalarValue(SmallVariant& s,
const DataType& value)
482 s.setValueAll(value);
484 void _setScalarValue(SmallVariant& s,
const String& value)
489 template <
typename DataType>
490 DataType setScalarValue(
const String& name,
const DataType& value)
492 DataType old_value = DataType();
493 MapType::iterator v = m_property_map.find(name);
494 if (v != m_property_map.end()) {
495 SmallVariant* x = v->second->getScalar();
497 throw ArgumentException(A_FUNCINFO,
"Bad data dimension for property (expecting scalar but property is array)");
500 _setScalarValue(*x, value);
504 _setScalarValue(sv, value);
505 m_property_map.insert(std::make_pair(name, PropertyVariant::create(sv)));
510 template <
typename DataType>
511 void setArrayValue(
const String& name, ConstArrayView<DataType> value)
513 MapType::iterator v = m_property_map.find(name);
514 if (v != m_property_map.end()) {
515 UniqueArray<DataType>* x = v->second->get(DataType());
517 throw ArgumentException(A_FUNCINFO,
"Bad datatype for property");
521 m_property_map.insert(std::make_pair(name, PropertyVariant::create(value)));
525 template <
typename DataType>
526 void getArrayValue(
const String& name, Array<DataType>& value)
528 MapType::const_iterator v = m_property_map.find(name);
529 if (v == m_property_map.end()) {
533 UniqueArray<DataType>* x = v->second->get(DataType());
535 throw ArgumentException(A_FUNCINFO,
"Bad datatype for property");
541 void print(std::ostream& o);
543 void serialize(ISerializer* serializer);
545 void serializeReserve(ISerializer* serializer);
546 void serializePut(ISerializer* serializer);
547 void serializeGet(ISerializer* serializer);
551 template <
typename DataType>
void
552 _serializeGetArray(ISerializer* serializer,
const String& name,
const DataType&)
554 UniqueArray<DataType> values;
555 _directGet(serializer, values);
556 setArrayValue(name, values.constView());
567, m_parent_property(0)
571 m_types.resize(PropertyVariant::NB_TYPE);
573 m_types[PropertyVariant::PV_ScalarReal] =
new ScalarPropertyType<Real>();
574 m_types[PropertyVariant::PV_ScalarInt32] =
new ScalarPropertyType<Int32>();
575 m_types[PropertyVariant::PV_ScalarInt64] =
new ScalarPropertyType<Int64>();
576 m_types[PropertyVariant::PV_ScalarBool] =
new ScalarPropertyType<bool>();
577 m_types[PropertyVariant::PV_ScalarString] =
new ScalarPropertyType<String>();
579 m_types[PropertyVariant::PV_ArrayReal] =
new ArrayPropertyType<Real>();
580 m_types[PropertyVariant::PV_ArrayInt32] =
new ArrayPropertyType<Int32>();
581 m_types[PropertyVariant::PV_ArrayInt64] =
new ArrayPropertyType<Int64>();
582 m_types[PropertyVariant::PV_ArrayBool] =
new ArrayPropertyType<bool>();
583 m_types[PropertyVariant::PV_ArrayString] =
new ArrayPropertyType<String>();
592 MapType::iterator v = m_property_map.begin();
593 MapType::iterator vend = m_property_map.end();
594 for (; v != vend; ++v)
597 for (
Integer i = 0, n = m_types.size(); i < n; ++i)
600 info(5) <<
"DESTROY PROPERTY name=" << m_name <<
" this=" <<
this;
607print(std::ostream& o)
609 MapType::iterator v = m_property_map.begin();
610 MapType::iterator vend = m_property_map.end();
611 for (; v != vend; ++v) {
612 PropertyVariant* p = v->second;
613 PropertyVariant::eType et = p->type();
614 IPropertyType* pt = m_types[et];
615 o <<
" " << v->first <<
" = ";
617 o <<
"(" << pt->typeName();
634 switch (serializer->mode()) {
635 case ISerializer::ModeReserve:
636 serializeReserve(serializer);
639 serializePut(serializer);
642 serializeGet(serializer);
653 serializer->reserveInt32(1);
654 serializer->reserveInt64(1);
656 MapType::iterator v = m_property_map.begin();
657 MapType::iterator vend = m_property_map.end();
658 for (; v != vend; ++v) {
659 PropertyVariant* p = v->second;
660 PropertyVariant::eType et = p->type();
661 serializer->reserveInt32(1);
662 serializer->reserve(v->first);
663 IPropertyType* pt = m_types[et];
664 pt->serializeReserve(serializer, p);
674 serializer->putInt32(SERIALIZE_VERSION);
677 serializer->putInt64(n);
679 MapType::iterator v = m_property_map.begin();
680 MapType::iterator vend = m_property_map.end();
681 for (; v != vend; ++v) {
682 PropertyVariant* p = v->second;
683 PropertyVariant::eType et = p->type();
684 IPropertyType* pt = m_types[et];
685 serializer->putInt32(et);
686 serializer->put(v->first);
687 pt->serializePut(serializer, p);
697 Int64 version = serializer->getInt32();
698 if (version != SERIALIZE_VERSION) {
703 pwarning() <<
"Can not reading properties from imcompatible checkpoint";
707 Int64 n = serializer->getInt64();
709 for (
Integer i = 0; i < n; ++i) {
710 Int32 type = serializer->getInt32();
711 serializer->get(name);
715 case PropertyVariant::PV_ScalarReal:
716 setScalarValue(name, serializer->getReal());
718 case PropertyVariant::PV_ScalarInt32:
719 setScalarValue(name, serializer->getInt32());
721 case PropertyVariant::PV_ScalarInt64:
722 setScalarValue(name, serializer->getInt64());
724 case PropertyVariant::PV_ScalarBool:
725 setScalarValue(name, (
bool)serializer->getByte());
727 case PropertyVariant::PV_ScalarString: {
729 serializer->get(str);
730 setScalarValue(name, str);
733 case PropertyVariant::PV_ArrayReal:
734 _serializeGetArray(serializer, name,
Real());
736 case PropertyVariant::PV_ArrayInt32:
737 _serializeGetArray(serializer, name,
Int32());
739 case PropertyVariant::PV_ArrayInt64:
740 _serializeGetArray(serializer, name,
Int64());
742 case PropertyVariant::PV_ArrayBool:
743 _serializeGetArray(serializer, name,
bool());
745 case PropertyVariant::PV_ArrayString:
746 _serializeGetArray(serializer, name, String());
749 throw FatalErrorException(A_FUNCINFO,
"Bad type");
766 bool has_create =
false;
774 pm->registerProperties(*
this);
787 bool has_create =
false;
795 pm->registerProperties(*
this);
845 m_p->setScalarValue(aname, value);
855 bool v = default_value;
865get(
const String& aname,
bool& value)
const
867 return m_p->getScalarValue(aname, value);
876 m_p->setScalarValue(aname, value);
886 Int32 v = default_value;
887 m_p->getScalarValue(
name, v);
898 return m_p->getScalarValue(
name, value);
908 m_p->setScalarValue(aname, value);
918 Int64 v = default_value;
919 m_p->getScalarValue(aname, v);
930 return m_p->getScalarValue(aname, value);
945 m_p->getScalarValue(aname, x);
960 m_p->setScalarValue(aname, value);
970 Real v = default_value;
971 m_p->getScalarValue(aname, v);
982 return m_p->getScalarValue(aname, value);
991 m_p->setScalarValue(aname, value);
1001 String v = default_value;
1002 m_p->getScalarValue(aname, v);
1013 return m_p->getScalarValue(aname, value);
1022 m_p->setArrayValue(aname, value);
1027 m_p->getArrayValue(aname, value);
1036 m_p->setArrayValue(aname, value);
1041 m_p->getArrayValue(aname, value);
1050 m_p->setArrayValue(aname, value);
1055 m_p->getArrayValue(aname, value);
1064 m_p->setArrayValue(aname, value);
1069 m_p->getArrayValue(aname, value);
1078 m_p->setArrayValue(aname, value);
1083 m_p->getArrayValue(aname, value);
1090print(std::ostream& o)
const
1101 m_p->serialize(serializer);
1119 return m_p->m_full_name;
1128 return m_p->m_property_mng;
Integer size() const
Number of elements in the vector.
Base class for 1D data vectors.
Span< const T > constSpan() const
Constant view of this array.
Interface of the property manager.
virtual void destroyProperties(const Properties &p)=0
Deletes the properties referenced by p.
@ ModePut
The serializer expects reserve().
@ ModeGet
The serializer expects get().
virtual void deleteMe()
Destroys the referenced object.
Int64 getInt64(const String &name) const
Value of the property named name.
bool get(const String &name, bool &value) const
Value of the property named name.
bool getBoolWithDefault(const String &name, bool default_value) const
Value of the property named name.
const String & name() const
Name of the property.
Int32 getInt32WithDefault(const String &name, Int32 default_value) const
Value of the property named name.
Int32 getInt32(const String &name) const
Value of the property named name.
Integer getInteger(const String &name) const
Value of the property named name.
Integer getIntegerWithDefault(const String &name, Integer default_value) const
Value of the property named name.
bool getBool(const String &name) const
Value of the property named name.
void setInteger(const String &name, Integer value)
Sets an Integer property of name name and value value.
void setInt64(const String &name, Int64 value)
Sets an Int64 property of name name and value value.
void destroy()
Destroys the associated values of properties linked to this reference.
Real getReal(const String &name) const
Value of the property named name.
void setInt32(const String &name, Int32 value)
Sets an Int32 property of name name and value value.
Int64 getInt64WithDefault(const String &name, Int64 default_value) const
Value of the property named name.
void set(const String &name, bool value)
Sets a boolean property of name name and value value.
String getString(const String &name) const
Value of the property named name.
Real getRealWithDefault(const String &name, Real default_value) const
Value of the property named name.
const Properties & operator=(const Properties &rhs)
Copy assignment operator.
String getStringWithDefault(const String &name, const String &default_value) const
Value of the property named name.
void serialize(ISerializer *serializer)
Performs the serialization of the properties.
Properties(IPropertyMng *pm, const String &name)
Creates or retrieves a list of properties with name name.
virtual ~Properties()
Destroys the reference to this property.
void setBool(const String &name, bool value)
Sets a boolean property of name name and value value.
void setString(const String &name, const String &value)
Sets a String property of name name and value value.
const String & fullName() const
Full name of the property.
void print(std::ostream &o) const
Prints the properties and their values to the stream o.
void setReal(const String &name, Real value)
Sets a Real property of name name and value value.
Class managing a polymorphic type.
View of an array of elements of type T.
Unicode character string.
TraceAccessor(ITraceMng *m)
Constructs an accessor via the trace manager m.
TraceMessage info() const
Flow for an information message.
TraceMessage pwarning() const
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Array< Int64 > Int64Array
Dynamic one-dimensional array of 64-bit integers.
ConstArrayView< String > StringConstArrayView
C equivalent of a 1D array of strings.
Array< String > StringArray
Dynamic one-dimensional array of strings.
Array< bool > BoolArray
Dynamic one-dimensional array of booleans.
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
ConstArrayView< Int32 > Int32ConstArrayView
C equivalent of a 1D array of 32-bit integers.
ConstArrayView< bool > BoolConstArrayView
C equivalent of a 1D array of booleans.
ConstArrayView< Int64 > Int64ConstArrayView
C equivalent of a 1D array of 64-bit integers.
double Real
Type representing a real number.
Array< Int32 > Int32Array
Dynamic one-dimensional array of 32-bit integers.
Array< Real > RealArray
Dynamic one-dimensional array of reals.
std::int32_t Int32
Signed integer type of 32 bits.
ConstArrayView< Real > RealConstArrayView
C equivalent of a 1D array of reals.