Skip to content

Commit

Permalink
Use move_only_function and move implementation to cpp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Jun 10, 2024
1 parent 9ea86ae commit deea0cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
7 changes: 6 additions & 1 deletion app/aspire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ auto main() -> int
window->setWidth(defaultWidth);
window->setHeight(defaultHeight);
window->setTitle("Aspire");
window->setColor(sf::Color::Green);

auto root = std::make_unique<aspire::scene::Node>();

auto rect = std::make_unique<aspire::scene::Rectangle>();
rect->setPosition({80, 80});
Expand All @@ -27,7 +30,9 @@ auto main() -> int
subrect->setColor(sf::Color::Red);
subrect->setRotation(30);
rect->addChild(std::move(subrect));
window->setRootNode(std::move(rect));

root->addChild(std::move(rect));
window->setRootNode(std::move(root));

kernel.addService(std::move(window));

Expand Down
17 changes: 3 additions & 14 deletions include/aspire/core/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,14 @@ namespace aspire::core
/// @return True if this object has been processed. Otherwise, false.
auto isStartup() -> bool;

template <typename T>
auto onChildAdded(T x)
{
return this->signalChildAdded.connect(std::forward<T>(x));
}

template <typename T>
auto onChildRemoved(T x)
{
return this->signalChildRemoved.connect(std::forward<T>(x));
}
auto onChildAdded(std::move_only_function<void(Object*)> x) -> sigslot::connection;
auto onChildRemoved(std::move_only_function<void(Object*)> x) -> sigslot::connection;

protected:
virtual auto onStartup() -> void;
auto kernel() -> Kernel&;

private:
sigslot::signal<Object*> signalChildAdded;
sigslot::signal<Object*> signalChildRemoved;

struct Impl;
Pimpl<Impl> pimpl;
};
Expand Down
18 changes: 16 additions & 2 deletions src/core/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ using aspire::core::Object;
struct Object::Impl
{
std::vector<std::unique_ptr<Object>> children;

sigslot::signal<Object*> childRemoved;
sigslot::signal<Object*> childAdded;

Object* parent{};
std::string name;
bool isStartup{false};
Expand Down Expand Up @@ -41,7 +45,7 @@ auto Object::addChild(std::unique_ptr<Object> x) -> void
x->pimpl->parent = this;
this->pimpl->children.emplace_back(std::move(x));

this->signalChildAdded(temp);
this->pimpl->childAdded(temp);

// Once the object has been started, we need to invoke any new objects that get added dynamically after the fact.
if(this->isStartup() && !temp->isStartup())
Expand All @@ -68,7 +72,7 @@ auto Object::remove() -> std::unique_ptr<Object>
this->pimpl->parent->pimpl->children.erase(beg, end);
node->pimpl->parent = nullptr;

this->signalChildRemoved(node.get());
this->pimpl->childRemoved(node.get());

return node;
}
Expand Down Expand Up @@ -104,6 +108,16 @@ auto Object::isStartup() -> bool
return this->pimpl->isStartup;
}

auto Object::onChildAdded(std::move_only_function<void(Object*)> x) -> sigslot::connection
{
return this->pimpl->childAdded.connect(std::move(x));
}

auto Object::onChildRemoved(std::move_only_function<void(Object*)> x) -> sigslot::connection
{
return this->pimpl->childRemoved.connect(std::move(x));
}

auto Object::onStartup() -> void
{
}

0 comments on commit deea0cd

Please sign in to comment.