ConfigKey.h revision 20e9e6231a1aba79b4e5ae47f3ccfb066920e60f
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 <string> 22 23namespace android { 24namespace os { 25namespace statsd { 26 27using std::hash; 28using std::string; 29 30/** 31 * Uniquely identifies a configuration. 32 */ 33class ConfigKey { 34public: 35 ConfigKey(); 36 explicit ConfigKey(const ConfigKey& that); 37 ConfigKey(int uid, const int64_t& id); 38 ~ConfigKey(); 39 40 inline int GetUid() const { 41 return mUid; 42 } 43 inline const int64_t& GetId() const { 44 return mId; 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 } 54 return mId < that.mId; 55 }; 56 57 inline bool operator==(const ConfigKey& that) const { 58 return mUid == that.mUid && mId == that.mId; 59 }; 60 61 string ToString() const; 62 63private: 64 int64_t mId; 65 int mUid; 66}; 67 68int64_t StrToInt64(const string& str); 69 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. 76 * Unfortunately this has to go in std namespace because C++ is fun! 77 */ 78namespace std { 79 80using android::os::statsd::ConfigKey; 81 82template <> 83struct hash<ConfigKey> { 84 std::size_t operator()(const ConfigKey& key) const { 85 return (7 * key.GetUid()) ^ ((hash<long long>()(key.GetId()))); 86 } 87}; 88 89} // namespace std 90