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 * @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 <iterator>
25#include <type_traits>
26
45#define DEFINE_ENUM_VECTOR( enumName, ... ) \
46 enum enumName __VA_ARGS__; \
47 static constexpr enumName enumName##_vector[] = __VA_ARGS__;
48
49#define DECLARE_ENUM_VECTOR( className, enumName ) \
50 constexpr className::enumName className::enumName##_vector[];
51
52
67#define DEFINE_ENUM_CLASS_WITH_ITERATOR( enumName, beginVal, ... ) \
68 enum class enumName : int { beginVal, __VA_ARGS__, _ENUM_END }; \
69 typedef ENUM_ITERATOR<enumName, enumName::beginVal, enumName::_ENUM_END> enumName##_ITERATOR;
70
71template <typename T, T beginVal, T endVal>
73{
74 typedef typename std::underlying_type<T>::type val_t;
75 int val;
76
77public:
78 using value_type = T;
79 using difference_type = std::ptrdiff_t;
80 using pointer = T*;
81 using reference = T&;
82 using iterator_category = std::input_iterator_tag;
83
84 ENUM_ITERATOR( const T& f ) : val( static_cast<val_t>( f ) ) {}
85 ENUM_ITERATOR() : val( static_cast<val_t>( beginVal ) ) {}
87 {
88 val++;
89 return *this;
90 }
91
92 T operator*() { return static_cast<T>( val ); }
93 ENUM_ITERATOR begin() { return *this; }
94 ENUM_ITERATOR end() { return ENUM_ITERATOR( endVal ); }
95 bool operator==( const ENUM_ITERATOR& aIt ) const { return val == aIt.val; }
96 bool operator!=( const ENUM_ITERATOR& aIt ) const { return val != aIt.val; }
97};
98
99#endif /* ENUM_VECTOR_H */
ENUM_ITERATOR end()
Definition: enum_vector.h:94
ENUM_ITERATOR begin()
Definition: enum_vector.h:93
ENUM_ITERATOR operator++()
Definition: enum_vector.h:86
std::underlying_type< T >::type val_t
Definition: enum_vector.h:74
std::ptrdiff_t difference_type
Definition: enum_vector.h:79
ENUM_ITERATOR(const T &f)
Definition: enum_vector.h:84
bool operator==(const ENUM_ITERATOR &aIt) const
Definition: enum_vector.h:95
bool operator!=(const ENUM_ITERATOR &aIt) const
Definition: enum_vector.h:96
std::input_iterator_tag iterator_category
Definition: enum_vector.h:82