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