Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "rve_common/uuid.h"
00031
00032 #include <rve_msgs/UUID.h>
00033
00034 #include <uuid/uuid.h>
00035
00036 #include <ros/assert.h>
00037
00038 #include <boost/cstdint.hpp>
00039 #include <boost/functional/hash.hpp>
00040
00041 namespace rve_common
00042 {
00043
00044 ROS_STATIC_ASSERT(sizeof(uuid_t) == 16);
00045 ROS_STATIC_ASSERT(sizeof(UUID) == sizeof(uuid_t));
00046
00047 UUID UUID::Null;
00048
00049 UUID UUID::generate()
00050 {
00051 uuid_t native;
00052 uuid_generate(native);
00053
00054 UUID uuid;
00055 memcpy(&uuid, native, sizeof(uuid));
00056
00057 return uuid;
00058 }
00059
00060 UUID::UUID()
00061 {
00062 data_.assign(0);
00063 }
00064
00065 UUID::UUID(const rve_msgs::UUID& rhs)
00066 {
00067 *this = rhs;
00068 }
00069
00070 bool UUID::isNull() const
00071 {
00072 return *this == Null;
00073 }
00074
00075 bool UUID::operator<(const UUID& rhs) const
00076 {
00077 return memcmp(this, &rhs, sizeof(rhs)) < 0;
00078 }
00079
00080 bool UUID::operator==(const UUID& rhs) const
00081 {
00082 return memcmp(this, &rhs, sizeof(rhs)) == 0;
00083 }
00084
00085 UUID& UUID::operator=(const rve_msgs::UUID& rhs)
00086 {
00087 std::copy(rhs.data.begin(), rhs.data.end(), data_.begin());
00088
00089 return *this;
00090 }
00091
00092 std::string UUID::toString() const
00093 {
00094 uuid_t native;
00095 memcpy(native, this, sizeof(native));
00096 char buf[37];
00097 uuid_unparse(native, buf);
00098 return std::string(buf, 36);
00099 }
00100
00101 void UUID::fromString(const std::string& str)
00102 {
00103 if (str.size() != 36)
00104 {
00105 return;
00106 }
00107
00108 uuid_t native;
00109 if (uuid_parse(str.c_str(), native) == 0)
00110 {
00111 memcpy(this, native, sizeof(*this));
00112 }
00113 }
00114
00115 UUID::operator rve_msgs::UUID() const
00116 {
00117 rve_msgs::UUID msg;
00118 std::copy(data_.begin(), data_.end(), msg.data.begin());
00119 return msg;
00120 }
00121
00122 std::ostream& operator<<(std::ostream& o, const UUID& u)
00123 {
00124 o << u.toString();
00125 return o;
00126 }
00127
00128 std::istream& operator>>(std::istream& i, UUID& u)
00129 {
00130 char buf[37];
00131 i.get(buf, 36);
00132 buf[36] = 0;
00133 u.fromString(buf);
00134
00135 return i;
00136 }
00137
00138 std::size_t hash_value(const UUID &uuid)
00139 {
00140 std::size_t seed = 0;
00141 boost::hash_combine( seed, *(boost::uint64_t*) &uuid );
00142 boost::hash_combine( seed, *(((boost::uint64_t*) &uuid)+1) );
00143 return seed;
00144 }
00145
00146 }
00147