opp_string.h Source File

API: opp_string.h Source File
API
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
25namespace omnetpp {
26
39class 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
181
185 opp_string operator+(const std::string& s) {return operator+(s.c_str());}
186
187};
188
189inline std::ostream& operator<<(std::ostream& out, const opp_string& s)
190{
191 out << s.c_str(); return out;
192}
193
194
203class 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
219class 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
const char * c_str() const
Definition opp_string.h:97
opp_string & operator=(const opp_string &s)
Definition opp_string.h:135
opp_string()
Definition opp_string.h:62
bool operator!=(const opp_string &s) const
Definition opp_string.h:155
opp_string(const char *s)
Definition opp_string.h:67
bool operator<(const opp_string &s) const
Definition opp_string.h:145
bool empty() const
Definition opp_string.h:107
opp_string(opp_string &&s)
Definition opp_string.h:87
opp_string(const std::string &s)
Definition opp_string.h:77
char * buffer()
Definition opp_string.h:114
opp_string & operator+=(const std::string &s)
Definition opp_string.h:170
opp_string operator+(const char *s)
Definition opp_string.h:175
char * reserve(unsigned size)
Definition opp_string.h:124
opp_string(const char *s, int n)
Definition opp_string.h:72
opp_string operator+(const opp_string &s)
Definition opp_string.h:180
const char * operator=(const char *s)
Definition opp_string.h:130
opp_string & operator=(const std::string &s)
Definition opp_string.h:140
opp_string operator+(const std::string &s)
Definition opp_string.h:185
bool operator==(const opp_string &s) const
Definition opp_string.h:150
opp_string & operator+=(const opp_string &s)
Definition opp_string.h:165
std::string str() const
Definition opp_string.h:102
opp_string(const opp_string &s)
Definition opp_string.h:82
opp_string & operator+=(const char *s)
Definition opp_string.h:160
int size() const
Definition opp_string.h:119
~opp_string()
Definition opp_string.h:92