summaryrefslogtreecommitdiffstats
path: root/include/o3tl
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-10-24 14:59:41 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-10-25 16:42:08 +0200
commita0032a2dc2e4ac7615baaacdde5fefa64048822e (patch)
tree67464978e1429652f66bae1668a9484226dce80c /include/o3tl
parentofz#3776 Divide-by-zero (diff)
downloadcore-a0032a2dc2e4ac7615baaacdde5fefa64048822e.tar.gz
core-a0032a2dc2e4ac7615baaacdde5fefa64048822e.zip
improve o3tl::enumarray const-ness
Change-Id: I7b0d10b024edf604a7dea0e3b1399073f4bcba92 Reviewed-on: https://gerrit.libreoffice.org/43773 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/o3tl')
-rw-r--r--include/o3tl/enumarray.hxx56
1 files changed, 45 insertions, 11 deletions
diff --git a/include/o3tl/enumarray.hxx b/include/o3tl/enumarray.hxx
index 766d9670b34a..ca012e197f27 100644
--- a/include/o3tl/enumarray.hxx
+++ b/include/o3tl/enumarray.hxx
@@ -28,6 +28,8 @@ namespace o3tl {
template<typename EA>
class enumarray_iterator;
+template<typename EA>
+class enumarray_const_iterator;
///
/// This is a container convenience class for arrays indexed by enum values.
@@ -45,6 +47,7 @@ class enumarray final
public:
typedef enumarray<E, V> self_type;
typedef enumarray_iterator<self_type> iterator;
+ typedef enumarray_const_iterator<self_type> const_iterator;
typedef V value_type;
typedef E key_type;
@@ -67,11 +70,13 @@ public:
void fill(V val)
{ for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; }
- static size_type size() { return max_index + 1; }
- iterator begin() { return iterator(*this, 0); }
- iterator end() { return iterator(*this, size()); }
+ static size_type size() { return max_index + 1; }
+ iterator begin() { return iterator(*this, 0); }
+ iterator end() { return iterator(*this, size()); }
+ const_iterator begin() const { return const_iterator(*this, 0); }
+ const_iterator end() const { return const_iterator(*this, size()); }
- V* data() { return detail_values; }
+ V* data() { return detail_values; }
//private:
V detail_values[max_index + 1];
@@ -80,7 +85,7 @@ public:
template<typename EA>
class enumarray_iterator {
- EA *m_buf;
+ EA& m_buf;
size_t m_pos;
public:
typedef enumarray_iterator<EA> self_type;
@@ -95,12 +100,41 @@ public:
typedef typename EA::value_type& reference;
enumarray_iterator(EA& b, size_t start_pos)
- : m_buf(&b), m_pos(start_pos) {}
- value_type &operator*() { return (*m_buf)[static_cast<key_type>(m_pos)]; }
- value_type *operator->() { return &(operator*()); }
- self_type &operator++() { ++m_pos; return *this; }
- bool operator!=(const self_type& other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
- bool operator==(const self_type& other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
+ : m_buf(b), m_pos(start_pos) {}
+ value_type& operator*() const { return m_buf[static_cast<key_type>(m_pos)]; }
+ value_type* operator->() const { return &(operator*()); }
+ self_type& operator++() { ++m_pos; return *this; }
+ bool operator!=(self_type const & other) const { return &m_buf != &other.m_buf || m_pos != other.m_pos; }
+ bool operator==(self_type const & other) const { return &m_buf == &other.m_buf && m_pos == other.m_pos; }
+ enumarray_iterator&
+ operator=(self_type const & other) { m_buf = other.m_buf; m_pos = other.m_pos; return *this; }
+};
+
+template<typename EA>
+class enumarray_const_iterator {
+ EA const & m_buf;
+ size_t m_pos;
+public:
+ typedef enumarray_const_iterator<EA> self_type;
+ typedef typename EA::value_type const value_type;
+ typedef typename EA::key_type key_type;
+ typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
+ typedef
+ typename std::make_signed<
+ typename std::underlying_type<typename EA::key_type>::type>::type
+ difference_type;
+ typedef typename EA::value_type const * pointer;
+ typedef typename EA::value_type const & reference;
+
+ enumarray_const_iterator(EA const & b, size_t start_pos)
+ : m_buf(b), m_pos(start_pos) {}
+ value_type& operator*() const { return m_buf[static_cast<key_type>(m_pos)]; }
+ value_type* operator->() const { return &(operator*()); }
+ self_type& operator++() { ++m_pos; return *this; }
+ bool operator!=(self_type const & other) const { return &m_buf != &other.m_buf || m_pos != other.m_pos; }
+ bool operator==(self_type const & other) const { return &m_buf == &other.m_buf && m_pos == other.m_pos; }
+ enumarray_const_iterator&
+ operator=(self_type const & other) { m_buf = other.m_buf; m_pos = other.m_pos; return *this; }
};
}; // namespace o3tl