MPD  0.20.18
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:
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 
65  :value(src.Steal()) {}
66 
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 
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 
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 
141  return Duplicate(c_str());
142  }
143 };
144 
145 #endif
AllocatedString Clone() const
constexpr bool operator==(std::nullptr_t) const
bool empty() const
reference_type operator[](size_type i)
static constexpr value_type SENTINEL
constexpr bool IsNull() const
static AllocatedString Empty()
StringPointer< T >::value_type value_type
static AllocatedString Donate(pointer_type value)
StringPointer< T >::const_pointer_type const_pointer_type
static AllocatedString Null()
constexpr bool operator!=(std::nullptr_t) const
static AllocatedString Duplicate(const_pointer_type src)
A string pointer whose memory is managed by this class.
Definition: Util.hxx:29
const T * const_pointer_type
pointer_type Steal()
AllocatedString & operator=(AllocatedString &&src)
static AllocatedString Duplicate(const_pointer_type begin, const_pointer_type end)
AllocatedString(AllocatedString &&src)
const reference_type operator[](size_type i) const
AllocatedString(std::nullptr_t n)
StringPointer< T >::pointer_type pointer_type
const T & const_reference_type
constexpr const_pointer_type c_str() const
StringPointer< T >::const_reference_type const_reference_type
static AllocatedString Duplicate(const_pointer_type begin, size_type length)
StringPointer< T >::reference_type reference_type