Skip to content

Commit

Permalink
Add templated method to insert items into SessionItem
Browse files Browse the repository at this point in the history
  • Loading branch information
gpospelov committed Feb 16, 2021
1 parent 3f65f56 commit 18b80bb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
8 changes: 4 additions & 4 deletions source/libmvvm_model/mvvm/model/compounditem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class MVVM_MODEL_EXPORT CompoundItem : public SessionItem {

template <typename T> T* CompoundItem::addProperty(const std::string& name)
{
auto property = std::make_unique<T>();
registerTag(TagInfo::propertyTag(name, property->modelType()));
property->setDisplayName(name);
return static_cast<T*>(insertItem(std::move(property), {name, 0}));
registerTag(TagInfo::propertyTag(name, T().modelType()));
auto result = insertItem<T>({name, 0});
result->setDisplayName(name);
return result;
}

inline PropertyItem* CompoundItem::addProperty(const std::string& name, const char* value)
Expand Down
11 changes: 10 additions & 1 deletion source/libmvvm_model/mvvm/model/sessionitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class MVVM_MODEL_EXPORT SessionItem {

bool insertItem(SessionItem* item, const TagRow& tagrow);

SessionItem *insertItem(std::unique_ptr<SessionItem> p_item, const TagRow& tagrow);
SessionItem* insertItem(std::unique_ptr<SessionItem> p_item, const TagRow& tagrow);
template <typename T = SessionItem> T* insertItem(const TagRow& tagrow);

std::unique_ptr<SessionItem> takeItem(const TagRow& tagrow);

Expand Down Expand Up @@ -175,6 +176,14 @@ template <typename T> std::vector<T*> SessionItem::items(const std::string& tag)
return result;
}

//! Creates a new item and insert it into given tag under the given row.
//! Returns pointer to inserted item to the user.

template <typename T> inline T* SessionItem::insertItem(const TagRow& tagrow)
{
return static_cast<T*>(insertItem(std::make_unique<T>(), tagrow));
}

//! Returns data stored in property item.
//! Property is single item registered under certain tag via CompoundItem::addProperty method.

Expand Down
11 changes: 2 additions & 9 deletions source/libmvvm_model/mvvm/standarditems/data1ditem.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,8 @@ template <typename T, typename... Args> T* Data1DItem::setAxis(Args&&... args)
if (getItem(T_AXIS, 0))
throw std::runtime_error("Axis was already set. Currently we do not support axis change");

T* result{nullptr};
if (model()) {
// acting through the model to enable undo/redo
result = model()->insertItem<T>(this);
}
else {
result = new T;
insertItem(result, {T_AXIS, 0});
}
// acting through the model, if model exists, to enable undo/redo
auto result = model() ? model()->insertItem<T>(this) : insertItem<T>({T_AXIS, 0});
result->setParameters(std::forward<Args>(args)...);
setValues(std::vector<double>(result->size(), 0.0));
return result;
Expand Down
26 changes: 26 additions & 0 deletions tests/testmodel/sessionitem.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "google_test.h"
#include "mvvm/model/itempool.h"
#include "mvvm/model/itemutils.h"
#include "mvvm/model/propertyitem.h"
#include "mvvm/model/sessionitemdata.h"
#include "mvvm/model/sessionitemtags.h"
#include "mvvm/model/taginfo.h"
Expand Down Expand Up @@ -275,6 +276,31 @@ TEST_F(SessionItemTest, insertItem)
EXPECT_EQ(inserted->parent(), parent.get());
}

//! Simple child insert.

TEST_F(SessionItemTest, insertItemTemplated)
{
auto parent = std::make_unique<SessionItem>();
parent->registerTag(TagInfo::universalTag("defaultTag"), /*set_as_default*/ true);

// inserting child
auto inserted = parent->insertItem({"", 0});
EXPECT_EQ(parent->childrenCount(), 1);
EXPECT_EQ(Utils::IndexOfChild(parent.get(), inserted), 0);
EXPECT_EQ(parent->children()[0], inserted);
EXPECT_EQ(parent->getItem("", 0), inserted);
EXPECT_EQ(inserted->parent(), parent.get());

// inserting property item
auto property = parent->insertItem<PropertyItem>({"", 1});
EXPECT_EQ(parent->childrenCount(), 2);
EXPECT_EQ(Utils::IndexOfChild(parent.get(), property), 1);
EXPECT_EQ(parent->children()[1], property);
EXPECT_EQ(parent->getItem("", 1), property);
EXPECT_EQ(property->parent(), parent.get());

}

//! Simple children insert.

TEST_F(SessionItemTest, insertChildren)
Expand Down

0 comments on commit 18b80bb

Please sign in to comment.