NEST main@caf0ae8
 
Loading...
Searching...
No Matches
enum_bitfield.h
Go to the documentation of this file.
1/*
2 * enum_bitfield.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST 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
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef ENUM_BITFIELD_H
24#define ENUM_BITFIELD_H
25
26namespace nest
27{
28
77template < typename Enum >
79{
80 static const bool enable = false;
81};
82
83template < typename Enum >
84constexpr typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
85operator|( const Enum lhs, const Enum rhs )
86{
87 using underlying = typename std::underlying_type< const Enum >::type;
88 return static_cast< const Enum >( static_cast< underlying >( lhs ) | static_cast< underlying >( rhs ) );
89}
90
91template < typename Enum >
92constexpr typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
93operator&( const Enum lhs, const Enum rhs )
94{
95 using underlying = typename std::underlying_type< Enum >::type;
96 return static_cast< const Enum >( static_cast< underlying >( lhs ) & static_cast< underlying >( rhs ) );
97}
98
99template < typename Enum >
100constexpr typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
101operator^( const Enum& lhs, const Enum rhs )
102{
103 using underlying = typename std::underlying_type< Enum >::type;
104 return static_cast< const Enum >( static_cast< underlying >( lhs ) ^ static_cast< underlying >( rhs ) );
105}
106
107template < typename Enum >
108typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
109operator|=( Enum& lhs, Enum rhs )
110{
111 using underlying = typename std::underlying_type< Enum >::type;
112 lhs = static_cast< Enum >( static_cast< underlying >( lhs ) | static_cast< underlying >( rhs ) );
113 return lhs;
114}
115
116template < typename Enum >
117typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
118operator&=( Enum& lhs, Enum rhs )
119{
120 using underlying = typename std::underlying_type< Enum >::type;
121 lhs = static_cast< Enum >( static_cast< underlying >( lhs ) & static_cast< underlying >( rhs ) );
122 return lhs;
123}
124
125template < typename Enum >
126typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
127operator^=( Enum& lhs, Enum rhs )
128{
129 using underlying = typename std::underlying_type< Enum >::type;
130 lhs = static_cast< Enum >( static_cast< underlying >( lhs ) ^ static_cast< underlying >( rhs ) );
131 return lhs;
132}
133
134template < typename Enum >
135bool
136flag_is_set( const Enum en, const Enum flag )
137{
138 using underlying = typename std::underlying_type< Enum >::type;
139 return static_cast< underlying >( en & flag ) != 0;
140}
141}
142
143
144#endif /* ENUM_BITFIELD_H */
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
constexpr std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|(const Enum lhs, const Enum rhs)
Definition enum_bitfield.h:85
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&=(Enum &lhs, Enum rhs)
Definition enum_bitfield.h:118
constexpr std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator&(const Enum lhs, const Enum rhs)
Definition enum_bitfield.h:93
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^=(Enum &lhs, Enum rhs)
Definition enum_bitfield.h:127
std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator|=(Enum &lhs, Enum rhs)
Definition enum_bitfield.h:109
constexpr std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type operator^(const Enum &lhs, const Enum rhs)
Definition enum_bitfield.h:101
bool flag_is_set(const Enum en, const Enum flag)
Definition enum_bitfield.h:136
These methods support type-safe bitfields using C++'s enum classes.
Definition enum_bitfield.h:79
static const bool enable
Definition enum_bitfield.h:80