blob: 25b8635d82b97d5afe61d10cd8b810dcd0b2e0e3 [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>
23#include <hei_includes.hpp>
24#include <string.h>
25
26namespace libhei
27{
28
29/**
Zane Shelley83da2452019-10-25 15:45:34 -050030 * ChipDataStream
31 * ChipDataStream offers convenient stream operator access to binary data.
32 **/
Paul Greenwood31a54882019-08-01 17:05:09 -050033class ChipDataStream
34{
35
36 private:
37
Zane Shelley83da2452019-10-25 15:45:34 -050038 /** 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 Greenwood31a54882019-08-01 17:05:09 -050047
48 public:
49
Zane Shelley83da2452019-10-25 15:45:34 -050050 /* Constructors */
Paul Greenwood31a54882019-08-01 17:05:09 -050051
Zane Shelley83da2452019-10-25 15:45:34 -050052 /**
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 Greenwood31a54882019-08-01 17:05:09 -050060
Zane Shelley83da2452019-10-25 15:45:34 -050061 /** Eliminate copy and assignment operator constructors **/
62 ChipDataStream(const ChipDataStream &) = delete;
63 ChipDataStream & operator=(const ChipDataStream &) = delete;
Paul Greenwood31a54882019-08-01 17:05:09 -050064
Zane Shelley83da2452019-10-25 15:45:34 -050065 /** Destructor **/
Paul Greenwood31a54882019-08-01 17:05:09 -050066
Zane Shelley83da2452019-10-25 15:45:34 -050067 ~ChipDataStream() = default;
Paul Greenwood31a54882019-08-01 17:05:09 -050068
Zane Shelley83da2452019-10-25 15:45:34 -050069 /** Functions **/
Paul Greenwood31a54882019-08-01 17:05:09 -050070
Zane Shelley83da2452019-10-25 15:45:34 -050071 /**
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 Greenwood31a54882019-08-01 17:05:09 -050083
84 private:
85
Zane Shelley83da2452019-10-25 15:45:34 -050086 /**
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 Greenwood31a54882019-08-01 17:05:09 -0500101};
102
103/*
Zane Shelley83da2452019-10-25 15:45:34 -0500104 * Note: The specializations below ensure the big-endian Chip Data File
105 * format is converted into the host format.
106 */
Paul Greenwood31a54882019-08-01 17:05:09 -0500107
108/**
Zane Shelley83da2452019-10-25 15:45:34 -0500109 * @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 Greenwood31a54882019-08-01 17:05:09 -0500113template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500114ChipDataStream & ChipDataStream::operator>>(uint16_t & o_right)
115{
116 read(&o_right, sizeof(o_right));
117 be16toh(o_right);
118 return *this;
119}
Paul Greenwood31a54882019-08-01 17:05:09 -0500120
121/**@brief This template extracts an int16_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500122 * @param o_right: A pointer to where the data is stored
123 * @return *this: A pointer to "this" object
124 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500125template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500126ChipDataStream & ChipDataStream::operator>>(int16_t & o_right)
127{
128 read(&o_right, sizeof(o_right));
129 be16toh(o_right);
130 return *this;
131}
Paul Greenwood31a54882019-08-01 17:05:09 -0500132
133/**@brief This template extracts a uint32_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500134 * @param o_right: A pointer to where the data is stored
135 * @return *this: A pointer to "this" object
136 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500137template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500138ChipDataStream & ChipDataStream::operator>>(uint32_t & o_right)
139{
140 read(&o_right, sizeof(o_right));
141 be32toh(o_right);
142 return *this;
143}
Paul Greenwood31a54882019-08-01 17:05:09 -0500144
145/**@brief This template extracts an int32_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500146 * @param o_right: A pointer to where the data is stored
147 * @return *this: A pointer to "this" object
148 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500149template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500150ChipDataStream & ChipDataStream::operator>>(int32_t & o_right)
151{
152 read(&o_right, sizeof(o_right));
153 be32toh(o_right);
154 return *this;
155}
Paul Greenwood31a54882019-08-01 17:05:09 -0500156
157/**@brief This template extracts a uint64_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500158 * @param o_right: A pointer to where the data is stored
159 * @return *this: A pointer to "this" object
160 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500161template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500162ChipDataStream & ChipDataStream::operator>>(uint64_t & o_right)
163{
164 read(&o_right, sizeof(o_right));
165 be64toh(o_right);
166 return *this;
167}
Paul Greenwood31a54882019-08-01 17:05:09 -0500168
169/**@brief This template extracts an int64_t type from the data buffer
Zane Shelley83da2452019-10-25 15:45:34 -0500170 * @param o_right: A pointer to where the data is stored
171 * @return *this: A pointer to "this" object
172 **/
Paul Greenwood31a54882019-08-01 17:05:09 -0500173template <> inline
Zane Shelley83da2452019-10-25 15:45:34 -0500174ChipDataStream & ChipDataStream::operator>>(int64_t & o_right)
175{
176 read(&o_right, sizeof(o_right));
177 be64toh(o_right);
178 return *this;
179}
Paul Greenwood31a54882019-08-01 17:05:09 -0500180
Zane Shelley83da2452019-10-25 15:45:34 -0500181} // namespace libhei