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