LibCBOR/LibCBOR/Include/CBOR/Decoder.hpp

195 lines
3.8 KiB
C++

#ifndef LIBCBOR_DECODER_HPP
#define LIBCBOR_DECODER_HPP
#include "Core.hpp"
#include <span>
#include <string_view>
namespace CBOR
{
class DecodeError: public Error
{
public:
using Error::Error;
};
// Forward decl
class Decoder;
class Item
{
private:
Item(Decoder &decoder);
public:
bool Bool();
Special Special();
std::int8_t Int8();
std::int16_t Int16();
std::int32_t Int32();
std::int64_t Int64();
std::uint8_t Uint8();
std::uint16_t Uint16();
std::uint32_t Uint32();
std::uint64_t Uint64();
// Note(3011): float16_t is currently not supported
float Float();
double Double();
std::span<std::uint8_t> Binary();
std::string_view String();
class Binary IndefiniteBinary();
class String IndefiniteString();
class Array Array();
class Map Map();
private:
friend class Decoder;
friend class Array;
friend class KeyValue;
Decoder *mDecoder;
};
class KeyValue
{
private:
KeyValue(Decoder &decoder);
public:
Item Key();
Item Value();
private:
friend class Decoder;
friend class Map;
enum class State
{
Initial, KeyPulled, Done,
};
State mState;
Decoder *mDecoder;
};
class Binary
{
private:
Binary(Decoder &decoder);
public:
bool Done();
std::span<std::uint8_t> Next();
private:
friend class Decoder;
bool mHeaderParsed;
bool mDone;
bool mCheckedDone;
Decoder *mDecoder;
};
class String
{
private:
String(Decoder &decoder);
public:
bool Done();
std::string_view Next();
private:
friend class Decoder;
bool mHeaderParsed;
bool mDone;
bool mCheckedDone;
Decoder *mDecoder;
};
class Array
{
private:
Array(Decoder &decoder);
public:
bool Done();
Item Next();
private:
friend class Decoder;
bool mHeaderParsed;
bool mDone;
bool mCheckedDone;
std::size_t mCurrent;
std::size_t mSize;
Decoder *mDecoder;
};
class Map
{
private:
Map(Decoder &decoder);
public:
bool Done();
KeyValue Next();
private:
friend class Decoder;
bool mHeaderParsed;
bool mDone;
bool mCheckedDone;
std::size_t mCurrent;
std::size_t mSize;
Decoder *mDecoder;
};
class Decoder
{
public:
Decoder(std::span<std::uint8_t> buffer);
bool Bool();
Special Special();
std::int8_t Int8();
std::int16_t Int16();
std::int32_t Int32();
std::int64_t Int64();
std::uint8_t Uint8();
std::uint16_t Uint16();
std::uint32_t Uint32();
std::uint64_t Uint64();
// Note(3011): float16_t is currently not supported
float Float();
double Double();
std::span<std::uint8_t> Binary();
std::string_view String();
class Binary IndefiniteBinary();
class String IndefiniteString();
class Array Array();
class Map Map();
private:
friend class Binary;
friend class String;
friend class Array;
friend class Map;
std::size_t mCurrent;
std::span<std::uint8_t> mBuffer;
};
}
#endif // LIBCBOR_DECODER_HPP