00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: Pointer.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "Pointer.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex Pointer::Pattern("^0x[0-9A-Fa-f]{8,16}$");
00016
00017 Pointer::Pointer() throw () : Nullable() { }
00018
00019 Pointer::Pointer(const std::string &value) throw (TypeCastException) : Nullable() {
00020 if (boost::regex_match(value, Pointer::Pattern)) {
00021 void* v = 0;
00022
00023 if (scanf(value.c_str(), "%p", &v) < 1) {
00024 THROW(TypeCastException, "unable to cast '%s' to a pointer value", value.c_str());
00025 }
00026
00027 this->value = std::make_shared<void*>(v);
00028 } else {
00029 THROW(TypeCastException, "unable to cast '%s' to a pointer value", value.c_str());
00030 }
00031 }
00032
00033 Pointer::Pointer(void* value) throw () : Nullable(std::make_shared<void*>(value)) { }
00034
00035 Pointer::Pointer(const Pointer &other) throw () : Nullable(other) {
00036 if (!other.isNull()) {
00037 this->value = std::make_shared<void*>((void*) *other.getPointer());
00038 }
00039 }
00040
00041 bool Pointer::operator==(const Pointer &other) const throw () {
00042 return (*std::static_pointer_cast<void*>(this->value) == *std::static_pointer_cast<void*>(other.value));
00043 }
00044
00045 bool Pointer::operator!=(const Pointer &other) const throw () {
00046 return !(*this == other);
00047 }
00048
00049 bool Pointer::operator>(const Pointer &other) const throw () {
00050 return (*std::static_pointer_cast<void*>(this->value) > *std::static_pointer_cast<void*>(other.value));
00051 }
00052
00053 bool Pointer::operator<(const Pointer &other) const throw () {
00054 return (*std::static_pointer_cast<void*>(this->value) < *std::static_pointer_cast<void*>(other.value));
00055 }
00056
00057 bool Pointer::operator!() throw () {
00058 return !(*std::static_pointer_cast<void*>(this->value));
00059 }
00060
00061 Pointer& Pointer::operator=(const Pointer &other) throw () {
00062 if (this != &other) {
00063 Nullable::operator=(other);
00064 if (!other.isNull()) {
00065 this->value = std::make_shared<void*>((void*) *other.getPointer());
00066 }
00067 }
00068
00069 return *this;
00070 }
00071
00072 Pointer::~Pointer() throw () { }
00073
00074 const void** Pointer::getPointer() const throw (UnsupportedOperatorException) {
00075 if (isNull()) {
00076 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00077 }
00078
00079 return (const void**) &(*std::static_pointer_cast<void*>(this->value));
00080 }
00081
00082 const std::string& Pointer::str() const throw () {
00083 if (cache.empty()) {
00084 if (this->isNull()) {
00085 cache = "null";
00086 } else {
00087 char buffer[4096];
00088
00089 snprintf(buffer, sizeof(buffer), "%p", *(this->getPointer()));
00090 cache = buffer;
00091 }
00092 }
00093
00094 return cache;
00095 }
00096
00097
00098
00099