uuid.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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]; // UUID is 36 bytes + NULL terminator
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 } // namespace rve_common
00147 


rve_common
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:30:49