~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/core/closure.h

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
4
   Clementine is free software: you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation, either version 3 of the License, or
 
7
   (at your option) any later version.
 
8
 
 
9
   Clementine is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#ifndef CLOSURE_H
 
19
#define CLOSURE_H
 
20
 
 
21
#include <tr1/functional>
 
22
 
 
23
#include <QMetaMethod>
 
24
#include <QObject>
 
25
 
 
26
#include <boost/noncopyable.hpp>
 
27
#include <boost/scoped_ptr.hpp>
 
28
 
 
29
#include "core/logging.h"
 
30
 
 
31
class ClosureArgumentWrapper {
 
32
 public:
 
33
  virtual ~ClosureArgumentWrapper() {}
 
34
 
 
35
  virtual QGenericArgument arg() const = 0;
 
36
};
 
37
 
 
38
template<typename T>
 
39
class ClosureArgument : public ClosureArgumentWrapper {
 
40
 public:
 
41
  ClosureArgument(const T& data) : data_(data) {}
 
42
 
 
43
  virtual QGenericArgument arg() const {
 
44
    return Q_ARG(T, data_);
 
45
  }
 
46
 
 
47
 private:
 
48
  T data_;
 
49
};
 
50
 
 
51
class Closure : public QObject, boost::noncopyable {
 
52
  Q_OBJECT
 
53
 
 
54
 public:
 
55
  Closure(QObject* sender, const char* signal,
 
56
          QObject* receiver, const char* slot,
 
57
          const ClosureArgumentWrapper* val0 = 0,
 
58
          const ClosureArgumentWrapper* val1 = 0,
 
59
          const ClosureArgumentWrapper* val2 = 0);
 
60
 
 
61
  Closure(QObject* sender, const char* signal,
 
62
          std::tr1::function<void()> callback);
 
63
 
 
64
 private slots:
 
65
  void Invoked();
 
66
  void Cleanup();
 
67
 
 
68
 private:
 
69
  void Connect(QObject* sender, const char* signal);
 
70
 
 
71
  QMetaMethod slot_;
 
72
  std::tr1::function<void()> callback_;
 
73
 
 
74
  boost::scoped_ptr<const ClosureArgumentWrapper> val0_;
 
75
  boost::scoped_ptr<const ClosureArgumentWrapper> val1_;
 
76
  boost::scoped_ptr<const ClosureArgumentWrapper> val2_;
 
77
};
 
78
 
 
79
#define C_ARG(type, data) new ClosureArgument<type>(data)
 
80
 
 
81
Closure* NewClosure(
 
82
    QObject* sender,
 
83
    const char* signal,
 
84
    QObject* receiver,
 
85
    const char* slot);
 
86
 
 
87
template <typename T>
 
88
Closure* NewClosure(
 
89
    QObject* sender,
 
90
    const char* signal,
 
91
    QObject* receiver,
 
92
    const char* slot,
 
93
    const T& val0) {
 
94
  return new Closure(
 
95
      sender, signal, receiver, slot,
 
96
      C_ARG(T, val0));
 
97
}
 
98
 
 
99
template <typename T0, typename T1>
 
100
Closure* NewClosure(
 
101
    QObject* sender,
 
102
    const char* signal,
 
103
    QObject* receiver,
 
104
    const char* slot,
 
105
    const T0& val0,
 
106
    const T1& val1) {
 
107
  return new Closure(
 
108
      sender, signal, receiver, slot,
 
109
      C_ARG(T0, val0), C_ARG(T1, val1));
 
110
}
 
111
 
 
112
template <typename T0, typename T1, typename T2>
 
113
Closure* NewClosure(
 
114
    QObject* sender,
 
115
    const char* signal,
 
116
    QObject* receiver,
 
117
    const char* slot,
 
118
    const T0& val0,
 
119
    const T1& val1,
 
120
    const T2& val2) {
 
121
  return new Closure(
 
122
      sender, signal, receiver, slot,
 
123
      C_ARG(T0, val0), C_ARG(T1, val1), C_ARG(T2, val2));
 
124
}
 
125
 
 
126
#endif  // CLOSURE_H