LibreOffice Module o3tl (master)  1
enumarray.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_O3TL_ENUMARRAY_HXX
21 #define INCLUDED_O3TL_ENUMARRAY_HXX
22 
23 #include <iterator>
24 #include <type_traits>
25 #include <cassert>
26 
27 namespace o3tl {
28 
29 template<typename EA>
31 template<typename EA>
33 
44 template<typename E, typename V>
45 class enumarray final
46 {
47 public:
51 
52  typedef V value_type;
53  typedef E key_type;
54  typedef size_t size_type;
55 
56  static const size_type max_index = static_cast<size_type>(E::LAST);
57 
58  const V& operator[](E index) const
59  {
60  assert(index>=static_cast<E>(0) && index<=E::LAST);
61  return detail_values[static_cast<size_type>(index)];
62  }
63 
64  V& operator[](E index)
65  {
66  assert(index>=static_cast<E>(0) && index<=E::LAST);
67  return detail_values[static_cast<size_type>(index)];
68  }
69 
70  void fill(V val)
71  { for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; }
72 
73  static size_type size() { return max_index + 1; }
74  iterator begin() { return iterator(*this, 0); }
75  iterator end() { return iterator(*this, size()); }
76  const_iterator begin() const { return const_iterator(*this, 0); }
77  const_iterator end() const { return const_iterator(*this, size()); }
78 
79  V* data() { return detail_values; }
80 
81 //private:
82  V detail_values[max_index + 1];
83 };
84 
85 
86 template<typename EA>
87 class enumarray_iterator {
88  EA* m_buf;
89  size_t m_pos;
90 public:
92  typedef typename EA::value_type value_type;
93  typedef typename EA::key_type key_type;
94  typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
95  typedef
96  typename std::make_signed<
97  typename std::underlying_type<typename EA::key_type>::type>::type
99  typedef typename EA::value_type* pointer;
100  typedef typename EA::value_type& reference;
101 
102  enumarray_iterator(EA& b, size_t start_pos)
103  : m_buf(&b), m_pos(start_pos) {}
104  value_type& operator*() const { return (*m_buf)[static_cast<key_type>(m_pos)]; }
105  value_type* operator->() const { return &(operator*()); }
106  self_type& operator++() { ++m_pos; return *this; }
107  bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
108  bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
109 };
110 
111 template<typename EA>
112 class enumarray_const_iterator {
113  EA const * m_buf;
114  size_t m_pos;
115 public:
117  typedef typename EA::value_type const value_type;
118  typedef typename EA::key_type key_type;
119  typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
120  typedef
121  typename std::make_signed<
122  typename std::underlying_type<typename EA::key_type>::type>::type
124  typedef typename EA::value_type const * pointer;
125  typedef typename EA::value_type const & reference;
126 
127  enumarray_const_iterator(EA const & b, size_t start_pos)
128  : m_buf(&b), m_pos(start_pos) {}
129  value_type& operator*() const { return (*m_buf)[static_cast<key_type>(m_pos)]; }
130  value_type* operator->() const { return &(operator*()); }
131  self_type& operator++() { ++m_pos; return *this; }
132  bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
133  bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
134 };
135 
136 }; // namespace o3tl
137 
138 #endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
139 
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
EA::value_type value_type
Definition: enumarray.hxx:92
V & operator[](E index)
Definition: enumarray.hxx:64
std::bidirectional_iterator_tag iterator_category
Definition: enumarray.hxx:94
std::bidirectional_iterator_tag iterator_category
Definition: enumarray.hxx:119
This is a container convenience class for arrays indexed by enum values.
Definition: enumarray.hxx:45
const sal_uInt8 V
std::make_signed< typename std::underlying_type< typename EA::key_type >::type >::type difference_type
Definition: enumarray.hxx:98
const_iterator end() const
Definition: enumarray.hxx:77
enumarray_const_iterator< self_type > const_iterator
Definition: enumarray.hxx:50
enumarray_const_iterator< EA > self_type
Definition: enumarray.hxx:116
void fill(V val)
Definition: enumarray.hxx:70
const_iterator begin() const
Definition: enumarray.hxx:76
const V & operator[](E index) const
Definition: enumarray.hxx:58
EA::value_type * pointer
Definition: enumarray.hxx:99
bool operator==(self_type const &other) const
Definition: enumarray.hxx:133
EA::value_type & reference
Definition: enumarray.hxx:100
V detail_values[max_index+1]
Definition: enumarray.hxx:82
bool operator!=(self_type const &other) const
Definition: enumarray.hxx:132
value_type & operator*() const
Definition: enumarray.hxx:104
int i
enumarray_const_iterator(EA const &b, size_t start_pos)
Definition: enumarray.hxx:127
bool operator!=(self_type const &other) const
Definition: enumarray.hxx:107
EA::value_type const * pointer
Definition: enumarray.hxx:124
value_type * operator->() const
Definition: enumarray.hxx:105
self_type & operator++()
Definition: enumarray.hxx:106
enumarray_iterator< self_type > iterator
Definition: enumarray.hxx:49
enumarray< E, V > self_type
Definition: enumarray.hxx:48
enumarray_iterator(EA &b, size_t start_pos)
Definition: enumarray.hxx:102
enumarray_iterator< EA > self_type
Definition: enumarray.hxx:91
static const size_type max_index
Definition: enumarray.hxx:56
EA::value_type const value_type
Definition: enumarray.hxx:117
static size_type size()
Definition: enumarray.hxx:73
std::make_signed< typename std::underlying_type< typename EA::key_type >::type >::type difference_type
Definition: enumarray.hxx:123
iterator begin()
Definition: enumarray.hxx:74
size_t size_type
Definition: enumarray.hxx:54
iterator end()
Definition: enumarray.hxx:75
bool operator==(self_type const &other) const
Definition: enumarray.hxx:108
value_type * operator->() const
Definition: enumarray.hxx:130
EA::value_type const & reference
Definition: enumarray.hxx:125
value_type & operator*() const
Definition: enumarray.hxx:129