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 <utility>
26#include <array>
27#include <cassert>
28
29namespace o3tl {
30
31template<typename EA>
32class enumarray_iterator;
33template<typename EA>
34class enumarray_const_iterator;
35
46template<typename E, typename V>
47class enumarray final
48{
49public:
53
54 typedef V value_type;
55 typedef E key_type;
56 typedef size_t size_type;
57
58 static const size_type max_index = static_cast<size_type>(E::LAST);
59
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)...}
68 {
69 static_assert(sizeof... (T) == max_index);
70 }
71
72 // coverity[uninit_ctor] - by design:
74
75 const V& operator[](E index) const
76 {
77 assert(index>=static_cast<E>(0) && index<=E::LAST);
78 return detail_values[static_cast<size_type>(index)];
79 }
80
81 V& operator[](E index)
82 {
83 assert(index>=static_cast<E>(0) && index<=E::LAST);
84 return detail_values[static_cast<size_type>(index)];
85 }
86
87 void fill(V val)
88 { for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; }
89
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()); }
95
96 V* data() { return detail_values.data(); }
97
98private:
99 std::array<V, max_index + 1> detail_values;
100};
101
102
103template<typename EA>
105 EA* m_buf;
106 size_t m_pos;
107public:
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
116 typedef typename EA::value_type* pointer;
117 typedef typename EA::value_type& reference;
118
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; }
126};
127
128template<typename EA>
130 EA const * m_buf;
131 size_t m_pos;
132public:
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
141 typedef typename EA::value_type const * pointer;
142 typedef typename EA::value_type const & reference;
143
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; }
151};
152
153}; // namespace o3tl
154
155#endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
value_type & operator*() const
Definition: enumarray.hxx:146
EA::value_type const value_type
Definition: enumarray.hxx:134
EA::value_type const * pointer
Definition: enumarray.hxx:141
enumarray_const_iterator(EA const &b, size_t start_pos)
Definition: enumarray.hxx:144
value_type * operator->() const
Definition: enumarray.hxx:147
std::make_signed< typenamestd::underlying_type< typenameEA::key_type >::type >::type difference_type
Definition: enumarray.hxx:140
EA::value_type const & reference
Definition: enumarray.hxx:142
bool operator==(self_type const &other) const
Definition: enumarray.hxx:150
std::bidirectional_iterator_tag iterator_category
Definition: enumarray.hxx:136
bool operator!=(self_type const &other) const
Definition: enumarray.hxx:149
enumarray_const_iterator< EA > self_type
Definition: enumarray.hxx:133
EA::value_type * pointer
Definition: enumarray.hxx:116
value_type * operator->() const
Definition: enumarray.hxx:122
EA::value_type & reference
Definition: enumarray.hxx:117
std::bidirectional_iterator_tag iterator_category
Definition: enumarray.hxx:111
bool operator==(self_type const &other) const
Definition: enumarray.hxx:125
EA::value_type value_type
Definition: enumarray.hxx:109
value_type & operator*() const
Definition: enumarray.hxx:121
bool operator!=(self_type const &other) const
Definition: enumarray.hxx:124
enumarray_iterator< EA > self_type
Definition: enumarray.hxx:108
std::make_signed< typenamestd::underlying_type< typenameEA::key_type >::type >::type difference_type
Definition: enumarray.hxx:115
enumarray_iterator(EA &b, size_t start_pos)
Definition: enumarray.hxx:119
self_type & operator++()
Definition: enumarray.hxx:123
This is a container convenience class for arrays indexed by enum values.
Definition: enumarray.hxx:48
void fill(V val)
Definition: enumarray.hxx:87
static const size_type max_index
Definition: enumarray.hxx:58
const_iterator begin() const
Definition: enumarray.hxx:93
iterator end()
Definition: enumarray.hxx:92
V & operator[](E index)
Definition: enumarray.hxx:81
const_iterator end() const
Definition: enumarray.hxx:94
enumarray_const_iterator< self_type > const_iterator
Definition: enumarray.hxx:52
constexpr enumarray(V const &arg, T &&...args)
Definition: enumarray.hxx:66
size_t size_type
Definition: enumarray.hxx:56
enumarray_iterator< self_type > iterator
Definition: enumarray.hxx:51
std::array< V, max_index+1 > detail_values
Definition: enumarray.hxx:99
const V & operator[](E index) const
Definition: enumarray.hxx:75
static size_type size()
Definition: enumarray.hxx:90
iterator begin()
Definition: enumarray.hxx:91
enumarray< E, V > self_type
Definition: enumarray.hxx:50
def forward(n)
int i
index
args
const sal_uInt8 V