blob: e8df355413d81ab1dc86b33b4b925644b0dcf7eb [file] [log] [blame]
Joe Onoratoda8cca72017-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
Tej Singhd2e51ec2021-04-19 20:26:29 -070019#include "src/statsd_config.pb.h"
Joe Onoratoda8cca72017-10-15 20:08:52 -070020
Joe Onoratoda8cca72017-10-15 20:08:52 -070021#include <string>
22
23namespace android {
24namespace os {
25namespace statsd {
26
27using std::hash;
Joe Onoratoda8cca72017-10-15 20:08:52 -070028using std::string;
29
30/**
31 * Uniquely identifies a configuration.
32 */
33class ConfigKey {
34public:
35 ConfigKey();
Chih-Hung Hsieh59b0b3b2018-12-20 13:42:28 -080036 ConfigKey(const ConfigKey& that);
Vova Sharaienko6c867d72024-01-04 01:40:51 +000037 ConfigKey(int uid, int64_t id);
Joe Onoratoda8cca72017-10-15 20:08:52 -070038 ~ConfigKey();
39
40 inline int GetUid() const {
41 return mUid;
42 }
Vova Sharaienko6c867d72024-01-04 01:40:51 +000043 inline int64_t GetId() const {
Yangster-macf8faf672018-01-02 16:03:03 -080044 return mId;
Joe Onoratoda8cca72017-10-15 20:08:52 -070045 }
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-macf8faf672018-01-02 16:03:03 -080054 return mId < that.mId;
Joe Onoratoda8cca72017-10-15 20:08:52 -070055 };
56
57 inline bool operator==(const ConfigKey& that) const {
Yangster-macf8faf672018-01-02 16:03:03 -080058 return mUid == that.mUid && mId == that.mId;
Joe Onoratoda8cca72017-10-15 20:08:52 -070059 };
60
61 string ToString() const;
62
63private:
Yangster-macf8faf672018-01-02 16:03:03 -080064 int64_t mId;
Joe Onoratoda8cca72017-10-15 20:08:52 -070065 int mUid;
66};
67
Yangster-macf8faf672018-01-02 16:03:03 -080068int64_t StrToInt64(const string& str);
69
Joe Onoratoda8cca72017-10-15 20:08:52 -070070} // 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 Onoratoda8cca72017-10-15 20:08:52 -070076 */
Joe Onoratoda8cca72017-10-15 20:08:52 -070077template <>
Vova Sharaienkodb355ed2023-12-22 02:56:08 +000078struct std::hash<android::os::statsd::ConfigKey> {
79 std::size_t operator()(const android::os::statsd::ConfigKey& key) const {
Yangster-macf8faf672018-01-02 16:03:03 -080080 return (7 * key.GetUid()) ^ ((hash<long long>()(key.GetId())));
Joe Onoratoda8cca72017-10-15 20:08:52 -070081 }
82};