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: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 38 | /** iv_buffer points to the first address of the Chip Data File |
| 39 | buffer. **/ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 40 | const void* const iv_buffer; |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 41 | /** iv_bufferSize is the size of the data buffer. It is |
| 42 | initialized when the ChipDataStream class is instantiated. **/ |
| 43 | const size_t iv_bufferSize; |
| 44 | /** iv_asyncOffset keeps an offset into the buffer. This |
| 45 | offset is incremented appropriately as data is read. **/ |
| 46 | size_t iv_asyncOffset; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 47 | |
| 48 | public: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 49 | /* Constructors */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 50 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 51 | /** |
| 52 | When instantiating the ChipDataStream object a pointer to a |
| 53 | data buffer containing all the data and its size is passed |
| 54 | into the class. |
| 55 | **/ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 56 | ChipDataStream(void* i_buffer, size_t i_bufferSize) : |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 57 | iv_asyncOffset(0), iv_buffer(i_buffer), iv_bufferSize(i_bufferSize) |
| 58 | {} |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 59 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 60 | /** Eliminate copy and assignment operator constructors **/ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 61 | ChipDataStream(const ChipDataStream&) = delete; |
| 62 | ChipDataStream& operator=(const ChipDataStream&) = delete; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 63 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 64 | /** Destructor **/ |
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 | ~ChipDataStream() = default; |
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 | /** Functions **/ |
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 | /** |
| 71 | *@brief Default case for "operator>>" template. |
| 72 | *@param D: A type |
| 73 | *@param o_right: A pointer to where the data is stored |
| 74 | *@return *this: A pointer to "this" object |
| 75 | **/ |
| 76 | template <class D> |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 77 | ChipDataStream& operator>>(D& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 78 | { |
| 79 | read(&o_right, sizeof(D)); |
| 80 | return *this; |
| 81 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 82 | |
| 83 | private: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 84 | /** |
| 85 | *@brief Read does the copy from the buffer |
| 86 | *@param o_buf a pointer to the location to copy to |
| 87 | *@param i_size the size (in bytes) to copy |
| 88 | *@return None\n\n |
| 89 | **/ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 90 | void read(void* o_buf, size_t i_size) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 91 | { |
| 92 | /* Ensure memory is not accessed outside i_buffer */ |
| 93 | HEI_ASSERT((iv_asyncOffset + i_size) <= iv_bufferSize); |
| 94 | /* Copy appropriate bytes from i_buffer to o_buff */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 95 | memcpy(o_buf, (char*)iv_buffer + iv_asyncOffset, i_size); |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 96 | /* Increment asynchronous offset to next piece of data */ |
| 97 | iv_asyncOffset = iv_asyncOffset + i_size; |
| 98 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 102 | * Note: The specializations below ensure the big-endian Chip Data File |
| 103 | * format is converted into the host format. |
| 104 | */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 105 | |
| 106 | /** |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 107 | * @brief This template extracts a uint16_t data type from the data buffer |
| 108 | * @param o_right: A pointer to where the data is stored |
| 109 | * @return *this: A pointer to "this" object |
| 110 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 111 | template <> |
| 112 | inline ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 113 | { |
| 114 | read(&o_right, sizeof(o_right)); |
| 115 | be16toh(o_right); |
| 116 | return *this; |
| 117 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 118 | |
| 119 | /**@brief This template extracts an int16_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 120 | * @param o_right: A pointer to where the data is stored |
| 121 | * @return *this: A pointer to "this" object |
| 122 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 123 | template <> |
| 124 | inline ChipDataStream& ChipDataStream::operator>>(int16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 125 | { |
| 126 | read(&o_right, sizeof(o_right)); |
| 127 | be16toh(o_right); |
| 128 | return *this; |
| 129 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 130 | |
| 131 | /**@brief This template extracts a uint32_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 132 | * @param o_right: A pointer to where the data is stored |
| 133 | * @return *this: A pointer to "this" object |
| 134 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 135 | template <> |
| 136 | inline ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 137 | { |
| 138 | read(&o_right, sizeof(o_right)); |
| 139 | be32toh(o_right); |
| 140 | return *this; |
| 141 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 142 | |
| 143 | /**@brief This template extracts an int32_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 144 | * @param o_right: A pointer to where the data is stored |
| 145 | * @return *this: A pointer to "this" object |
| 146 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 147 | template <> |
| 148 | inline ChipDataStream& ChipDataStream::operator>>(int32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 149 | { |
| 150 | read(&o_right, sizeof(o_right)); |
| 151 | be32toh(o_right); |
| 152 | return *this; |
| 153 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 154 | |
| 155 | /**@brief This template extracts a uint64_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 156 | * @param o_right: A pointer to where the data is stored |
| 157 | * @return *this: A pointer to "this" object |
| 158 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 159 | template <> |
| 160 | inline ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 161 | { |
| 162 | read(&o_right, sizeof(o_right)); |
| 163 | be64toh(o_right); |
| 164 | return *this; |
| 165 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 166 | |
| 167 | /**@brief This template extracts an int64_t type from the data buffer |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 168 | * @param o_right: A pointer to where the data is stored |
| 169 | * @return *this: A pointer to "this" object |
| 170 | **/ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 171 | template <> |
| 172 | inline ChipDataStream& ChipDataStream::operator>>(int64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 173 | { |
| 174 | read(&o_right, sizeof(o_right)); |
| 175 | be64toh(o_right); |
| 176 | return *this; |
| 177 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 178 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 179 | } // namespace libhei |