1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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 INCLUDE_PERFETTO_EXT_BASE_UUID_H_ 18 #define INCLUDE_PERFETTO_EXT_BASE_UUID_H_ 19 20 #include <string.h> 21 #include <array> 22 #include <cstdint> 23 #include <optional> 24 #include <string> 25 26 namespace perfetto { 27 namespace base { 28 29 class Uuid { 30 public: 31 explicit Uuid(const std::string& s); 32 explicit Uuid(int64_t lsb, int64_t msb); 33 Uuid(); 34 data()35 std::array<uint8_t, 16>* data() { return &data_; } data()36 const std::array<uint8_t, 16>* data() const { return &data_; } 37 38 bool operator==(const Uuid& other) const { return data_ == other.data_; } 39 40 bool operator!=(const Uuid& other) const { return !(*this == other); } 41 42 explicit operator bool() const { return *this != Uuid(); } 43 msb()44 int64_t msb() const { 45 int64_t result; 46 memcpy(&result, data_.data() + 8, 8); 47 return result; 48 } 49 lsb()50 int64_t lsb() const { 51 int64_t result; 52 memcpy(&result, data_.data(), 8); 53 return result; 54 } 55 set_lsb_msb(int64_t lsb,int64_t msb)56 void set_lsb_msb(int64_t lsb, int64_t msb) { 57 set_lsb(lsb); 58 set_msb(msb); 59 } set_msb(int64_t msb)60 void set_msb(int64_t msb) { memcpy(data_.data() + 8, &msb, 8); } set_lsb(int64_t lsb)61 void set_lsb(int64_t lsb) { memcpy(data_.data(), &lsb, 8); } 62 63 std::string ToString() const; 64 std::string ToPrettyString() const; 65 66 private: 67 std::array<uint8_t, 16> data_{}; 68 }; 69 70 Uuid Uuidv4(); 71 72 } // namespace base 73 } // namespace perfetto 74 75 #endif // INCLUDE_PERFETTO_EXT_BASE_UUID_H_ 76