libutil++  1.9.3
 All Classes Functions Variables
String.cpp
1 /*
2 ** libutil++
3 ** $Id: String.cpp 1702 2016-08-13 23:26:06Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: String.cpp 1702 2016-08-13 23:26:06Z sella $";
10 
11 #include "String.h"
12 #include "../util/CommonMacro.h"
13 
14 using namespace sella::variant;
15 
16 String::String() throw () : Nullable(std::make_shared<std::string>()) { }
17 
18 String::String(const std::string &value) throw () : Nullable(std::make_shared<std::string>(value)) { }
19 
20 String::String(std::string &&value) throw () : Nullable(std::make_shared<std::string>(std::move(value))) { }
21 
22 String::String(const String &other) throw () : Nullable() {
23  if (LIKELY(this != &other)) {
24  if (LIKELY(!other.isNull())) {
25  if (UNLIKELY(isNull())) {
26  this->value = std::make_shared<std::string>(other.get());
27  } else {
28  *std::static_pointer_cast<std::string>(this->value) = *std::static_pointer_cast<std::string>(other.value);
29  }
30  } else {
31  this->value.reset();
32  }
33  }
34 }
35 
36 String::String(String &&other) throw () : Nullable() {
37  if (LIKELY(this != &other)) {
38  if (LIKELY(!other.isNull())) {
39  this->value = std::move(other.value);
40  } else {
41  this->value.reset();
42  }
43  }
44 }
45 
46 String::~String() throw () { }
47 
48 bool String::operator==(const String &other) const throw () {
49  return (*std::static_pointer_cast<std::string>(this->value) == *std::static_pointer_cast<std::string>(other.value));
50 }
51 
52 bool String::operator!=(const String &other) const throw () {
53  return !(*this == other);
54 }
55 
56 bool String::operator>(const String &other) const throw () {
57  return (*std::static_pointer_cast<std::string>(this->value) > *std::static_pointer_cast<std::string>(other.value));
58 }
59 
60 bool String::operator<(const String &other) const throw () {
61  return (*std::static_pointer_cast<std::string>(this->value) < *std::static_pointer_cast<std::string>(other.value));
62 }
63 
64 String& String::operator=(const String &other) throw () {
65  if (LIKELY(this != &other)) {
66  if (LIKELY(!other.isNull())) {
67  if (UNLIKELY(isNull())) {
68  this->value = std::make_shared<std::string>(other.get());
69  } else {
70  *std::static_pointer_cast<std::string>(this->value) = *std::static_pointer_cast<std::string>(other.value);
71  }
72  } else {
73  this->value.reset();
74  }
75  }
76 
77  return *this;
78 }
79 
80 String& String::operator=(String &&other) throw () {
81  if (LIKELY(this != &other)) {
82  this->value = std::move(other.value);
83  }
84 
85  return *this;
86 }
87 
88 String& String::operator=(const std::string &other) throw () {
89  if (UNLIKELY(isNull())) {
90  this->value = std::make_shared<std::string>(other);
91  } else {
92  *std::static_pointer_cast<std::string>(this->value) = other;
93  }
94 
95  return *this;
96 }
97 
98 String& String::operator=(const char *other) throw () {
99  if (UNLIKELY(isNull())) {
100  this->value = std::make_shared<std::string>(other);
101  } else {
102  *std::static_pointer_cast<std::string>(this->value) = other;
103  }
104 
105  return *this;
106 }
107 
108 String& String::operator+=(const std::string &value) throw () {
109  (*std::static_pointer_cast<std::string>(this->value)) += value;
110 
111  return *this;
112 }
113 
114 const std::string& String::get() const throw (UnsupportedOperatorException) {
115  if (UNLIKELY(isNull())) {
116  THROW(UnsupportedOperatorException, "unable to take reference of null value");
117  }
118 
119  return *std::static_pointer_cast<std::string>(this->value);
120 }
121 
122 std::string& String::get() throw (UnsupportedOperatorException) {
123  if (UNLIKELY(isNull())) {
124  THROW(UnsupportedOperatorException, "unable to take reference of null value");
125  }
126 
127  return *std::static_pointer_cast<std::string>(this->value);
128 }
129 
130 size_t String::size() const throw () {
131  if (UNLIKELY(isNull())) {
132  return 0;
133  }
134 
135  return get().size();
136 }
137 
138 bool String::empty() const throw () {
139  if (UNLIKELY(isNull())) {
140  return true;
141  }
142 
143  return get().empty();
144 }
145 
146 void String::clear() throw() {
147  if (UNLIKELY(isNull())) {
148  this->value = std::make_shared<std::string>();
149  } else {
150  std::static_pointer_cast<std::string>(this->value)->clear();
151  }
152 }
153 
154 void String::shrink_to_fit() throw () {
155  if (LIKELY(!isNull())) {
156  auto &s = *std::static_pointer_cast<std::string>(this->value);
157 
158  std::string(s.begin(), s.end()).swap(s);
159  }
160 }
161 
162 void String::msgpack(sella::util::MessagePack &mpack, uint32_t &offset) const throw (TypeCastException) {
163  uint32_t c;
164 
165  if (UNLIKELY(isNull())) {
166  c = mpack.append(offset);
167  } else {
168  c = mpack.append(offset, *std::static_pointer_cast<std::string>(this->value));
169  }
170 
171  offset += c;
172 
173  if (UNLIKELY(c == 0)) {
174  THROW(TypeCastException, "Failed to append string '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
175  }
176 }
177 
178 void String::msgpackBufferSize(size_t &size) const throw () {
179  if (UNLIKELY(isNull())) {
180  size += 1;
181 
182  return;
183  }
184 
185  size += 5 + std::static_pointer_cast<std::string>(this->value)->size();
186 }
187 
188 std::string String::str(int type) const throw () {
189  if (UNLIKELY(isNull())) {
190  return "null";
191  }
192 
193  return this->get();
194 }
195 
196 bool String::isType(const char *value, size_t size) throw () {
197  return (size > 1 && value[0] == '"' && value[size - 1] == '"');
198 }
199 
200 /*
201 ** vim: noet ts=3 sw=3
202 */