• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// To generate callback.h from callback.h.pump, execute:
12// ../third_party/googletest/src/googletest/scripts/pump.py callback.h.pump
13
14// Callbacks are callable object containers. They can hold a function pointer
15// or a function object and behave like a value type. Internally, data is
16// reference-counted, making copies and pass-by-value inexpensive.
17//
18// Callbacks are typed using template arguments.  The format is:
19//   CallbackN<ReturnType, ParamType1, ..., ParamTypeN>
20// where N is the number of arguments supplied to the callable object.
21// Callbacks are invoked using operator(), just like a function or a function
22// object. Default-constructed callbacks are "empty," and executing an empty
23// callback does nothing. A callback can be made empty by assigning it from
24// a default-constructed callback.
25//
26// Callbacks are similar in purpose to std::function (which isn't available on
27// all platforms we support) and a lightweight alternative to sigslots. Since
28// they effectively hide the type of the object they call, they're useful in
29// breaking dependencies between objects that need to interact with one another.
30// Notably, they can hold the results of Bind(), std::bind*, etc, without needing
31// to know the resulting object type of those calls.
32//
33// Sigslots, on the other hand, provide a fuller feature set, such as multiple
34// subscriptions to a signal, optional thread-safety, and lifetime tracking of
35// slots. When these features are needed, choose sigslots.
36//
37// Example:
38//   int sqr(int x) { return x * x; }
39//   struct AddK {
40//     int k;
41//     int operator()(int x) const { return x + k; }
42//   } add_k = {5};
43//
44//   Callback1<int, int> my_callback;
45//   cout << my_callback.empty() << endl;  // true
46//
47//   my_callback = Callback1<int, int>(&sqr);
48//   cout << my_callback.empty() << endl;  // false
49//   cout << my_callback(3) << endl;  // 9
50//
51//   my_callback = Callback1<int, int>(add_k);
52//   cout << my_callback(10) << endl;  // 15
53//
54//   my_callback = Callback1<int, int>();
55//   cout << my_callback.empty() << endl;  // true
56
57#ifndef RTC_BASE_CALLBACK_H_
58#define RTC_BASE_CALLBACK_H_
59
60#include "rtc_base/ref_count.h"
61#include "rtc_base/ref_counted_object.h"
62#include "api/scoped_refptr.h"
63
64namespace rtc {
65
66$var n = 5
67$range i 0..n
68$for i [[
69$range j 1..i
70
71template <class R$for j [[,
72          class P$j]]>
73class Callback$i {
74 public:
75  // Default copy operations are appropriate for this class.
76  Callback$i() {}
77  template <class T> Callback$i(const T& functor)
78      : helper_(new RefCountedObject< HelperImpl<T> >(functor)) {}
79  R operator()($for j , [[P$j p$j]]) {
80    if (empty())
81      return R();
82    return helper_->Run($for j , [[p$j]]);
83  }
84  bool empty() const { return !helper_; }
85
86 private:
87  struct Helper : RefCountInterface {
88    virtual ~Helper() {}
89    virtual R Run($for j , [[P$j p$j]]) = 0;
90  };
91  template <class T> struct HelperImpl : Helper {
92    explicit HelperImpl(const T& functor) : functor_(functor) {}
93    virtual R Run($for j , [[P$j p$j]]) {
94      return functor_($for j , [[p$j]]);
95    }
96    T functor_;
97  };
98  scoped_refptr<Helper> helper_;
99};
100
101]]
102}  // namespace rtc
103
104#endif  // RTC_BASE_CALLBACK_H_
105