summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Brockus <dbrockus@google.com>2023-03-22 13:47:37 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2023-03-23 19:27:03 +0000
commit54cf5df0ee02c0573b302410c871f80673d55a2c (patch)
tree428c6d1065f2e71d004afefee355037150eb81a7
parente5d5bde929391e62e82261f29bd68ec39f02a01e (diff)
downloadlibchrome-gestures-54cf5df0ee02c0573b302410c871f80673d55a2c.tar.gz
Move list with .at into generic in util.h
Instead of repeating making a class that inherits from std::list, make this gestures::List a single point of truth for this code. This is still just a std::list but adds .at() to this class. BUG=b:271591258 TEST=USE="coverage" FEATURES="test noclean" emerge-brya chromeos-base/gestures Change-Id: Id612a6501d5caabea8195b8a14c76c28dcaced5a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/gestures/+/4361866 Code-Coverage: Henry Barnor <hbarnor@chromium.org> Reviewed-by: Harry Cutts <hcutts@chromium.org> Reviewed-by: Sean O'Brien <seobrien@chromium.org> Auto-Submit: Denis Brockus <dbrockus@chromium.org> Tested-by: Denis Brockus <dbrockus@chromium.org> Commit-Queue: Denis Brockus <dbrockus@chromium.org>
-rw-r--r--include/lookahead_filter_interpreter.h6
-rw-r--r--include/metrics_filter_interpreter.h7
-rw-r--r--include/trend_classifying_filter_interpreter.h6
-rw-r--r--include/util.h35
-rw-r--r--src/util_unittest.cc20
5 files changed, 25 insertions, 49 deletions
diff --git a/include/lookahead_filter_interpreter.h b/include/lookahead_filter_interpreter.h
index c37f170..ba2f92b 100644
--- a/include/lookahead_filter_interpreter.h
+++ b/include/lookahead_filter_interpreter.h
@@ -107,11 +107,7 @@ class LookaheadFilterInterpreter : public FilterInterpreter {
stime_t ExtraVariableDelay() const;
- struct QStateList : public std::list<QState> {
- QState& at(int offset) {
- return ListAt<QState>(*this, offset);
- }
- } queue_;
+ List<QState> queue_;
// The last id assigned to a contact (part of drumroll suppression)
short last_id_;
diff --git a/include/metrics_filter_interpreter.h b/include/metrics_filter_interpreter.h
index 9dc1978..816f849 100644
--- a/include/metrics_filter_interpreter.h
+++ b/include/metrics_filter_interpreter.h
@@ -54,12 +54,7 @@ class MetricsFilterInterpreter : public FilterInterpreter {
// struct for one finger's data of one frame.
typedef State<FingerState, 3> MState;
-
- struct FingerHistory : public std::list<MState> {
- MState& at(int offset) {
- return ListAt<MState>(*this, offset);
- }
- };
+ typedef List<MState> FingerHistory;
// Push the new data into the buffer.
void AddNewStateToBuffer(FingerHistory& history,
diff --git a/include/trend_classifying_filter_interpreter.h b/include/trend_classifying_filter_interpreter.h
index 6c05073..049c5e0 100644
--- a/include/trend_classifying_filter_interpreter.h
+++ b/include/trend_classifying_filter_interpreter.h
@@ -153,11 +153,7 @@ private:
}
};
- struct FingerHistory : public std::list<KState> {
- KState& at(int offset) {
- return ListAt<KState>(*this, offset);
- }
- };
+ typedef List<KState> FingerHistory;
// Trend types for internal use
enum TrendType {
diff --git a/include/util.h b/include/util.h
index afaf61a..f0c2a39 100644
--- a/include/util.h
+++ b/include/util.h
@@ -95,24 +95,27 @@ inline bool SetContainsValue(const Set& the_set,
}
template<typename Elem>
-Elem& ListAt(std::list<Elem>& the_list, int offset) {
- // Traverse to the appropriate offset
- if (offset < 0) {
- // negative offset is from end to begin
- for (auto iter = the_list.rbegin(); iter != the_list.rend(); ++iter) {
- if (++offset == 0)
- return *iter;
- }
- } else {
- // positive offset is from begin to end
- for (auto iter = the_list.begin(); iter != the_list.end(); ++iter) {
- if (offset-- == 0)
- return *iter;
+class List : public std::list<Elem> {
+public:
+ Elem& at(int offset) {
+ // Traverse to the appropriate offset
+ if (offset < 0) {
+ // negative offset is from end to begin
+ for (auto iter = this->rbegin(); iter != this->rend(); ++iter) {
+ if (++offset == 0)
+ return *iter;
+ }
+ } else {
+ // positive offset is from begin to end
+ for (auto iter = this->begin(); iter != this->end(); ++iter) {
+ if (offset-- == 0)
+ return *iter;
+ }
}
+ // Invalid offset
+ abort();
}
- // Invalid offset
- abort();
-}
+};
} // namespace gestures
diff --git a/src/util_unittest.cc b/src/util_unittest.cc
index 9d6bd40..f19fe1f 100644
--- a/src/util_unittest.cc
+++ b/src/util_unittest.cc
@@ -27,11 +27,7 @@ TEST(UtilTest, ListAtTest) {
int x;
};
- struct ElemList : public std::list<element> {
- element& at(int offset) {
- return ListAt<element>(*this, offset);
- }
- } list;
+ List<element> list;
for (auto i = 0; i < kMaxElements; ++i) {
auto& elem = list.emplace_back();
@@ -53,14 +49,9 @@ TEST(UtilTest, ListAtTest) {
}
TEST(UtilTest, ListAtDeathForwardTest) {
+ List<int> list;
const int kMaxElements = 3;
- struct IntList : public std::list<int> {
- int& at(int offset) {
- return ListAt<int>(*this, offset);
- }
- } list;
-
for (auto i = 0; i < kMaxElements; ++i) {
list.emplace_back(i);
}
@@ -68,14 +59,9 @@ TEST(UtilTest, ListAtDeathForwardTest) {
}
TEST(UtilTest, ListAtDeathBackwardTest) {
+ List<int> list;
const int kMaxElements = 3;
- struct IntList : public std::list<int> {
- int& at(int offset) {
- return ListAt<int>(*this, offset);
- }
- } list;
-
for (auto i = 0; i < kMaxElements; ++i) {
list.emplace_back(i);
}