Line data Source code
1 : #pragma once
2 :
3 : #include <AH/STL/vector> // std::vector
4 : #include <Settings/NamespaceSettings.hpp>
5 :
6 : BEGIN_CS_NAMESPACE
7 :
8 : /**
9 : * @brief Helper to pull bytes or other objects out of a buffer.
10 : * @ingroup MIDIParsers
11 : */
12 : template <class T = uint8_t>
13 : class BufferPuller_ {
14 : public:
15 62 : BufferPuller_(const T *buffer, size_t length)
16 62 : : buffer(buffer), end(buffer + length) {}
17 :
18 : /// Pull a value out of the buffer.
19 : /// @param[out] output
20 : /// A new value from the buffer (if available).
21 : /// @return True if a value was available, false otherwise.
22 740 : bool pull(T &output) {
23 740 : if (buffer != end) {
24 690 : output = *buffer++;
25 690 : return true;
26 : }
27 50 : return false;
28 : }
29 :
30 : private:
31 : const T *buffer;
32 : const T *const end;
33 : };
34 :
35 : template <class T>
36 : BufferPuller_<T> BufferPuller(const T *buffer, size_t length) {
37 : return {buffer, length};
38 : }
39 : template <class T>
40 2 : BufferPuller_<T> BufferPuller(const std::vector<T> &buffer) {
41 2 : return {buffer.data(), buffer.size()};
42 : }
43 : template <class T, size_t N>
44 60 : BufferPuller_<T> BufferPuller(const T (&buffer)[N]) {
45 60 : return {buffer, N};
46 : }
47 :
48 : END_CS_NAMESPACE
|