blob: 3ad0eed3f2b92c37d041c63ba339ddc09893f051 [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);
Yangster-mac94e197c2018-01-02 16:03:03 -080040 ConfigKey(int uid, const int64_t& id);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070041 ~ConfigKey();
42
43 inline int GetUid() const {
44 return mUid;
45 }
Yangster-mac94e197c2018-01-02 16:03:03 -080046 inline const int64_t& GetId() const {
47 return mId;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070048 }
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 }
Yangster-mac94e197c2018-01-02 16:03:03 -080057 return mId < that.mId;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070058 };
59
60 inline bool operator==(const ConfigKey& that) const {
Yangster-mac94e197c2018-01-02 16:03:03 -080061 return mUid == that.mUid && mId == that.mId;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070062 };
63
64 string ToString() const;
65
66private:
Yangster-mac94e197c2018-01-02 16:03:03 -080067 int64_t mId;
Joe Onorato9fc9edf2017-10-15 20:08:52 -070068 int mUid;
69};
70
71inline ostream& operator<<(ostream& os, const ConfigKey& config) {
72 return os << config.ToString();
73}
74
Yangster-mac94e197c2018-01-02 16:03:03 -080075int64_t StrToInt64(const string& str);
76
Joe Onorato9fc9edf2017-10-15 20:08:52 -070077} // namespace statsd
78} // namespace os
79} // namespace android
80
81/**
82 * A hash function for ConfigKey so it can be used for unordered_map/set.
yro87d983c2017-11-14 21:31:43 -080083 * Unfortunately this has to go in std namespace because C++ is fun!
Joe Onorato9fc9edf2017-10-15 20:08:52 -070084 */
85namespace std {
86
87using android::os::statsd::ConfigKey;
88
89template <>
90struct hash<ConfigKey> {
91 std::size_t operator()(const ConfigKey& key) const {
Yangster-mac94e197c2018-01-02 16:03:03 -080092 return (7 * key.GetUid()) ^ ((hash<long long>()(key.GetId())));
Joe Onorato9fc9edf2017-10-15 20:08:52 -070093 }
94};
95
96} // namespace std