Skip to content

Commit 9d05fa2

Browse files
lemirepMiKom
authored andcommitted
Object: make it possible to create children that take in refs as ctor parameters
Change-Id: I0718c791f94a7ce43a0e268f1be3ac47df621c99
1 parent 52cd356 commit 9d05fa2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/KDFoundation/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class KDFOUNDATION_API Object : public EventReceiver
6262
}
6363

6464
template<typename T, typename... Ts>
65-
T *createChild(Ts... args)
65+
T *createChild(Ts &&...args)
6666
{
6767
auto child = std::make_unique<T>(std::forward<Ts>(args)...);
6868
return static_cast<T *>(this->addChild(std::move(child)));

tests/auto/foundation/object/tst_object.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ class UserEvent : public Event
9999
}
100100
};
101101

102+
class ObjectThatTakesInRef : public Object
103+
{
104+
public:
105+
ObjectThatTakesInRef(Object &obj)
106+
: ref(obj)
107+
{
108+
}
109+
110+
Object &ref;
111+
};
112+
102113
TEST_CASE("Object construction")
103114
{
104115
SUBCASE("can create an object with no parent")
@@ -131,6 +142,29 @@ TEST_CASE("Object construction")
131142
REQUIRE(std::get<1>(childAddedSpy.args()) == child);
132143
}
133144

145+
SUBCASE("can create an object that takes in a ref with a parent")
146+
{
147+
// GIVEN
148+
auto parent = std::make_unique<Object>();
149+
SignalSpy<Object *, Object *> childAddedSpy(parent->childAdded);
150+
151+
// THEN
152+
REQUIRE(childAddedSpy.count() == 0);
153+
154+
// WHEN
155+
{
156+
auto child = parent->createChild<ObjectThatTakesInRef>(*parent);
157+
158+
// THEN
159+
REQUIRE(child != nullptr);
160+
REQUIRE(child->parent() == parent.get());
161+
REQUIRE(parent->children().size() == 1);
162+
REQUIRE(childAddedSpy.count() == 1);
163+
REQUIRE(std::get<0>(childAddedSpy.args()) == parent.get());
164+
REQUIRE(std::get<1>(childAddedSpy.args()) == child);
165+
}
166+
}
167+
134168
SUBCASE("can create a subclass of Object taking an argument")
135169
{
136170
auto obj = std::make_unique<IntObject>(7);

0 commit comments

Comments
 (0)