From 63e8dadad951b478f77981011a92477993a3b86d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 16 Feb 2022 21:09:08 +0800 Subject: [PATCH] allow passing move-only parameter to Make() (#338) ftxui::Ref<> is used for passing Options, for instance, MenuOption, to the corresponding component which is supposed to hold a strong reference of it. and we can observe the events sent to the component by setting callback(s) in the option instance passed to the owner component. but the callback function is not always copyable, despite that it might be moveable. in this change, * Make<>() is updated to use the perfect forwarding to avoid enforcing its parameters to be copyable. * Ref<> is also updated to take a rvalue reference, so we can move away from the contructor parameter when creating an instance of Ref<>() from it. Signed-off-by: Kefu Chai --- include/ftxui/component/component.hpp | 2 +- include/ftxui/util/ref.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 1582e3b..78d34e8 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -23,7 +23,7 @@ struct MenuEntryOption; template std::shared_ptr Make(Args&&... args) { - return std::make_shared(args...); + return std::make_shared(std::forward(args)...); } namespace Container { diff --git a/include/ftxui/util/ref.hpp b/include/ftxui/util/ref.hpp index ff1133a..e806200 100644 --- a/include/ftxui/util/ref.hpp +++ b/include/ftxui/util/ref.hpp @@ -27,7 +27,8 @@ template class Ref { public: Ref() {} - Ref(T t) : owned_(t) {} + Ref(const T& t) : owned_(t) {} + Ref(T&& t) : owned_(std::forward(t)) {} Ref(T* t) : address_(t) {} T& operator*() { return address_ ? *address_ : owned_; } T& operator()() { return address_ ? *address_ : owned_; }