1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
24 #include <type_traits>
30 class enumarray_iterator
;
32 class enumarray_const_iterator
;
35 /// This is a container convenience class for arrays indexed by enum values.
37 /// This assumes that the 'enum class' definition
39 /// - has no holes in its sequence of values
40 /// - defines a value called LAST which refers to the greatest constant.
42 /// \param E the 'enum class' type.
43 /// \param V the value type to be stored in the array
44 template<typename E
, typename V
>
48 typedef enumarray
<E
, V
> self_type
;
49 typedef enumarray_iterator
<self_type
> iterator
;
50 typedef enumarray_const_iterator
<self_type
> const_iterator
;
54 typedef size_t size_type
;
56 static const size_type max_index
= static_cast<size_type
>(E::LAST
);
58 const V
& operator[](E index
) const
60 assert(index
>=static_cast<E
>(0) && index
<=E::LAST
);
61 return detail_values
[static_cast<size_type
>(index
)];
64 V
& operator[](E index
)
66 assert(index
>=static_cast<E
>(0) && index
<=E::LAST
);
67 return detail_values
[static_cast<size_type
>(index
)];
71 { for (size_type i
=0; i
<=max_index
; ++i
) detail_values
[i
] = val
; }
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()); }
79 V
* data() { return detail_values
; }
82 V detail_values
[max_index
+ 1];
87 class enumarray_iterator
{
91 typedef enumarray_iterator
<EA
> self_type
;
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
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
;
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
; }
110 operator=(self_type
const & other
) { m_buf
= other
.m_buf
; m_pos
= other
.m_pos
; return *this; }
113 template<typename EA
>
114 class enumarray_const_iterator
{
118 typedef enumarray_const_iterator
<EA
> self_type
;
119 typedef typename
EA::value_type
const value_type
;
120 typedef typename
EA::key_type key_type
;
121 typedef std::bidirectional_iterator_tag iterator_category
; //should be random access, but that would require define subtraction operators on the enums
123 typename
std::make_signed
<
124 typename
std::underlying_type
<typename
EA::key_type
>::type
>::type
126 typedef typename
EA::value_type
const * pointer
;
127 typedef typename
EA::value_type
const & reference
;
129 enumarray_const_iterator(EA
const & b
, size_t start_pos
)
130 : m_buf(b
), m_pos(start_pos
) {}
131 value_type
& operator*() const { return m_buf
[static_cast<key_type
>(m_pos
)]; }
132 value_type
* operator->() const { return &(operator*()); }
133 self_type
& operator++() { ++m_pos
; return *this; }
134 bool operator!=(self_type
const & other
) const { return &m_buf
!= &other
.m_buf
|| m_pos
!= other
.m_pos
; }
135 bool operator==(self_type
const & other
) const { return &m_buf
== &other
.m_buf
&& m_pos
== other
.m_pos
; }
136 enumarray_const_iterator
&
137 operator=(self_type
const & other
) { m_buf
= other
.m_buf
; m_pos
= other
.m_pos
; return *this; }
142 #endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */