blob: 3489c43c8052502c7294dc11ed2418290f53ca5c [file] [log] [blame]
Joe Onorato9fc9edf2017-10-15 20:08:52 -07001/*
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
25namespace android {
26namespace os {
27namespace statsd {
28
29using std::hash;
30using std::ostream;
31using std::string;
32
33/**
34 * Uniquely identifies a configuration.
35 */
36class ConfigKey {
37public:
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
66private:
67 string mName;
68 int mUid;
69};
70
71inline 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.
yro87d983c2017-11-14 21:31:43 -080081 * Unfortunately this has to go in std namespace because C++ is fun!
Joe Onorato9fc9edf2017-10-15 20:08:52 -070082 */
83namespace std {
84
85using android::os::statsd::ConfigKey;
86
87template <>
88struct 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