Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2013 Florian Philipp
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <bitset>
- #include <cassert>
- namespace {
- template<class EnumClass, EnumClass min, EnumClass max>
- class EnumSet
- {
- /* static_cast to size_t works in all cases, even when underlying enum
- * values are negative
- */
- typedef std::bitset<
- static_cast<std::size_t>(max) - static_cast<std::size_t>(min) + 1> set_t;
- set_t set;
- public:
- typedef typename set_t::reference reference;
- reference operator[](EnumClass e)
- {
- return set[static_cast<std::size_t>(e) - static_cast<std::size_t>(min)];
- }
- bool operator[](EnumClass e) const
- {
- return set[static_cast<std::size_t>(e) - static_cast<std::size_t>(min)];
- }
- };
- enum class Source {
- analog_radio, internet_radio, bluetooth
- };
- typedef EnumSet<Source, Source::analog_radio, Source::bluetooth> SourceSet;
- }
- int main()
- {
- SourceSet attributes;
- attributes[Source::analog_radio] = true;
- attributes[Source::bluetooth] = true;
- assert(attributes[Source::analog_radio] == true);
- assert(attributes[Source::internet_radio] == false);
- assert(attributes[Source::bluetooth] == true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment