Add variable arg overload of addComponent()

master
rubenwardy 2019-08-08 15:42:03 +01:00
parent 62dc516cf4
commit 188b49e962
2 changed files with 11 additions and 2 deletions

View File

@ -24,11 +24,20 @@ public:
updateGlobalPosition();
}
template<class T>
template<typename T>
void addComponent(T *comp) {
_components[T::className] = std::unique_ptr<Component>(comp);
}
template<typename T, typename... _Args>
inline T *addComponent(_Args&&... __args) {
auto ptr = std::make_unique<T>(std::forward<_Args>(__args)...);
T *rptr = ptr.get();
_components[T::className] = std::move(ptr);
return rptr;
}
template<class T>
void recurseComponents(const std::function<bool(T*)> &cb) {
auto &name = T::className;

View File

@ -19,7 +19,7 @@ int main() {
auto root = std::make_shared<Node>();
{
auto square = std::make_shared<Node>();
square->addComponent<MeshDrawComponent>(new MeshDrawComponent(square, shaderProgram));
square->addComponent<MeshDrawComponent>(square, shaderProgram);
root->addChild(square);
}