Statistics
| Branch: | Revision:

root / include / cpacketqueue.h @ fbe00e73

History | View | Annotate | Download (5.26 KB)

1
//==========================================================================
2
//  CPACKETQUEUE.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

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

    
18
#ifndef __CPACKETQUEUE_H
19
#define __CPACKETQUEUE_H
20

    
21
#include "cqueue.h"
22
#include "cmessage.h"
23

    
24
NAMESPACE_BEGIN
25

    
26

    
27
/**
28
 * A queue class specialized for cPacket objects. The added value
29
 * is that this class keeps track of the total queue length in bits
30
 * as well.
31
 *
32
 * @ingroup Containers
33
 */
34
class SIM_API cPacketQueue : public cQueue
35
{
36
  private:
37
    int64 bitlength;
38

    
39
  protected:
40
    // internal
41
    void addLen(cPacket *pkt);
42

    
43
  public:
44
    /** @name Constructors, destructor, assignment. */
45
    //@{
46
    /**
47
     * Constructor.
48
     */
49
    cPacketQueue(const char *name=NULL, CompareFunc cmp=NULL);
50

    
51
    /**
52
     * Copy constructor. Contained objects that are owned by the queue
53
     * will be duplicated so that the new queue will have its own copy
54
     * of them.
55
     */
56
    cPacketQueue(const cPacketQueue& queue);
57

    
58
    /**
59
     * Assignment operator. Duplication and assignment work all right with cPacketQueue.
60
     * Contained objects that are owned by the queue will be duplicated
61
     * so that the new queue will have its own copy of them.
62
     *
63
     * The name member is not copied; see cNamedObject's operator=() for more details.
64
     */
65
    cPacketQueue& operator=(const cPacketQueue& queue);
66
    //@}
67

    
68
    /** @name Redefined cObject member functions. */
69
    //@{
70

    
71
    /**
72
     * Duplication and assignment work all right with cPacketQueue.
73
     * Contained objects that are owned by the queue will be duplicated
74
     * so that the new queue will have its own copy of them.
75
     */
76
    virtual cPacketQueue *dup() const  {return new cPacketQueue(*this);}
77

    
78
    /**
79
     * Produces a one-line description of the object's contents.
80
     * See cObject for more details.
81
     */
82
    virtual std::string info() const;
83

    
84
    /**
85
     * Serializes the object into an MPI send buffer.
86
     * Used by the simulation kernel for parallel execution.
87
     * See cObject for more details.
88
     */
89
    virtual void parsimPack(cCommBuffer *buffer);
90

    
91
    /**
92
     * Deserializes the object from an MPI receive buffer
93
     * Used by the simulation kernel for parallel execution.
94
     * See cObject for more details.
95
     */
96
    virtual void parsimUnpack(cCommBuffer *buffer);
97
    //@}
98

    
99
    /** @name Setup, insertion and removal functions. */
100
    //@{
101
    /**
102
     * Adds an element to the back of the queue. Trying to insert a
103
     * NULL pointer is an error (throws cRuntimeError). The given
104
     * object must be an instance of cPacket.
105
     */
106
    virtual void insert(cPacket *pkt);
107

    
108
    /**
109
     * Inserts exactly before the given object. If the given position
110
     * does not exist or if you try to insert a NULL pointer,
111
     * cRuntimeError is thrown. The given object must be an instance
112
     * of cPacket.
113
     */
114
    virtual void insertBefore(cPacket *where, cPacket *pkt);
115

    
116
    /**
117
     * Inserts exactly after the given object. If the given position
118
     * does not exist or if you try to insert a NULL pointer,
119
     * cRuntimeError is thrown. The given object must be an instance
120
     * of cPacket.
121
     */
122
    virtual void insertAfter(cPacket *where, cPacket *pkt);
123

    
124
    /**
125
     * Unlinks and returns the object given. If the object is not in the
126
     * queue, NULL pointer is returned. The given object must be an instance
127
     * of cPacket.
128
     */
129
    virtual cPacket *remove(cPacket *pkt);
130

    
131
    /**
132
     * Unlinks and returns the front element in the queue. If the queue
133
     * is empty, cRuntimeError is thrown.
134
     */
135
    virtual cPacket *pop();
136
    //@}
137

    
138
    /** @name Query functions. */
139
    //@{
140
    /**
141
     * Returns the total size of the messages in the queue, in bits.
142
     * This is the sum of the message bit lengths; see cPacket::getBitLength().
143
     */
144
    int64 getBitLength() const  {return bitlength;}
145

    
146
    /**
147
     * Returns the sum of the message lengths in bytes, that is, getBitLength()/8.
148
     * If getBitLength() is not a multiple of 8, the result is rounded up.
149
     */
150
    int64 getByteLength() const  {return (bitlength+7)>>3;}
151

    
152
    /**
153
     * Returns pointer to the object at the front of the queue.
154
     * This is the element to be return by pop().
155
     * Returns NULL if the queue is empty.
156
     */
157
    virtual cPacket *front() const  {return (cPacket *)cQueue::front();}
158

    
159
    /**
160
     * Returns pointer to the last (back) element in the queue.
161
     * This is the element most recently added by insert().
162
     * Returns NULL if the queue is empty.
163
     */
164
    virtual cPacket *back() const  {return (cPacket *)cQueue::back();}
165

    
166
    /**
167
     * Returns the ith element in the queue, or NULL if i is out of range.
168
     * get(0) returns the front element. This method performs linear
169
     * search.
170
     */
171
    cPacket *get(int i) const  {return (cPacket *)cQueue::get(i);}
172
    //@}
173
};
174

    
175
NAMESPACE_END
176

    
177

    
178
#endif
179