Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / cnamedobject.cc @ fbe00e73

History | View | Annotate | Download (3.41 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  CNAMEDOBJECT.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga
8
//
9
//=========================================================================
10
11
/*--------------------------------------------------------------*
12
  Copyright (C) 1992-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18
19
#include <stdio.h>           // sprintf
20
#include <string.h>          // strcpy, strlen etc.
21
#include "cnamedobject.h"
22
#include "cownedobject.h"
23
#include "globals.h"
24
25
#ifdef WITH_PARSIM
26
#include "ccommbuffer.h"
27
#endif
28
29
USING_NAMESPACE
30
31
using std::ostream;
32
33
Register_Class(cNamedObject);
34
35
36
// static class members
37
cDummyStringPool cNamedObject::stringPool("cNamedObject::stringPool");
38
39
cNamedObject::cNamedObject()
40
{
41
    namep = NULL;
42
    flags = FL_NAMEPOOLING;
43
}
44
45
cNamedObject::cNamedObject(const char *name, bool namepooling)
46
{
47
    flags = namepooling ? FL_NAMEPOOLING : 0;
48
    if (!name)
49
        namep = NULL;
50
    else if (namepooling)
51
        namep = stringPool.get(name);
52
    else
53
        namep  = opp_strdup(name);
54
}
55
56
cNamedObject::cNamedObject(const cNamedObject& obj)
57
{
58
    flags = obj.flags; // copy all flags, incl namepooling
59
    namep = NULL;
60
    setName(obj.getName());
61
    operator=(obj);
62
}
63
64
cNamedObject::~cNamedObject()
65
{
66
    if (namep)
67
    {
68
        if (flags & FL_NAMEPOOLING)
69
            stringPool.release(namep);
70
        else
71
            delete [] namep;
72
    }
73
}
74
75
cNamedObject& cNamedObject::operator=(const cNamedObject& obj)
76
{
77
    // Not much to do:
78
    // - ownership not affected
79
    // - name string is NOT copied from other object
80
    // - flags are taken over, except for FL_NAMEPOOLING which is preserved
81
    unsigned short namePooling = flags & FL_NAMEPOOLING;
82
    flags = (obj.flags & ~FL_NAMEPOOLING) | namePooling;
83
    return *this;
84
}
85
86
void cNamedObject::setName(const char *s)
87
{
88
    // release name string
89
    if (namep)
90
    {
91
        if (flags & FL_NAMEPOOLING)
92
            stringPool.release(namep);
93
        else
94
            delete [] namep;
95
    }
96
97
    // set new string
98
    if (!s)
99
        namep = NULL;
100
    else if (flags & FL_NAMEPOOLING)
101
        namep = stringPool.get(s);
102
    else
103
        namep = opp_strdup(s);
104
}
105
106
void cNamedObject::setNamePooling(bool pooling)
107
{
108
    if ((flags & FL_NAMEPOOLING) == pooling)
109
        return;
110
    if (pooling)
111
    {
112
        // turn on
113
        flags |= FL_NAMEPOOLING;
114
        if (namep)
115
        {
116
            const char *oldname = namep;
117
            namep = stringPool.get(oldname);
118
            delete [] oldname;
119
        }
120
    }
121
    else
122
    {
123
        // turn off
124
        flags &= ~FL_NAMEPOOLING;
125
        if (namep)
126
        {
127
            const char *oldname = namep;
128
            namep = opp_strdup(oldname);
129
            stringPool.release(oldname);
130
        }
131
    }
132
}
133
134
void cNamedObject::parsimPack(cCommBuffer *buffer)
135
{
136
#ifndef WITH_PARSIM
137
    throw cRuntimeError(this,eNOPARSIM);
138
#else
139
    buffer->pack(getName());
140
    buffer->pack(flags);
141
#endif
142
}
143
144
void cNamedObject::parsimUnpack(cCommBuffer *buffer)
145
{
146
#ifndef WITH_PARSIM
147
    throw cRuntimeError(this,eNOPARSIM);
148
#else
149
    opp_string tmp;
150
    buffer->unpack(tmp);
151
    setName(tmp.buffer());
152
    buffer->unpack(flags);
153
#endif
154
}
155