Project

General

Profile

Statistics
| Branch: | Revision:

root / include / packing.h @ e1750c09

History | View | Annotate | Download (7.16 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  PACKING.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//==========================================================================
7
8
/*--------------------------------------------------------------*
9
  Copyright (C) 1992-2008 Andras Varga
10
  Copyright (C) 2006-2008 OpenSim Ltd.
11

12
  This file is distributed WITHOUT ANY WARRANTY. See the file
13
  `license' for details on this and other legal matters.
14
*--------------------------------------------------------------*/
15
16
#ifndef __PACKING_H
17
#define __PACKING_H
18
19
#include <vector>
20
#include <list>
21
#include <set>
22
#include <map>
23
#include "ccommbuffer.h"
24
25
NAMESPACE_BEGIN
26
27
//
28
// This file contains methods for generated parsimPack/parsimUnpack methods
29
//
30
31
//
32
// pack/unpack functions for primitive types
33
//
34
// Note: pack doesn't use "const", as the template rule somehow doesn't fire
35
// then (VC++ 7.1)
36
//
37
#define DOPACKING(T,R) \
38
          inline void doPacking(cCommBuffer *b, /*const*/ T R a) {b->pack(a);}  \
39
          inline void doPacking(cCommBuffer *b, /*const*/ T *a, int n) {b->pack(a,n);}  \
40
          inline void doUnpacking(cCommBuffer *b, T& a) {b->unpack(a);}  \
41
          inline void doUnpacking(cCommBuffer *b, T *a, int n) {b->unpack(a,n);}
42
#define _
43
DOPACKING(char,_)
44
DOPACKING(unsigned char,_)
45
DOPACKING(bool,_)
46
DOPACKING(short,_)
47
DOPACKING(unsigned short,_)
48
DOPACKING(int,_)
49
DOPACKING(unsigned int,_)
50
DOPACKING(long,_)
51
DOPACKING(unsigned long,_)
52
DOPACKING(float,_)
53
DOPACKING(double,_)
54
DOPACKING(long double,_)
55
DOPACKING(char *,_)
56
DOPACKING(SimTime,_)
57
DOPACKING(opp_string,&)
58
#undef _
59
#undef DOPACKING
60
61
//
62
// Default pack/unpack function for arrays
63
//
64
template<typename T>
65
void doPacking(cCommBuffer *b, /*const*/ T *t, int n)
66
{
67
    for (int i=0; i<n; i++)
68
        doPacking(b, t[i]);
69
}
70
71
template<typename T>
72
void doUnpacking(cCommBuffer *b, T *t, int n)
73
{
74
    for (int i=0; i<n; i++)
75
        doUnpacking(b, t[i]);
76
}
77
78
79
//
80
// Packing/unpacking an std::vector
81
//
82
template<typename T>
83
void doPacking(cCommBuffer *buffer, /*const*/ std::vector<T>& v)
84
{
85
    int n = v.size();
86
    doPacking(buffer, n);
87
    for (int i=0; i<n; i++)
88
        doPacking(buffer, v[i]);
89
}
90
91
template<typename T>
92
void doUnpacking(cCommBuffer *buffer, std::vector<T>& v)
93
{
94
    int n;
95
    doUnpacking(buffer, n);
96
    v.resize(n);
97
    for (int i=0; i<n; i++)
98
        doUnpacking(buffer, v[i]);
99
}
100
101
template<typename T, typename A>
102
void doPacking(cCommBuffer *buffer, /*const*/ std::vector<T,A>& v)
103
{
104
    int n = v.size();
105
    doPacking(buffer, n);
106
    for (int i=0; i<n; i++)
107
        doPacking(buffer, v[i]);
108
}
109
110
template<typename T, typename A>
111
void doUnpacking(cCommBuffer *buffer, std::vector<T,A>& v)
112
{
113
    int n;
114
    doUnpacking(buffer, n);
115
    v.resize(n);
116
    for (int i=0; i<n; i++)
117
        doUnpacking(buffer, v[i]);
118
}
119
120
121
//
122
// Packing/unpacking an std::list
123
//
124
template<typename T>
125
void doPacking(cCommBuffer *buffer, /*const*/ std::list<T>& l)
126
{
127
    doPacking(buffer, (int)l.size());
128
    for (typename std::list<T>::const_iterator it = l.begin(); it != l.end(); it++)
129
        doPacking(buffer, (T&)*it);
130
}
131
132
template<typename T>
133
void doUnpacking(cCommBuffer *buffer, std::list<T>& l)
134
{
135
    int n;
136
    doUnpacking(buffer, n);
137
    for (int i=0; i<n; i++) {
138
        l.push_back(T());
139
        doUnpacking(buffer, l.back());
140
    }
141
}
142
143
template<typename T, typename A>
144
void doPacking(cCommBuffer *buffer, /*const*/ std::list<T,A>& l)
145
{
146
    doPacking(buffer, (int)l.size());
147
    for (typename std::list<T,A>::const_iterator it = l.begin(); it != l.end(); it++)
148
        doPacking(buffer, (T&)*it);
149
}
150
151
template<typename T, typename A>
152
void doUnpacking(cCommBuffer *buffer, std::list<T,A>& l)
153
{
154
    int n;
155
    doUnpacking(buffer, n);
156
    for (int i=0; i<n; i++) {
157
        l.push_back(T());
158
        doUnpacking(buffer, l.back());
159
    }
160
}
161
162
//
163
// Packing/unpacking an std::set
164
//
165
template<typename T>
166
void doPacking(cCommBuffer *buffer, /*const*/ std::set<T>& s)
167
{
168
    doPacking(buffer, (int)s.size());
169
    for (typename std::set<T>::const_iterator it = s.begin(); it != s.end(); it++)
170
        doPacking(buffer, (T&)*it);
171
}
172
173
template<typename T>
174
void doUnpacking(cCommBuffer *buffer, std::set<T>& s)
175
{
176
    int n;
177
    doUnpacking(buffer, n);
178
    for (int i=0; i<n; i++) {
179
        T x;
180
        doUnpacking(buffer, x);
181
        s.insert(x);
182
    }
183
}
184
185
template<typename T, typename Tr>
186
void doPacking(cCommBuffer *buffer, /*const*/ std::set<T,Tr>& s)
187
{
188
    doPacking(buffer, (int)s.size());
189
    for (typename std::set<T,Tr>::const_iterator it = s.begin(); it != s.end(); it++)
190
        doPacking(buffer, (T&)*it);
191
}
192
193
template<typename T, typename Tr>
194
void doUnpacking(cCommBuffer *buffer, std::set<T,Tr>& s)
195
{
196
    int n;
197
    doUnpacking(buffer, n);
198
    for (int i=0; i<n; i++) {
199
        T x;
200
        doUnpacking(buffer, x);
201
        s.insert(x);
202
    }
203
}
204
205
template<typename T, typename Tr, typename A>
206
void doPacking(cCommBuffer *buffer, /*const*/ std::set<T,Tr,A>& s)
207
{
208
    doPacking(buffer, (int)s.size());
209
    for (typename std::set<T,Tr,A>::const_iterator it = s.begin(); it != s.end(); it++)
210
        doPacking(buffer, *it);
211
}
212
213
template<typename T, typename Tr, typename A>
214
void doUnpacking(cCommBuffer *buffer, std::set<T,Tr,A>& s)
215
{
216
    int n;
217
    doUnpacking(buffer, n);
218
    for (int i=0; i<n; i++) {
219
        T x;
220
        doUnpacking(buffer, x);
221
        s.insert(x);
222
    }
223
}
224
225
//
226
// Packing/unpacking an std::map
227
//
228
template<typename K, typename V>
229
void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V>& m)
230
{
231
    doPacking(buffer, (int)m.size());
232
    for (typename std::map<K,V>::const_iterator it = m.begin(); it != m.end(); it++) {
233
        doPacking(buffer, (K&)it->first);
234
        doPacking(buffer, (V&)it->second);
235
    }
236
}
237
238
template<typename K, typename V>
239
void doUnpacking(cCommBuffer *buffer, std::map<K,V>& m)
240
{
241
    int n;
242
    doUnpacking(buffer, n);
243
    for (int i=0; i<n; i++) {
244
        K k; V v;
245
        doUnpacking(buffer, k);
246
        doUnpacking(buffer, v);
247
        m[k] = v;
248
    }
249
}
250
251
template<typename K, typename V, typename Tr>
252
void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V,Tr>& m)
253
{
254
    doPacking(buffer, (int)m.size());
255
    for (typename std::map<K,V,Tr>::const_iterator it = m.begin(); it != m.end(); it++) {
256
        doPacking(buffer, it->first);
257
        doPacking(buffer, it->second);
258
    }
259
}
260
261
template<typename K, typename V, typename Tr>
262
void doUnpacking(cCommBuffer *buffer, std::map<K,V,Tr>& m)
263
{
264
    int n;
265
    doUnpacking(buffer, n);
266
    for (int i=0; i<n; i++) {
267
        K k; V v;
268
        doUnpacking(buffer, k);
269
        doUnpacking(buffer, v);
270
        m[k] = v;
271
    }
272
}
273
274
template<typename K, typename V, typename Tr, typename A>
275
void doPacking(cCommBuffer *buffer, /*const*/ std::map<K,V,Tr,A>& m)
276
{
277
    doPacking(buffer, (int)m.size());
278
    for (typename std::map<K,V,Tr,A>::const_iterator it = m.begin(); it != m.end(); it++) {
279
        doPacking(buffer, it->first);
280
        doPacking(buffer, it->second);
281
    }
282
}
283
284
template<typename K, typename V, typename Tr, typename A>
285
void doUnpacking(cCommBuffer *buffer, std::map<K,V,Tr,A>& m)
286
{
287
    int n;
288
    doUnpacking(buffer, n);
289
    for (int i=0; i<n; i++) {
290
        K k; V v;
291
        doUnpacking(buffer, k);
292
        doUnpacking(buffer, v);
293
        m[k] = v;
294
    }
295
}
296
297
NAMESPACE_END
298
299
#endif