Valkka  1.6.1
OpenSource Video Management
enumiter.h
Go to the documentation of this file.
1 #ifndef enumiter_HEADER_GUARD
2 #define enumiter_HEADER_GUARD
3 /*
4  * enumiter.h : iterate over enum class
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
36 // ripped from: https://stackoverflow.com/questions/8498300/allow-for-range-based-for-with-enum-classes
37 
38 #include <iostream>
39 
40 template< typename T >
41 class Enum
42 {
43 public:
44  class Iterator
45  {
46  public:
47  Iterator( int value ) :
48  m_value( value )
49  { }
50 
51  T operator*( void ) const
52  {
53  return (T)m_value;
54  }
55 
56  void operator++( void )
57  {
58  ++m_value;
59  }
60 
61  bool operator!=( Iterator rhs )
62  {
63  return m_value != rhs.m_value;
64  }
65 
66  private:
67  int m_value;
68  };
69 
70 };
71 
72 template< typename T >
73 typename Enum<T>::Iterator begin( Enum<T> )
74 {
75  return typename Enum<T>::Iterator( (int)T::First );
76 }
77 
78 template< typename T >
79 typename Enum<T>::Iterator end( Enum<T> )
80 {
81  return typename Enum<T>::Iterator( ((int)T::Last) + 1 );
82 }
83 
84 /* use like this:
85 for( auto e: Enum<FrameClass>() ) {
86  std::cout << ((int)e) << std::endl;
87 }
88 */
89 
90 
91 #endif
Definition: enumiter.h:45
Definition: enumiter.h:42