Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Tej Singh | d2e51ec | 2021-04-19 20:26:29 -0700 | [diff] [blame] | 19 | #include "src/statsd_config.pb.h" |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
| 27 | using std::hash; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 28 | using std::string; |
| 29 | |
| 30 | /** |
| 31 | * Uniquely identifies a configuration. |
| 32 | */ |
| 33 | class ConfigKey { |
| 34 | public: |
| 35 | ConfigKey(); |
Chih-Hung Hsieh | 59b0b3b | 2018-12-20 13:42:28 -0800 | [diff] [blame] | 36 | ConfigKey(const ConfigKey& that); |
Vova Sharaienko | 6c867d7 | 2024-01-04 01:40:51 +0000 | [diff] [blame] | 37 | ConfigKey(int uid, int64_t id); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 38 | ~ConfigKey(); |
| 39 | |
| 40 | inline int GetUid() const { |
| 41 | return mUid; |
| 42 | } |
Vova Sharaienko | 6c867d7 | 2024-01-04 01:40:51 +0000 | [diff] [blame] | 43 | inline int64_t GetId() const { |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 44 | return mId; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | inline bool operator<(const ConfigKey& that) const { |
| 48 | if (mUid < that.mUid) { |
| 49 | return true; |
| 50 | } |
| 51 | if (mUid > that.mUid) { |
| 52 | return false; |
| 53 | } |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 54 | return mId < that.mId; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | inline bool operator==(const ConfigKey& that) const { |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 58 | return mUid == that.mUid && mId == that.mId; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | string ToString() const; |
| 62 | |
| 63 | private: |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 64 | int64_t mId; |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 65 | int mUid; |
| 66 | }; |
| 67 | |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 68 | int64_t StrToInt64(const string& str); |
| 69 | |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 70 | } // namespace statsd |
| 71 | } // namespace os |
| 72 | } // namespace android |
| 73 | |
| 74 | /** |
| 75 | * A hash function for ConfigKey so it can be used for unordered_map/set. |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 76 | */ |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 77 | template <> |
Vova Sharaienko | db355ed | 2023-12-22 02:56:08 +0000 | [diff] [blame] | 78 | struct std::hash<android::os::statsd::ConfigKey> { |
| 79 | std::size_t operator()(const android::os::statsd::ConfigKey& key) const { |
Yangster-mac | f8faf67 | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 80 | return (7 * key.GetUid()) ^ ((hash<long long>()(key.GetId()))); |
Joe Onorato | da8cca7 | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 81 | } |
| 82 | }; |