00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: UUID.cpp 645 2013-05-04 21:21:45Z sella $";
00010
00011 #include "UUID.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex UUID::Pattern("^[0-9a-f]{8}-?(?:[0-9a-f]{4}-?){3}[0-9a-f]{12}$", boost::regex::icase);
00016
00017 UUID::UUID() throw () : Nullable() { }
00018
00019 UUID::UUID(const std::string &value) throw (TypeCastException) : Nullable() {
00020 if (boost::regex_match(value.c_str(), UUID::Pattern)) {
00021 try {
00022 this->value = std::make_shared<sella::util::UUID>(value);
00023 } catch (sella::util::UUIDException &e) {
00024 THROW(TypeCastException, "unable to cast '%s' to a UUID value: %s", value.c_str(), e.what());
00025 }
00026 } else {
00027 THROW(TypeCastException, "unable to cast '%s' to a UUID value", value.c_str());
00028 }
00029 }
00030
00031 UUID::UUID(const char *value) throw (TypeCastException) : Nullable() {
00032 if (boost::regex_match(value, UUID::Pattern)) {
00033 this->value = std::make_shared<sella::util::UUID>(value);
00034 } else {
00035 THROW(TypeCastException, "unable to cast '%s' to a UUID value", value);
00036 }
00037 }
00038
00039 UUID::UUID(uuid_t &value) throw () : Nullable() {
00040 this->value = std::make_shared<sella::util::UUID>(value);
00041 }
00042
00043 UUID::UUID(const sella::util::UUID &value) throw () : Nullable(std::make_shared<sella::util::UUID>(value)) { }
00044
00045 UUID::UUID(const UUID &other) throw () : Nullable(other) {
00046 if (!other.isNull()) {
00047 this->value = std::make_shared<sella::util::UUID>(*other.getUUID());
00048 }
00049 }
00050
00051 UUID::~UUID() throw () { }
00052
00053 bool UUID::operator==(const UUID &other) const throw () {
00054 return (*std::static_pointer_cast<sella::util::UUID>(this->value) == *std::static_pointer_cast<sella::util::UUID>(other.value));
00055 }
00056
00057 bool UUID::operator!=(const UUID &other) const throw () {
00058 return !(*this == other);
00059 }
00060
00061 bool UUID::operator>(const UUID &other) const throw () {
00062 return (*std::static_pointer_cast<sella::util::UUID>(this->value) > *std::static_pointer_cast<sella::util::UUID>(other.value));
00063 }
00064
00065 bool UUID::operator<(const UUID &other) const throw () {
00066 return (*std::static_pointer_cast<sella::util::UUID>(this->value) < *std::static_pointer_cast<sella::util::UUID>(other.value));
00067 }
00068
00069 UUID& UUID::operator=(const UUID &other) throw () {
00070 if (this != &other) {
00071 Nullable::operator=(other);
00072 if (!other.isNull()) {
00073 this->value = std::make_shared<sella::util::UUID>(*other.getUUID());
00074 }
00075 }
00076
00077 return *this;
00078 }
00079
00080 const sella::util::UUID* UUID::getUUID() const throw () {
00081 return &(*std::static_pointer_cast<sella::util::UUID>(this->value));
00082 }
00083
00084 void UUID::shrink_to_fit() throw () {
00085 auto &u = *std::static_pointer_cast<sella::util::UUID>(this->value);
00086
00087 u.shrink_to_fit();
00088 std::string(cache.begin(), cache.end()).swap(cache);
00089 }
00090
00091 const std::string& UUID::str() const throw () {
00092 if (cache.empty()) {
00093 if (this->isNull()) {
00094 cache = "null";
00095 } else {
00096 cache = this->getUUID()->str();
00097 }
00098 }
00099
00100 return cache;
00101 }
00102
00103
00104
00105