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