mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Refactor MessageReader into WireDecoder.
Change-Id: If5842b4f6b9a7db3d3de515cd71324ab7ee82f50
This commit is contained in:
108
src/nt_wiredecoder.h
Normal file
108
src/nt_wiredecoder.h
Normal file
@@ -0,0 +1,108 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NT_WIREDECODER_H_
|
||||
#define NT_WIREDECODER_H_
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "ntcore.h"
|
||||
#include "nt_leb128.h"
|
||||
#include "nt_raw_istream.h"
|
||||
#include "nt_encoding.h"
|
||||
|
||||
namespace NtImpl {
|
||||
|
||||
class WireDecoder
|
||||
{
|
||||
public:
|
||||
explicit WireDecoder(raw_istream& is, unsigned int proto_rev);
|
||||
~WireDecoder();
|
||||
|
||||
void SetProtocolRev(unsigned int proto_rev)
|
||||
{
|
||||
m_proto_rev = proto_rev;
|
||||
}
|
||||
|
||||
void Reset()
|
||||
{
|
||||
m_error = 0;
|
||||
}
|
||||
|
||||
const char *GetError() const
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
bool Read(char **buf, std::size_t len)
|
||||
{
|
||||
if (len > m_allocated)
|
||||
Realloc(len);
|
||||
*buf = m_buf;
|
||||
return m_is.read(m_buf, len);
|
||||
}
|
||||
|
||||
bool Read8(unsigned int *val)
|
||||
{
|
||||
char *buf;
|
||||
if (!Read(&buf, 1)) return false;
|
||||
*val = NtImpl::Read8(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Read16(unsigned int *val)
|
||||
{
|
||||
char *buf;
|
||||
if (!Read(&buf, 2)) return false;
|
||||
*val = NtImpl::Read16(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Read32(unsigned long *val)
|
||||
{
|
||||
char *buf;
|
||||
if (!Read(&buf, 4)) return false;
|
||||
*val = NtImpl::Read32(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadDouble(double *val)
|
||||
{
|
||||
char *buf;
|
||||
if (!Read(&buf, 8)) return false;
|
||||
*val = NtImpl::ReadDouble(buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadULEB128(unsigned long *val)
|
||||
{
|
||||
return read_uleb128(m_is, val);
|
||||
}
|
||||
|
||||
bool ReadType(NT_Type *type);
|
||||
bool ReadValue(NT_Type type, NT_Value *value);
|
||||
bool ReadString(NT_String *str);
|
||||
|
||||
protected:
|
||||
unsigned int m_proto_rev;
|
||||
const char *m_error;
|
||||
|
||||
private:
|
||||
WireDecoder(const WireDecoder&);
|
||||
WireDecoder& operator= (const WireDecoder&);
|
||||
|
||||
void Realloc(std::size_t len);
|
||||
|
||||
raw_istream &m_is;
|
||||
|
||||
char *m_buf;
|
||||
std::size_t m_allocated;
|
||||
};
|
||||
|
||||
} // namespace NtImpl
|
||||
|
||||
#endif /* NT_WIREDECODER_H_ */
|
||||
Reference in New Issue
Block a user