File: SignalProxy.h

package info (click to toggle)
kwave 0.7.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,048 kB
  • ctags: 4,906
  • sloc: cpp: 31,275; ansic: 13,111; sh: 9,511; perl: 2,724; makefile: 786; asm: 145
file content (163 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***************************************************************************
		  SignalProxy.h  - threadsafe proxy for signals/slots
			     -------------------
    begin                : Mon Sep 11 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _SIGNAL_PROXY_H_
#define _SIGNAL_PROXY_H_

#include "config.h"
#include <qptrqueue.h>
#include "mt/AsyncSync.h"
#include "mt/Mutex.h"
#include "mt/MutexGuard.h"

//***************************************************************************
template <class T>
class SignalProxy: public AsyncSync
{
public:
    SignalProxy(QObject *owner, const char *slot);
};

//***************************************************************************
template <class T>
SignalProxy<T>::SignalProxy(QObject *owner, const char *slot)
    :AsyncSync()
{
    QObject::connect(this, SIGNAL(Activated()), owner, slot);
}

//***************************************************************************
//***************************************************************************
/**
 * \class SignalProxy1
 * Provides a way of emitting signals that contain some data from inside
 * a thread out to the GUI thread (X11). Each event will be put into a
 * queue from within a thread and read out from the X11 side.
 * If a limit is specified, the content will be limited to the specified
 * number of entries, the oldest entry will be removed and deleted.
 *
 * This class is completely threadsafe !
 */
template <class T>
class SignalProxy1: public AsyncSync
{
public:
    SignalProxy1(QObject *owner, const char *slot, unsigned int limit=0);
    virtual ~SignalProxy1();
    virtual void enqueue(const T &param);
    virtual T *dequeue();
    virtual unsigned int count();
    virtual void setLimit(unsigned int limit);
    virtual unsigned int limit();
private:
    QPtrQueue<T> m_queue;
    Mutex m_lock;
    unsigned int m_limit;
};

//***************************************************************************
template <class T>
SignalProxy1<T>::SignalProxy1(QObject *owner,
    const char *slot, unsigned int limit)
    :AsyncSync(), m_lock("SignalProxy1")
{
    m_limit = limit;
    m_queue.setAutoDelete(false);
    QObject::connect(this, SIGNAL(Activated()), owner, slot);
}

//***************************************************************************
template <class T>
SignalProxy1<T>::~SignalProxy1()
{
    MutexGuard lock(m_lock);
    m_queue.setAutoDelete(true);
    m_queue.clear();
}

//***************************************************************************
template <class T>
void SignalProxy1<T>::enqueue(const T &param)
{
    MutexGuard lock(m_lock);
    bool call_async = true;

    // dequeue the first (oldest) object of the queue if the limit
    // has been reached.
    if ((m_limit) && (m_queue.count() >= m_limit)) {
	T *p1 = m_queue.dequeue();
	Q_ASSERT(p1);
	if (p1) delete p1;

	// if the queue already was full, don't call the AsyncHandler()
	// once more, the X11 synchronization pipe is already filled
	// with the correct number of events
	call_async = false;
    }

    // enqueue a copy of the object
    T *copy = new T(param);
    Q_ASSERT(copy);
    m_queue.enqueue(copy);

    if (call_async) AsyncHandler();
}

//***************************************************************************
template <class T>
T *SignalProxy1<T>::dequeue()
{
    MutexGuard lock(m_lock);
    T *p1 = m_queue.dequeue();
    Q_ASSERT(p1);
    T *copy = 0;
    if (p1) {
	copy = new T(*p1);
	Q_CHECK_PTR(copy);
	delete p1;
    }
    return copy;
}

//***************************************************************************
template <class T>
unsigned int SignalProxy1<T>::count()
{
    MutexGuard lock(m_lock);
    return m_queue.count();
}

//***************************************************************************
template <class T>
void SignalProxy1<T>::setLimit(unsigned int limit)
{
    MutexGuard lock(m_lock);
    m_limit = limit;
}

//***************************************************************************
template <class T>
unsigned int SignalProxy1<T>::limit()
{
    MutexGuard lock(m_lock);
    return m_limit;
}

#endif //  _SIGNAL_PROXY_H_

//***************************************************************************
//***************************************************************************