blob: e9cfc75e9db3e8caaf58842e72af07c31a2833b4 [file] [log] [blame]
Paul Greenwood31a54882019-08-01 17:05:09 -05001#pragma once
2
Paul Greenwood31a54882019-08-01 17:05:09 -05003#include <endian.h>
Paul Greenwood31a54882019-08-01 17:05:09 -05004#include <string.h>
5
Zane Shelleyca9f6252019-10-25 21:17:30 -05006#include <hei_includes.hpp>
Ben Tyner032bf9e2020-05-06 21:27:54 -05007#include <hei_macros.hpp>
Zane Shelleyca9f6252019-10-25 21:17:30 -05008
Paul Greenwood31a54882019-08-01 17:05:09 -05009namespace libhei
10{
11
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050012/** @brief A streaming utility to read a Chip Data File buffer. */
Paul Greenwood31a54882019-08-01 17:05:09 -050013class ChipDataStream
14{
Paul Greenwood31a54882019-08-01 17:05:09 -050015 public:
Zane Shelley83da2452019-10-25 15:45:34 -050016 /**
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050017 * @brief Constructor.
18 * @param i_buffer A pointer to the buffer.
19 * @param i_bufferSize The buffer size.
20 */
Zane Shelleyfe27b652019-10-28 11:33:07 -050021 ChipDataStream(void* i_buffer, size_t i_bufferSize) :
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050022 iv_buffer(i_buffer), iv_bufferSize(i_bufferSize)
23 {
24 HEI_ASSERT(nullptr != i_buffer);
25 HEI_ASSERT(0 < i_bufferSize);
26 }
Paul Greenwood31a54882019-08-01 17:05:09 -050027
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050028 /** @brief Destructor. */
Zane Shelley83da2452019-10-25 15:45:34 -050029 ~ChipDataStream() = default;
Paul Greenwood31a54882019-08-01 17:05:09 -050030
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050031 /** @brief Copy constructor. */
32 ChipDataStream(const ChipDataStream&) = delete;
Paul Greenwood31a54882019-08-01 17:05:09 -050033
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050034 /** @brief Assignment operator. */
35 ChipDataStream& operator=(const ChipDataStream&) = delete;
36
37 private:
38 /** Pointer to the first address of the Chip Data File buffer. */
39 const void* const iv_buffer;
40
41 /** The Chip Data File buffer size. */
42 const size_t iv_bufferSize;
43
44 /** Current byte index within the buffer. */
45 size_t iv_currentIndex = 0;
46
47 public:
48 /** @brief Output stream operator. */
Zane Shelley83da2452019-10-25 15:45:34 -050049 template <class D>
Zane Shelleyfe27b652019-10-28 11:33:07 -050050 ChipDataStream& operator>>(D& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -050051 {
52 read(&o_right, sizeof(D));
53 return *this;
54 }
Paul Greenwood31a54882019-08-01 17:05:09 -050055
56 private:
Zane Shelley83da2452019-10-25 15:45:34 -050057 /**
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050058 * @brief Copies the given number of data bytes into the buffer from the
59 * current index and then increments the index.
60 * @param o_buf The output buffer.
61 * @param i_size The number of bytes to copy.
62 */
Zane Shelleyfe27b652019-10-28 11:33:07 -050063 void read(void* o_buf, size_t i_size)
Zane Shelley83da2452019-10-25 15:45:34 -050064 {
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050065 // Check for buffer overflow.
66 HEI_ASSERT((iv_currentIndex + i_size) <= iv_bufferSize);
67
68 // Copy the buffer.
69 memcpy(o_buf, (char*)iv_buffer + iv_currentIndex, i_size);
70
71 // Increment the curent index for the next piece of data.
72 iv_currentIndex += i_size;
Zane Shelley83da2452019-10-25 15:45:34 -050073 }
Paul Greenwood31a54882019-08-01 17:05:09 -050074};
75
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050076/** @brief Template specialization for uint16_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -050077template <>
78inline ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -050079{
80 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050081 o_right = be16toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -050082 return *this;
83}
Paul Greenwood31a54882019-08-01 17:05:09 -050084
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050085/** @brief Template specialization for int16_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -050086template <>
87inline ChipDataStream& ChipDataStream::operator>>(int16_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -050088{
89 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050090 o_right = be16toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -050091 return *this;
92}
Paul Greenwood31a54882019-08-01 17:05:09 -050093
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050094/** @brief Template specialization for uint32_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -050095template <>
96inline ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -050097{
98 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -050099 o_right = be32toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -0500100 return *this;
101}
Paul Greenwood31a54882019-08-01 17:05:09 -0500102
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500103/** @brief Template specialization for int32_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500104template <>
105inline ChipDataStream& ChipDataStream::operator>>(int32_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500106{
107 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500108 o_right = be32toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -0500109 return *this;
110}
Paul Greenwood31a54882019-08-01 17:05:09 -0500111
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500112/** @brief Template specialization for uint64_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500113template <>
114inline ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500115{
116 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500117 o_right = be64toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -0500118 return *this;
119}
Paul Greenwood31a54882019-08-01 17:05:09 -0500120
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500121/** @brief Template specialization for int64_t. */
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500122template <>
123inline ChipDataStream& ChipDataStream::operator>>(int64_t& o_right)
Zane Shelley83da2452019-10-25 15:45:34 -0500124{
125 read(&o_right, sizeof(o_right));
Zane Shelleyb8b3cdf2020-05-04 13:28:04 -0500126 o_right = be64toh(o_right); // Chip Data is big-endian
Zane Shelley83da2452019-10-25 15:45:34 -0500127 return *this;
128}
Paul Greenwood31a54882019-08-01 17:05:09 -0500129
Zane Shelley83da2452019-10-25 15:45:34 -0500130} // namespace libhei