blob: e6754204d292648265ebc38773612915fa0f1d12 [file] [log] [blame]
Paul Greenwood31a54882019-08-01 17:05:09 -05001#pragma once
2
3/**
4@file hei_chip_data_stream.hpp
5@brief Description: The ChipDataStream class
6
Zane Shelley83da2452019-10-25 15:45:34 -05007ChipDataStream makes it possible to read a buffer of binary data using the
8stream operators, ">>".
Paul Greenwood31a54882019-08-01 17:05:09 -05009
10Instantiate ChipDataStream class with a pointer to a data buffer
11and its size in bytes as below:
12
13 ChipDataStream cds(streamData,552);
14
15Use the ">>" stream operator then to read the basic integral types
16(bool, char, uint8_t, int8_t, uint16_t, int16_t, uint32_t,
17int32_t, uint64_t, and int64_t).
18
19Endian issues are taken care of by template specialization.
20**/
21
22#include <endian.h>
Paul Greenwood31a54882019-08-01 17:05:09 -050023#include <string.h>
24
Zane Shelleyca9f6252019-10-25 21:17:30 -050025#include <hei_includes.hpp>
26
Paul Greenwood31a54882019-08-01 17:05:09 -050027namespace libhei
28{
29
30/**
Zane Shelley83da2452019-10-25 15:45:34 -050031 * ChipDataStream
32 * ChipDataStream offers convenient stream operator access to binary data.
33 **/
Paul Greenwood31a54882019-08-01 17:05:09 -050034class ChipDataStream
35{
36
37 private:
38
Zane Shelley83da2452019-10-25 15:45:34 -050039 /** iv_buffer points to the first address of the Chip Data File
40 buffer. **/
Zane Shelleyfe27b652019-10-28 11:33:07 -050041 const void* const iv_buffer;
Zane Shelley83da2452019-10-25 15:45:34 -050042 /** 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 Greenwood31a54882019-08-01 17:05:09 -050048
49 public:
50
Zane Shelley83da2452019-10-25 15:45:34 -050051 /* Constructors */
Paul Greenwood31a54882019-08-01 17:05:09 -050052
Zane Shelley83da2452019-10-25 15:45:34 -050053 /**
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 **/
Zane Shelleyfe27b652019-10-28 11:33:07 -050058 ChipDataStream(void* i_buffer, size_t i_bufferSize) :
Zane Shelley83da2452019-10-25 15:45:34 -050059 iv_asyncOffset(0), iv_buffer(i_buffer), iv_bufferSize(i_bufferSize)
60 {}
Paul Greenwood31a54882019-08-01 17:05:09 -050061
Zane Shelley83da2452019-10-25 15:45:34 -050062 /** Eliminate copy and assignment operator constructors **/
Zane Shelleyfe27b652019-10-28 11:33:07 -050063 ChipDataStream(const ChipDataStream&) = delete;
64 ChipDataStream& operator=(const ChipDataStream&) = delete;
Paul Greenwood31a54882019-08-01 17:05:09 -050065
Zane Shelley83da2452019-10-25 15:45:34 -050066 /** Destructor **/
Paul Greenwood31a54882019-08-01 17:05:09 -050067
Zane Shelley83da2452019-10-25 15:45:34 -050068 ~ChipDataStream() = default;
Paul Greenwood31a54882019-08-01 17:05:09 -050069
Zane Shelley83da2452019-10-25 15:45:34 -050070 /** Functions **/
Paul Greenwood31a54882019-08-01 17:05:09 -050071
Zane Shelley83da2452019-10-25 15:45:34 -050072 /**
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>
Zane Shelleyfe27b652019-10-28 11:33:07 -050079 ChipDataStream& operator>>(D& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -050080 {
81 read(&o_right, sizeof(D));
82 return *this;
83 }
Paul Greenwood31a54882019-08-01 17:05:09 -050084
85 private:
86
Zane Shelley83da2452019-10-25 15:45:34 -050087 /**
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 **/
Zane Shelleyfe27b652019-10-28 11:33:07 -050093 void read(void* o_buf, size_t i_size)
Zane Shelley83da2452019-10-25 15:45:34 -050094 {
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 */
Zane Shelleyfe27b652019-10-28 11:33:07 -050098 memcpy(o_buf, (char*)iv_buffer + iv_asyncOffset, i_size);
Zane Shelley83da2452019-10-25 15:45:34 -050099 /* Increment asynchronous offset to next piece of data */
100 iv_asyncOffset = iv_asyncOffset + i_size;
101 }
Paul Greenwood31a54882019-08-01 17:05:09 -0500102};
103
104/*
Zane Shelley83da2452019-10-25 15:45:34 -0500105 * Note: The specializations below ensure the big-endian Chip Data File
106 * format is converted into the host format.
107 */
Paul Greenwood31a54882019-08-01 17:05:09 -0500108
109/**
Zane Shelley83da2452019-10-25 15:45:34 -0500110 * @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 Greenwood31a54882019-08-01 17:05:09 -0500114template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500115ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500116{
117 read(&o_right, sizeof(o_right));
118 be16toh(o_right);
119 return *this;
120}
Paul Greenwood31a54882019-08-01 17:05:09 -0500121
122/**@brief This template extracts an int16_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500123 * @param o_right: A pointer to where the data is stored
124 * @return *this: A pointer to "this" object
125 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500126template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500127ChipDataStream& ChipDataStream::operator>>(int16_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500128{
129 read(&o_right, sizeof(o_right));
130 be16toh(o_right);
131 return *this;
132}
Paul Greenwood31a54882019-08-01 17:05:09 -0500133
134/**@brief This template extracts a uint32_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500135 * @param o_right: A pointer to where the data is stored
136 * @return *this: A pointer to "this" object
137 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500138template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500139ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500140{
141 read(&o_right, sizeof(o_right));
142 be32toh(o_right);
143 return *this;
144}
Paul Greenwood31a54882019-08-01 17:05:09 -0500145
146/**@brief This template extracts an int32_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500147 * @param o_right: A pointer to where the data is stored
148 * @return *this: A pointer to "this" object
149 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500150template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500151ChipDataStream& ChipDataStream::operator>>(int32_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500152{
153 read(&o_right, sizeof(o_right));
154 be32toh(o_right);
155 return *this;
156}
Paul Greenwood31a54882019-08-01 17:05:09 -0500157
158/**@brief This template extracts a uint64_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500159 * @param o_right: A pointer to where the data is stored
160 * @return *this: A pointer to "this" object
161 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500162template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500163ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500164{
165 read(&o_right, sizeof(o_right));
166 be64toh(o_right);
167 return *this;
168}
Paul Greenwood31a54882019-08-01 17:05:09 -0500169
170/**@brief This template extracts an int64_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500171 * @param o_right: A pointer to where the data is stored
172 * @return *this: A pointer to "this" object
173 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500174template <> inline
Zane Shelleyfe27b652019-10-28 11:33:07 -0500175ChipDataStream& ChipDataStream::operator>>(int64_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500176{
177 read(&o_right, sizeof(o_right));
178 be64toh(o_right);
179 return *this;
180}
Paul Greenwood31a54882019-08-01 17:05:09 -0500181
Zane Shelley83da2452019-10-25 15:45:34 -0500182} // namespace libhei