Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | #pragma once |
| 18 | #include <android-base/stringprintf.h> |
| 19 | #include <utils/Trace.h> |
| 20 | #include <cmath> |
| 21 | #include <string> |
| 22 | |
| 23 | template <typename T> |
| 24 | class TracedOrdinal { |
| 25 | public: |
| 26 | static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()), |
| 27 | "Type is not supported. Please test it with systrace before adding " |
| 28 | "it to the list."); |
| 29 | |
Greg Kaiser | 5a01879 | 2019-07-29 07:32:35 -0700 | [diff] [blame] | 30 | TracedOrdinal(const std::string& name, T initialValue) |
Ady Abraham | 50204dd | 2019-07-19 15:47:11 -0700 | [diff] [blame] | 31 | : mName(name), |
| 32 | mNameNegative(android::base::StringPrintf("%sNegative", name.c_str())), |
| 33 | mHasGoneNegative(std::signbit(initialValue)), |
| 34 | mData(initialValue) { |
| 35 | trace(); |
| 36 | } |
| 37 | |
| 38 | operator T() const { return mData; } |
| 39 | |
| 40 | TracedOrdinal& operator=(T other) { |
| 41 | mData = other; |
| 42 | mHasGoneNegative = mHasGoneNegative || std::signbit(mData); |
| 43 | trace(); |
| 44 | return *this; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | void trace() { |
| 49 | if (!std::signbit(mData)) { |
| 50 | ATRACE_INT64(mName.c_str(), int64_t(mData)); |
| 51 | if (mHasGoneNegative) { |
| 52 | ATRACE_INT64(mNameNegative.c_str(), 0); |
| 53 | } |
| 54 | } else { |
| 55 | ATRACE_INT64(mNameNegative.c_str(), -int64_t(mData)); |
| 56 | ATRACE_INT64(mName.c_str(), 0); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | const std::string mName; |
| 61 | const std::string mNameNegative; |
| 62 | bool mHasGoneNegative; |
| 63 | T mData; |
| 64 | }; |