hb-iter.hh 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright © 2018  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifndef HB_ITER_HH
#define HB_ITER_HH

#include "hb.hh"


/* Unified iterator object.
 *
 * The goal of this template is to make the same iterator interface
 * available to all types, and make it very easy and compact to use.
37
 * hb_iter_tator objects are small, light-weight, objects that can be
38
 * copied by value.  If the collection / object being iterated on
39
 * is writable, then the iterator returns lvalues, otherwise it
40 41 42
 * returns rvalues.
 */

43
/* Base class for all iterators. */
B
Behdad Esfahbod 已提交
44
template <typename Iter, typename Item = typename Iter::__item_type__>
45
struct hb_iter_t
46
{
B
Behdad Esfahbod 已提交
47
  typedef Iter iter_t;
48
  typedef iter_t const_iter_t;
B
Behdad Esfahbod 已提交
49
  typedef Item item_t;
50 51

  /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */ 
B
Behdad Esfahbod 已提交
52 53
  const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
        iter_t* thiz ()       { return static_cast<      iter_t *> (this); }
54 55

  /* Operators. */
B
Behdad Esfahbod 已提交
56
  operator iter_t () { return iter(); }
57
  explicit_operator bool () const { return more (); }
B
Behdad Esfahbod 已提交
58 59 60 61 62 63 64 65 66 67
  item_t& operator * () { return item (); }
  item_t& operator [] (unsigned i) { return item (i); }
  iter_t& operator += (unsigned count) { forward (count); return *thiz(); }
  iter_t& operator ++ () { next (); return *thiz(); }
  iter_t& operator -= (unsigned count) { rewind (count); return *thiz(); }
  iter_t& operator -- () { prev (); return *thiz(); }
  iter_t operator + (unsigned count) { iter_t c (*thiz()); c += count; return c; }
  iter_t operator ++ (int) { iter_t c (*thiz()); ++*thiz(); return c; }
  iter_t operator - (unsigned count) { iter_t c (*thiz()); c -= count; return c; }
  iter_t operator -- (int) { iter_t c (*thiz()); --*thiz(); return c; }
68 69

  /* Methods. */
B
Behdad Esfahbod 已提交
70
  iter_t iter () const { return *thiz(); }
71
  const_iter_t const_iter () const { return iter (); }
B
Behdad Esfahbod 已提交
72 73
  item_t& item () const { return thiz()->__item__ (); }
  item_t& item_at (unsigned i) const { return thiz()->__item_at__ (i); }
74
  bool more () const { return thiz()->__more__ (); }
B
Behdad Esfahbod 已提交
75
  void next () { thiz()->__next__ (); }
76
  void forward (unsigned n) { thiz()->__forward__ (n); }
B
Behdad Esfahbod 已提交
77 78
  void prev () { thiz()->__prev__ (); }
  void rewind (unsigned n) { thiz()->__rewind__ (n); }
79
  unsigned len () const { return thiz()->__len__ (); }
B
Behdad Esfahbod 已提交
80
  bool random_access () const { return thiz()->__random_access__ (); }
81 82 83 84 85

  /*
   * Subclasses overrides:
   */

B
Behdad Esfahbod 已提交
86
  /* Access: Implement __item__(), or __item_at__() if random-access. */
B
Behdad Esfahbod 已提交
87 88
  item_t& __item__ () const { return thiz()->item_at (0); }
  item_t& __item_at__ (unsigned i) const { return *(thiz() + i); }
89

B
Behdad Esfahbod 已提交
90 91 92
  /* Termination: Implement __more__(), or __end__() and operator ==. */
  bool __more__ () const { return !(*thiz() == thiz()->__end__ ()); }
  iter_t __end__ () const;
93 94 95 96 97

  /* Advancing: Implement __next__(), or __forward__() if random-access. */
  void __next__ () { thiz()->forward (1); }
  void __forward__ (unsigned n) { while (n--) next (); }

B
Behdad Esfahbod 已提交
98 99 100 101
  /* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
  void __prev__ () { thiz()->rewind (1); }
  void __rewind__ (unsigned n) { while (n--) prev (); }

102 103
  /* Population: Implement __len__() if known. */
  unsigned __len__ () const
B
Behdad Esfahbod 已提交
104
  { iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
B
Behdad Esfahbod 已提交
105 106 107

  /* Random access: Return true if len(), forward(), item_at() are fast. */
  bool __random_access__ () const { return false; }
108 109 110 111 112

  protected:
  hb_iter_t () {}
  hb_iter_t (const hb_iter_t &o) {}
  void operator = (const hb_iter_t &o) {}
113 114 115 116
};


#endif /* HB_ITER_HH */