WeakCallback.h revision a0e66be7
1a0e66be7SShuo Chen// excerpts from http://code.google.com/p/muduo/
2a0e66be7SShuo Chen//
3a0e66be7SShuo Chen// Use of this source code is governed by a BSD-style license
4a0e66be7SShuo Chen// that can be found in the License file.
5a0e66be7SShuo Chen//
6a0e66be7SShuo Chen// Author: Shuo Chen (giantchen at gmail dot com)
7a0e66be7SShuo Chen
8a0e66be7SShuo Chen#ifndef MUDUO_BASE_WEAKCALLBACK_H
9a0e66be7SShuo Chen#define MUDUO_BASE_WEAKCALLBACK_H
10a0e66be7SShuo Chen
11a0e66be7SShuo Chen#include <memory>
12a0e66be7SShuo Chen
13a0e66be7SShuo Chennamespace muduo
14a0e66be7SShuo Chen{
15a0e66be7SShuo Chen
16a0e66be7SShuo Chen// A barely usable WeakCallback
17a0e66be7SShuo Chentemplate<typename CLASS, typename... ARGS>
18a0e66be7SShuo Chenclass WeakCallback
19a0e66be7SShuo Chen{
20a0e66be7SShuo Chen public:
21a0e66be7SShuo Chen
22a0e66be7SShuo Chen  WeakCallback(const std::weak_ptr<CLASS>&         object,
23a0e66be7SShuo Chen               const std::function<void (CLASS*, ARGS...)>& function)
24a0e66be7SShuo Chen    : object_(object), function_(function)
25a0e66be7SShuo Chen  {
26a0e66be7SShuo Chen  }
27a0e66be7SShuo Chen
28a0e66be7SShuo Chen  // Default dtor, copy ctor and assignment are okay
29a0e66be7SShuo Chen
30a0e66be7SShuo Chen  void operator()(ARGS&&... args) const
31a0e66be7SShuo Chen  {
32a0e66be7SShuo Chen    std::shared_ptr<CLASS> ptr(object_.lock());
33a0e66be7SShuo Chen    if(ptr)
34a0e66be7SShuo Chen    {
35a0e66be7SShuo Chen      function_(ptr.get(), std::forward<ARGS>(args)...);
36a0e66be7SShuo Chen    }
37a0e66be7SShuo Chen  }
38a0e66be7SShuo Chen
39a0e66be7SShuo Chen private:
40a0e66be7SShuo Chen
41a0e66be7SShuo Chen  std::weak_ptr<CLASS> object_;
42a0e66be7SShuo Chen  std::function<void (CLASS*, ARGS...)> function_;
43a0e66be7SShuo Chen};
44a0e66be7SShuo Chen
45a0e66be7SShuo Chen/*
46a0e66be7SShuo Chentemplate<typename CLASS, typename... ARGS>
47a0e66be7SShuo ChenWeakCallback<CLASS> makeWeakCallback(const std::shared_ptr<CLASS>& object,
48a0e66be7SShuo Chen                                     const std::function<void (CLASS*, ARGS...)>& function)
49a0e66be7SShuo Chen{
50a0e66be7SShuo Chen  return WeakCallback<CLASS, ARGS...>(object, function);
51a0e66be7SShuo Chen}
52a0e66be7SShuo Chen*/
53a0e66be7SShuo Chen
54a0e66be7SShuo Chentemplate<typename CLASS, typename... ARGS>
55a0e66be7SShuo ChenWeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
56a0e66be7SShuo Chen                                              void (CLASS::*function)(ARGS...) )
57a0e66be7SShuo Chen{
58a0e66be7SShuo Chen  return WeakCallback<CLASS, ARGS...>(object, function);
59a0e66be7SShuo Chen}
60a0e66be7SShuo Chen
61a0e66be7SShuo Chentemplate<typename CLASS, typename... ARGS>
62a0e66be7SShuo ChenWeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
63a0e66be7SShuo Chen                                              void (CLASS::*function)(ARGS...) const)
64a0e66be7SShuo Chen{
65a0e66be7SShuo Chen  return WeakCallback<CLASS, ARGS...>(object, function);
66a0e66be7SShuo Chen}
67a0e66be7SShuo Chen
68a0e66be7SShuo Chen}
69a0e66be7SShuo Chen
70a0e66be7SShuo Chen#endif
71