xref: /aosp_15_r20/frameworks/native/libs/battery/LongArrayMultiStateCounter.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  * Android BPF library - public API
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <vector>
21 #include "MultiStateCounter.h"
22 
23 namespace android {
24 namespace battery {
25 
26 /**
27  * Wrapper for an array of uint64's.
28  */
29 class Uint64Array {
30   protected:
31     size_t mSize;
32 
33   public:
Uint64Array()34     Uint64Array() : Uint64Array(0) {}
35 
Uint64Array(size_t size)36     Uint64Array(size_t size) : mSize(size) {}
37 
~Uint64Array()38     virtual ~Uint64Array() {}
39 
size()40     size_t size() const { return mSize; }
41 
42     /**
43      * Returns the wrapped array.
44      *
45      * Nullable! Null should be interpreted the same as an array of zeros
46      */
data()47     virtual const uint64_t *data() const { return nullptr; }
48 
49     friend std::ostream &operator<<(std::ostream &os, const Uint64Array &v);
50 
51     // Test API
52     bool operator==(const Uint64Array &other) const;
53 };
54 
55 /**
56  * Mutable version of Uint64Array.
57  */
58 class Uint64ArrayRW: public Uint64Array {
59     uint64_t* mData;
60 
61 public:
Uint64ArrayRW()62     Uint64ArrayRW() : Uint64ArrayRW(0) {}
63 
Uint64ArrayRW(size_t size)64     Uint64ArrayRW(size_t size) : Uint64Array(size), mData(nullptr) {}
65 
66     Uint64ArrayRW(const Uint64Array &copy);
67 
68     // Need an explicit copy constructor. In the initialization context C++ does not understand that
69     // a Uint64ArrayRW is a Uint64Array.
Uint64ArrayRW(const Uint64ArrayRW & copy)70     Uint64ArrayRW(const Uint64ArrayRW &copy) : Uint64ArrayRW((const Uint64Array &) copy) {}
71 
72     // Test API
73     Uint64ArrayRW(std::initializer_list<uint64_t> init);
74 
~Uint64ArrayRW()75     ~Uint64ArrayRW() override { delete[] mData; }
76 
data()77     const uint64_t *data() const override { return mData; }
78 
79     // NonNull. Will initialize the wrapped array if it is null.
80     uint64_t *dataRW();
81 
82     Uint64ArrayRW &operator=(const Uint64Array &t);
83 };
84 
85 typedef MultiStateCounter<Uint64ArrayRW, Uint64Array> LongArrayMultiStateCounter;
86 
87 } // namespace battery
88 } // namespace android
89