harfbuzz/src/hb-iter.hh

117 lines
4.5 KiB
C++
Raw Normal View History

/*
* 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.
2018-12-21 06:54:55 +01:00
* hb_iter_tator objects are small, light-weight, objects that can be
* copied by value. If the collection / object being iterated on
2018-12-21 06:54:55 +01:00
* is writable, then the iterator returns lvalues, otherwise it
* returns rvalues.
*/
2018-12-21 06:54:55 +01:00
/* Base class for all iterators. */
2018-12-21 07:53:27 +01:00
template <typename Iter, typename Item = typename Iter::__item_type__>
2018-12-21 06:54:55 +01:00
struct hb_iter_t
{
2018-12-21 07:57:02 +01:00
typedef Iter iter_t;
2018-12-21 22:19:44 +01:00
typedef iter_t const_iter_t;
2018-12-21 07:57:02 +01:00
typedef Item item_t;
2018-12-21 06:54:55 +01:00
/* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
2018-12-21 07:57:02 +01:00
const iter_t* thiz () const { return static_cast<const iter_t *> (this); }
iter_t* thiz () { return static_cast< iter_t *> (this); }
2018-12-21 06:54:55 +01:00
/* Operators. */
2018-12-21 07:57:02 +01:00
operator iter_t () { return iter(); }
2018-12-21 06:54:55 +01:00
explicit_operator bool () const { return more (); }
2018-12-21 07:57:02 +01:00
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; }
2018-12-21 06:54:55 +01:00
/* Methods. */
2018-12-21 07:57:02 +01:00
iter_t iter () const { return *thiz(); }
2018-12-21 22:19:44 +01:00
const_iter_t const_iter () const { return iter (); }
2018-12-21 07:57:02 +01:00
item_t& item () const { return thiz()->__item__ (); }
item_t& item_at (unsigned i) const { return thiz()->__item_at__ (i); }
2018-12-21 06:54:55 +01:00
bool more () const { return thiz()->__more__ (); }
2018-12-21 07:17:35 +01:00
void next () { thiz()->__next__ (); }
2018-12-21 06:54:55 +01:00
void forward (unsigned n) { thiz()->__forward__ (n); }
2018-12-21 07:17:35 +01:00
void prev () { thiz()->__prev__ (); }
void rewind (unsigned n) { thiz()->__rewind__ (n); }
2018-12-21 06:54:55 +01:00
unsigned len () const { return thiz()->__len__ (); }
2018-12-21 21:53:09 +01:00
bool random_access () const { return thiz()->__random_access__ (); }
2018-12-21 06:54:55 +01:00
/*
* Subclasses overrides:
*/
2018-12-21 07:53:27 +01:00
/* Access: Implement __item__(), or __item_at__() if random-access. */
2018-12-21 07:57:02 +01:00
item_t& __item__ () const { return thiz()->item_at (0); }
item_t& __item_at__ (unsigned i) const { return *(thiz() + i); }
2018-12-21 06:54:55 +01:00
2018-12-21 21:19:22 +01:00
/* Termination: Implement __more__(), or __end__() and operator ==. */
bool __more__ () const { return !(*thiz() == thiz()->__end__ ()); }
iter_t __end__ () const;
2018-12-21 06:54:55 +01:00
/* Advancing: Implement __next__(), or __forward__() if random-access. */
void __next__ () { thiz()->forward (1); }
void __forward__ (unsigned n) { while (n--) next (); }
2018-12-21 07:17:35 +01:00
/* Rewinding: Implement __prev__() or __rewind__() if bidirectional. */
void __prev__ () { thiz()->rewind (1); }
void __rewind__ (unsigned n) { while (n--) prev (); }
2018-12-21 06:54:55 +01:00
/* Population: Implement __len__() if known. */
unsigned __len__ () const
2018-12-21 07:57:02 +01:00
{ iter_t c (*thiz()); unsigned l = 0; while (c) { c++; l++; }; return l; }
2018-12-21 21:53:09 +01:00
/* Random access: Return true if len(), forward(), item_at() are fast. */
bool __random_access__ () const { return false; }
protected:
hb_iter_t () {}
hb_iter_t (const hb_iter_t &o) {}
void operator = (const hb_iter_t &o) {}
};
#endif /* HB_ITER_HH */