Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 #pragma once
     18 
     19 #include <iterator>
     20 #include <memory>
     21 
     22 namespace bluetooth {
     23 
     24 // Forward declare Packet class
     25 class Packet;
     26 
     27 // Iterator is a custom iterator class for Packets.
     28 class Iterator
     29     : public std::iterator<std::random_access_iterator_tag, uint8_t> {
     30  public:
     31   Iterator(std::shared_ptr<const Packet> packet, size_t i);
     32   Iterator(const Iterator& itr);
     33 
     34   // All addition and subtraction operators are bounded from 0 to the length of
     35   // the packet.
     36   Iterator operator+(size_t offset);
     37   Iterator& operator+=(size_t offset);
     38   Iterator operator++(int);
     39   Iterator& operator++();
     40 
     41   Iterator operator-(size_t offset);
     42   int operator-(const Iterator& itr);
     43   Iterator& operator-=(size_t offset);
     44   Iterator operator--(int);
     45   Iterator& operator--();
     46 
     47   Iterator& operator=(const Iterator& itr);
     48 
     49   bool operator!=(const Iterator& itr) const;
     50   bool operator==(const Iterator& itr) const;
     51 
     52   bool operator<(const Iterator& itr) const;
     53   bool operator>(const Iterator& itr) const;
     54 
     55   bool operator<=(const Iterator& itr) const;
     56   bool operator>=(const Iterator& itr) const;
     57 
     58   uint8_t operator*() const;
     59 
     60   template <typename FixedWidthIntegerType>
     61   FixedWidthIntegerType extract() {
     62     static_assert(std::is_integral<FixedWidthIntegerType>::value,
     63                   "Iterator::extract requires an integral type.");
     64 
     65     FixedWidthIntegerType extracted_value = 0;
     66     for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) {
     67       extracted_value |= static_cast<FixedWidthIntegerType>(**this) << i * 8;
     68       (*this)++;
     69     }
     70 
     71     return extracted_value;
     72   }
     73 
     74   uint8_t extract8() { return extract<uint8_t>(); }
     75   uint16_t extract16() { return extract<uint16_t>(); }
     76   uint32_t extract32() { return extract<uint32_t>(); }
     77   uint64_t extract64() { return extract<uint64_t>(); }
     78 
     79  private:
     80   std::shared_ptr<const Packet> packet_;
     81   size_t index_;
     82 };  // Iterator
     83 
     84 }  // namespace bluetooth