Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / o3tl / enumarray.hxx
blob4fd5cab3155f0272705ba4454570495894c13803
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_O3TL_ENUMARRAY_HXX
21 #define INCLUDED_O3TL_ENUMARRAY_HXX
23 #include <iterator>
24 #include <type_traits>
25 #include <utility>
26 #include <array>
27 #include <cassert>
29 namespace o3tl {
31 template<typename EA>
32 class enumarray_iterator;
33 template<typename EA>
34 class enumarray_const_iterator;
36 ///
37 /// This is a container convenience class for arrays indexed by enum values.
38 ///
39 /// This assumes that the 'enum class' definition
40 /// - starts at zero
41 /// - has no holes in its sequence of values
42 /// - defines a value called LAST which refers to the greatest constant.
43 ///
44 /// \param E the 'enum class' type.
45 /// \param V the value type to be stored in the array
46 template<typename E, typename V>
47 class enumarray final
49 public:
50 typedef enumarray<E, V> self_type;
51 typedef enumarray_iterator<self_type> iterator;
52 typedef enumarray_const_iterator<self_type> const_iterator;
54 typedef V value_type;
55 typedef E key_type;
56 typedef size_t size_type;
58 static const size_type max_index = static_cast<size_type>(E::LAST);
60 // If this ctor only had the args parameter pack, it would erroneously get picked as a better
61 // choice than the (implicit) copy ctor (taking a const lvalue reference) when a copy is made
62 // from a non-const lvalue enumarray; the easiest way to avoid that is the additional arg
63 // parameter; and to keep things simple that parameter is always passed by const lvalue
64 // reference for now even if there could be cases where passing it by rvalue reference might be
65 // beneficial or even necessary if V is a move-only type:
66 template<typename... T> constexpr enumarray(V const & arg, T && ...args):
67 detail_values{arg, std::forward<T>(args)...}
69 static_assert(sizeof... (T) == max_index);
72 // coverity[uninit_ctor] - by design:
73 enumarray() {}
75 const V& operator[](E index) const
77 assert(index>=static_cast<E>(0) && index<=E::LAST);
78 return detail_values[static_cast<size_type>(index)];
81 V& operator[](E index)
83 assert(index>=static_cast<E>(0) && index<=E::LAST);
84 return detail_values[static_cast<size_type>(index)];
87 void fill(V val)
88 { for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; }
90 static size_type size() { return max_index + 1; }
91 iterator begin() { return iterator(*this, 0); }
92 iterator end() { return iterator(*this, size()); }
93 const_iterator begin() const { return const_iterator(*this, 0); }
94 const_iterator end() const { return const_iterator(*this, size()); }
96 V* data() { return detail_values.data(); }
98 private:
99 std::array<V, max_index + 1> detail_values;
103 template<typename EA>
104 class enumarray_iterator {
105 EA* m_buf;
106 size_t m_pos;
107 public:
108 typedef enumarray_iterator<EA> self_type;
109 typedef typename EA::value_type value_type;
110 typedef typename EA::key_type key_type;
111 typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
112 typedef
113 typename std::make_signed<
114 typename std::underlying_type<typename EA::key_type>::type>::type
115 difference_type;
116 typedef typename EA::value_type* pointer;
117 typedef typename EA::value_type& reference;
119 enumarray_iterator(EA& b, size_t start_pos)
120 : m_buf(&b), m_pos(start_pos) {}
121 value_type& operator*() const { return (*m_buf)[static_cast<key_type>(m_pos)]; }
122 value_type* operator->() const { return &(operator*()); }
123 self_type& operator++() { ++m_pos; return *this; }
124 bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
125 bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
128 template<typename EA>
129 class enumarray_const_iterator {
130 EA const * m_buf;
131 size_t m_pos;
132 public:
133 typedef enumarray_const_iterator<EA> self_type;
134 typedef typename EA::value_type const value_type;
135 typedef typename EA::key_type key_type;
136 typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
137 typedef
138 typename std::make_signed<
139 typename std::underlying_type<typename EA::key_type>::type>::type
140 difference_type;
141 typedef typename EA::value_type const * pointer;
142 typedef typename EA::value_type const & reference;
144 enumarray_const_iterator(EA const & b, size_t start_pos)
145 : m_buf(&b), m_pos(start_pos) {}
146 value_type& operator*() const { return (*m_buf)[static_cast<key_type>(m_pos)]; }
147 value_type* operator->() const { return &(operator*()); }
148 self_type& operator++() { ++m_pos; return *this; }
149 bool operator!=(self_type const & other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
150 bool operator==(self_type const & other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
153 }; // namespace o3tl
155 #endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */