Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1169)

Unified Diff: counter.h

Issue 2729018: Readability review. (Closed) Base URL: ssh://git@chromiumos-git/metrics.git
Patch Set: Address comments -- empty line, const method. Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | counter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: counter.h
diff --git a/counter.h b/counter.h
index aac00af730e0716950d0506083a0cdc9c5310572..5fecff6bc55087f46bdb68ed1d3735ce0a3ca9ba 100644
--- a/counter.h
+++ b/counter.h
@@ -5,6 +5,7 @@
#ifndef METRICS_COUNTER_H_
#define METRICS_COUNTER_H_
+#include <base/basictypes.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST
namespace chromeos_metrics {
@@ -18,6 +19,11 @@ namespace chromeos_metrics {
// event counts. The aggregated count is reported through the
// callback when the counter is explicitly flushed or when data for a
// new tag arrives.
+//
+// The primary reason for using an interface is to allow easier unit
+// testing in clients through mocking thus avoiding file access and
+// callbacks. Of course, it also enables alternative implementations
+// of the counter with additional features.
class TaggedCounterInterface {
public:
// Callback type used for reporting aggregated or flushed data.
@@ -28,7 +34,7 @@ class TaggedCounterInterface {
// |handle| is the |reporter_handle| pointer passed through Init.
// |tag| is the tag associated with the aggregated count.
// |count| is aggregated count.
- typedef void (*Reporter)(void* handle, int tag, int count);
+ typedef void (*Reporter)(void* handle, int32 tag, int32 count);
virtual ~TaggedCounterInterface() {}
@@ -45,7 +51,7 @@ class TaggedCounterInterface {
// Adds |count| of events for the given |tag|. If there's an
// existing aggregated count for a different tag, it's reported
// through the reporter callback and discarded.
- virtual void Update(int tag, int count) = 0;
+ virtual void Update(int32 tag, int32 count) = 0;
// Reports the current aggregated count (if any) through the
// reporter callback and discards it.
@@ -59,7 +65,7 @@ class TaggedCounter : public TaggedCounterInterface {
// Implementation of interface methods.
void Init(const char* filename, Reporter reporter, void* reporter_handle);
- void Update(int tag, int count);
+ void Update(int32 tag, int32 count);
void Flush();
private:
@@ -90,25 +96,25 @@ class TaggedCounter : public TaggedCounterInterface {
// Initializes with |tag| and |count|. If |count| is negative,
// |count_| is set to 0.
- void Init(int tag, int count);
+ void Init(int32 tag, int32 count);
// Adds |count| to the current |count_|. Negative |count| is
// ignored. In case of positive overflow, |count_| is saturated to
- // INT_MAX.
- void Add(int count);
+ // kint32max.
+ void Add(int32 count);
- int tag() const { return tag_; }
- int count() const { return count_; }
+ int32 tag() const { return tag_; }
+ int32 count() const { return count_; }
private:
- int tag_;
- int count_;
+ int32 tag_;
+ int32 count_;
};
// Implementation of the Update and Flush methods. Goes through the
// necessary steps to read, report, update, and sync the aggregated
// record.
- void UpdateInternal(int tag, int count, bool flush);
+ void UpdateInternal(int32 tag, int32 count, bool flush);
// If the current cached record is invalid, reads it from persistent
// storage specified through file descriptor |fd| and updates the
@@ -120,12 +126,12 @@ class TaggedCounter : public TaggedCounterInterface {
// or the new |tag| is different than the old one, reports the
// aggregated data through the reporter callback and resets the
// cached record.
- void ReportRecord(int tag, bool flush);
+ void ReportRecord(int32 tag, bool flush);
// Updates the cached record given the new |tag| and |count|. This
// method expects either a null cached record, or a valid cached
// record with the same tag as |tag|.
- void UpdateRecord(int tag, int count);
+ void UpdateRecord(int32 tag, int32 count);
// If the cached record state is dirty, updates the persistent
// storage specified through file descriptor |fd| and switches the
« no previous file with comments | « no previous file | counter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698