blob: b1cfcf4efe63bb0e6d385a130f2fe1eeee19a547 [file] [log] [blame]
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef METRICS_PERSISTENT_INTEGER_H_
6#define METRICS_PERSISTENT_INTEGER_H_
7
Ben Chanf05ab402014-08-07 00:54:59 -07008#include <stdint.h>
9
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080010#include <string>
11
12namespace chromeos_metrics {
13
14// PersistentIntegers is a named 64-bit integer value backed by a file.
15// The in-memory value acts as a write-through cache of the file value.
16// If the backing file doesn't exist or has bad content, the value is 0.
17
18class PersistentInteger {
19 public:
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070020 explicit PersistentInteger(const std::string& name);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080021
22 // Virtual only because of mock.
23 virtual ~PersistentInteger();
24
25 // Sets the value. This writes through to the backing file.
Ben Chanf05ab402014-08-07 00:54:59 -070026 void Set(int64_t v);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080027
28 // Gets the value. May sync from backing file first.
Ben Chanf05ab402014-08-07 00:54:59 -070029 int64_t Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080030
31 // Returns the name of the object.
32 std::string Name() { return name_; }
33
34 // Convenience function for Get() followed by Set(0).
Ben Chanf05ab402014-08-07 00:54:59 -070035 int64_t GetAndClear();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080036
37 // Convenience function for v = Get, Set(v + x).
38 // Virtual only because of mock.
Ben Chanf05ab402014-08-07 00:54:59 -070039 virtual void Add(int64_t x);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080040
41 // After calling with |testing| = true, changes some behavior for the purpose
42 // of testing. For instance: instances created while testing use the current
43 // directory for the backing files.
44 static void SetTestingMode(bool testing);
45
46 private:
47 static const int kVersion = 1001;
48
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070049 // Writes |value_| to the backing file, creating it if necessary.
50 void Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080051
52 // Reads the value from the backing file, stores it in |value_|, and returns
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070053 // true if the backing file is valid. Returns false otherwise, and creates
54 // a valid backing file as a side effect.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080055 bool Read();
56
Ben Chanf05ab402014-08-07 00:54:59 -070057 int64_t value_;
58 int32_t version_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080059 std::string name_;
60 std::string backing_file_name_;
61 bool synced_;
62 static bool testing_;
63};
64
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070065} // namespace chromeos_metrics
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080066
67#endif // METRICS_PERSISTENT_INTEGER_H_