Statistics
| Branch: | Revision:

root / include / cnamedobject.h @ master

History | View | Annotate | Download (3.79 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  CNAMEDOBJECT.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 __CNAMEDOBJECT_H
19
#define __CNAMEDOBJECT_H
20
21
#include <typeinfo>
22
#include <iostream>
23
#include "simkerneldefs.h"
24
#include "simutil.h"
25
#include "cobject.h"
26
#include "cexception.h"
27
#include "cdummystringpool.h"
28
NAMESPACE_BEGIN
29
30
31
/**
32
 * Extends cObject with a name string. Also includes a "flags" member,
33
 * with bits open for use by subclasses.
34
 *
35
 * @ingroup SimCore
36
 */
37
class SIM_API cNamedObject : public cObject
38
{
39
  private:
40
    const char *namep;  // object name (stringpooled if flags&FL_NAMEPOOLING!=0)
41
42
  protected:
43
    unsigned short flags;  // FL_NAMEPOOLING flag; other bits used by derived classes
44
    unsigned short unused; // space lost to due to word aligment; one may make use of it in subclasses (cModule, cSimpleModule, cGate)
45
    enum {FL_NAMEPOOLING = 1};
46
47
  protected:
48
    // internal: set a bit in flags; flag is one of the FL_xxx constants
49
    void setFlag(int flag, bool value) {if (value) flags|=flag; else flags&=~flag;}
50
51
  private:
52
    // pool for shared storage of object names
53
    static cDummyStringPool stringPool;
54
55
  public:
56
    /** @name Constructors, destructor, assignment. */
57
    //@{
58
    /**
59
     * Create object without a name.
60
     */
61
    cNamedObject();
62
63
    /**
64
     * Create object with given name. Name pooling is an optimization feature.
65
     */
66
    explicit cNamedObject(const char *name, bool namepooling=true);
67
68
    /**
69
     * Copy constructor. In derived classes, it is usually implemented
70
     * as <tt>{operator=(obj);</tt>
71
     */
72
    cNamedObject(const cNamedObject& obj);
73
74
    /**
75
     * Destructor.
76
     */
77
    virtual ~cNamedObject();
78
79
    /**
80
     * The assignment operator. Derived classes should contain similar
81
     * methods (<tt>cClassName& cClassName::operator=(cClassName&)</tt>).
82
     *
83
     * Assigment copies the contents of the object EXCEPT for the name string.
84
     * If you want to copy the name string, you can do it by hand:
85
     * <tt>setName(o.getName()</tt>).
86
     */
87
    cNamedObject& operator=(const cNamedObject& o);
88
89
    // Note: dup() is still the original cObject one, which throws an error
90
    // to indicate that dup() has to be redefined in each subclass.
91
92
    /**
93
     * Serializes the object into a buffer.
94
     */
95
    virtual void parsimPack(cCommBuffer *buffer);
96
97
    /**
98
     * Deserializes the object from a buffer.
99
     */
100
    virtual void parsimUnpack(cCommBuffer *buffer);
101
    //@}
102
103
    /** @name Handling the name string. */
104
    //@{
105
    /**
106
     * Sets object's name. The object creates its own copy of the string.
107
     * NULL pointer may also be passed, which will be interpreted as an
108
     * empty string ("").
109
     */
110
    virtual void setName(const char *s);
111
112
    /**
113
     * Returns pointer to the object's name, a string stored in the object.
114
     * This function never returns NULL.
115
     */
116
    virtual const char *getName() const  {return namep ? namep : "";}
117
118
    /**
119
     * Turn name pooling on/off. Name pooling is an optimization technique that saves
120
     * memory if several objects have identical names.
121
     */
122
    virtual void setNamePooling(bool b);
123
124
    /**
125
     * Returns whether name pooling is turned on for this object.
126
     */
127
    virtual bool getNamePooling() {return flags&FL_NAMEPOOLING;}
128
    //@}
129
};
130
131
NAMESPACE_END
132
133
134
#endif