Line data Source code
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_ENUMRANGE_HXX
21 : #define INCLUDED_O3TL_ENUMRANGE_HXX
22 :
23 : #include <sal/config.h>
24 :
25 : namespace o3tl {
26 :
27 : ///
28 : /// This is a container convenience class iterating over scoped enumerations.
29 : ///
30 : /// This assumes that the 'enum class' definition
31 : /// - starts at zero
32 : /// - has no holes in it's sequence of values
33 : /// - defines a value called LAST which refers to the greatest constant.
34 : ///
35 : /// Use like this:
36 : /// enum class COLOR { RED, GREEN, BLUE, LAST=BLUE };
37 : /// for( auto e : o3tl::enumrange<Color>() )
38 : /// .....;
39 : ///
40 : /// \param T the 'enum class' type.
41 : template< typename T>
42 : class enumrange
43 : {
44 : public:
45 : class Iterator
46 : {
47 : public:
48 1582 : Iterator( int value ) :
49 1582 : m_value( value )
50 : {
51 1582 : }
52 :
53 9563 : T operator*( void ) const
54 : {
55 9563 : return static_cast<T>(m_value);
56 : }
57 :
58 9563 : void operator++( void )
59 : {
60 9563 : ++m_value;
61 9563 : }
62 :
63 10354 : bool operator!=( Iterator rhs )
64 : {
65 10354 : return m_value != rhs.m_value;
66 : }
67 :
68 : private:
69 : int m_value;
70 : };
71 : };
72 :
73 : template< typename T >
74 791 : typename enumrange<T>::Iterator begin( enumrange<T> )
75 : {
76 791 : return typename enumrange<T>::Iterator( (int)0 );
77 : }
78 :
79 : template< typename T >
80 791 : typename enumrange<T>::Iterator end( enumrange<T> )
81 : {
82 791 : return typename enumrange<T>::Iterator( (static_cast<int>(T::LAST)) + 1 );
83 : }
84 :
85 : }; // namespace o3tl
86 :
87 : #endif /* INCLUDED_O3TL_ENUMRANGE_HXX */
88 :
89 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|