Home | History | Annotate | Download | only in include
      1 #pragma once
      2 #include <iterator>
      3 #include <memory>
      4 #include <type_traits>
      5 
      6 #include "base/logging.h"
      7 
      8 namespace test_vendor_lib {
      9 
     10 // Iterator is a custom iterator class for HciPackets.
     11 class Iterator
     12     : public std::iterator<std::random_access_iterator_tag, uint8_t> {
     13  public:
     14   Iterator(std::shared_ptr<class HciPacket> packet, size_t i);
     15   Iterator(const Iterator& itr);
     16 
     17   ~Iterator() {}
     18 
     19   operator bool() const;
     20 
     21   // All addition and subtraction operators are bounded from 0 to the length of
     22   // the packet.
     23   Iterator operator+(size_t offset);
     24   Iterator& operator+=(size_t offset);
     25   Iterator operator++(int);
     26   Iterator& operator++();
     27 
     28   Iterator operator-(size_t offset);
     29   int operator-(Iterator& itr);
     30   Iterator& operator-=(size_t offset);
     31   Iterator operator--(int);
     32   Iterator& operator--();
     33 
     34   Iterator& operator=(const Iterator& itr);
     35 
     36   bool operator!=(Iterator& itr);
     37   bool operator==(Iterator& itr);
     38 
     39   bool operator<(Iterator& itr);
     40   bool operator>(Iterator& itr);
     41 
     42   bool operator<=(Iterator& itr);
     43   bool operator>=(Iterator& itr);
     44 
     45   uint8_t& operator*() const;
     46   uint8_t* operator->() const;
     47 
     48   // Get the next sizeof(FixedWidthIntegerType) bytes and return the filled type
     49   template <typename FixedWidthIntegerType>
     50   FixedWidthIntegerType extract() {
     51     static_assert(std::is_integral<FixedWidthIntegerType>::value,
     52                   "Iterator::extract requires an integral type.");
     53     FixedWidthIntegerType extracted_value = 0;
     54 
     55     for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) {
     56       extracted_value |= static_cast<FixedWidthIntegerType>(**this) << i * 8;
     57       (*this)++;
     58     }
     59 
     60     return extracted_value;
     61   }
     62 
     63  private:
     64   std::shared_ptr<class HciPacket> hci_packet_;
     65   size_t index_;
     66 
     67 };  // Iterator
     68 
     69 // HciPacket is an abstract class that will serve as the base class for all
     70 // packet types.
     71 class HciPacket : public std::enable_shared_from_this<HciPacket> {
     72  public:
     73   virtual ~HciPacket() = default;
     74 
     75   Iterator get_begin();
     76   Iterator get_end();
     77 
     78   uint8_t operator[](size_t i);
     79 
     80   virtual size_t get_length() = 0;
     81 
     82   virtual uint8_t& get_at_index(size_t index) = 0;
     83 
     84 };  // HciPacket
     85 
     86 };  // namespace test_vendor_lib
     87