Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unique-id.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Lukasz Kostyra <l.kostyra@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef CARGO_IPC_UNIQUE_ID_HPP
26 #define CARGO_IPC_UNIQUE_ID_HPP
27 
28 #include <ostream>
29 #include <chrono>
30 #include <uuid/uuid.h>
31 #include <time.h>
32 
33 namespace cargo {
34 namespace ipc {
35 
36 class UniqueID {
37 public:
38  typedef struct timespec TimestampType;
39  typedef uuid_t UUIDType;
40 
44  UniqueID();
45 
49  void generate();
50 
57  bool operator==(const UniqueID& other) const;
58 
62  operator std::string() const;
63 
67  friend std::ostream& operator<<(std::ostream& str, const UniqueID& id);
68 
69 
72 };
73 
74 template <typename T>
75 class hash;
76 
77 template <>
78 class hash<cargo::ipc::UniqueID>
79 {
80 public:
81  std::size_t operator()(const cargo::ipc::UniqueID& id) const
82  {
83  char uuid[37]; // 36 chars for UUID + terminating zero
84  ::uuid_unparse(id.mUUID, uuid);
85 
86  // STL does not provide correct hash implementation for char *
87  // Instead, just convert it to string
88  std::string uuids(uuid);
89 
90  return std::hash<time_t>()(id.mTime.tv_sec)
91  ^ std::hash<long>()(id.mTime.tv_nsec)
92  ^ std::hash<std::string>()(uuids);
93  }
94 };
95 
96 } // namespace ipc
97 } // namespace cargo
98 
99 #endif // CARGO_IPC_UNIQUE_ID_HPP
UniqueID()
Default constructor.
Definition: unique-id.cpp:30
friend std::ostream & operator<<(std::ostream &str, const UniqueID &id)
Overloaded << operator for debugging purposes.
Definition: unique-id.cpp:57
Definition: unique-id.hpp:75
Definition: unique-id.hpp:36
TimestampType mTime
timestamp when generate() was called
Definition: unique-id.hpp:70
bool operator==(const UniqueID &other) const
Compare two IDs.
Definition: unique-id.cpp:43
struct timespec TimestampType
Definition: unique-id.hpp:38
uuid_t UUIDType
Definition: unique-id.hpp:39
UUIDType mUUID
random UUID generated with libuuid
Definition: unique-id.hpp:71
void generate()
Generate new timestamp and UUID pair.
Definition: unique-id.cpp:37
std::size_t operator()(const cargo::ipc::UniqueID &id) const
Definition: unique-id.hpp:81