Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | /** |
| 4 | @file hei_chip_data_stream.hpp |
| 5 | @brief Description: The ChipDataStream class |
| 6 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 7 | ChipDataStream makes it possible to read a buffer of binary data using the |
| 8 | stream operators, ">>". |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 9 | |
| 10 | Instantiate ChipDataStream class with a pointer to a data buffer |
| 11 | and its size in bytes as below: |
| 12 | |
| 13 | ChipDataStream cds(streamData,552); |
| 14 | |
| 15 | Use the ">>" stream operator then to read the basic integral types |
| 16 | (bool, char, uint8_t, int8_t, uint16_t, int16_t, uint32_t, |
| 17 | int32_t, uint64_t, and int64_t). |
| 18 | |
| 19 | Endian issues are taken care of by template specialization. |
| 20 | **/ |
| 21 | |
| 22 | #include <endian.h> |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Zane Shelley | ca9f625 | 2019-10-25 21:17:30 -0500 | [diff] [blame^] | 25 | #include <hei_includes.hpp> |
| 26 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 27 | namespace libhei |
| 28 | { |
| 29 | |
| 30 | /** |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 31 | * ChipDataStream |
| 32 | * ChipDataStream offers convenient stream operator access to binary data. |
| 33 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 34 | class ChipDataStream |
| 35 | { |
| 36 | |
| 37 | private: |
| 38 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 39 | /** iv_buffer points to the first address of the Chip Data File |
| 40 | buffer. **/ |
| 41 | const void * const iv_buffer; |
| 42 | /** iv_bufferSize is the size of the data buffer. It is |
| 43 | initialized when the ChipDataStream class is instantiated. **/ |
| 44 | const size_t iv_bufferSize; |
| 45 | /** iv_asyncOffset keeps an offset into the buffer. This |
| 46 | offset is incremented appropriately as data is read. **/ |
| 47 | size_t iv_asyncOffset; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 48 | |
| 49 | public: |
| 50 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 51 | /* Constructors */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 52 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 53 | /** |
| 54 | When instantiating the ChipDataStream object a pointer to a |
| 55 | data buffer containing all the data and its size is passed |
| 56 | into the class. |
| 57 | **/ |
| 58 | ChipDataStream(void * i_buffer, size_t i_bufferSize) : |
| 59 | iv_asyncOffset(0), iv_buffer(i_buffer), iv_bufferSize(i_bufferSize) |
| 60 | {} |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 61 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 62 | /** Eliminate copy and assignment operator constructors **/ |
| 63 | ChipDataStream(const ChipDataStream &) = delete; |
| 64 | ChipDataStream & operator=(const ChipDataStream &) = delete; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 65 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 66 | /** Destructor **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 67 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 68 | ~ChipDataStream() = default; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 69 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 70 | /** Functions **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 71 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 72 | /** |
| 73 | *@brief Default case for "operator>>" template. |
| 74 | *@param D: A type |
| 75 | *@param o_right: A pointer to where the data is stored |
| 76 | *@return *this: A pointer to "this" object |
| 77 | **/ |
| 78 | template <class D> |
| 79 | ChipDataStream & operator>>(D & o_right) |
| 80 | { |
| 81 | read(&o_right, sizeof(D)); |
| 82 | return *this; |
| 83 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 87 | /** |
| 88 | *@brief Read does the copy from the buffer |
| 89 | *@param o_buf a pointer to the location to copy to |
| 90 | *@param i_size the size (in bytes) to copy |
| 91 | *@return None\n\n |
| 92 | **/ |
| 93 | void read(void * o_buf, size_t i_size) |
| 94 | { |
| 95 | /* Ensure memory is not accessed outside i_buffer */ |
| 96 | HEI_ASSERT((iv_asyncOffset + i_size) <= iv_bufferSize); |
| 97 | /* Copy appropriate bytes from i_buffer to o_buff */ |
| 98 | memcpy(o_buf, (char *)iv_buffer + iv_asyncOffset, i_size); |
| 99 | /* Increment asynchronous offset to next piece of data */ |
| 100 | iv_asyncOffset = iv_asyncOffset + i_size; |
| 101 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 105 | * Note: The specializations below ensure the big-endian Chip Data File |
| 106 | * format is converted into the host format. |
| 107 | */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 108 | |
| 109 | /** |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 110 | * @brief This template extracts a uint16_t data type from the data buffer |
| 111 | * @param o_right: A pointer to where the data is stored |
| 112 | * @return *this: A pointer to "this" object |
| 113 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 114 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 115 | ChipDataStream & ChipDataStream::operator>>(uint16_t & o_right) |
| 116 | { |
| 117 | read(&o_right, sizeof(o_right)); |
| 118 | be16toh(o_right); |
| 119 | return *this; |
| 120 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 121 | |
| 122 | /**@brief This template extracts an int16_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 123 | * @param o_right: A pointer to where the data is stored |
| 124 | * @return *this: A pointer to "this" object |
| 125 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 126 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 127 | ChipDataStream & ChipDataStream::operator>>(int16_t & o_right) |
| 128 | { |
| 129 | read(&o_right, sizeof(o_right)); |
| 130 | be16toh(o_right); |
| 131 | return *this; |
| 132 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 133 | |
| 134 | /**@brief This template extracts a uint32_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 135 | * @param o_right: A pointer to where the data is stored |
| 136 | * @return *this: A pointer to "this" object |
| 137 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 138 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 139 | ChipDataStream & ChipDataStream::operator>>(uint32_t & o_right) |
| 140 | { |
| 141 | read(&o_right, sizeof(o_right)); |
| 142 | be32toh(o_right); |
| 143 | return *this; |
| 144 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 145 | |
| 146 | /**@brief This template extracts an int32_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 147 | * @param o_right: A pointer to where the data is stored |
| 148 | * @return *this: A pointer to "this" object |
| 149 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 150 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 151 | ChipDataStream & ChipDataStream::operator>>(int32_t & o_right) |
| 152 | { |
| 153 | read(&o_right, sizeof(o_right)); |
| 154 | be32toh(o_right); |
| 155 | return *this; |
| 156 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 157 | |
| 158 | /**@brief This template extracts a uint64_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 159 | * @param o_right: A pointer to where the data is stored |
| 160 | * @return *this: A pointer to "this" object |
| 161 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 162 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 163 | ChipDataStream & ChipDataStream::operator>>(uint64_t & o_right) |
| 164 | { |
| 165 | read(&o_right, sizeof(o_right)); |
| 166 | be64toh(o_right); |
| 167 | return *this; |
| 168 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 169 | |
| 170 | /**@brief This template extracts an int64_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 171 | * @param o_right: A pointer to where the data is stored |
| 172 | * @return *this: A pointer to "this" object |
| 173 | **/ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 174 | template <> inline |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 175 | ChipDataStream & ChipDataStream::operator>>(int64_t & o_right) |
| 176 | { |
| 177 | read(&o_right, sizeof(o_right)); |
| 178 | be64toh(o_right); |
| 179 | return *this; |
| 180 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 181 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 182 | } // namespace libhei |