client-cpp  0.6.3
UuidGenerator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef UUIDGENERATOR_HPP_
18 #define UUIDGENERATOR_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #if defined(KAA_USE_EVENTS) || defined(KAA_USE_LOGGING)
23 
24 #include <string>
25 #include <sstream>
26 #include <boost/uuid/uuid.hpp>
27 #include <boost/uuid/uuid_io.hpp>
28 #include <boost/uuid/name_generator.hpp>
29 #include <boost/uuid/random_generator.hpp>
30 
31 namespace kaa {
32 
33 class UuidGenerator {
34 public:
35  static std::string generateUuid() {
36  boost::uuids::basic_random_generator<boost::mt19937> gen;
37  boost::uuids::uuid uuid = gen();
38  std::stringstream ss;
39  ss << uuid;
40  return ss.str();
41  }
42 
43  static std::int32_t generateRandomInt() {
44  boost::uuids::basic_random_generator<boost::mt19937> gen;
45  boost::uuids::uuid uuid = gen();
46  std::int32_t rand = (uuid.data[12] << 24) | (uuid.data[13] << 16)
47  | (uuid.data[14] << 8) | (uuid.data[15]);
48  return rand;
49  }
50 
51  static void generateUuid(std::string& uuid_s) {
52  boost::uuids::basic_random_generator<boost::mt19937> gen;
53  boost::uuids::uuid uuid = gen();
54  std::stringstream ss;
55  ss << uuid;
56  uuid_s.assign(ss.str());
57  }
58 
59  static void generateUuid(std::string& uuid_s, std::string data) {
60  namespace buuids = boost::uuids;
61  buuids::uuid seed;
62  buuids::name_generator generator(seed);
63  buuids::uuid uuid = generator(data);
64 
65  std::stringstream ss;
66  ss << uuid;
67  uuid_s.assign(ss.str());
68  }
69 };
70 
71 } // namespace kaa
72 
73 #endif
74 
75 #endif /* UUIDGENERATOR_HPP_ */