RSC  0.16.0
UUID.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSC project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "UUID.h"
28 
29 #include <sstream>
30 
31 #include <boost/uuid/uuid_io.hpp>
32 
33 using namespace std;
34 
35 namespace rsc {
36 namespace misc {
37 
38 boost::uuids::nil_generator UUID::nilGen =
39  boost::uuids::nil_generator();
40 
41 boost::uuids::basic_random_generator<boost::mt19937> UUID::randomGen =
42  boost::uuids::basic_random_generator<boost::mt19937>();
43 
44 UUID::UUID(const bool& random) :
45  id(nilGen()) {
46  if (random) {
47  id = randomGen();
48  }
49 }
50 
51 UUID::UUID(const string& uuid) {
52  if (uuid != "") {
53  boost::uuids::string_generator gen;
54  id = gen(uuid);
55  }
56 }
57 
58 UUID::UUID(const char* uuid) {
59  if (uuid != string("")) {
60  boost::uuids::string_generator gen;
61  id = gen(uuid);
62  }
63 }
64 
65 UUID::UUID(boost::uint8_t* data) {
66  memcpy(id.data, data, 16);
67 }
68 
69 UUID::UUID(const UUID& ns, const string& name) :
70  id(boost::uuids::name_generator(ns.getId())(name)) {
71 }
72 
74 }
75 
76 boost::uuids::uuid UUID::getId() const {
77  return id;
78 }
79 
80 string UUID::getIdAsString() const {
81  std::stringstream ss;
82  ss << id;
83  return ss.str();
84 }
85 
86 bool UUID::operator==(const UUID& other) const {
87  return !(*this < other) && !(other < *this);
88 }
89 
90 bool UUID::operator!=(const UUID& other) const {
91  return !operator==(other);
92 }
93 
94 bool UUID::operator<(const UUID& other) const {
95  return id < other.id;
96 }
97 
98 ostream& operator<<(ostream& stream, const UUID& id) {
99  return stream << "UUID[" << id.id << "]";
100 }
101 
102 }
103 }
boost::uuids::uuid id
Definition: UUID.h:117
bool operator==(const UUID &other) const
Definition: UUID.cpp:86
STL namespace.
std::string getIdAsString() const
Returns a string representing the UUID.
Definition: UUID.cpp:80
Encapsulates the generation and handling of UUIDs.
Definition: UUID.h:46
virtual ~UUID()
Definition: UUID.cpp:73
static boost::uuids::basic_random_generator< boost::mt19937 > randomGen
Definition: UUID.h:120
bool operator!=(const UUID &other) const
Definition: UUID.cpp:90
UUID(const bool &random=true)
Creates a new UUID object that is either random or the nil UUID.
Definition: UUID.cpp:44
boost::uuids::uuid getId() const
Returns the contained UUID on boost format.
Definition: UUID.cpp:76
friend RSC_EXPORT std::ostream & operator<<(std::ostream &stream, const UUID &id)
Definition: UUID.cpp:98
bool operator<(const UUID &other) const
Definition: UUID.cpp:94