forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplainstring.h
54 lines (38 loc) · 1.13 KB
/
plainstring.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef PLAINSTRING_H
#define PLAINSTRING_H
#include <cstring>
#include "../simtypes.h"
class plainstring
{
public:
plainstring() : str_(NULL) {}
plainstring(char const* const s) : str_(copy_string(s)) {}
plainstring(const plainstring &other) : str_(copy_string(other.c_str())) {}
~plainstring() { delete [] str_; }
plainstring& operator =(char const* const o)
{
char* const s = copy_string(o);
delete [] str_;
str_ = s;
return *this;
}
plainstring& operator =(plainstring const& o) { return *this = o.str_; }
char const* c_str() const { return str_; }
operator char const*() const { return str_; }
operator char*() { return str_; }
bool operator ==(char const* const o) const { return str_ && o ? std::strcmp(str_, o) == 0 : str_ == o; }
bool operator !=(char const* const o) const { return !(*this == o); }
private:
static char* copy_string(char const* const s)
{
if (s) {
size_t const n = std::strlen(s) + 1;
return static_cast<char*>(std::memcpy(new char[n], s, n));
} else {
return 0;
}
}
char* str_;
};
void free(plainstring const&) DELETED;
#endif