• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SRC_TRACE_PROCESSOR_TYPES_VARIADIC_H_
18 #define SRC_TRACE_PROCESSOR_TYPES_VARIADIC_H_
19 
20 #include "src/trace_processor/containers/string_pool.h"
21 
22 namespace perfetto {
23 namespace trace_processor {
24 
25 // Variadic type representing value of different possible types.
26 struct Variadic {
27   enum Type : size_t {
28     kInt,
29     kUint,
30     kString,
31     kReal,
32     kPointer,
33     kBool,
34     kJson,
35     kNull,
36     kMaxType = kNull,
37   };
38 
39   static constexpr const char* const kTypeNames[] = {
40       "int", "uint", "string", "real", "pointer", "bool", "json", "null"};
41 
IntegerVariadic42   static Variadic Integer(int64_t int_value) {
43     Variadic variadic;
44     variadic.type = Type::kInt;
45     variadic.int_value = int_value;
46     return variadic;
47   }
48 
49   // BEWARE: Unsigned 64-bit integers will be handled as signed integers by
50   // SQLite for built-in SQL operators. This variadic type is used to
51   // distinguish between int64 and uint64 for correct JSON export of TrackEvent
52   // arguments.
UnsignedIntegerVariadic53   static Variadic UnsignedInteger(uint64_t uint_value) {
54     Variadic variadic;
55     variadic.type = Type::kUint;
56     variadic.uint_value = uint_value;
57     return variadic;
58   }
59 
StringVariadic60   static Variadic String(StringPool::Id string_id) {
61     Variadic variadic;
62     variadic.type = Type::kString;
63     variadic.string_value = string_id;
64     return variadic;
65   }
66 
RealVariadic67   static Variadic Real(double real_value) {
68     Variadic variadic;
69     variadic.type = Type::kReal;
70     variadic.real_value = real_value;
71     return variadic;
72   }
73 
74   // This variadic type is used to distinguish between integers and pointer
75   // values for correct JSON export of TrackEvent arguments.
PointerVariadic76   static Variadic Pointer(uint64_t pointer_value) {
77     Variadic variadic;
78     variadic.type = Type::kPointer;
79     variadic.pointer_value = pointer_value;
80     return variadic;
81   }
82 
BooleanVariadic83   static Variadic Boolean(bool bool_value) {
84     Variadic variadic;
85     variadic.type = Type::kBool;
86     variadic.bool_value = bool_value;
87     return variadic;
88   }
89 
90   // This variadic type is used to distinguish between regular string and JSON
91   // string values for correct JSON export of TrackEvent arguments.
JsonVariadic92   static Variadic Json(StringPool::Id json_value) {
93     Variadic variadic;
94     variadic.type = Type::kJson;
95     variadic.json_value = json_value;
96     return variadic;
97   }
98 
NullVariadic99   static Variadic Null() {
100     Variadic variadic;
101     variadic.type = Type::kNull;
102     return variadic;
103   }
104 
105   // Used in tests.
106   bool operator==(const Variadic& other) const {
107     if (type == other.type) {
108       switch (type) {
109         case kInt:
110           return int_value == other.int_value;
111         case kUint:
112           return uint_value == other.uint_value;
113         case kString:
114           return string_value == other.string_value;
115         case kReal:
116           return std::equal_to<double>()(real_value, other.real_value);
117         case kPointer:
118           return pointer_value == other.pointer_value;
119         case kBool:
120           return bool_value == other.bool_value;
121         case kJson:
122           return json_value == other.json_value;
123         case kNull:
124           return true;
125       }
126     }
127     return false;
128   }
129 
130   Type type;
131   union {
132     int64_t int_value;
133     uint64_t uint_value;
134     StringPool::Id string_value;
135     double real_value;
136     uint64_t pointer_value;
137     bool bool_value;
138     StringPool::Id json_value;
139   };
140 };
141 
142 }  // namespace trace_processor
143 }  // namespace perfetto
144 
145 #endif  // SRC_TRACE_PROCESSOR_TYPES_VARIADIC_H_
146