From 8001e00a470ad06f0307002b4cade5612ee7b521 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod <behdad@behdad.org> Date: Fri, 21 Dec 2018 01:53:27 -0500 Subject: [PATCH] [iter] First sample use --- src/Makefile.am | 5 ++++- src/hb-iter.hh | 25 +++++++++++-------------- src/test-iter.cc | 25 ++++++++++++++++++++----- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 84fed30e4..109572dad 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -388,13 +388,16 @@ compiled_tests = test-iter test-ot-tag test-unicode-ranges check_PROGRAMS += $(compiled_tests) TESTS += $(compiled_tests) -test_iter_SOURCES = test-iter.cc +test_iter_SOURCES = test-iter.cc hb-static.cc +test_iter_CPPFLAGS = $(HBCFLAGS) -DMAIN +test_iter_LDADD = libharfbuzz.la $(HBLIBS) test_ot_tag_SOURCES = hb-ot-tag.cc test_ot_tag_CPPFLAGS = $(HBCFLAGS) -DMAIN test_ot_tag_LDADD = libharfbuzz.la $(HBLIBS) test_unicode_ranges_SOURCES = test-unicode-ranges.cc +test_unicode_ranges_CPPFLAGS = $(HBCFLAGS) -DMAIN test_unicode_ranges_LDADD = libharfbuzz.la $(HBLIBS) TESTS_ENVIRONMENT = \ diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 53bfbb518..8a3aa0177 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -41,11 +41,11 @@ */ /* Base class for all iterators. */ -template <typename Iter> +template <typename Iter, typename Item = typename Iter::__item_type__> struct hb_iter_t { typedef Iter type_t; - typedef typename Iter::__type__ item_type_t; + typedef Item item_type_t; /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ const type_t* thiz () const { return static_cast<const type_t *> (this); } @@ -56,10 +56,10 @@ struct hb_iter_t explicit_operator bool () const { return more (); } item_type_t& operator * () { return item (); } item_type_t& operator [] (unsigned i) { return item (i); } - type_t& operator += (unsigned count) { forward (count); return thiz(); } - type_t& operator ++ () { next (); return thiz(); } - type_t& operator -= (unsigned count) { rewind (count); return thiz(); } - type_t& operator -- () { prev (); return thiz(); } + type_t& operator += (unsigned count) { forward (count); return *thiz(); } + type_t& operator ++ () { next (); return *thiz(); } + type_t& operator -= (unsigned count) { rewind (count); return *thiz(); } + type_t& operator -- () { prev (); return *thiz(); } type_t operator + (unsigned count) { type_t c (*thiz()); c += count; return c; } type_t operator ++ (int) { type_t c (*thiz()); ++*thiz(); return c; } type_t operator - (unsigned count) { type_t c (*thiz()); c -= count; return c; } @@ -67,8 +67,8 @@ struct hb_iter_t /* Methods. */ type_t iter () const { return *thiz(); } - item_type_t item () const { return thiz()->__item__ (); } - item_type_t item (unsigned i) const { return thiz()->__item__ (i); } + item_type_t& item () const { return thiz()->__item__ (); } + item_type_t& item_at (unsigned i) const { return thiz()->__item_at__ (i); } bool more () const { return thiz()->__more__ (); } void next () { thiz()->__next__ (); } void forward (unsigned n) { thiz()->__forward__ (n); } @@ -80,12 +80,9 @@ struct hb_iter_t * Subclasses overrides: */ - /* Item type. */ - //typedef ... __type__; - - /* Access: Implement __item__(), or __item__(n) if random-access. */ - item_type_t __item__ () const { return thiz()->item (0); } - item_type_t __item__ (unsigned i) const { return *(thiz() + i); } + /* Access: Implement __item__(), or __item_at__() if random-access. */ + item_type_t& __item__ () const { return thiz()->item_at (0); } + item_type_t& __item_at__ (unsigned i) const { return *(thiz() + i); } /* Termination: Implement __more__() or __end__(). */ bool __more__ () const { return item () != thiz()->__end__ (); } diff --git a/src/test-iter.cc b/src/test-iter.cc index 162b7b74b..6427ca0a1 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -26,16 +26,32 @@ #include "hb-iter.hh" +#include "hb-array.hh" + +template <typename T> +struct array_iter_t : hb_iter_t<array_iter_t<T>, T> +{ + array_iter_t (hb_array_t<T> arr_) : arr (arr_) {} + + T& __item_at__ (unsigned i) const { return arr[i]; } + bool __more__ () const { return arr.len; } + void __forward__ (unsigned n) { arr += n; } + void __rewind__ (unsigned n) { arr -= n; } + unsigned __len__ () const { return arr.len; } + + private: + hb_array_t<T> arr; +}; + int main (int argc, char **argv) { const int src[10] = {}; int dst[20]; -#if 0 - hb_iter_t<const int *> s (src); - hb_iter_t<const int *> s2 (src, 5); - hb_iter_t<int *> t (dst); + array_iter_t<const int> s (src); + array_iter_t<const int> s2 (src); + array_iter_t<int> t (dst); s2 = s; @@ -43,7 +59,6 @@ main (int argc, char **argv) { *t = *s; } -#endif return 0; }