2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_DETAIL_BITITERATORDETAIL_H_
18 #define FOLLY_DETAIL_BITITERATORDETAIL_H_
21 #include <type_traits>
22 #include <boost/iterator/iterator_adaptor.hpp>
26 template <class BaseIter> class BitIterator;
28 namespace bititerator_detail {
30 // Reference to a bit.
31 // Templatize on both parent reference and value types to capture
32 // const-ness correctly and to work with the case where Ref is a
33 // reference-like type (not T&), just like our BitReference here.
34 template <class Ref, class Value>
37 BitReference(Ref r, size_t bit) : ref_(r), bit_(bit) { }
39 /* implicit */ operator bool() const {
40 return ref_ & (one_ << bit_);
43 BitReference& operator=(bool b) {
53 ref_ |= (one_ << bit_);
57 ref_ &= ~(one_ << bit_);
61 ref_ ^= (one_ << bit_);
65 // shortcut to avoid writing static_cast everywhere
66 const static Value one_ = 1;
72 template <class BaseIter>
73 struct BitIteratorBase {
74 static_assert(std::is_integral<typename BaseIter::value_type>::value,
75 "BitIterator may only be used with integral types");
76 typedef boost::iterator_adaptor<
77 BitIterator<BaseIter>, // Derived
80 boost::use_default, // CategoryOrTraversal
81 bititerator_detail::BitReference<
82 typename std::iterator_traits<BaseIter>::reference,
83 typename std::iterator_traits<BaseIter>::value_type
89 } // namespace bititerator_detail
92 #endif /* FOLLY_DETAIL_BITITERATORDETAIL_H_ */