• 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     kMaxType = kJson,
36   };
37 
38   static constexpr const char* const kTypeNames[] = {
39       "int", "uint", "string", "real", "pointer", "bool", "json"};
40 
IntegerVariadic41   static Variadic Integer(int64_t int_value) {
42     Variadic variadic;
43     variadic.type = Type::kInt;
44     variadic.int_value = int_value;
45     return variadic;
46   }
47 
48   // BEWARE: Unsigned 64-bit integers will be handled as signed integers by
49   // SQLite for built-in SQL operators. This variadic type is used to
50   // distinguish between int64 and uint64 for correct JSON export of TrackEvent
51   // arguments.
UnsignedIntegerVariadic52   static Variadic UnsignedInteger(uint64_t uint_value) {
53     Variadic variadic;
54     variadic.type = Type::kUint;
55     variadic.uint_value = uint_value;
56     return variadic;
57   }
58 
StringVariadic59   static Variadic String(StringPool::Id string_id) {
60     Variadic variadic;
61     variadic.type = Type::kString;
62     variadic.string_value = string_id;
63     return variadic;
64   }
65 
RealVariadic66   static Variadic Real(double real_value) {
67     Variadic variadic;
68     variadic.type = Type::kReal;
69     variadic.real_value = real_value;
70     return variadic;
71   }
72 
73   // This variadic type is used to distinguish between integers and pointer
74   // values for correct JSON export of TrackEvent arguments.
PointerVariadic75   static Variadic Pointer(uint64_t pointer_value) {
76     Variadic variadic;
77     variadic.type = Type::kPointer;
78     variadic.pointer_value = pointer_value;
79     return variadic;
80   }
81 
BooleanVariadic82   static Variadic Boolean(bool bool_value) {
83     Variadic variadic;
84     variadic.type = Type::kBool;
85     variadic.bool_value = bool_value;
86     return variadic;
87   }
88 
89   // This variadic type is used to distinguish between regular string and JSON
90   // string values for correct JSON export of TrackEvent arguments.
JsonVariadic91   static Variadic Json(StringPool::Id json_value) {
92     Variadic variadic;
93     variadic.type = Type::kJson;
94     variadic.json_value = json_value;
95     return variadic;
96   }
97 
98   // Used in tests.
99   bool operator==(const Variadic& other) const {
100     if (type == other.type) {
101       switch (type) {
102         case kInt:
103           return int_value == other.int_value;
104         case kUint:
105           return uint_value == other.uint_value;
106         case kString:
107           return string_value == other.string_value;
108         case kReal:
109           return std::equal_to<double>()(real_value, other.real_value);
110         case kPointer:
111           return pointer_value == other.pointer_value;
112         case kBool:
113           return bool_value == other.bool_value;
114         case kJson:
115           return json_value == other.json_value;
116       }
117     }
118     return false;
119   }
120 
121   Type type;
122   union {
123     int64_t int_value;
124     uint64_t uint_value;
125     StringPool::Id string_value;
126     double real_value;
127     uint64_t pointer_value;
128     bool bool_value;
129     StringPool::Id json_value;
130   };
131 };
132 
133 }  // namespace trace_processor
134 }  // namespace perfetto
135 
136 #endif  // SRC_TRACE_PROCESSOR_TYPES_VARIADIC_H_
137