From 6e13ee173e23bd6c2027400bd0492a3ad3280821 Mon Sep 17 00:00:00 2001 From: peri4 Date: Sun, 27 Nov 2022 12:40:31 +0300 Subject: [PATCH] PIVariant::fromType() methods --- libs/main/types/pivariant.cpp | 9 +++++++++ libs/main/types/pivariant.h | 18 ++++++++++++++++-- main.cpp | 6 +++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/libs/main/types/pivariant.cpp b/libs/main/types/pivariant.cpp index 59802111..f08bb250 100644 --- a/libs/main/types/pivariant.cpp +++ b/libs/main/types/pivariant.cpp @@ -338,6 +338,14 @@ PIVariant PIVariant::fromValue(const PIByteArray & c, const PIString & type) { } +PIVariant PIVariant::fromType(uint type_id) { + __PIVariantInfo__ * vi = __PIVariantInfoStorage__::get()->map->value(type_id); + if (!vi) return PIVariant(); + piCout << "empty for" << type_id << "=" << vi->empty; + return PIVariant::fromValue(vi->empty, type_id); +} + + PIString PIVariant::typeName(PIVariant::Type type) { switch (type) { case PIVariant::pivBool: return "Bool"; @@ -370,6 +378,7 @@ PIString PIVariant::typeName(PIVariant::Type type) { case PIVariant::pivRect: return "Rect"; case PIVariant::pivMathVector: return "Vector"; case PIVariant::pivMathMatrix: return "Matrix"; + case PIVariant::pivIODevice: return "IODevice"; case PIVariant::pivCustom: return "Custom"; default: break; } diff --git a/libs/main/types/pivariant.h b/libs/main/types/pivariant.h index 294aef12..94e6842d 100644 --- a/libs/main/types/pivariant.h +++ b/libs/main/types/pivariant.h @@ -69,9 +69,14 @@ public: }; struct PIP_EXPORT __PIVariantInfo__ { - __PIVariantInfo__(const PIString & n) {typeName = n; typeID = n.hash();} + __PIVariantInfo__(const PIString & n, const PIByteArray & e) { + typeName = n; + typeID = n.hash(); + empty = e; + } typedef PIByteArray(*castHelperFunc)(PIByteArray); PIMap cast; + PIByteArray empty; PIString typeName; uint typeID = 0; bool simple = false; @@ -119,7 +124,8 @@ public: uint type_id = __PIVariantFunctions__< classname >::typeIDHelper(); \ PIString type_name = __PIVariantFunctions__< classname >::typeNameHelper(); \ if (__PIVariantInfoStorage__::get()->map->contains(type_id)) return; \ - (*(__PIVariantInfoStorage__::get()->map))[type_id] = new __PIVariantInfo__(type_name); \ + PIByteArray empty; empty << classname(); \ + (*(__PIVariantInfoStorage__::get()->map))[type_id] = new __PIVariantInfo__(type_name, empty); \ STATIC_INITIALIZER_END @@ -714,6 +720,14 @@ public: static PIVariant fromValue(const PIByteArray & c, uint type_id); static PIVariant fromValue(const PIByteArray & c, const PIString & type); + //! \~english Returns new %PIVariant from default-constructed type with name "type". + //! \~russian Возвращает новый %PIVariant из пустого конструктора типа "type". + static PIVariant fromType(const PIString & type) {return fromType(typeIDFromName(type));} + + //! \~english Returns new %PIVariant from default-constructed type with type ID "type_id". + //! \~russian Возвращает новый %PIVariant из пустого конструктора типа c ID "type_id". + static PIVariant fromType(uint type_id); + //! \~english Returns type from its name. //! \~russian Возвращает тип из его названия. diff --git a/main.cpp b/main.cpp index dc5e48b6..1d428d93 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,7 @@ public: Editable = 0x02, Label = 0x04, ArrayReorder = 0x1001, - ArrayDynamic = 0x1002, + ArrayResize = 0x1002, }; bool isNull() const {return _is_null;} @@ -175,11 +175,11 @@ int main(int argc, char * argv[]) { //for (auto it: root.children()) { // piCout << it.name() << it.value(); //} - PIVariant v = PIVariant::fromValue(SomeType{1, 0.5f}); + PIVariant v = PIVariant::fromType("PIVariantTypes::IODevice"); piCout << v; piCout << v.toString(); v.setValueFromString("2;-0.1"); - piCout << v; + piCout << v.value(); piCout << v.toString(); return 0;