// excerpts from http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (giantchen at gmail dot com) #ifndef MUDUO_BASE_WEAKCALLBACK_H #define MUDUO_BASE_WEAKCALLBACK_H #include namespace muduo { // A barely usable WeakCallback template class WeakCallback { public: WeakCallback(const std::weak_ptr& object, const std::function& function) : object_(object), function_(function) { } // Default dtor, copy ctor and assignment are okay void operator()(ARGS&&... args) const { std::shared_ptr ptr(object_.lock()); if(ptr) { function_(ptr.get(), std::forward(args)...); } } private: std::weak_ptr object_; std::function function_; }; /* template WeakCallback makeWeakCallback(const std::shared_ptr& object, const std::function& function) { return WeakCallback(object, function); } */ template WeakCallback makeWeakCallback(const std::shared_ptr& object, void (CLASS::*function)(ARGS...) ) { return WeakCallback(object, function); } template WeakCallback makeWeakCallback(const std::shared_ptr& object, void (CLASS::*function)(ARGS...) const) { return WeakCallback(object, function); } } #endif