KiCad PCB EDA Suite
enum_vector.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 CERN
5 * @author Maciej Suminski <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef ENUM_VECTOR_H
22#define ENUM_VECTOR_H
23
24#include <type_traits>
25
44#define DEFINE_ENUM_VECTOR( enumName, ... ) \
45 enum enumName __VA_ARGS__; \
46 static constexpr enumName enumName##_vector[] = __VA_ARGS__;
47
48#define DECLARE_ENUM_VECTOR( className, enumName ) \
49 constexpr className::enumName className::enumName##_vector[];
50
51
66#define DEFINE_ENUM_CLASS_WITH_ITERATOR( enumName, beginVal, ... ) \
67 enum class enumName : int { beginVal, __VA_ARGS__, _ENUM_END }; \
68 typedef ENUM_ITERATOR<enumName, enumName::beginVal, enumName::_ENUM_END> enumName##_ITERATOR;
69
70template <typename T, T beginVal, T endVal>
72{
73 typedef typename std::underlying_type<T>::type val_t;
74 int val;
75
76public:
77 ENUM_ITERATOR( const T& f ) : val( static_cast<val_t>( f ) ) {}
78 ENUM_ITERATOR() : val( static_cast<val_t>( beginVal ) ) {}
80 {
81 val++;
82 return *this;
83 }
84
85 T operator*() { return static_cast<T>( val ); }
86 ENUM_ITERATOR begin() { return *this; }
87 ENUM_ITERATOR end() { return ENUM_ITERATOR( endVal ); }
88 bool operator!=( const ENUM_ITERATOR& aIt ) { return val != aIt.val; }
89};
90
91#endif /* ENUM_VECTOR_H */
ENUM_ITERATOR end()
Definition: enum_vector.h:87
ENUM_ITERATOR begin()
Definition: enum_vector.h:86
ENUM_ITERATOR operator++()
Definition: enum_vector.h:79
std::underlying_type< T >::type val_t
Definition: enum_vector.h:73
bool operator!=(const ENUM_ITERATOR &aIt)
Definition: enum_vector.h:88
ENUM_ITERATOR(const T &f)
Definition: enum_vector.h:77