summaryrefslogtreecommitdiff
path: root/libs/battery/LongArrayMultiStateCounter.h
blob: e00c96898e67a1527c4c9804b90445217a65fae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright (C) 2021 The Android Open Source Project
 * Android BPF library - public API
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <vector>
#include "MultiStateCounter.h"

namespace android {
namespace battery {

/**
 * Wrapper for an array of uint64's.
 */
class Uint64Array {
  protected:
    size_t mSize;

  public:
    Uint64Array() : Uint64Array(0) {}

    Uint64Array(size_t size) : mSize(size) {}

    virtual ~Uint64Array() {}

    size_t size() const { return mSize; }

    /**
     * Returns the wrapped array.
     *
     * Nullable! Null should be interpreted the same as an array of zeros
     */
    virtual const uint64_t *data() const { return nullptr; }

    friend std::ostream &operator<<(std::ostream &os, const Uint64Array &v);

    // Test API
    bool operator==(const Uint64Array &other) const;
};

/**
 * Mutable version of Uint64Array.
 */
class Uint64ArrayRW: public Uint64Array {
    uint64_t* mData;

public:
    Uint64ArrayRW() : Uint64ArrayRW(0) {}

    Uint64ArrayRW(size_t size) : Uint64Array(size), mData(nullptr) {}

    Uint64ArrayRW(const Uint64Array &copy);

    // Need an explicit copy constructor. In the initialization context C++ does not understand that
    // a Uint64ArrayRW is a Uint64Array.
    Uint64ArrayRW(const Uint64ArrayRW &copy) : Uint64ArrayRW((const Uint64Array &) copy) {}

    // Test API
    Uint64ArrayRW(std::initializer_list<uint64_t> init);

    ~Uint64ArrayRW() override { delete[] mData; }

    const uint64_t *data() const override { return mData; }

    // NonNull. Will initialize the wrapped array if it is null.
    uint64_t *dataRW();

    Uint64ArrayRW &operator=(const Uint64Array &t);
};

typedef MultiStateCounter<Uint64ArrayRW, Uint64Array> LongArrayMultiStateCounter;

} // namespace battery
} // namespace android