File: datapacket.h

package info (click to toggle)
arts 1.5.9-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,436 kB
  • ctags: 9,848
  • sloc: ansic: 44,670; cpp: 33,776; sh: 10,486; perl: 3,470; makefile: 372; yacc: 347; lex: 160
file content (230 lines) | stat: -rw-r--r-- 5,442 bytes parent folder | download | duplicates (4)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#ifndef DATAPACKET_H
#define DATAPACKET_H

#include "arts_export.h"
#include "buffer.h"

/*
 * BC - Status (2002-03-08): GenericDataChannel, DataPacket types
 *
 * These classes must be kept binary compatible, as the do interact with
 * generated code. So you MUST KNOW WHAT YOU ARE DOING, once you start
 * using the provided d pointers for extensions.
 */

namespace Arts {
class GenericDataChannelPrivate;
class GenericDataPacket;
/*
 * The GenericDataChannel interface is to be implemented by the flowsystem
 */
class ARTS_EXPORT GenericDataChannel {
private:
	GenericDataChannelPrivate *d;	// unused

protected:
	friend class GenericDataPacket;

	/*
	 * this is used internally by DataPacket
	 */
	virtual void processedPacket(GenericDataPacket *packet) = 0;

	/*
	 * used internally by DataPacket
	 */
	virtual void sendPacket(GenericDataPacket *packet) = 0;

public:
	/*
	 * used to set pull delivery mode
	 */
	virtual void setPull(int packets, int capacity) = 0;
	virtual void endPull() = 0;

	GenericDataChannel() : d(0)
	{
	}
};

/*
 * DataPackets are the heard of asynchronous streaming (MCOP has synchronous
 * and asynchronous streams). They are used
 *
 *  - in the interface async streams expose to C++ implementations of MCOP
 *    interfaces (they directly deal with datapackets)
 *
 *  - from the FlowSystem implemenentations
 */

/**
 * The GenericDataPacket class provides the interface the flow system can
 * use to deal with data packets.
 */
class GenericDataPacketPrivate;

class ARTS_EXPORT GenericDataPacket {
private:
	GenericDataPacketPrivate *d;
	static long _staticDataPacketCount;

public:
	/**
	 * the amount of active data packets (memory leak debugging only)
	 */
	static long _dataPacketCount() { return _staticDataPacketCount; }

	/**
	 * the channel this datapacket belongs to
	 */
	GenericDataChannel *channel;

	/**
	 * ensureCapactity ensures that there is room for at least capacity
	 * Elements in the packet. This is implemented destructive - that
	 * means: you may not find your old contents in the packet after
	 * calling ensureCapacity
	 */
	virtual void ensureCapacity(int capacity) = 0;

	/**
	 * read/write write the contents of the packet. Read will also
	 * automatically ensure that capacity is adapted before reading.
	 */
	virtual void read(Buffer& stream) = 0;
	virtual void write(Buffer& stream) = 0;

	/** 
	 * having size here (and not in the derived concrete DataPackets) is so
	 * that we can see whether the sender can't supply more data (and starts
	 * sending zero size packets
	 */
	int size;

	/**
	 * useCount is to be set from sendPacket
	 */
	int useCount;

	inline void send()
	{
		channel->sendPacket(this);
	}
	inline void processed()
	{
		useCount--;
		if(useCount == 0)
		{
			if(channel)
				channel->processedPacket(this);
			else
				delete this;
		}
	}

	virtual ~GenericDataPacket()
	{
		_staticDataPacketCount--;
	}

protected:
	GenericDataPacket(GenericDataChannel *channel)
		:d(0),channel(channel),useCount(0)
	{
		_staticDataPacketCount++;
	}
};

/**
 * The DataPacket<T> interface is what C++ implementations of MCOP interfaces
 * will need to use.
 */
template<class T>
class DataPacket : public GenericDataPacket {
public:
	T *contents;

protected:	
	DataPacket(GenericDataChannel *channel)
	    : GenericDataPacket(channel) {}
	~DataPacket() {}
};

/**
 * The RawDataPacket<T> interface handles raw class T arrays of data
 */
template<class T>
class RawDataPacket : public DataPacket<T> {
protected:
	int capacity;
	void ensureCapacity(int newCapacity)
	{
		if(newCapacity > capacity)
		{
			delete[] this->contents;
			capacity = newCapacity;
			this->contents = new T[capacity];
		}
	}
	RawDataPacket(int capacity, GenericDataChannel *channel)
		:DataPacket<T>(channel), capacity(capacity)
	{
		this->size = capacity;
		this->contents = new T[capacity];
	}
	~RawDataPacket()
	{
		delete[] this->contents;
	}
};

/**
 * FloatDataPacket finally is one concrete DataPacket (which contains the
 * information how to marshal a datapacket of type float)
 */
class ARTS_EXPORT FloatDataPacket : public RawDataPacket<float> {
public:
	FloatDataPacket(int capacity, GenericDataChannel *channel)
			: RawDataPacket<float>(capacity, channel)
	{
		//
	}
	void read(Buffer& stream);
	void write(Buffer& stream);
};

class ARTS_EXPORT ByteDataPacket : public RawDataPacket<mcopbyte> {
public:
	ByteDataPacket(int capacity, GenericDataChannel *channel)
			: RawDataPacket<mcopbyte>(capacity, channel)
	{
		//
	}
	void read(Buffer& stream);
	void write(Buffer& stream);
};

}
#endif