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