Statistics
| Branch: | Revision:

root / include / opp_string.h @ 47c4b975

History | View | Annotate | Download (4.42 KB)

1
//==========================================================================
2
//   OPP_STRING.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  opp_string class
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
#ifndef __OPP_STRING_H
20
#define __OPP_STRING_H
21

    
22
#include <vector>
23
#include <map>
24
#include <ostream>
25
#include "simkerneldefs.h"
26
#include "simutil.h"
27

    
28
NAMESPACE_BEGIN
29

    
30
/**
31
 * Lightweight string class, used internally in some parts of \opp.
32
 * In simulation models it is better to use std::string or const char *
33
 * instead.
34
 *
35
 * opp_string has only one data member, a char* pointer. Allocation and
36
 * deallocation of the contents takes place via opp_strdup() and operator
37
 * delete.
38
 *
39
 * @ingroup Internals
40
 */
41
class SIM_API opp_string
42
{
43
  private:
44
    char *str;
45

    
46
  public:
47
    /**
48
     * Constructor.
49
     */
50
    opp_string()  {str = 0;}
51

    
52
    /**
53
     * Constructor.
54
     */
55
    opp_string(const char *s)  {str = opp_strdup(s);}
56

    
57
    /**
58
     * Constructor.
59
     */
60
    opp_string(const std::string& s)  {str = opp_strdup(s.c_str());}
61

    
62
    /**
63
     * Copy constructor.
64
     */
65
    opp_string(const opp_string& s)  {str = opp_strdup(s.str);}
66

    
67
    /**
68
     * Destructor.
69
     */
70
    ~opp_string()  {delete [] str;}
71

    
72
    /**
73
     * Return pointer to the string.
74
     */
75
    const char *c_str() const  {return str ? str : "";}
76

    
77
    /**
78
     * Null (empty) string or not.
79
     */
80
    bool empty() const  {return !str || !str[0];}
81

    
82
    /**
83
     * Returns pointer to the internal buffer where the string is stored.
84
     * It is allowed to write into the string via this pointer, but the
85
     * length of the string should not be exceeded.
86
     */
87
    char *buffer()  {return str;}
88

    
89
    /**
90
     * Allocates a buffer of the given size.
91
     */
92
    char *reserve(unsigned size)  {delete[] str;str=new char[size];return str;}
93

    
94
    /**
95
     * Deletes the old value and opp_strdup()'s the new value
96
     * to create the object's own copy.
97
     */
98
    const char *operator=(const char *s)  {delete[] str;str=opp_strdup(s);return str;}
99

    
100
    /**
101
     * Assignment.
102
     */
103
    opp_string& operator=(const opp_string& s)  {operator=(s.str); return *this;}
104

    
105
    /**
106
     * Assignment.
107
     */
108
    opp_string& operator=(const std::string& s)  {operator=(s.c_str()); return *this;}
109

    
110
    /**
111
     * Comparison.
112
     */
113
    bool operator<(const opp_string& s) const  {return opp_strcmp(str,s.str) < 0;}
114

    
115
    /**
116
     * Concatenation
117
     */
118
    opp_string& operator+=(const char *s) {return operator=(std::string(str).append(s));}
119

    
120
    /**
121
     * Concatenation
122
     */
123
    opp_string& operator+=(const opp_string& s) {operator+=(s.str); return *this;}
124

    
125
    /**
126
     * Concatenation
127
     */
128
    opp_string& operator+=(const std::string& s) {operator+=(s.c_str()); return *this;}
129

    
130
    /**
131
     * Concatenation
132
     */
133
    opp_string operator+(const char *s) {return opp_string((std::string(str)+s).c_str());}
134

    
135
    /**
136
     * Concatenation
137
     */
138
    opp_string operator+(const opp_string& s) {return operator+(s.c_str());}
139

    
140
    /**
141
     * Concatenation
142
     */
143
    opp_string operator+(const std::string& s) {return operator+(s.c_str());}
144

    
145
};
146

    
147
inline std::ostream& operator<<(std::ostream& out, const opp_string& s)
148
{
149
    out << s.c_str(); return out;
150
}
151

    
152

    
153
/**
154
 * Lightweight string vector, used internally in some parts of \opp.
155
 * Inheritance is used to "de-templatize" the vector class, because the
156
 * Windows DLL interface is not really a friend of templated classes.
157
 *
158
 * @ingroup Internals
159
 */
160
class SIM_API opp_string_vector : public std::vector<opp_string>
161
{
162
  public:
163
    opp_string_vector() {}
164
    opp_string_vector(const opp_string_vector& other) {*this = other;}
165
};
166

    
167

    
168
/**
169
 * Lightweight string vector, used internally in some parts of \opp.
170
 * Inheritance is used to "de-templatize" the map class, because the
171
 * Windows DLL interface is not really a friend of templated classes.
172
 *
173
 * @ingroup Internals
174
 */
175
class SIM_API opp_string_map : public std::map<opp_string,opp_string>
176
{
177
  public:
178
    opp_string_map() {}
179
    opp_string_map(const opp_string_map& other) {*this = other;}
180
};
181

    
182
NAMESPACE_END
183

    
184

    
185
#endif
186

    
187