00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: String.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "String.h"
00012
00013 using namespace sella::variant;
00014
00015 String::String() throw () : Nullable() { }
00016
00017 String::String(const std::string &value) throw () : Nullable(std::make_shared<std::string>(value)) { }
00018
00019 String::String(const String &other) throw () : Nullable(other) {
00020 if (!other.isNull()) {
00021 this->value = std::make_shared<std::string>(*other.getString());
00022 }
00023 }
00024
00025 String::~String() throw () { }
00026
00027 bool String::operator==(const String &other) const throw () {
00028 return (*std::static_pointer_cast<std::string>(this->value) == *std::static_pointer_cast<std::string>(other.value));
00029 }
00030
00031 bool String::operator!=(const String &other) const throw () {
00032 return !(*this == other);
00033 }
00034
00035 bool String::operator>(const String &other) const throw () {
00036 return (*std::static_pointer_cast<std::string>(this->value) > *std::static_pointer_cast<std::string>(other.value));
00037 }
00038
00039 bool String::operator<(const String &other) const throw () {
00040 return (*std::static_pointer_cast<std::string>(this->value) < *std::static_pointer_cast<std::string>(other.value));
00041 }
00042
00043 String& String::operator=(const String &other) throw () {
00044 if (this != &other) {
00045 Nullable::operator=(other);
00046 if (!other.isNull()) {
00047 this->value = std::make_shared<std::string>(*other.getString());
00048 }
00049 }
00050
00051 return *this;
00052 }
00053
00054 String& String::operator+=(const std::string &value) throw () {
00055 cache.clear();
00056 (*std::static_pointer_cast<std::string>(this->value)) += value;
00057
00058 return *this;
00059 }
00060
00061 const std::string* String::getString() const throw (UnsupportedOperatorException) {
00062 if (isNull()) {
00063 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00064 }
00065
00066 return &(*std::static_pointer_cast<std::string>(this->value));
00067 }
00068
00069 size_t String::size() const throw () {
00070 if (isNull()) {
00071 return -1;
00072 }
00073
00074 return getString()->size();
00075 }
00076
00077 bool String::empty() const throw () {
00078 if (isNull()) {
00079 return true;
00080 }
00081
00082 return getString()->empty();
00083 }
00084
00085 void String::shrink_to_fit() throw () {
00086 auto &s = *std::static_pointer_cast<std::string>(this->value);
00087
00088 std::string(s.begin(), s.end()).swap(s);
00089 std::string(cache.begin(), cache.end()).swap(cache);
00090 }
00091
00092 const std::string& String::str() const throw () {
00093 if (cache.empty()) {
00094 if (this->isNull()) {
00095 cache = "null";
00096 } else {
00097 cache = *(this->getString());
00098 }
00099 }
00100
00101 return cache;
00102 }
00103
00104
00105
00106