From bc56c807c19b7e5af81bfd2598666d0ffd95ba97 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Tue, 31 Mar 2020 14:00:44 +0200 Subject: [PATCH] #5749 Python : Add support for std::vector as return value of a function --- .../cafPdmScriptingBasicTest.cpp | 22 ++++++++- .../cafInternalPdmXmlFieldCapability.h | 22 +++++++++ .../cafInternalPdmXmlFieldCapability.inl | 48 +++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/Fwk/AppFwk/cafPdmScripting/cafPdmScripting_UnitTests/cafPdmScriptingBasicTest.cpp b/Fwk/AppFwk/cafPdmScripting/cafPdmScripting_UnitTests/cafPdmScriptingBasicTest.cpp index 918a629e6d..0610abb3fb 100644 --- a/Fwk/AppFwk/cafPdmScripting/cafPdmScripting_UnitTests/cafPdmScriptingBasicTest.cpp +++ b/Fwk/AppFwk/cafPdmScripting/cafPdmScripting_UnitTests/cafPdmScriptingBasicTest.cpp @@ -146,7 +146,7 @@ public: "Enter some small number here", "This is a place you can enter a small integer value if you want" ); - CAF_PDM_InitField( &m_textField, "TextField", QString( "ÆØÅ Test text end" ), "TextField", "", "Tooltip", "WhatsThis" ); + CAF_PDM_InitField( &m_textField, "TextField", QString( "��� Test text end" ), "TextField", "", "Tooltip", "WhatsThis" ); CAF_PDM_InitFieldNoDefault( &m_simpleObjPtrField, "SimpleObjPtrField", "SimpleObjPtrField", "", "Tooltip", "WhatsThis" ); CAF_PDM_InitFieldNoDefault( &m_simpleObjPtrField2, "SimpleObjPtrField2", "SimpleObjPtrField2", "", "Tooltip", "WhatsThis" ); m_simpleObjPtrField2 = new SimpleObj; @@ -191,6 +191,7 @@ public: "Script comment test" ); CAF_PDM_InitScriptableFieldWithIONoDefault( &m_texts, "Texts", "Some words", "", "", "" ); + CAF_PDM_InitScriptableFieldWithIONoDefault( &m_numbers, "Numbers", "Some words", "", "", "" ); CAF_PDM_InitFieldNoDefault( &m_testEnumField, "TestEnumValue", "An Enum", "", "", "" ); CAF_PDM_InitFieldNoDefault( &m_simpleObjectsField, "SimpleObjects", @@ -202,7 +203,9 @@ public: ~InheritedDemoObj() { m_simpleObjectsField.deleteAllChildObjects(); } - caf::PdmField> m_texts; + caf::PdmField> m_texts; + caf::PdmField> m_numbers; + caf::PdmField> m_testEnumField; caf::PdmChildArrayField m_simpleObjectsField; }; @@ -248,4 +251,19 @@ TEST( PdmScriptingTest, BasicUse ) std::unique_ptr generator( caf::PdmCodeGeneratorFactory::instance()->create( fileExt ) ); auto generatedText = generator->generate( caf::PdmDefaultObjectFactory::instance() ); + + auto string = generatedText.toStdString(); +} + +//-------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------- +TEST( PdmScriptingTest, CheckIsVector ) +{ + InheritedDemoObj obj; + + auto isVector = obj.m_numbers.xmlCapability()->isVectorField(); + EXPECT_TRUE( isVector ); + + // auto xmlCap = obj.xmlCapability(); + // auto string = xmlCap->writeObjectToXmlString(); } diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.h index 8973dfcdbf..ba73353dfd 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.h @@ -133,6 +133,28 @@ private: FieldType* m_field; }; +template < typename DataType> +class PdmFieldXmlCap< PdmField> > : public PdmXmlFieldHandle +{ + typedef PdmField> FieldType; +public: + PdmFieldXmlCap(FieldType* field, bool giveOwnership) : PdmXmlFieldHandle(field, giveOwnership) + { + m_field = field; + + m_dataTypeName = QString("%1").arg(typeid(DataType).name()); + } + + // Xml Serializing +public: + void readFieldData(QXmlStreamReader& xmlStream, PdmObjectFactory* objectFactory) override; + void writeFieldData(QXmlStreamWriter& xmlStream) const override; + bool resolveReferences() override; + bool isVectorField() const; +private: + FieldType* m_field; +}; + diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.inl b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.inl index a00b7da813..673258a43b 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.inl +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmXmlFieldCapability.inl @@ -449,4 +449,52 @@ bool caf::PdmFieldXmlCap< caf::PdmChildArrayField >::isVectorField() return true; } +//================================================================================================== +/// XML Implementation for PdmFieldXmlCap> methods +/// +//================================================================================================== + + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template < typename DataType> +bool caf::PdmFieldXmlCap>>::isVectorField() const +{ + return true; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template + void caf::PdmFieldXmlCap>>::readFieldData(QXmlStreamReader& xmlStream, + PdmObjectFactory* objectFactory) + { + this->assertValid(); + typename FieldType::FieldDataType value; + PdmFieldReader::readFieldData(value, xmlStream, objectFactory); + m_field->setValue(value); + } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template +void caf::PdmFieldXmlCap>>::writeFieldData(QXmlStreamWriter& xmlStream) const + { + this->assertValid(); + PdmFieldWriter::writeFieldData(m_field->value(), xmlStream); + } + + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +template < typename DataType> +bool caf::PdmFieldXmlCap>>::resolveReferences() +{ + return true; +} + } // End namespace caf