| 15 | |
| 16 | template<typename T> |
| 17 | class Parser { |
| 18 | public: |
| 19 | Parser(const T* begin, const T* end) : _begin(begin), _end(end), _current(begin) {} |
| 20 | |
| 21 | template<typename Out> |
| 22 | [[nodiscard]] Result<const Out*> parse(size_t requiredSize) { |
| 23 | return consumeAndAdvance<Out>(requiredSize); |
| 24 | } |
| 25 | |
| 26 | template<typename Out> |
| 27 | [[nodiscard]] Result<const Out*> parseStruct() { |
| 28 | return parse<Out>(sizeof(Out)); |
| 29 | } |
| 30 | |
| 31 | template<typename Out> |
| 32 | [[nodiscard]] Result<Out> parseValue() { |
| 33 | return parseStruct<Out>().template map<Out>([](const Out* value) { return *value; }); |
| 34 | } |
| 35 | |
| 36 | bool isAtEnd() { |
| 37 | return _current == _end; |
| 38 | } |
| 39 | |
| 40 | const T* getCurrent() const { |
| 41 | return _current; |
| 42 | } |
| 43 | |
| 44 | const T* getBegin() const { |
| 45 | return _begin; |
| 46 | } |
| 47 | |
| 48 | const T* getEnd() const { |
| 49 | return _end; |
| 50 | } |
| 51 | |
| 52 | void setEnd(const T* end) { |
| 53 | _end = end; |
| 54 | } |
| 55 | |
| 56 | size_t getDistanceToEnd() { |
| 57 | return static_cast<size_t>(_end - _current); |
| 58 | } |
| 59 | |
| 60 | size_t getDistanceToBegin() { |
| 61 | return static_cast<size_t>(_current - _begin); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | const T* _begin; |
| 66 | const T* _end; |
| 67 | const T* _current; |
| 68 | |
| 69 | template<typename Out> |
| 70 | [[nodiscard]] Result<const Out*> consumeAndAdvance(size_t requiredSize) { |
| 71 | if (getDistanceToEnd() < requiredSize) { |
| 72 | return Error(STRING_FORMAT("Unable to parse, missing at least {} bytes, only have {} bytes remaining", |
| 73 | requiredSize, |
| 74 | _end - _current)); |
no outgoing calls
no test coverage detected