Joe Onorato | 9fc9edf | 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 | |
| 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 20 | |
| 21 | #include <functional> |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
| 29 | using std::hash; |
| 30 | using std::ostream; |
| 31 | using std::string; |
| 32 | |
| 33 | /** |
| 34 | * Uniquely identifies a configuration. |
| 35 | */ |
| 36 | class ConfigKey { |
| 37 | public: |
| 38 | ConfigKey(); |
| 39 | explicit ConfigKey(const ConfigKey& that); |
| 40 | ConfigKey(int uid, const string& name); |
| 41 | ~ConfigKey(); |
| 42 | |
| 43 | inline int GetUid() const { |
| 44 | return mUid; |
| 45 | } |
| 46 | inline const string& GetName() const { |
| 47 | return mName; |
| 48 | } |
| 49 | |
| 50 | inline bool operator<(const ConfigKey& that) const { |
| 51 | if (mUid < that.mUid) { |
| 52 | return true; |
| 53 | } |
| 54 | if (mUid > that.mUid) { |
| 55 | return false; |
| 56 | } |
| 57 | return mName < that.mName; |
| 58 | }; |
| 59 | |
| 60 | inline bool operator==(const ConfigKey& that) const { |
| 61 | return mUid == that.mUid && mName == that.mName; |
| 62 | }; |
| 63 | |
| 64 | string ToString() const; |
| 65 | |
| 66 | private: |
| 67 | string mName; |
| 68 | int mUid; |
| 69 | }; |
| 70 | |
| 71 | inline ostream& operator<<(ostream& os, const ConfigKey& config) { |
| 72 | return os << config.ToString(); |
| 73 | } |
| 74 | |
| 75 | } // namespace statsd |
| 76 | } // namespace os |
| 77 | } // namespace android |
| 78 | |
| 79 | /** |
| 80 | * A hash function for ConfigKey so it can be used for unordered_map/set. |
yro | 87d983c | 2017-11-14 21:31:43 -0800 | [diff] [blame] | 81 | * Unfortunately this has to go in std namespace because C++ is fun! |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 82 | */ |
| 83 | namespace std { |
| 84 | |
| 85 | using android::os::statsd::ConfigKey; |
| 86 | |
| 87 | template <> |
| 88 | struct hash<ConfigKey> { |
| 89 | std::size_t operator()(const ConfigKey& key) const { |
| 90 | return (7 * key.GetUid()) ^ ((hash<string>()(key.GetName()))); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | } // namespace std |