• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
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 MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_BASE_H
18 #define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_UUID_BASE_H
19 
20 #include <stdint.h>
21 #include <algorithm>
22 #include <sstream>
23 #include <iostream>
24 #include <iomanip>
25 #include <string>
26 #include "async/option.h"
27 
28 namespace mindspore {
29 namespace uuids {
30 const std::size_t UUID_SIZE = 16;
31 
32 struct uuid {
33  public:
34   static std::size_t Size();
35 
36   static std::string ToBytes(const uuid &u);
37 
38   static Option<unsigned char> GetValue(char c);
39 
40   static Option<uuid> FromString(const std::string &s);
41 
42   // To check whether uuid looks like 0000000-000-000-000-000000000000000
43   bool IsNilUUID() const;
44 
45   const uint8_t *Get() const;
46 
47  private:
48   const uint8_t *BeginAddress() const;
49 
50   const uint8_t *EndAddress() const;
51 
52   uint8_t *BeginAddress();
53 
54   uint8_t *EndAddress();
55 
56   friend class RandomBasedGenerator;
57   friend bool operator==(uuid const &left, uuid const &right);
58   friend bool operator!=(uuid const &left, uuid const &right);
59   template <typename T, typename F>
60   friend std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid);
61   uint8_t uuidData[UUID_SIZE];
62 };
63 
64 // operator override
65 inline bool operator==(uuid const &left, uuid const &right) {
66   return std::equal(left.BeginAddress(), left.EndAddress(), right.BeginAddress());
67 }
68 
69 // operator override
70 inline bool operator!=(uuid const &left, uuid const &right) { return !(left == right); }
71 
72 // operator override
73 template <typename T, typename F>
74 std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid) {
75   const int FIRST_DELIM_OFFSET = 3;
76   const int SECOND_DELIM_OFFSET = 5;
77   const int THIRD_DELIM_OFFSET = 7;
78   const int FOURTH_DELIM_OFFSET = 9;
79   const int UUID_WIDTH = 2;
80   s << std::hex << std::setfill(static_cast<T>('0'));
81 
82   int i = 0;
83   for (const uint8_t *ptr = outputUuid.BeginAddress(); ptr < outputUuid.EndAddress(); ++ptr, ++i) {
84     s << std::setw(UUID_WIDTH) << static_cast<int>(*ptr);
85     if (i == FIRST_DELIM_OFFSET || i == SECOND_DELIM_OFFSET || i == THIRD_DELIM_OFFSET || i == FOURTH_DELIM_OFFSET) {
86       s << '-';
87     }
88   }
89 
90   s << std::setfill(static_cast<T>(' ')) << std::dec;
91   return s;
92 }
93 }  // namespace uuids
94 }  // namespace mindspore
95 
96 #endif /* UUID_BASE_HPP_ */
97