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 31 const std::size_t UUID_SIZE = 16; 32 33 struct uuid { 34 public: 35 static std::size_t Size(); 36 37 static std::string ToBytes(const uuid &u); 38 39 static Option<uuid> FromBytes(const std::string &s); 40 41 static Option<unsigned char> GetValue(char c); 42 43 static Option<uuid> FromString(const std::string &s); 44 45 // To check whether uuid looks like 0000000-000-000-000-000000000000000 46 bool IsNilUUID() const; 47 48 const uint8_t *Get() const; 49 50 private: 51 const uint8_t *BeginAddress() const; 52 53 const uint8_t *EndAddress() const; 54 55 uint8_t *BeginAddress(); 56 57 uint8_t *EndAddress(); 58 59 friend class RandomBasedGenerator; 60 friend bool operator==(uuid const &left, uuid const &right); 61 friend bool operator!=(uuid const &left, uuid const &right); 62 template <typename T, typename F> 63 friend std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid); 64 uint8_t uuidData[UUID_SIZE]; 65 }; 66 67 class RandomBasedGenerator { 68 public: 69 static uuid GenerateRandomUuid(); 70 }; 71 72 // operator override 73 inline bool operator==(uuid const &left, uuid const &right) { 74 return std::equal(left.BeginAddress(), left.EndAddress(), right.BeginAddress()); 75 } 76 77 // operator override 78 inline bool operator!=(uuid const &left, uuid const &right) { return !(left == right); } 79 80 // operator override 81 template <typename T, typename F> 82 std::basic_ostream<T, F> &operator<<(std::basic_ostream<T, F> &s, const struct uuid &outputUuid) { 83 const int FIRST_DELIM_OFFSET = 3; 84 const int SECOND_DELIM_OFFSET = 5; 85 const int THIRD_DELIM_OFFSET = 7; 86 const int FOURTH_DELIM_OFFSET = 9; 87 const int UUID_WIDTH = 2; 88 s << std::hex << std::setfill(static_cast<T>('0')); 89 90 int i = 0; 91 for (const uint8_t *ptr = outputUuid.BeginAddress(); ptr < outputUuid.EndAddress(); ++ptr, ++i) { 92 s << std::setw(UUID_WIDTH) << static_cast<int>(*ptr); 93 if (i == FIRST_DELIM_OFFSET || i == SECOND_DELIM_OFFSET || i == THIRD_DELIM_OFFSET || i == FOURTH_DELIM_OFFSET) { 94 s << '-'; 95 } 96 } 97 98 s << std::setfill(static_cast<T>(' ')) << std::dec; 99 return s; 100 } 101 102 } // namespace uuids 103 } // namespace mindspore 104 105 #endif /* UUID_BASE_HPP_ */ 106