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
23 #include <sal/config.h>
29 class enumarray_iterator
;
32 /// This is a container convenience class for arrays indexed by enum values.
34 /// This assumes that the 'enum class' definition
36 /// - has no holes in it's sequence of values
37 /// - defines a value called LAST which refers to the greatest constant.
39 /// \param E the 'enum class' type.
40 /// \param V the value type to be stored in the array
41 template<typename E
, typename V
>
42 class enumarray SAL_FINAL
45 typedef enumarray
<E
, V
> self_type
;
46 typedef enumarray_iterator
<self_type
> iterator
;
50 typedef size_t size_type
;
52 static const size_type max_index
= static_cast<size_type
>(E::LAST
);
54 const V
& operator[](E index
) const
56 assert(index
>=static_cast<E
>(0) && index
<=E::LAST
);
57 return detail_values
[static_cast<size_type
>(index
)];
60 V
& operator[](E index
)
62 assert(index
>=static_cast<E
>(0) && index
<=E::LAST
);
63 return detail_values
[static_cast<size_type
>(index
)];
67 { for (size_type i
=0; i
<=max_index
; ++i
) detail_values
[i
] = val
; }
69 static size_type
size() { return max_index
+ 1; }
70 iterator
begin() { return iterator(*this, 0); }
71 iterator
end() { return iterator(*this, size()); }
73 V
* data() { return detail_values
; }
76 V detail_values
[max_index
+ 1];
81 class enumarray_iterator
{
85 typedef enumarray_iterator
<EA
> self_type
;
86 typedef typename
EA::value_type value_type
;
87 typedef typename
EA::key_type key_type
;
88 typedef std::bidirectional_iterator_tag iterator_category
; //should be random access, but that would require define subtraction operators on the enums
89 typedef typename
EA::key_type difference_type
;
90 typedef typename
EA::value_type
* pointer
;
91 typedef typename
EA::value_type
& reference
;
93 enumarray_iterator(EA
& b
, size_t start_pos
)
94 : m_buf(&b
), m_pos(start_pos
) {}
95 value_type
&operator*() { return (*m_buf
)[static_cast<key_type
>(m_pos
)]; }
96 value_type
*operator->() { return &(operator*()); }
97 self_type
&operator++() { ++m_pos
; return *this; }
98 bool operator!=(const self_type
& other
) { return m_buf
!= other
.m_buf
|| m_pos
!= other
.m_pos
; }
99 bool operator==(const self_type
& other
) { return m_buf
== other
.m_buf
&& m_pos
== other
.m_pos
; }
104 #endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */