KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef ENUM_VECTOR_H
23#define ENUM_VECTOR_H
24
25#include <iterator>
26#include <type_traits>
27
47#define DEFINE_ENUM_VECTOR( enumName, ... ) \
48 enum enumName __VA_ARGS__; \
49 static constexpr enumName enumName##_vector[] = __VA_ARGS__;
50
51#define DECLARE_ENUM_VECTOR( className, enumName ) \
52 constexpr className::enumName className::enumName##_vector[];
53
54
69#define DEFINE_ENUM_CLASS_WITH_ITERATOR( enumName, beginVal, ... ) \
70 enum class enumName : int { beginVal, __VA_ARGS__, _ENUM_END }; \
71 typedef ENUM_ITERATOR<enumName, enumName::beginVal, enumName::_ENUM_END> enumName##_ITERATOR;
72
73template <typename T, T beginVal, T endVal>
75{
76 typedef typename std::underlying_type<T>::type val_t;
77 int val;
78
79public:
80 using value_type = T;
81 using difference_type = std::ptrdiff_t;
82 using pointer = T*;
83 using reference = T&;
84 using iterator_category = std::input_iterator_tag;
85
86 ENUM_ITERATOR( const T& f ) : val( static_cast<val_t>( f ) ) {}
87 ENUM_ITERATOR() : val( static_cast<val_t>( beginVal ) ) {}
89 {
90 val++;
91 return *this;
92 }
93
94 T operator*() { return static_cast<T>( val ); }
95 ENUM_ITERATOR begin() { return *this; }
96 ENUM_ITERATOR end() { return ENUM_ITERATOR( endVal ); }
97 bool operator==( const ENUM_ITERATOR& aIt ) const { return val == aIt.val; }
98 bool operator!=( const ENUM_ITERATOR& aIt ) const { return val != aIt.val; }
99};
100
101#endif /* ENUM_VECTOR_H */
ENUM_ITERATOR end()
Definition: enum_vector.h:96
ENUM_ITERATOR begin()
Definition: enum_vector.h:95
ENUM_ITERATOR operator++()
Definition: enum_vector.h:88
std::underlying_type< T >::type val_t
Definition: enum_vector.h:76
std::ptrdiff_t difference_type
Definition: enum_vector.h:81
ENUM_ITERATOR(const T &f)
Definition: enum_vector.h:86
bool operator==(const ENUM_ITERATOR &aIt) const
Definition: enum_vector.h:97
bool operator!=(const ENUM_ITERATOR &aIt) const
Definition: enum_vector.h:98
std::input_iterator_tag iterator_category
Definition: enum_vector.h:84