Interface to extract data from any generic container. More...
#include <typeconverter.h>
Inherited by QtJsonSerializer::TypeExtractors::OptionalExtractor< TValue >, QtJsonSerializer::TypeExtractors::PairExtractor< TPair, TFirst, TSecond >, QtJsonSerializer::TypeExtractors::SmartPointerExtractor< TSmartPointer, TType >, QtJsonSerializer::TypeExtractors::SmartPointerExtractor< QPointer, TType >, QtJsonSerializer::TypeExtractors::TupleExtractor< TValues >, and QtJsonSerializer::TypeExtractors::VariantExtractor< TValues >.
Public Member Functions | |
virtual QByteArray | baseType () const =0 |
Returns the identifier of the general type this extractor is for. More... | |
virtual QList< int > | subtypes () const =0 |
Returns an ordered list of subtypes found in types handled by this extractor. More... | |
virtual QVariant | extract (const QVariant &value, int index=-1) const =0 |
Extracts the data of a value of the extractors type at the given index. More... | |
virtual void | emplace (QVariant &target, const QVariant &value, int index=-1) const =0 |
Emplaces the value into the target of the extractors type at the given index. More... | |
Interface to extract data from any generic container.
This interface is used by custom TypeConverter classes to get data from a generic container in a non-generic fashion. For example, an extractor for QPair<T1, T2>
allows the converters to access the first
and second
elements of the pair as QVariant, without having to know the actual types of T1 and T2.
When creating extractors, they are typlically generic themselves, as a generic extractor is registered to provide the non generic API. The following code is a basic sample for a QPair converter. Further details about the methods can be found here.
Definition at line 19 of file typeconverter.h.
|
pure virtual |
Returns the identifier of the general type this extractor is for.
The base type should be a simple string representing the generic type this extractor provides. It must be the same for any generic instance created, independent of the generic type, i.e. a QPair<T1, T2>
extractor could for example return QPair
or pair
etc.
When creating a converter that uses this extractor, you can easily check if your converter supports a certain type by checking this base type:
|
pure virtual |
Emplaces the value into the target of the extractors type at the given index.
target | The data to emplace the subdata into |
value | The subvalue to emplace into the target |
index | An optional index, if the value holds more than one subvalue |
Use this method in your custom converters TypeConverter::deserialize to get data into a generic container. If a value is supported by the extractor, you can set the subvalues via this method. For simple containers (such as std::optional), you can ignore the index, as there is only one value. For more complex types, such as QPair<T1, T2>
, use the index to access the elements.
The type of each value expected by a certain index will match the type at the same index from subtypes(). The subtypes list will also tell you, how many elements there are in the generic type.
To, for example, emplace the value of first
of a QPair<T1, T2>
after deserialize it, the following code can be used
|
pure virtual |
Extracts the data of a value of the extractors type at the given index.
value | The data to extract subdata from |
index | An optional index, if the value holds more than one subvalue |
Use this method in your custom converters TypeConverter::serialize to get data out of a generic container. If a value is supported by the extractor, you can get the subvalues via this method. For simple containers (such as std::optional), you can ignore the index, as there is only one value. For more complex types, such as QPair<T1, T2>
, use the index to access the elements.
The type of each value returned by a certain index will match the type at the same index from subtypes(). The subtypes list will also tell you, how many elements there are in the generic type.
To, for example, access the value of second
of a QPair<T1, T2>
and then serialize it, the following code can be used
|
pure virtual |
Returns an ordered list of subtypes found in types handled by this extractor.
This list must contain the type ids of all elements contained in the container, in the same order as they are accessed by extract() and emplace(). For example, for a QPair<T1, T2>
, the type ids would be qMetaTypeId<T1>()
and qMetaTypeId<T2>()
.
You can use these in your custom converter to pass a type id to the TypeConverter::SerializationHelper::serializeSubtype and TypeConverter::SerializationHelper::serializeSubtype methods when handling elements of the contained data.