blob: fec001fc7799d79e715c3ce0c48be91725a9269f [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 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 */
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080016
17#ifndef METRICS_PERSISTENT_INTEGER_H_
18#define METRICS_PERSISTENT_INTEGER_H_
19
Ben Chanf05ab402014-08-07 00:54:59 -070020#include <stdint.h>
21
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080022#include <string>
23
24namespace chromeos_metrics {
25
26// PersistentIntegers is a named 64-bit integer value backed by a file.
27// The in-memory value acts as a write-through cache of the file value.
28// If the backing file doesn't exist or has bad content, the value is 0.
29
30class PersistentInteger {
31 public:
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070032 explicit PersistentInteger(const std::string& name);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080033
34 // Virtual only because of mock.
35 virtual ~PersistentInteger();
36
37 // Sets the value. This writes through to the backing file.
Ben Chanf05ab402014-08-07 00:54:59 -070038 void Set(int64_t v);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080039
40 // Gets the value. May sync from backing file first.
Ben Chanf05ab402014-08-07 00:54:59 -070041 int64_t Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080042
43 // Returns the name of the object.
44 std::string Name() { return name_; }
45
46 // Convenience function for Get() followed by Set(0).
Ben Chanf05ab402014-08-07 00:54:59 -070047 int64_t GetAndClear();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080048
49 // Convenience function for v = Get, Set(v + x).
50 // Virtual only because of mock.
Ben Chanf05ab402014-08-07 00:54:59 -070051 virtual void Add(int64_t x);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080052
53 // After calling with |testing| = true, changes some behavior for the purpose
54 // of testing. For instance: instances created while testing use the current
55 // directory for the backing files.
56 static void SetTestingMode(bool testing);
57
58 private:
59 static const int kVersion = 1001;
60
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070061 // Writes |value_| to the backing file, creating it if necessary.
62 void Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080063
64 // Reads the value from the backing file, stores it in |value_|, and returns
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070065 // true if the backing file is valid. Returns false otherwise, and creates
66 // a valid backing file as a side effect.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080067 bool Read();
68
Ben Chanf05ab402014-08-07 00:54:59 -070069 int64_t value_;
70 int32_t version_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080071 std::string name_;
72 std::string backing_file_name_;
73 bool synced_;
74 static bool testing_;
75};
76
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070077} // namespace chromeos_metrics
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080078
79#endif // METRICS_PERSISTENT_INTEGER_H_