~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qinternal_p.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QINTERNAL_P_H
 
30
#define QINTERNAL_P_H
 
31
 
 
32
//
 
33
//  W A R N I N G
 
34
//  -------------
 
35
//
 
36
// This file is not part of the Qt API.  It exists for the convenience
 
37
// of a number of Qt sources files.  This header file may change from
 
38
// version to version without notice, or even be removed.
 
39
//
 
40
// We mean it.
 
41
//
 
42
 
 
43
#include "qnamespace.h"
 
44
#include "qlist.h"
 
45
#include "qiodevice.h"
 
46
#include "qbytearray.h"
 
47
 
 
48
class QWidget;
 
49
class QPainter;
 
50
class QPixmap;
 
51
 
 
52
class QCircularBuffer {
 
53
    QByteArray buf[2];
 
54
    uint curr_used, start_off, start_buff, curr_buff, buff_growth;
 
55
public:
 
56
    inline QCircularBuffer(uint growth) : curr_used(0), start_off(0), start_buff(0),
 
57
                                          curr_buff(0), buff_growth(growth) { }
 
58
 
 
59
    char *alloc(uint buflen);
 
60
    char *take(uint maxsize, uint *realsize=0);
 
61
    inline void free(uint buflen);
 
62
    void push(char c);
 
63
    inline void truncate(uint len) { curr_used -= len; }
 
64
 
 
65
    inline int growth() const { return buff_growth; }
 
66
    inline int numBuffers() const { return 2; }
 
67
    inline bool isEmpty() const { return !used(); }
 
68
    inline uint used() const { return curr_used; }
 
69
    inline void clear() { if(!isEmpty()) free(used()); }
 
70
};
 
71
 
 
72
inline char *QCircularBuffer::alloc(uint size)
 
73
{
 
74
    if(buf[curr_buff].size() <
 
75
       (int)(curr_used+size+(curr_buff == start_buff ? start_off : 0))) {
 
76
        if(curr_buff == start_buff && buf[curr_buff].size()) {
 
77
            buf[curr_buff].resize(start_off + curr_used);
 
78
            curr_buff = !curr_buff;
 
79
            if(!buf[curr_buff].size())
 
80
                buf[curr_buff].resize(buff_growth*2);
 
81
        } else {
 
82
            int sz = buf[curr_buff].size();
 
83
            buf[curr_buff].resize(qMax((uint)sz + (sz / 2), (buff_growth*2)));
 
84
        }
 
85
    }
 
86
    int off = curr_used;
 
87
    curr_used += size;
 
88
    if(curr_buff != start_buff)
 
89
        off -= buf[start_buff].size() - start_off;
 
90
    else
 
91
        off += start_off;
 
92
    return buf[curr_buff].data()+off;
 
93
}
 
94
inline char *QCircularBuffer::take(uint size, uint *real_size)
 
95
{
 
96
    if(size > curr_used) {
 
97
        qWarning("Warning: asked to take too much %d [%d]", size, curr_used);
 
98
        size = curr_used;
 
99
    }
 
100
    if(real_size)
 
101
        *real_size = qMin(size, buf[start_buff].size() - start_off);
 
102
    return buf[start_buff].data()+start_off;
 
103
}
 
104
 
 
105
inline void QCircularBuffer::free(uint size)
 
106
{
 
107
    if(size > curr_used) {
 
108
        qWarning("Warning: asked to free too much %d [%d]", size, curr_used);
 
109
        size = curr_used;
 
110
    }
 
111
    curr_used -= size;
 
112
    if(curr_used == 0) {
 
113
        curr_buff = start_buff = start_off = 0;
 
114
        return;
 
115
    }
 
116
 
 
117
    uint start_size = buf[start_buff].size() - start_off;
 
118
    if(start_size > size) {
 
119
        start_off += size;
 
120
    } else if(start_buff != curr_buff) {
 
121
        start_buff = curr_buff;
 
122
        start_off = start_size - size;
 
123
    } else {
 
124
        start_off = 0;
 
125
    }
 
126
}
 
127
 
 
128
inline void QCircularBuffer::push(char ch)
 
129
{
 
130
    curr_used++;
 
131
    buf[start_buff].insert(start_off, ch);
 
132
}
 
133
 
 
134
class Q_CORE_EXPORT QRingBuffer
 
135
{
 
136
public:
 
137
    QRingBuffer(int growth = 4096);
 
138
 
 
139
    int nextDataBlockSize() const;
 
140
    char *readPointer() const;
 
141
    void free(int bytes);
 
142
    char *reserve(int bytes);
 
143
    void truncate(int bytes);
 
144
 
 
145
    bool isEmpty() const;
 
146
 
 
147
    int getChar();
 
148
    void putChar(char c);
 
149
    void ungetChar(char c);
 
150
 
 
151
    int size() const;
 
152
    void clear();
 
153
    int indexOf(char c) const;
 
154
    int readLine(char *data, int maxLength);
 
155
    bool canReadLine() const;
 
156
 
 
157
private:
 
158
    QList<QByteArray> buffers;
 
159
    int head, tail;
 
160
    int tailBuffer;
 
161
    int basicBlockSize;
 
162
    int bufferSize;
 
163
};
 
164
 
 
165
#endif // QINTERNAL_P_H