forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is a simple string class, which automatically manages its buffer, provides assignment, comparison and conversion to char const*. git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@4981 8aca7d54-2c30-db11-9de9-000461428c89
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef PLAINSTRING_H | ||
#define PLAINSTRING_H | ||
|
||
#include <cstring> | ||
|
||
#include "../simtypes.h" | ||
|
||
|
||
class plainstring | ||
{ | ||
public: | ||
plainstring() : str_() {} | ||
|
||
plainstring(char const* const s) : str_(copy_string(s)) {} | ||
|
||
~plainstring() { delete [] str_; } | ||
|
||
plainstring& operator =(char const* const o) | ||
{ | ||
char* const s = copy_string(o); | ||
delete [] str_; | ||
str_ = s; | ||
return *this; | ||
} | ||
|
||
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 |