I have created a class member invocation function, which does a delayed call, like:
func = NotifyAll(
&get_file_listener,
&GetFileListener::OnGetFileSuccess, container.c_str(), name.c_str());
// ... long delay and different scope
func();
But due to the nature of “execution occurring later” I realize that variables are captured by value will work, but obviously this won’t work for char*
arguments, as in:
class ListenerInterface {
public:
virtual ~ListenerInterface() {}
};
class GetFileListener: public ListenerInterface {
public:
virtual void OnGetFileSuccess(const char* container, const char* name);
virtual ~GetFileListener();
};
I have done this by templating and forwarding the member function and class type, then calling the function when required:
template<typename> struct extract_class_from_member_function_ptr;
template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::*)(ArgTypes...)> {
using type = CLASS;
};
template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::* const)(ArgTypes...)> {
using type = CLASS;
};
template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::*&)(ArgTypes...)> {
using type = CLASS;
};
template <typename RET_T, typename CLASS, class... ArgTypes>
struct extract_class_from_member_function_ptr<RET_T(CLASS::* const&)(ArgTypes...)> {
using type = CLASS;
};
template <typename T, class FuncT, class... ArgTypes>
void NotifyAllNow(T* one_time_specific_listener, FuncT&& Function, ArgTypes&&... Arguments) {
using BaseT = extract_class_from_member_function_ptr<FuncT>::type;
BaseT* casted_listener = (BaseT*)(one_time_specific_listener);
//assert(casted_listener != nullptr);
std::invoke(std::forward<FuncT>(Function), casted_listener, std::forward<ArgTypes>(Arguments)...);
}
template <typename... ArgTypes>
auto NotifyAll(ArgTypes&&... Arguments)
{
return std::bind_front([](auto&&... args) {
NotifyAllNow(std::forward<decltype(args)>(args)...);
}, std::forward<ArgTypes>(Arguments)...);
}
I somehow need to extend the life of some parameters, I think something like an optional LifeExtender
(or something else) could work:
class LifeExtender {
std::optional<std::string> data;
public:
LifeExtender(const char* const str) : data{} {
if (str != nullptr) {
data = std::string(str);
}
}
LifeExtender(char* str) : data{} {
if (str != nullptr) {
data = std::string(str);
}
}
LifeExtender(const std::string& str) : data{str} {}
~LifeExtender() {}
operator const char* const() const noexcept {
if (data) {
return data->c_str();
}
return nullptr;
}
operator const char* () const noexcept {
if (data) {
return data->c_str();
}
return nullptr;
}
};
So that I could just do:
func_fixed = NotifyAll(
get_file_listener,
&GetFileListener::OnGetFileSuccess, LifeExtender(container.c_str()), LifeExtender(name.c_str()), 0, 0);
And have everything work.
But now I’m stuck with trying to cast any LifeExtender
object to char*
‘s (in this case by just calling ()
on them) in the templated invoke
:
std::invoke(std::forward<FuncT>(Function), casted_listener, std::forward<ArgTypes>(Arguments)...);
I have no idea how to “for each forwarded argument” perform an std::conditional_t
or make a separate specialization using std::enable_if
to dereference / call ()
when the argument is of type LifeExtender
, and just pass it along if not.
I think I should be taking inspiration form this post: Using std::forward on casted arguments
But templates are still difficult for me sometimes, I don’t think I know how to apply this.
What would be the correct way to solve this life extension issue in some neat way, so that it would allow me to keep calling member functions later (issue notifications)?
I unfortunately cannot change the parameters of the methods which receive the notifications as they are not controlled by me at all, I have to pass char*
‘s unfortunately.
Changing everything to std::string
would make this a non-issue, but I have to work with the cards I was dealt.
A full example can be found here:
https://coliru.stacked-crooked.com/a/15349c40ef71e904