MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

AllocatedString.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2015 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef ALLOCATED_STRING_HXX
31 #define ALLOCATED_STRING_HXX
32
33 #include "StringPointer.hxx"
34
35 #include <utility>
36 #include <algorithm>
37
43 template<typename T=char>
44 class AllocatedString {
45 public:
46 typedef typename StringPointer<T>::value_type value_type;
47 typedef typename StringPointer<T>::reference_type reference_type;
48 typedef typename StringPointer<T>::const_reference_type const_reference_type;
49 typedef typename StringPointer<T>::pointer_type pointer_type;
50 typedef typename StringPointer<T>::const_pointer_type const_pointer_type;
51 typedef size_t size_type;
52
53 static constexpr value_type SENTINEL = '\0';
54
55 private:
56 pointer_type value;
57
58 explicit AllocatedString(pointer_type _value)
59 :value(_value) {}
60
61 public:
62 AllocatedString(std::nullptr_t n):value(n) {}
63
64 AllocatedString(AllocatedString &&src)
65 :value(src.Steal()) {}
66
67 ~AllocatedString() {
68 delete[] value;
69 }
70
71 static AllocatedString Donate(pointer_type value) {
72 return AllocatedString(value);
73 }
74
75 static AllocatedString Null() {
76 return nullptr;
77 }
78
79 static AllocatedString Empty() {
80 auto p = new value_type[1];
81 p[0] = SENTINEL;
82 return Donate(p);
83 }
84
85 static AllocatedString Duplicate(const_pointer_type src);
86
87 static AllocatedString Duplicate(const_pointer_type begin,
88 const_pointer_type end) {
89 auto p = new value_type[end - begin + 1];
90 *std::copy(begin, end, p) = SENTINEL;
91 return Donate(p);
92 }
93
94 static AllocatedString Duplicate(const_pointer_type begin,
95 size_type length) {
96 auto p = new value_type[length + 1];
97 *std::copy_n(begin, length, p) = SENTINEL;
98 return Donate(p);
99 }
100
101 AllocatedString &operator=(AllocatedString &&src) {
102 std::swap(value, src.value);
103 return *this;
104 }
105
106 constexpr bool operator==(std::nullptr_t) const {
107 return value == nullptr;
108 }
109
110 constexpr bool operator!=(std::nullptr_t) const {
111 return value != nullptr;
112 }
113
114 constexpr bool IsNull() const {
115 return value == nullptr;
116 }
117
118 constexpr const_pointer_type c_str() const {
119 return value;
120 }
121
122 bool empty() const {
123 return *value == SENTINEL;
124 }
125
126 reference_type operator[](size_type i) {
127 return value[i];
128 }
129
130 const reference_type operator[](size_type i) const {
131 return value[i];
132 }
133
134 pointer_type Steal() {
135 pointer_type result = value;
136 value = nullptr;
137 return result;
138 }
139
140 AllocatedString Clone() const {
141 return Duplicate(c_str());
142 }
143 };
144
145 #endif
StringPointer::reference_type
T & reference_type
Definition: StringPointer.hxx:40
AllocatedString::size_type
size_t size_type
Definition: AllocatedString.hxx:51
AllocatedString::Clone
AllocatedString Clone() const
Definition: AllocatedString.hxx:140
AllocatedString::~AllocatedString
~AllocatedString()
Definition: AllocatedString.hxx:67
AllocatedString::operator==
constexpr bool operator==(std::nullptr_t) const
Definition: AllocatedString.hxx:106
AllocatedString::empty
bool empty() const
Definition: AllocatedString.hxx:122
StringPointer::pointer_type
T * pointer_type
Definition: StringPointer.hxx:42
AllocatedString::operator[]
reference_type operator[](size_type i)
Definition: AllocatedString.hxx:126
AllocatedString::SENTINEL
static constexpr value_type SENTINEL
Definition: AllocatedString.hxx:53
AllocatedString::IsNull
constexpr bool IsNull() const
Definition: AllocatedString.hxx:114
AllocatedString::Empty
static AllocatedString Empty()
Definition: AllocatedString.hxx:79
AllocatedString::value_type
StringPointer< T >::value_type value_type
Definition: AllocatedString.hxx:46
AllocatedString::Donate
static AllocatedString Donate(pointer_type value)
Definition: AllocatedString.hxx:71
AllocatedString::const_pointer_type
StringPointer< T >::const_pointer_type const_pointer_type
Definition: AllocatedString.hxx:50
AllocatedString::Null
static AllocatedString Null()
Definition: AllocatedString.hxx:75
AllocatedString::operator!=
constexpr bool operator!=(std::nullptr_t) const
Definition: AllocatedString.hxx:110
AllocatedString::Duplicate
static AllocatedString Duplicate(const_pointer_type src)
AllocatedString
A string pointer whose memory is managed by this class.
Definition: Collate.hxx:26
StringPointer::const_pointer_type
const T * const_pointer_type
Definition: StringPointer.hxx:43
AllocatedString::Steal
pointer_type Steal()
Definition: AllocatedString.hxx:134
AllocatedString::operator=
AllocatedString & operator=(AllocatedString &&src)
Definition: AllocatedString.hxx:101
StringPointer.hxx
AllocatedString::Duplicate
static AllocatedString Duplicate(const_pointer_type begin, const_pointer_type end)
Definition: AllocatedString.hxx:87
AllocatedString::AllocatedString
AllocatedString(AllocatedString &&src)
Definition: AllocatedString.hxx:64
AllocatedString::operator[]
const reference_type operator[](size_type i) const
Definition: AllocatedString.hxx:130
AllocatedString::AllocatedString
AllocatedString(std::nullptr_t n)
Definition: AllocatedString.hxx:62
StringPointer::value_type
T value_type
Definition: StringPointer.hxx:39
AllocatedString::pointer_type
StringPointer< T >::pointer_type pointer_type
Definition: AllocatedString.hxx:49
StringPointer::const_reference_type
const T & const_reference_type
Definition: StringPointer.hxx:41
AllocatedString::c_str
constexpr const_pointer_type c_str() const
Definition: AllocatedString.hxx:118
AllocatedString::const_reference_type
StringPointer< T >::const_reference_type const_reference_type
Definition: AllocatedString.hxx:48
AllocatedString::Duplicate
static AllocatedString Duplicate(const_pointer_type begin, size_type length)
Definition: AllocatedString.hxx:94
AllocatedString::reference_type
StringPointer< T >::reference_type reference_type
Definition: AllocatedString.hxx:47