Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 1 | // 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 Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | namespace 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 | |
| 18 | class PersistentInteger { |
| 19 | public: |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 20 | explicit PersistentInteger(const std::string& name); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 21 | |
| 22 | // Virtual only because of mock. |
| 23 | virtual ~PersistentInteger(); |
| 24 | |
| 25 | // Sets the value. This writes through to the backing file. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 26 | void Set(int64_t v); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 27 | |
| 28 | // Gets the value. May sync from backing file first. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 29 | int64_t Get(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 30 | |
| 31 | // Returns the name of the object. |
| 32 | std::string Name() { return name_; } |
| 33 | |
| 34 | // Convenience function for Get() followed by Set(0). |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 35 | int64_t GetAndClear(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 36 | |
| 37 | // Convenience function for v = Get, Set(v + x). |
| 38 | // Virtual only because of mock. |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 39 | virtual void Add(int64_t x); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 40 | |
| 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 Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 49 | // Writes |value_| to the backing file, creating it if necessary. |
| 50 | void Write(); |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 51 | |
| 52 | // Reads the value from the backing file, stores it in |value_|, and returns |
Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 53 | // true if the backing file is valid. Returns false otherwise, and creates |
| 54 | // a valid backing file as a side effect. |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 55 | bool Read(); |
| 56 | |
Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 57 | int64_t value_; |
| 58 | int32_t version_; |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 59 | std::string name_; |
| 60 | std::string backing_file_name_; |
| 61 | bool synced_; |
| 62 | static bool testing_; |
| 63 | }; |
| 64 | |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 65 | } // namespace chromeos_metrics |
Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 66 | |
| 67 | #endif // METRICS_PERSISTENT_INTEGER_H_ |