mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
System : Added iterators and at() to ChildArrayField
This commit is contained in:
parent
e378d5637e
commit
d2b5d7ae92
@ -26,6 +26,8 @@ public:
|
||||
virtual void insertAt(int indexAfter, PdmObjectHandle* obj) = 0;
|
||||
virtual void erase(size_t index) = 0;
|
||||
virtual void deleteAllChildObjects() = 0;
|
||||
|
||||
virtual PdmObjectHandle* at(size_t index) = 0;
|
||||
|
||||
bool hasSameFieldCountForAllObjects();
|
||||
};
|
||||
@ -62,8 +64,9 @@ public:
|
||||
virtual size_t size() const { return m_pointers.size(); }
|
||||
virtual bool empty() const { return m_pointers.empty(); }
|
||||
virtual void clear();
|
||||
virtual void deleteAllChildObjects();
|
||||
virtual void deleteAllChildObjects();
|
||||
virtual void insertAt(int indexAfter, PdmObjectHandle* obj);
|
||||
virtual PdmObjectHandle* at(size_t index);
|
||||
|
||||
// std::vector-like access
|
||||
|
||||
@ -78,6 +81,12 @@ public:
|
||||
void erase(size_t index);
|
||||
size_t index(DataType* pointer);
|
||||
|
||||
typename std::vector< PdmPointer<DataType> >::iterator begin() { return m_pointers.begin(); };
|
||||
typename std::vector< PdmPointer<DataType> >::iterator end() { return m_pointers.end(); };
|
||||
|
||||
typename std::vector< PdmPointer<DataType> >::const_iterator begin() const { return m_pointers.begin(); };
|
||||
typename std::vector< PdmPointer<DataType> >::const_iterator end() const { return m_pointers.end(); };
|
||||
|
||||
// Child objects
|
||||
|
||||
virtual void childObjects(std::vector<PdmObjectHandle*>* objects);
|
||||
|
@ -228,6 +228,15 @@ void PdmChildArrayField<DataType*>::insertAt(int indexAfter, PdmObjectHandle* ob
|
||||
obj->setAsParentField(this);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
template<typename DataType>
|
||||
PdmObjectHandle* PdmChildArrayField<DataType*>::at(size_t index)
|
||||
{
|
||||
return m_pointers[index].rawPtr();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -18,6 +18,7 @@ set( PROJECT_FILES
|
||||
|
||||
cafPdmCoreBasicTest.cpp
|
||||
cafPdmReferenceHelperTest.cpp
|
||||
cafPdmChildArrayFieldHandleTest.cpp
|
||||
|
||||
Child.cpp
|
||||
Child.h
|
||||
|
@ -0,0 +1,130 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "cafAppEnum.h"
|
||||
|
||||
#include "cafPdmObjectHandle.h"
|
||||
#include "cafPdmProxyValueField.h"
|
||||
#include "cafPdmPtrField.h"
|
||||
#include "cafPdmReferenceHelper.h"
|
||||
#include "cafPdmDataValueField.h"
|
||||
#include "cafPdmChildArrayField.h"
|
||||
#include "cafPdmChildField.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class MsjSimpleObj : public caf::PdmObjectHandle
|
||||
{
|
||||
public:
|
||||
MsjSimpleObj() : PdmObjectHandle()
|
||||
{
|
||||
this->addField(&name, "Name");
|
||||
this->addField(&id, "ID");
|
||||
|
||||
static int a = 0;
|
||||
|
||||
id = a++;
|
||||
name = QString("Name %1").arg(id);
|
||||
}
|
||||
|
||||
caf::PdmDataValueField<QString> name;
|
||||
caf::PdmDataValueField<int> id;
|
||||
};
|
||||
|
||||
class SimpleObjDerived : public MsjSimpleObj
|
||||
{
|
||||
public:
|
||||
SimpleObjDerived() : MsjSimpleObj()
|
||||
{
|
||||
this->addField(&valueA, "valueA");
|
||||
}
|
||||
|
||||
caf::PdmDataValueField<int> valueA;
|
||||
};
|
||||
|
||||
class SimpleObjDerivedOther : public MsjSimpleObj
|
||||
{
|
||||
public:
|
||||
SimpleObjDerivedOther() : MsjSimpleObj()
|
||||
{
|
||||
this->addField(&valueDouble, "valueDouble");
|
||||
}
|
||||
|
||||
caf::PdmDataValueField<double> valueDouble;
|
||||
};
|
||||
|
||||
class ContainerObj : public caf::PdmObjectHandle
|
||||
{
|
||||
public:
|
||||
ContainerObj() : PdmObjectHandle()
|
||||
{
|
||||
this->addField(&derivedObjs, "derivedObjs");
|
||||
}
|
||||
|
||||
~ContainerObj()
|
||||
{
|
||||
derivedObjs.deleteAllChildObjects();
|
||||
}
|
||||
|
||||
caf::PdmChildArrayField<SimpleObjDerived*> derivedObjs;
|
||||
caf::PdmChildArrayField<SimpleObjDerivedOther*> derivedOtherObjs;
|
||||
};
|
||||
|
||||
template <class U, typename T>
|
||||
U findObjectById(T start, T end, int id)
|
||||
{
|
||||
for (T it = start; it != end; it++)
|
||||
{
|
||||
if (id == it->p()->id())
|
||||
{
|
||||
return it->p();
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TEST(ChildArrayFieldHandle, DerivedObjects)
|
||||
{
|
||||
ContainerObj* containerObj = new ContainerObj;
|
||||
|
||||
SimpleObjDerived* s0 = new SimpleObjDerived;
|
||||
SimpleObjDerived* s1 = new SimpleObjDerived;
|
||||
SimpleObjDerived* s2 = new SimpleObjDerived;
|
||||
containerObj->derivedObjs.push_back(s0);
|
||||
containerObj->derivedObjs.push_back(s1);
|
||||
containerObj->derivedObjs.push_back(s2);
|
||||
|
||||
SimpleObjDerived* myObj = NULL;
|
||||
myObj = findObjectById<SimpleObjDerived*>(containerObj->derivedObjs.begin(), containerObj->derivedObjs.end(), 2);
|
||||
EXPECT_EQ(s2, myObj);
|
||||
|
||||
myObj = findObjectById<SimpleObjDerived*>(containerObj->derivedObjs.begin(), containerObj->derivedObjs.end(), -1);
|
||||
EXPECT_EQ(NULL, myObj);
|
||||
|
||||
delete containerObj;
|
||||
}
|
||||
|
||||
TEST(ChildArrayFieldHandle, DerivedOtherObjects)
|
||||
{
|
||||
ContainerObj* containerObj = new ContainerObj;
|
||||
|
||||
SimpleObjDerivedOther* s0 = new SimpleObjDerivedOther;
|
||||
SimpleObjDerivedOther* s1 = new SimpleObjDerivedOther;
|
||||
SimpleObjDerivedOther* s2 = new SimpleObjDerivedOther;
|
||||
|
||||
int s2Id = s2->id;
|
||||
|
||||
containerObj->derivedOtherObjs.push_back(s0);
|
||||
containerObj->derivedOtherObjs.push_back(s1);
|
||||
containerObj->derivedOtherObjs.push_back(s2);
|
||||
|
||||
SimpleObjDerivedOther* myObj = NULL;
|
||||
myObj = findObjectById<SimpleObjDerivedOther*>(containerObj->derivedOtherObjs.begin(), containerObj->derivedOtherObjs.end(), s2Id);
|
||||
EXPECT_EQ(s2, myObj);
|
||||
|
||||
myObj = findObjectById<SimpleObjDerivedOther*>(containerObj->derivedOtherObjs.begin(), containerObj->derivedOtherObjs.end(), -1);
|
||||
EXPECT_EQ(NULL, myObj);
|
||||
|
||||
delete containerObj;
|
||||
}
|
Loading…
Reference in New Issue
Block a user