OMNeT++ Simulation Library  5.6.1
opp_string.h
1 //==========================================================================
2 // OPP_STRING.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 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 __OMNETPP_OPP_STRING_H
17 #define __OMNETPP_OPP_STRING_H
18 
19 #include <vector>
20 #include <map>
21 #include <ostream>
22 #include "simkerneldefs.h"
23 #include "simutil.h"
24 
25 namespace omnetpp {
26 
39 class SIM_API opp_string
40 {
41  private:
42  char *buf;
43 
44  public:
48  opp_string() {buf = nullptr;}
49 
53  opp_string(const char *s) {buf = opp_strdup(s);}
54 
58  opp_string(const char *s, int n) {buf = new char[n+1]; strncpy(buf, s?s:"", n); buf[n] = '\0';}
59 
63  opp_string(const std::string& s) {buf = opp_strdup(s.c_str());}
64 
68  opp_string(const opp_string& s) {buf = opp_strdup(s.buf);}
69 
73  ~opp_string() {delete [] buf;}
74 
78  const char *c_str() const {return buf ? buf : "";}
79 
83  std::string str() const {return buf ? buf : "";}
84 
88  bool empty() const {return !buf || !buf[0];}
89 
95  char *buffer() {return buf;}
96 
100  int size() const {return buf ? strlen(buf) : 0;}
101 
105  char *reserve(unsigned size) {delete[] buf;buf=new char[size];return buf;}
106 
111  const char *operator=(const char *s) {delete[] buf;buf=opp_strdup(s);return buf;}
112 
116  opp_string& operator=(const opp_string& s) {operator=(s.buf); return *this;}
117 
121  opp_string& operator=(const std::string& s) {operator=(s.c_str()); return *this;}
122 
126  bool operator<(const opp_string& s) const {return opp_strcmp(buf,s.buf) < 0;}
127 
131  bool operator==(const opp_string& s) const {return opp_strcmp(buf,s.buf) == 0;}
132 
136  bool operator!=(const opp_string& s) const {return opp_strcmp(buf,s.buf) != 0;}
137 
141  opp_string& operator+=(const char *s) {return operator=(std::string(buf).append(s));}
142 
146  opp_string& operator+=(const opp_string& s) {operator+=(s.buf); return *this;}
147 
151  opp_string& operator+=(const std::string& s) {operator+=(s.c_str()); return *this;}
152 
156  opp_string operator+(const char *s) {return opp_string((std::string(buf)+s).c_str());}
157 
161  opp_string operator+(const opp_string& s) {return operator+(s.c_str());}
162 
166  opp_string operator+(const std::string& s) {return operator+(s.c_str());}
167 
168 };
169 
170 inline std::ostream& operator<<(std::ostream& out, const opp_string& s)
171 {
172  out << s.c_str(); return out;
173 }
174 
175 
184 class SIM_API opp_string_vector : public std::vector<opp_string>
185 {
186  public:
187  opp_string_vector() {}
188  opp_string_vector(const opp_string_vector& other) : std::vector<opp_string>(other) {}
189 };
190 
191 
200 class SIM_API opp_string_map : public std::map<opp_string,opp_string>
201 {
202  public:
203  opp_string_map() {}
204  opp_string_map(const opp_string_map& other) : std::map<opp_string,opp_string>(other) {}
205 };
206 
207 } // namespace omnetpp
208 
209 
210 #endif
211 
212 
Lightweight string class, used internally in some parts of OMNeT++.
Definition: opp_string.h:39
opp_string()
Definition: opp_string.h:48
opp_string(const std::string &s)
Definition: opp_string.h:63
const char * operator=(const char *s)
Definition: opp_string.h:111
opp_string & operator+=(const std::string &s)
Definition: opp_string.h:151
std::string str() const
Definition: opp_string.h:83
opp_string & operator=(const std::string &s)
Definition: opp_string.h:121
opp_string(const char *s)
Definition: opp_string.h:53
opp_string(const char *s, int n)
Definition: opp_string.h:58
char * opp_strdup(const char *)
Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr...
Definition: simutil.h:290
opp_string & operator=(const opp_string &s)
Definition: opp_string.h:116
int size() const
Definition: opp_string.h:100
bool operator<(const opp_string &s) const
Definition: opp_string.h:126
Lightweight string-to-string map, used internally in some parts of OMNeT++.
Definition: opp_string.h:200
opp_string operator+(const char *s)
Definition: opp_string.h:156
char * buffer()
Definition: opp_string.h:95
bool operator!=(const opp_string &s) const
Definition: opp_string.h:136
opp_string(const opp_string &s)
Definition: opp_string.h:68
opp_string & operator+=(const opp_string &s)
Definition: opp_string.h:146
char * reserve(unsigned size)
Definition: opp_string.h:105
opp_string operator+(const std::string &s)
Definition: opp_string.h:166
const char * c_str() const
Definition: opp_string.h:78
bool operator==(const opp_string &s) const
Definition: opp_string.h:131
Definition: cabstracthistogram.h:21
opp_string operator+(const opp_string &s)
Definition: opp_string.h:161
int opp_strcmp(const char *, const char *)
Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string (""...
Definition: simutil.h:307
opp_string & operator+=(const char *s)
Definition: opp_string.h:141
Lightweight string vector, used internally in some parts of OMNeT++.
Definition: opp_string.h:184
bool empty() const
Definition: opp_string.h:88
~opp_string()
Definition: opp_string.h:73