Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / parsim / ccommbufferbase.h @ 81ad8b66

History | View | Annotate | Download (2.69 KB)

1
//=========================================================================
2
//  CCOMMBUFFERBASE.H - 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
#ifndef __CCOMMBUFFERBASE_H__
22
#define __CCOMMBUFFERBASE_H__
23

    
24
#include "ccommbuffer.h"
25

    
26
NAMESPACE_BEGIN
27

    
28
/**
29
 * Adds buffer (re)allocation functions to cCommBuffer. This functionality
30
 * is not always needed, e.g. PVM manages its pack/unpack buffers internally.
31
 */
32
class SIM_API cCommBufferBase : public cCommBuffer
33
{
34
  protected:
35
    char *mBuffer;    // the buffer
36
    int mBufferSize;  // size of buffer allocated
37
    int mMsgSize;     // current msg size (incremented by pack() functions)
38
    int mPosition;    // current position in buffer for unpacking
39

    
40
  protected:
41
    void extendBufferFor(int dataSize);
42

    
43
  public:
44
    /**
45
     * Constructor.
46
     */
47
    cCommBufferBase();
48

    
49
    /**
50
     * Destructor.
51
     */
52
    virtual ~cCommBufferBase();
53

    
54
    /** @name Buffer management */
55
    //@{
56
    /**
57
     * Returns the buffer after packing.
58
     */
59
    char *getBuffer() const;
60

    
61
    /**
62
     * Returns the size of the buffer.
63
     */
64
    int getBufferLength() const;
65

    
66
    /**
67
     * Extend buffer to the given size is needed. Existing buffer contents
68
     * may be lost.
69
     */
70
    void allocateAtLeast(int size);
71

    
72
    /**
73
     * Set message length in the buffer. Used after receiving a message
74
     * and copying it to the buffer.
75
     */
76
    void setMessageSize(int size);
77

    
78
    /**
79
     * Returns message length in the buffer.
80
     */
81
    int getMessageSize() const;
82

    
83
    /**
84
     * Reset buffer to an empty state.
85
     */
86
    void reset();
87

    
88
    /**
89
     * Returns true if all data in buffer was used up during unpacking.
90
     * Returns false if there was underflow (too much data unpacked)
91
     * or still there are unpacked data in the buffer.
92
     */
93
    virtual bool isBufferEmpty() const;
94

    
95
    /**
96
     * Utility function. To be called after unpacking a communication buffer,
97
     * it checks whether the buffer is empty. If it is not (i.e. an underflow
98
     * or overflow occurred), an exception is thrown.
99
     */
100
    virtual void assertBufferEmpty();
101
    //@}
102
};
103

    
104
NAMESPACE_END
105

    
106

    
107
#endif