An immutable string. */
| 44 | |
| 45 | /** An immutable string. */ |
| 46 | class const_string : public cstring { |
| 47 | public: |
| 48 | const_string(const std::string& s) |
| 49 | : cstring(strcpy(new char[s.size() + 1], s.c_str())) { } |
| 50 | |
| 51 | #if __GXX_EXPERIMENTAL_CXX0X__ |
| 52 | const_string(const_string&& s) : cstring(s.m_p) { s.m_p = NULL; } |
| 53 | #endif |
| 54 | |
| 55 | #if 0 |
| 56 | /* Should be like this, but... */ |
| 57 | const_string(const const_string& s) |
| 58 | : cstring(strcpy(new char[s.size() + 1], s.c_str())) { } |
| 59 | #else |
| 60 | /** Copy constructor. |
| 61 | * When a vector grows, libstdc++ calls the copy constructor for |
| 62 | * each element of the vector, which would invalidate any cstring |
| 63 | * that point to this const_string. To work around this issue, the |
| 64 | * new const_string gets the original data, and the old |
| 65 | * const_string gets the copy, which will probably be destructed |
| 66 | * soon. Making the copy is wasteful, but the C++ standard does |
| 67 | * not help us out here. |
| 68 | */ |
| 69 | const_string(const const_string& s) : cstring(s.c_str()) |
| 70 | { |
| 71 | const_cast<const_string&>(s).m_p |
| 72 | = strcpy(new char[s.size() + 1], s.c_str()); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | ~const_string() { delete[] m_p; } |
| 77 | |
| 78 | const_string& operator=(const const_string& s) |
| 79 | { |
| 80 | assert(false); |
| 81 | if (this == &s) |
| 82 | return *this; |
| 83 | assert(m_p != s.m_p); |
| 84 | delete[] m_p; |
| 85 | m_p = strcpy(new char[s.size() + 1], s.c_str()); |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | void swap(const_string& s) { std::swap(m_p, s.m_p); } |
| 90 | |
| 91 | private: |
| 92 | const_string(); |
| 93 | const_string(const char* s); |
| 94 | const_string(const cstring&); |
| 95 | bool operator==(const const_string& s); |
| 96 | }; |
| 97 | |
| 98 | namespace std { |
| 99 | template <> |