impl/unique_id.h
Go to the documentation of this file.
1 /* -*- mode: C++ -*- */
2 /*********************************************************************
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (C) 2012 Jack O'Quin
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of the author nor other contributors may be
19 * used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *********************************************************************/
35 
36 #ifndef _IMPL_UNIQUE_ID_H_
37 #define _IMPL_UNIQUE_ID_H_ 1
38 
46 #include <ros/ros.h>
47 
48 #include <boost/uuid/uuid_generators.hpp>
49 
50 namespace unique_id
51 {
52 
54  namespace impl
55  {
56  /* Instantiate boost string UUID generator. */
57  static boost::uuids::string_generator genString;
58 
59  /* Instantiate boost random UUID generator. */
60  static boost::uuids::random_generator genRandom;
61 
62  /* RFC 4122 namespace for URL identifiers. */
63  static const std::string url_namespace =
64  "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
65 
66  /* RFC 4122 UUID for URL identifiers. */
68  genString(url_namespace);
69 
70  /* Instantiate boost URL name UUID generator. */
71  static boost::uuids::name_generator genURL(url_namespace_uuid);
72 
73  /* Generate RFC4122 Version 1 Time-Based UUID */
74  static boost::uuids::uuid genTime(ros::Time uuid_time, uint64_t hw_addr)
75  {
76  // Get no. of 100 nanosecond intervals since 00:00:00 October 15, 1582
77  // The RFC4122 standard only requires the least significant 60 bits for timestamp
78 
79  // No. of 100 ns intervals between 00:00:00 Oct 15, 1582 and 00:00:00 Jan 1, 1970
80  uint64_t offset = 122192928000000000;
81 
82  uint64_t num_hundred_epoch = static_cast<uint64_t>(uuid_time.sec) / (1e-9 * 100) +
83  static_cast<uint64_t>(uuid_time.nsec) / 100;
84  // Note: Adding offset in the same line as the above statement may yield incorrect
85  // values on some systems
86  uint64_t num_hundred_rfc = num_hundred_epoch + offset;
87 
88  uint32_t time_low = static_cast<uint32_t>(num_hundred_rfc);
89  uint16_t time_mid = static_cast<uint16_t>(num_hundred_rfc >> 32);
90  uint16_t version = 0x1000;
91  uint16_t time_hi_and_version = static_cast<uint16_t>(num_hundred_rfc >> (32 + 16)) | version;
92 
93  // Generate Clock Sequence ID based on random number
94  uint16_t clock_seq_and_reserved = (rand() % (1 << 14)) | 0x8000;
95 
96  boost::uuids::uuid uu = {
97  static_cast<uint8_t>(time_low >> 24), static_cast<uint8_t>(time_low >> 16),
98  static_cast<uint8_t>(time_low >> 8), static_cast<uint8_t>(time_low),
99  static_cast<uint8_t>(time_mid >> 8), static_cast<uint8_t>(time_mid),
100  static_cast<uint8_t>(time_hi_and_version >> 8),
101  static_cast<uint8_t>(time_hi_and_version),
102  static_cast<uint8_t>(clock_seq_and_reserved >> 8),
103  static_cast<uint8_t>(clock_seq_and_reserved),
104  static_cast<uint8_t>(hw_addr >> 40), static_cast<uint8_t>(hw_addr >> 32),
105  static_cast<uint8_t>(hw_addr >> 24), static_cast<uint8_t>(hw_addr >> 16),
106  static_cast<uint8_t>(hw_addr >> 8), static_cast<uint8_t>(hw_addr)
107  };
108  return uu;
109  }
110 
111  } // end namespace impl
112 
113 } // end namespace unique_id
114 
115 #endif // _IMPL_UNIQUE_ID_H_
static const boost::uuids::uuid url_namespace_uuid
C++ namespace for unique_id helper functions.
static boost::uuids::random_generator genRandom
static boost::uuids::uuid genTime(ros::Time uuid_time, uint64_t hw_addr)
static boost::uuids::string_generator genString
static const std::string url_namespace
boost::uuids::uuid uuid
static boost::uuids::name_generator genURL(url_namespace_uuid)


unique_id
Author(s): Jack O'Quin
autogenerated on Sat Dec 12 2020 03:08:50