Statistics
| Branch: | Revision:

root / include / cproperty.h @ 01873262

History | View | Annotate | Download (6.26 KB)

1
//==========================================================================
2
//  CPROPERTY.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 __CPROPERTY_H
17
#define __CPROPERTY_H
18

    
19
#include <map>
20
#include <vector>
21
#include "simkerneldefs.h"
22
#include "globals.h"
23
#include "cobject.h"
24
#include "cdummystringpool.h"
25
#include "cproperties.h"
26

    
27
NAMESPACE_BEGIN
28

    
29

    
30
/**
31
 * Stores a property with its value. The value consists of key-valuelist pairs;
32
 * the key might be empty (default key).
33
 *
34
 * @ingroup Internals
35
 */
36
class SIM_API cProperty : public cNamedObject
37
{
38
  public:
39
    static const char *DEFAULTKEY;
40

    
41
  private:
42
    enum {
43
      FL_ISLOCKED = 4,
44
      FL_ISIMPLICIT = 8,
45
    };
46

    
47
  protected:
48
    // property names, keys and values are all stringpooled to reduce memory consumption
49
    static cDummyStringPool stringPool;
50

    
51
    cProperties *ownerp;
52

    
53
    const char *propindex;
54
    mutable const char *propfullname;
55

    
56
    typedef std::vector<const char *> CharPtrVector;
57
    CharPtrVector keyv;
58
    std::vector<CharPtrVector> valuesv;
59

    
60
  protected:
61
    static void releaseValues(CharPtrVector& vals);
62
    int findKey(const char *key) const;
63
    CharPtrVector& getValuesVector(const char *key) const;
64

    
65
  public:
66
    // internal: locks the object against modifications. It cannot be unlocked
67
    // -- one must copy contents to an unlocked object, or call dup()
68
    // (new objects are created in unlocked state).
69
    virtual void lock() {setFlag(FL_ISLOCKED, true);}
70

    
71
    // internal:
72
    virtual bool isLocked() const {return flags&FL_ISLOCKED;}
73

    
74
    // internal: set the containing cProperties
75
    virtual void setOwner(cProperties *p) {ownerp = p;}
76

    
77
  public:
78
    /** @name Constructors, destructor, assignment. */
79
    //@{
80

    
81
    /**
82
     * Constructor. The property name should be specified without the "@" mark.
83
     */
84
    explicit cProperty(const char *name=NULL, const char *index=NULL);
85

    
86
    /**
87
     * Copy constructor.
88
     */
89
    cProperty(const cProperty& other) {setFlag(FL_ISLOCKED,false); propindex=propfullname=NULL; operator=(other);}
90

    
91
    /**
92
     * Destructor.
93
     */
94
    virtual ~cProperty();
95

    
96
    /**
97
     * Assignment operator.
98
     */
99
    cProperty& operator=(const cProperty& other);
100
    //@}
101

    
102
    /** @name Redefined cObject functions */
103
    //@{
104
    /**
105
     * Redefined.
106
     * @see setIndex()
107
     */
108
    virtual void setName(const char *name);
109

    
110
    /**
111
     * Redefined to return the property name plus optional index.
112
     */
113
    virtual const char *getFullName() const;
114

    
115
    /**
116
     * Creates and returns an exact copy of this object.
117
     */
118
    virtual cProperty *dup() const   {return new cProperty(*this);}
119

    
120
    /**
121
     * Produces a one-line description of the object's contents.
122
     */
123
    virtual std::string info() const;
124

    
125
    /**
126
     * Serializes the object into a buffer.
127
     */
128
    virtual void parsimPack(cCommBuffer *buffer);
129

    
130
    /**
131
     * Deserializes the object from a buffer.
132
     */
133
    virtual void parsimUnpack(cCommBuffer *buffer);
134
    //@}
135

    
136
    /** @name Property getter/setter methods */
137
    //@{
138
    /**
139
     * Sets the index of this property; see NED syntax
140
     * <tt>\@propname[index](keys-and-values)</tt>.
141
     */
142
    virtual void setIndex(const char *index);
143

    
144
    /**
145
     * Returns the index of this property; see NED syntax
146
     * <tt>\@propname[index](keys-and-values)</tt>.
147
     * Returns NULL if the property has no index
148
     * (<tt>\@propname(keys-and-values)</tt>).
149
     */
150
    virtual const char *getIndex() const;
151

    
152
    /**
153
     * Returns the "implicit" flag of this property.
154
     */
155
    virtual void setIsImplicit(bool b);
156

    
157
    /**
158
     * Returns whether this property is implicit. Implicit properties
159
     * are those not spelled out in the NED file, but rather, added
160
     * automatically during the parsing process or later.
161
     */
162
    virtual bool isImplicit() const;
163

    
164
    /**
165
     * Returns the list of keys if this property. The default key is
166
     * listed as "".
167
     */
168
    virtual const std::vector<const char *>& getKeys() const;
169

    
170
    /**
171
     * Returns true if the property contains the given key. Specify ""
172
     * or DEFAULTKEY for the default key.
173
     */
174
    virtual bool containsKey(const char *key) const;
175

    
176
    /**
177
     * Adds the given key to the property. Has no effect if the key already
178
     * exists.
179
     */
180
    virtual void addKey(const char *key);
181

    
182
    /**
183
     * Returns the number of values for the given key in the property.
184
     * Specify "" or DEFAULTKEY for the default key.
185
     *
186
     * Returns 0 if the given key does not exist.
187
     */
188
    virtual int getNumValues(const char *key) const;
189

    
190
    /**
191
     * Expands or trims the list of values for the given key in the property,
192
     * by discarding elements or adding "" elements. Specify "" or DEFAULTKEY
193
     * for the default key. Note that simply setting an element above
194
     * getNumValues(key) will also expand the list.
195
     *
196
     * Throws an error if the given key does not exist.
197
     */
198
    virtual void setNumValues(const char *key, int size);
199

    
200
    /**
201
     * Returns the indexth value for the given key in the property.
202
     * Specify "" or DEFAULTKEY for the default key. If the key
203
     * does not exist or the index is out of bounds, NULL is returned.
204
     */
205
    virtual const char *getValue(const char *key, int index=0) const;
206

    
207
    /**
208
     * Replaces a value for the given key in the property. Specify "" or
209
     * DEFAULTKEY for the default key. cProperty will create its own copy
210
     * of the string passed. index may be greater than getNumValues(key);
211
     * that will cause the values list to expand, the new elements filled with "".
212
     *
213
     * Throws an error if the given key does not exist, or the index is
214
     * negative.
215
     */
216
    virtual void setValue(const char *key, int index, const char *value);
217

    
218
    /**
219
     * Erases the given key and all its values.
220
     */
221
    virtual void erase(const char *key);
222
    //@}
223
};
224

    
225
NAMESPACE_END
226

    
227

    
228
#endif
229

    
230