2015-08-11 06:18:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
#ifndef METRICS_PERSISTENT_INTEGER_H_
|
|
|
|
#define METRICS_PERSISTENT_INTEGER_H_
|
|
|
|
|
2014-08-07 15:54:59 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2014-02-27 03:53:16 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace chromeos_metrics {
|
|
|
|
|
|
|
|
// PersistentIntegers is a named 64-bit integer value backed by a file.
|
|
|
|
// The in-memory value acts as a write-through cache of the file value.
|
|
|
|
// If the backing file doesn't exist or has bad content, the value is 0.
|
|
|
|
|
|
|
|
class PersistentInteger {
|
|
|
|
public:
|
2014-07-10 07:35:23 +08:00
|
|
|
explicit PersistentInteger(const std::string& name);
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// Virtual only because of mock.
|
|
|
|
virtual ~PersistentInteger();
|
|
|
|
|
|
|
|
// Sets the value. This writes through to the backing file.
|
2014-08-07 15:54:59 +08:00
|
|
|
void Set(int64_t v);
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// Gets the value. May sync from backing file first.
|
2014-08-07 15:54:59 +08:00
|
|
|
int64_t Get();
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// Returns the name of the object.
|
|
|
|
std::string Name() { return name_; }
|
|
|
|
|
|
|
|
// Convenience function for Get() followed by Set(0).
|
2014-08-07 15:54:59 +08:00
|
|
|
int64_t GetAndClear();
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// Convenience function for v = Get, Set(v + x).
|
|
|
|
// Virtual only because of mock.
|
2014-08-07 15:54:59 +08:00
|
|
|
virtual void Add(int64_t x);
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// After calling with |testing| = true, changes some behavior for the purpose
|
|
|
|
// of testing. For instance: instances created while testing use the current
|
|
|
|
// directory for the backing files.
|
|
|
|
static void SetTestingMode(bool testing);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int kVersion = 1001;
|
|
|
|
|
2014-04-16 00:31:47 +08:00
|
|
|
// Writes |value_| to the backing file, creating it if necessary.
|
|
|
|
void Write();
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
// Reads the value from the backing file, stores it in |value_|, and returns
|
2014-04-16 00:31:47 +08:00
|
|
|
// true if the backing file is valid. Returns false otherwise, and creates
|
|
|
|
// a valid backing file as a side effect.
|
2014-02-27 03:53:16 +08:00
|
|
|
bool Read();
|
|
|
|
|
2014-08-07 15:54:59 +08:00
|
|
|
int64_t value_;
|
|
|
|
int32_t version_;
|
2014-02-27 03:53:16 +08:00
|
|
|
std::string name_;
|
|
|
|
std::string backing_file_name_;
|
|
|
|
bool synced_;
|
|
|
|
static bool testing_;
|
|
|
|
};
|
|
|
|
|
2014-07-10 07:35:23 +08:00
|
|
|
} // namespace chromeos_metrics
|
2014-02-27 03:53:16 +08:00
|
|
|
|
|
|
|
#endif // METRICS_PERSISTENT_INTEGER_H_
|