OMNeT++ API 6.1
Discrete Event Simulation Library
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 <cstring>
20 #include <vector>
21 #include <map>
22 #include <ostream>
23 #include "simkerneldefs.h"
24 
25 namespace omnetpp {
26 
39 class SIM_API opp_string
40 {
41  private:
42  char *buf;
43 
44  static char *opp_strdup(const char *s) {
45  if (!s || !s[0]) return nullptr;
46  char *p = new char[strlen(s)+1];
47  strcpy(p,s);
48  return p;
49  }
50 
51  static int opp_strcmp(const char *s1, const char *s2) {
52  if (s1)
53  return s2 ? strcmp(s1,s2) : (*s1 ? 1 : 0);
54  else
55  return (s2 && *s2) ? -1 : 0;
56  }
57 
58  public:
62  opp_string() {buf = nullptr;}
63 
67  opp_string(const char *s) {buf = opp_strdup(s);}
68 
72  opp_string(const char *s, int n) {buf = new char[n+1]; strncpy(buf, s?s:"", n); buf[n] = '\0';}
73 
77  opp_string(const std::string& s) {buf = opp_strdup(s.c_str());}
78 
82  opp_string(const opp_string& s) {buf = opp_strdup(s.buf);}
83 
87  opp_string(opp_string&& s) {buf = s.buf; s.buf = nullptr;}
88 
92  ~opp_string() {delete [] buf;}
93 
97  const char *c_str() const {return buf ? buf : "";}
98 
102  std::string str() const {return buf ? buf : "";}
103 
107  bool empty() const {return !buf || !buf[0];}
108 
114  char *buffer() {return buf;}
115 
119  int size() const {return buf ? strlen(buf) : 0;}
120 
124  char *reserve(unsigned size) {delete[] buf;buf=new char[size];return buf;}
125 
130  const char *operator=(const char *s) {delete[] buf;buf=opp_strdup(s);return buf;}
131 
135  opp_string& operator=(const opp_string& s) {operator=(s.buf); return *this;}
136 
140  opp_string& operator=(const std::string& s) {operator=(s.c_str()); return *this;}
141 
145  bool operator<(const opp_string& s) const {return opp_strcmp(buf,s.buf) < 0;}
146 
150  bool operator==(const opp_string& s) const {return opp_strcmp(buf,s.buf) == 0;}
151 
155  bool operator!=(const opp_string& s) const {return opp_strcmp(buf,s.buf) != 0;}
156 
160  opp_string& operator+=(const char *s) {return operator=(std::string(buf).append(s));}
161 
165  opp_string& operator+=(const opp_string& s) {operator+=(s.buf); return *this;}
166 
170  opp_string& operator+=(const std::string& s) {operator+=(s.c_str()); return *this;}
171 
175  opp_string operator+(const char *s) {return opp_string((std::string(buf)+s).c_str());}
176 
180  opp_string operator+(const opp_string& s) {return operator+(s.c_str());}
181 
185  opp_string operator+(const std::string& s) {return operator+(s.c_str());}
186 
187 };
188 
189 inline std::ostream& operator<<(std::ostream& out, const opp_string& s)
190 {
191  out << s.c_str(); return out;
192 }
193 
194 
203 class SIM_API opp_string_vector : public std::vector<opp_string>
204 {
205  public:
206  opp_string_vector() {}
207  opp_string_vector(const opp_string_vector& other) = default;
208 };
209 
210 
219 class SIM_API opp_string_map : public std::map<opp_string,opp_string>
220 {
221  public:
222  opp_string_map() {}
223  opp_string_map(const opp_string_map& other) = default;
224 };
225 
226 } // namespace omnetpp
227 
228 
229 #endif
230 
231 
omnetpp::opp_string::buffer
char * buffer()
Definition: opp_string.h:114
omnetpp::opp_string::~opp_string
~opp_string()
Definition: opp_string.h:92
omnetpp::opp_string::operator=
const char * operator=(const char *s)
Definition: opp_string.h:130
omnetpp::opp_string_vector
Lightweight string vector, used internally in some parts of OMNeT++.
Definition: opp_string.h:203
omnetpp::opp_string::operator+=
opp_string & operator+=(const opp_string &s)
Definition: opp_string.h:165
omnetpp::opp_string::operator+
opp_string operator+(const std::string &s)
Definition: opp_string.h:185
omnetpp::opp_string::operator<
bool operator<(const opp_string &s) const
Definition: opp_string.h:145
omnetpp::opp_string::operator=
opp_string & operator=(const std::string &s)
Definition: opp_string.h:140
omnetpp::opp_string::str
std::string str() const
Definition: opp_string.h:102
omnetpp::opp_string
Lightweight string class, used internally in some parts of OMNeT++.
Definition: opp_string.h:39
omnetpp::opp_string::c_str
const char * c_str() const
Definition: opp_string.h:97
omnetpp::opp_string_map
Lightweight string-to-string map, used internally in some parts of OMNeT++.
Definition: opp_string.h:219
omnetpp::opp_string::opp_string
opp_string(const char *s)
Definition: opp_string.h:67
omnetpp::opp_strdup
char * opp_strdup(const char *s)
Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr.
Definition: stringutil.h:67
omnetpp::opp_string::opp_string
opp_string(const std::string &s)
Definition: opp_string.h:77
omnetpp::opp_string::opp_string
opp_string(const char *s, int n)
Definition: opp_string.h:72
omnetpp::opp_string::operator+=
opp_string & operator+=(const char *s)
Definition: opp_string.h:160
omnetpp::opp_string::operator+
opp_string operator+(const char *s)
Definition: opp_string.h:175
omnetpp::opp_string::opp_string
opp_string(opp_string &&s)
Definition: opp_string.h:87
omnetpp::opp_string::reserve
char * reserve(unsigned size)
Definition: opp_string.h:124
omnetpp::opp_string::operator=
opp_string & operator=(const opp_string &s)
Definition: opp_string.h:135
omnetpp::opp_string::opp_string
opp_string(const opp_string &s)
Definition: opp_string.h:82
omnetpp::opp_string::operator!=
bool operator!=(const opp_string &s) const
Definition: opp_string.h:155
omnetpp::opp_string::size
int size() const
Definition: opp_string.h:119
omnetpp::opp_string::operator+
opp_string operator+(const opp_string &s)
Definition: opp_string.h:180
omnetpp::opp_string::operator==
bool operator==(const opp_string &s) const
Definition: opp_string.h:150
omnetpp::opp_string::opp_string
opp_string()
Definition: opp_string.h:62
omnetpp::opp_string::operator+=
opp_string & operator+=(const std::string &s)
Definition: opp_string.h:170
omnetpp::opp_string::empty
bool empty() const
Definition: opp_string.h:107
omnetpp::opp_strcmp
int opp_strcmp(const char *s1, const char *s2)
Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string (""...
Definition: stringutil.h:88