summaryrefslogtreecommitdiff
path: root/src/compat
diff options
context:
space:
mode:
authorBen Longbons <b.r.longbons@gmail.com>2014-05-18 02:43:55 -0700
committerBen Longbons <b.r.longbons@gmail.com>2014-05-19 17:10:09 -0700
commit2410aeb608329ed57a31315effdfd5a751788616 (patch)
tree4b961100e9aec99c16c6ccae7f1857e763d58b14 /src/compat
parentfccd01bd0fba297531595b51ffb8db044ed6f22c (diff)
downloadtmwa-2410aeb608329ed57a31315effdfd5a751788616.tar.gz
tmwa-2410aeb608329ed57a31315effdfd5a751788616.tar.bz2
tmwa-2410aeb608329ed57a31315effdfd5a751788616.tar.xz
tmwa-2410aeb608329ed57a31315effdfd5a751788616.zip
Convert login/char and login/admin server components to proto-v2
Diffstat (limited to 'src/compat')
-rw-r--r--src/compat/iter.hpp54
-rw-r--r--src/compat/iter_test.cpp53
2 files changed, 107 insertions, 0 deletions
diff --git a/src/compat/iter.hpp b/src/compat/iter.hpp
index 130bdf9..659aca9 100644
--- a/src/compat/iter.hpp
+++ b/src/compat/iter.hpp
@@ -94,4 +94,58 @@ IteratorPair<ValueIterator<T>> value_range(T b, T e)
return {b, e};
}
+
+template<class T, bool(*filter)(T), class C>
+class FilterIterator
+{
+ C *container;
+ using InnerIterator = decltype(std::begin(*container));
+ InnerIterator impl;
+public:
+ void post_adv()
+ {
+ while (impl != std::end(*container))
+ {
+ if (filter(*impl))
+ break;
+ ++impl;
+ }
+ }
+
+ FilterIterator(C *c)
+ : container(c), impl(std::begin(*c))
+ {
+ post_adv();
+ }
+
+ void operator ++()
+ {
+ ++impl;
+ post_adv();
+ }
+
+ T operator *()
+ {
+ return *impl;
+ }
+
+ friend
+ bool operator != (FilterIterator l, FilterIterator)
+ {
+ return l.impl != std::end(*l.container);
+ }
+};
+
+template<class T>
+bool is_truthy(T v)
+{
+ return v;
+}
+
+template<class T, bool(*filter)(T)=is_truthy, class C>
+IteratorPair<FilterIterator<T, filter, C>> filter_iterator(C *c)
+{
+ return {FilterIterator<T, filter, C>(c), FilterIterator<T, filter, C>(c)};
+}
+
#endif // TMWA_COMPAT_ITER_HPP
diff --git a/src/compat/iter_test.cpp b/src/compat/iter_test.cpp
index 2c3cdc8..f6f59d4 100644
--- a/src/compat/iter_test.cpp
+++ b/src/compat/iter_test.cpp
@@ -102,3 +102,56 @@ TEST(iterpair, unsigned8)
};
EXPECT_TRUE(std::equal(pair.begin(), pair.end(), arr));
}
+
+static
+bool is_odd_ref(int& i)
+{
+ return i % 2;
+}
+
+TEST(iterpair, filter1)
+{
+ int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+
+ int expected_arr[] = {1, 3, 5, 7};
+ int *expected_it = expected_arr;
+ int *expected_end = expected_arr + 4;
+
+ for (int& i : filter_iterator<int&, is_odd_ref>(&arr))
+ {
+ EXPECT_EQ(i, *expected_it);
+ ++expected_it;
+ }
+ EXPECT_EQ(expected_it, expected_end);
+}
+
+TEST(iterpair, filter2)
+{
+ std::vector<int> vals = {0, 1, 0, 2, 0, 3, 0};
+
+ int sum = 0, count = 0;
+ for (int i : filter_iterator<int>(&vals))
+ {
+ sum += i;
+ count++;
+ }
+ EXPECT_EQ(sum, 6);
+ EXPECT_EQ(count, 3);
+}
+
+TEST(iterpair, filter3)
+{
+ int one = 1;
+ int two = 2;
+ int three = 3;
+ std::vector<int *> vals = {0, &one, 0, &two, 0, &three, 0};
+
+ int sum = 0, count = 0;
+ for (int *i : filter_iterator<int *>(&vals))
+ {
+ sum += *i;
+ count++;
+ }
+ EXPECT_EQ(sum, 6);
+ EXPECT_EQ(count, 3);
+}