diff options
Diffstat (limited to 'cpp/src/qpid/messaging')
| -rw-r--r-- | cpp/src/qpid/messaging/Uuid.cpp | 140 | ||||
| -rw-r--r-- | cpp/src/qpid/messaging/Variant.cpp | 28 |
2 files changed, 168 insertions, 0 deletions
diff --git a/cpp/src/qpid/messaging/Uuid.cpp b/cpp/src/qpid/messaging/Uuid.cpp new file mode 100644 index 0000000000..87eb34456f --- /dev/null +++ b/cpp/src/qpid/messaging/Uuid.cpp @@ -0,0 +1,140 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +#include "qpid/messaging/Uuid.h" +#include "qpid/sys/uuid.h" +#include <sstream> +#include <iostream> +#include <string.h> + +namespace qpid { +namespace messaging { + +using namespace std; + +const size_t Uuid::SIZE=16; +static const size_t UNPARSED_SIZE=36; + +Uuid::Uuid(bool unique) +{ + if (unique) { + generate(); + } else { + clear(); + } +} + +Uuid::Uuid(const Uuid& other) +{ + ::memcpy(bytes, other.bytes, Uuid::SIZE); +} + +Uuid::Uuid(const unsigned char* uuid) +{ + ::memcpy(bytes, uuid, Uuid::SIZE); +} + +Uuid& Uuid::operator=(const Uuid& other) +{ + if (this == &other) return *this; + ::memcpy(bytes, other.bytes, Uuid::SIZE); + return *this; +} + +void Uuid::generate() +{ + uuid_generate(bytes); +} + +void Uuid::clear() +{ + uuid_clear(bytes); +} + +// Force int 0/!0 to false/true; avoids compile warnings. +bool Uuid::isNull() const +{ + return !!uuid_is_null(bytes); +} + +Uuid::operator bool() const { return !isNull(); } +bool Uuid::operator!() const { return isNull(); } + +size_t Uuid::size() const { return SIZE; } + +const unsigned char* Uuid::data() const +{ + return bytes; +} + +bool operator==(const Uuid& a, const Uuid& b) +{ + return uuid_compare(a.bytes, b.bytes) == 0; +} + +bool operator!=(const Uuid& a, const Uuid& b) +{ + return !(a == b); +} + +bool operator<(const Uuid& a, const Uuid& b) +{ + return uuid_compare(a.bytes, b.bytes) < 0; +} + +bool operator>(const Uuid& a, const Uuid& b) +{ + return uuid_compare(a.bytes, b.bytes) > 0; +} + +bool operator<=(const Uuid& a, const Uuid& b) +{ + return uuid_compare(a.bytes, b.bytes) <= 0; +} + +bool operator>=(const Uuid& a, const Uuid& b) +{ + return uuid_compare(a.bytes, b.bytes) >= 0; +} + +ostream& operator<<(ostream& out, Uuid uuid) +{ + char unparsed[UNPARSED_SIZE + 1]; + uuid_unparse(uuid.bytes, unparsed); + return out << unparsed; +} + +istream& operator>>(istream& in, Uuid& uuid) +{ + char unparsed[UNPARSED_SIZE + 1] = {0}; + in.get(unparsed, sizeof(unparsed)); + if (uuid_parse(unparsed, uuid.bytes) != 0) + in.setstate(ios::failbit); + return in; +} + +std::string Uuid::str() const +{ + std::ostringstream os; + os << *this; + return os.str(); +} + +}} // namespace qpid::messaging diff --git a/cpp/src/qpid/messaging/Variant.cpp b/cpp/src/qpid/messaging/Variant.cpp index 9c2f92f47a..116018f797 100644 --- a/cpp/src/qpid/messaging/Variant.cpp +++ b/cpp/src/qpid/messaging/Variant.cpp @@ -52,6 +52,7 @@ class VariantImpl VariantImpl(const std::string&); VariantImpl(const Variant::Map&); VariantImpl(const Variant::List&); + VariantImpl(const Uuid&); ~VariantImpl(); VariantType getType() const; @@ -68,6 +69,7 @@ class VariantImpl float asFloat() const; double asDouble() const; std::string asString() const; + Uuid asUuid() const; const Variant::Map& asMap() const; Variant::Map& asMap(); @@ -130,6 +132,7 @@ VariantImpl::VariantImpl(double d) : type(VAR_DOUBLE) { value.d = d; } VariantImpl::VariantImpl(const std::string& s) : type(VAR_STRING) { value.v = new std::string(s); } VariantImpl::VariantImpl(const Variant::Map& m) : type(VAR_MAP) { value.v = new Variant::Map(m); } VariantImpl::VariantImpl(const Variant::List& l) : type(VAR_LIST) { value.v = new Variant::List(l); } +VariantImpl::VariantImpl(const Uuid& u) : type(VAR_UUID) { value.v = new Uuid(u); } VariantImpl::~VariantImpl() { switch (type) { @@ -142,6 +145,9 @@ VariantImpl::~VariantImpl() { case VAR_LIST: delete reinterpret_cast<Variant::List*>(value.v); break; + case VAR_UUID: + delete reinterpret_cast<Uuid*>(value.v); + break; default: break; } @@ -312,11 +318,19 @@ std::string VariantImpl::asString() const case VAR_DOUBLE: return boost::lexical_cast<std::string>(value.d); case VAR_FLOAT: return boost::lexical_cast<std::string>(value.f); case VAR_STRING: return *reinterpret_cast<std::string*>(value.v); + case VAR_UUID: return reinterpret_cast<Uuid*>(value.v)->str(); case VAR_LIST: return toString(asList()); case VAR_MAP: return toString(asMap()); default: throw InvalidConversion(QPID_MSG("Cannot convert from " << getTypeName(type) << " to " << getTypeName(VAR_STRING))); } } +Uuid VariantImpl::asUuid() const +{ + switch(type) { + case VAR_UUID: return *reinterpret_cast<Uuid*>(value.v); + default: throw InvalidConversion(QPID_MSG("Cannot convert from " << getTypeName(type) << " to " << getTypeName(VAR_UUID))); + } +} bool VariantImpl::isEqualTo(VariantImpl& other) const { @@ -336,6 +350,8 @@ bool VariantImpl::isEqualTo(VariantImpl& other) const case VAR_FLOAT: return value.f == other.value.f; case VAR_STRING: return *reinterpret_cast<std::string*>(value.v) == *reinterpret_cast<std::string*>(other.value.v); + case VAR_UUID: return *reinterpret_cast<Uuid*>(value.v) + == *reinterpret_cast<Uuid*>(other.value.v); case VAR_LIST: return equal(asList(), other.asList()); case VAR_MAP: return equal(asMap(), other.asMap()); } @@ -412,6 +428,7 @@ std::string VariantImpl::getTypeName(VariantType type) const case VAR_STRING: return "string"; case VAR_MAP: return "map"; case VAR_LIST: return "list"; + case VAR_UUID: return "uuid"; } return "<unknown>";//should never happen } @@ -433,6 +450,7 @@ VariantImpl* VariantImpl::create(const Variant& v) case VAR_STRING: return new VariantImpl(v.asString()); case VAR_MAP: return new VariantImpl(v.asMap()); case VAR_LIST: return new VariantImpl(v.asList()); + case VAR_UUID: return new VariantImpl(v.asUuid()); default: return new VariantImpl(); } } @@ -454,6 +472,7 @@ Variant::Variant(const char* s) : impl(new VariantImpl(std::string(s))) {} Variant::Variant(const Map& m) : impl(new VariantImpl(m)) {} Variant::Variant(const List& l) : impl(new VariantImpl(l)) {} Variant::Variant(const Variant& v) : impl(VariantImpl::create(v)) {} +Variant::Variant(const Uuid& u) : impl(new VariantImpl(u)) {} Variant::~Variant() { if (impl) delete impl; } @@ -548,6 +567,13 @@ Variant& Variant::operator=(const char* s) return *this; } +Variant& Variant::operator=(const Uuid& u) +{ + if (impl) delete impl; + impl = new VariantImpl(u); + return *this; +} + Variant& Variant::operator=(const Map& m) { if (impl) delete impl; @@ -583,6 +609,7 @@ int64_t Variant::asInt64() const { return impl->asInt64(); } float Variant::asFloat() const { return impl->asFloat(); } double Variant::asDouble() const { return impl->asDouble(); } std::string Variant::asString() const { return impl->asString(); } +Uuid Variant::asUuid() const { return impl->asUuid(); } const Variant::Map& Variant::asMap() const { return impl->asMap(); } Variant::Map& Variant::asMap() { return impl->asMap(); } const Variant::List& Variant::asList() const { return impl->asList(); } @@ -604,6 +631,7 @@ Variant::operator int64_t() const { return asInt64(); } Variant::operator float() const { return asFloat(); } Variant::operator double() const { return asDouble(); } Variant::operator const char*() const { return asString().c_str(); } +Variant::operator Uuid() const { return asUuid(); } std::ostream& operator<<(std::ostream& out, const Variant::Map& map) { |
