Statistics
| Branch: | Revision:

root / src / sim / parsim / cmemcommbuffer.cc @ e1750c09

History | View | Annotate | Download (8.57 KB)

1
//=========================================================================
2
//  CMEMCOMMBUFFER.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2003
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2003-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21
#include <string.h>
22
#include "cmemcommbuffer.h"
23
#include "simutil.h"
24
#include "cownedobject.h"
25
#include "globals.h"
26
#include "regmacros.h"
27

    
28
USING_NAMESPACE
29

    
30
Register_Class(cMemCommBuffer);
31

    
32

    
33
#define STORE(type,d)    {*(type *)(mBuffer+mMsgSize)=d; mMsgSize+=sizeof(type);}
34
#define EXTRACT(type,d)  {d=*(type *)(mBuffer+mPosition); mPosition+=sizeof(type);}
35

    
36
#define STOREARRAY(type,d,size)   {memcpy(mBuffer+mMsgSize,d,size*sizeof(type)); mMsgSize+=size*sizeof(type);}
37
#define EXTRACTARRAY(type,d,size) {memcpy(d,mBuffer+mPosition,size*sizeof(type)); mPosition+=size*sizeof(type);}
38

    
39

    
40
cMemCommBuffer::cMemCommBuffer()
41
{
42
}
43

    
44
cMemCommBuffer::~cMemCommBuffer ()
45
{
46
}
47

    
48
void cMemCommBuffer::pack(char d)
49
{
50
    extendBufferFor(sizeof(char));
51
    STORE(char,d);
52
}
53

    
54
void cMemCommBuffer::pack(unsigned char d)
55
{
56
    extendBufferFor(sizeof(unsigned char));
57
    STORE(unsigned char,d);
58
}
59

    
60
void cMemCommBuffer::pack(bool d)
61
{
62
    extendBufferFor(sizeof(bool));
63
    STORE(bool,d);
64
}
65

    
66
void cMemCommBuffer::pack(short d)
67
{
68
    extendBufferFor(sizeof(short));
69
    STORE(short,d);
70
}
71

    
72
void cMemCommBuffer::pack(unsigned short d)
73
{
74
    extendBufferFor(sizeof(unsigned short));
75
    STORE(unsigned short,d);
76
}
77

    
78
void cMemCommBuffer::pack(int d)
79
{
80
    extendBufferFor(sizeof(int));
81
    STORE(int,d);
82
}
83

    
84
void cMemCommBuffer::pack(unsigned int d)
85
{
86
    extendBufferFor(sizeof(unsigned int));
87
    STORE(unsigned int,d);
88
}
89

    
90
void cMemCommBuffer::pack(long d)
91
{
92
    extendBufferFor(sizeof(long));
93
    STORE(long,d);
94
}
95

    
96
void cMemCommBuffer::pack(unsigned long d)
97
{
98
    extendBufferFor(sizeof(unsigned long));
99
    STORE(unsigned long,d);
100
}
101

    
102
void cMemCommBuffer::pack(opp_long_long d)
103
{
104
    extendBufferFor(sizeof(opp_long_long));
105
    STORE(opp_long_long,d);
106
}
107

    
108
void cMemCommBuffer::pack(opp_unsigned_long_long d)
109
{
110
    extendBufferFor(sizeof(opp_unsigned_long_long));
111
    STORE(opp_unsigned_long_long,d);
112
}
113

    
114
void cMemCommBuffer::pack(float d)
115
{
116
    extendBufferFor(sizeof(float));
117
    STORE(float,d);
118
}
119

    
120
void cMemCommBuffer::pack(double d)
121
{
122
    extendBufferFor(sizeof(double));
123
    STORE(double,d);
124
}
125

    
126
void cMemCommBuffer::pack(long double d)
127
{
128
    extendBufferFor(sizeof(long double));
129
    STORE(long double,d);
130
}
131

    
132
// pack a string
133
void cMemCommBuffer::pack(const char *d)
134
{
135
    int len = d ? strlen(d) : 0;
136
    pack(len);
137
    extendBufferFor(len*sizeof(char));
138
    STOREARRAY(char,d,len);
139
}
140

    
141
void cMemCommBuffer::pack(const opp_string& d)
142
{
143
    pack(d.c_str());
144
}
145

    
146
void cMemCommBuffer::pack(SimTime d)
147
{
148
    pack((opp_long_long)d.raw());
149
}
150

    
151
void cMemCommBuffer::pack(const char *d, int size)
152
{
153
    extendBufferFor(size*sizeof(char));
154
    STOREARRAY(char,d,size);
155
}
156

    
157
void cMemCommBuffer::pack(const unsigned char *d, int size)
158
{
159
    extendBufferFor(size*sizeof(unsigned char));
160
    STOREARRAY(unsigned char,d,size);
161
}
162

    
163
void cMemCommBuffer::pack(const bool *d, int size)
164
{
165
    extendBufferFor(size*sizeof(bool));
166
    STOREARRAY(bool,d,size);
167
}
168

    
169
void cMemCommBuffer::pack(const short *d, int size)
170
{
171
    extendBufferFor(size*sizeof(short));
172
    STOREARRAY(short,d,size);
173
}
174

    
175
void cMemCommBuffer::pack(const unsigned short *d, int size)
176
{
177
    extendBufferFor(size*sizeof(unsigned short));
178
    STOREARRAY(unsigned short,d,size);
179
}
180

    
181
void cMemCommBuffer::pack(const int *d, int size)
182
{
183
    extendBufferFor(size*sizeof(int));
184
    STOREARRAY(int,d,size);
185
}
186

    
187
void cMemCommBuffer::pack(const unsigned int *d, int size)
188
{
189
    extendBufferFor(size*sizeof(unsigned int));
190
    STOREARRAY(unsigned int,d,size);
191
}
192

    
193
void cMemCommBuffer::pack(const long *d, int size)
194
{
195
    extendBufferFor(size*sizeof(long));
196
    STOREARRAY(long,d,size);
197
}
198

    
199
void cMemCommBuffer::pack(const unsigned long *d, int size)
200
{
201
    extendBufferFor(size*sizeof(unsigned long));
202
    STOREARRAY(unsigned long,d,size);
203
}
204

    
205
void cMemCommBuffer::pack(const opp_long_long *d, int size)
206
{
207
    extendBufferFor(size*sizeof(opp_long_long));
208
    STOREARRAY(opp_long_long,d,size);
209
}
210

    
211
void cMemCommBuffer::pack(const opp_unsigned_long_long *d, int size)
212
{
213
    extendBufferFor(size*sizeof(opp_unsigned_long_long));
214
    STOREARRAY(opp_unsigned_long_long,d,size);
215
}
216

    
217
void cMemCommBuffer::pack(const float *d, int size)
218
{
219
    extendBufferFor(size*sizeof(float));
220
    STOREARRAY(float,d,size);
221
}
222

    
223
void cMemCommBuffer::pack(const double *d, int size)
224
{
225
    extendBufferFor(size*sizeof(double));
226
    STOREARRAY(double,d,size);
227
}
228

    
229
void cMemCommBuffer::pack(const long double *d, int size)
230
{
231
    extendBufferFor(size*sizeof(long double));
232
    STOREARRAY(long double,d,size);
233
}
234

    
235
// pack string array
236
void cMemCommBuffer::pack(const char **d, int size)
237
{
238
    for (int i = 0; i < size; i++)
239
        pack(d[i]);
240
}
241

    
242
void cMemCommBuffer::pack(const opp_string *d, int size)
243
{
244
    for (int i = 0; i < size; i++)
245
        pack(d[i]);
246
}
247

    
248
void cMemCommBuffer::pack(const SimTime *d, int size)
249
{
250
    for (int i = 0; i < size; i++)
251
        pack(d[i]);
252
}
253

    
254
//--------------------------------
255

    
256
void cMemCommBuffer::unpack(char& d)
257
{
258
    EXTRACT(char,d);
259
}
260

    
261
void cMemCommBuffer::unpack(unsigned char& d)
262
{
263
    EXTRACT(unsigned char,d);
264
}
265

    
266
void cMemCommBuffer::unpack(bool& d)
267
{
268
    EXTRACT(bool,d);
269
}
270

    
271
void cMemCommBuffer::unpack(short& d)
272
{
273
    EXTRACT(short,d);
274
}
275

    
276
void cMemCommBuffer::unpack(unsigned short& d)
277
{
278
    EXTRACT(unsigned short,d);
279
}
280

    
281
void cMemCommBuffer::unpack(int& d)
282
{
283
    EXTRACT(int,d);
284
}
285

    
286
void cMemCommBuffer::unpack(unsigned int& d)
287
{
288
    EXTRACT(unsigned int,d);
289
}
290

    
291
void cMemCommBuffer::unpack(long& d)
292
{
293
    EXTRACT(long,d);
294
}
295

    
296
void cMemCommBuffer::unpack(unsigned long& d)
297
{
298
    EXTRACT(unsigned long,d);
299
}
300

    
301
void cMemCommBuffer::unpack(opp_long_long& d)
302
{
303
    EXTRACT(opp_long_long,d);
304
}
305

    
306
void cMemCommBuffer::unpack(opp_unsigned_long_long& d)
307
{
308
    EXTRACT(opp_unsigned_long_long,d);
309
}
310

    
311
void cMemCommBuffer::unpack(float& d)
312
{
313
    EXTRACT(float,d);
314
}
315

    
316
void cMemCommBuffer::unpack(double& d)
317
{
318
    EXTRACT(double,d);
319
}
320

    
321
void cMemCommBuffer::unpack(long double& d)
322
{
323
    EXTRACT(long double,d);
324
}
325

    
326
// unpack a string
327
void cMemCommBuffer::unpack(const char *&d)
328
{
329
    int len;
330
    unpack(len);
331
    char *tmp = new char[len+1];
332
    EXTRACTARRAY(char,tmp,len);
333
    tmp[len] = '\0';
334
    d = tmp;
335
}
336

    
337
void cMemCommBuffer::unpack(opp_string& d)
338
{
339
    int len;
340
    unpack(len);
341
    d.reserve(len+1);
342
    EXTRACTARRAY(char,d.buffer(),len);
343
    d.buffer()[len] = '\0';
344
}
345

    
346
void cMemCommBuffer::unpack(SimTime& d)
347
{
348
    opp_long_long raw;
349
    unpack(raw);
350
    d.setRaw(raw);
351
}
352

    
353
void cMemCommBuffer::unpack(char *d, int size)
354
{
355
    EXTRACTARRAY(char,d,size);
356
}
357

    
358
void cMemCommBuffer::unpack(unsigned char *d, int size)
359
{
360
    EXTRACTARRAY(unsigned char,d,size);
361
}
362

    
363
void cMemCommBuffer::unpack(bool *d, int size)
364
{
365
    EXTRACTARRAY(bool,d,size);
366
}
367

    
368
void cMemCommBuffer::unpack(short *d, int size)
369
{
370
    EXTRACTARRAY(short,d,size);
371
}
372

    
373
void cMemCommBuffer::unpack(unsigned short *d, int size)
374
{
375
    EXTRACTARRAY(unsigned short,d,size);
376
}
377

    
378
void cMemCommBuffer::unpack(int *d, int size)
379
{
380
    EXTRACTARRAY(int,d,size);
381
}
382

    
383
void cMemCommBuffer::unpack(unsigned int *d, int size)
384
{
385
    EXTRACTARRAY(unsigned,d,size);
386
}
387

    
388
void cMemCommBuffer::unpack(long *d, int size)
389
{
390
    EXTRACTARRAY(long,d,size);
391
}
392

    
393
void cMemCommBuffer::unpack(unsigned long *d, int size)
394
{
395
    EXTRACTARRAY(unsigned long,d,size);
396
}
397

    
398
void cMemCommBuffer::unpack(opp_long_long *d, int size)
399
{
400
    EXTRACTARRAY(opp_long_long,d,size);
401
}
402

    
403
void cMemCommBuffer::unpack(opp_unsigned_long_long *d, int size)
404
{
405
    EXTRACTARRAY(opp_unsigned_long_long,d,size);
406
}
407

    
408
void cMemCommBuffer::unpack(float *d, int size)
409
{
410
    EXTRACTARRAY(float,d,size);
411
}
412

    
413
void cMemCommBuffer::unpack(double *d, int size)
414
{
415
    EXTRACTARRAY(double,d,size);
416
}
417

    
418
void cMemCommBuffer::unpack(long double *d, int size)
419
{
420
    EXTRACTARRAY(long double,d,size);
421
}
422

    
423
void cMemCommBuffer::unpack(const char **d, int size)
424
{
425
    for (int i = 0; i < size; i++)
426
        unpack(d[i]);
427
}
428

    
429
void cMemCommBuffer::unpack(opp_string *d, int size)
430
{
431
    for (int i = 0; i < size; i++)
432
        unpack(d[i]);
433
}
434

    
435
void cMemCommBuffer::unpack(SimTime *d, int size)
436
{
437
    for (int i = 0; i < size; i++)
438
        unpack(d[i]);
439
}
440