Make all decoder components move-only

This commit is contained in:
TennesseeTrash 2025-09-26 02:54:05 +02:00
parent ca8750aab2
commit d5dee8d802
3 changed files with 174 additions and 1 deletions

View file

@ -19,7 +19,14 @@ namespace CBOR
private: private:
Item(class Decoder &decoder); Item(class Decoder &decoder);
Item(const Item &) = delete;
Item &operator=(const Item &) = delete;
public: public:
Item(Item &&other);
Item &operator=(Item &&other);
~Item() = default;
static constexpr static constexpr
std::uint64_t ArgumentIndefinite = std::numeric_limits<std::uint64_t>::max(); std::uint64_t ArgumentIndefinite = std::numeric_limits<std::uint64_t>::max();
@ -66,7 +73,14 @@ namespace CBOR
private: private:
TaggedItem(class Decoder &decoder); TaggedItem(class Decoder &decoder);
TaggedItem(const TaggedItem &) = delete;
TaggedItem &operator=(const TaggedItem &) = delete;
public: public:
TaggedItem(TaggedItem &&other);
TaggedItem &operator=(TaggedItem &&other);
~TaggedItem() = default;
std::uint64_t Tag(); std::uint64_t Tag();
Item Item(); Item Item();
@ -87,7 +101,14 @@ namespace CBOR
private: private:
KeyValue(class Decoder &decoder); KeyValue(class Decoder &decoder);
KeyValue(const KeyValue &) = delete;
KeyValue &operator=(const KeyValue &) = delete;
public: public:
KeyValue(KeyValue &&other);
KeyValue &operator=(KeyValue &&other);
~KeyValue() = default;
Item Key(); Item Key();
Item Value(); Item Value();
private: private:
@ -108,7 +129,14 @@ namespace CBOR
private: private:
Binary(class Decoder &decoder); Binary(class Decoder &decoder);
Binary(const Binary &) = delete;
Binary &operator=(const Binary &) = delete;
public: public:
Binary(Binary &&other);
Binary &operator=(Binary &&other);
~Binary() = default;
bool Done(); bool Done();
ConstBuffer Next(); ConstBuffer Next();
@ -126,7 +154,14 @@ namespace CBOR
private: private:
String(class Decoder &decoder); String(class Decoder &decoder);
String(const String &) = delete;
String &operator=(const String &) = delete;
public: public:
String(String &&other);
String &operator=(String &&other);
~String() = default;
bool Done(); bool Done();
std::string_view Next(); std::string_view Next();
@ -144,7 +179,14 @@ namespace CBOR
private: private:
Array(class Decoder &decoder); Array(class Decoder &decoder);
Array(const Array &) = delete;
Array &operator=(const Array &) = delete;
public: public:
Array(Array &&other);
Array &operator=(Array &&other);
~Array() = default;
bool Done(); bool Done();
Item Next(); Item Next();
@ -164,7 +206,14 @@ namespace CBOR
private: private:
Map(class Decoder &decoder); Map(class Decoder &decoder);
Map(const Map &) = delete;
Map &operator=(const Map &) = delete;
public: public:
Map(Map &&other);
Map &operator=(Map &&other);
~Map() = default;
bool Done(); bool Done();
KeyValue Next(); KeyValue Next();
@ -184,6 +233,12 @@ namespace CBOR
public: public:
Decoder(ConstBuffer buffer); Decoder(ConstBuffer buffer);
Decoder(const Decoder &) = delete;
Decoder &operator=(const Decoder &) = delete;
Decoder(Decoder &&other);
Decoder &operator=(Decoder &&other);
~Decoder() = default;
static constexpr static constexpr
std::uint64_t ArgumentIndefinite = std::numeric_limits<std::uint64_t>::max(); std::uint64_t ArgumentIndefinite = std::numeric_limits<std::uint64_t>::max();

View file

@ -411,6 +411,16 @@ namespace CBOR
: mDecoder(&decoder) : mDecoder(&decoder)
{} {}
Item::Item(Item &&other)
: mDecoder(std::exchange(other.mDecoder, nullptr))
{}
Item &Item::operator=(Item &&other)
{
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
MajorType Item::GetMajor() const MajorType Item::GetMajor() const
{ {
return mDecoder->GetMajor(); return mDecoder->GetMajor();
@ -525,6 +535,18 @@ namespace CBOR
: mState(State::Initial), mDecoder(&decoder) : mState(State::Initial), mDecoder(&decoder)
{} {}
TaggedItem::TaggedItem(TaggedItem &&other)
: mState(std::exchange(other.mState, State::Done))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
TaggedItem &TaggedItem::operator=(TaggedItem &&other)
{
mState = std::exchange(other.mState, State::Done);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
std::uint64_t TaggedItem::Tag() std::uint64_t TaggedItem::Tag()
{ {
if (mState != State::Initial) { if (mState != State::Initial) {
@ -555,6 +577,18 @@ namespace CBOR
: mState(State::Initial), mDecoder(&decoder) : mState(State::Initial), mDecoder(&decoder)
{} {}
KeyValue::KeyValue(KeyValue &&other)
: mState(std::exchange(other.mState, State::Done))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
KeyValue &KeyValue::operator=(KeyValue &&other)
{
mState = std::exchange(other.mState, State::Done);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
Item KeyValue::Key() Item KeyValue::Key()
{ {
if (mState != State::Initial) { if (mState != State::Initial) {
@ -582,6 +616,22 @@ namespace CBOR
, mDecoder(&decoder) , mDecoder(&decoder)
{} {}
Binary::Binary(Binary &&other)
: mHeaderParsed(std::exchange(other.mHeaderParsed, true))
, mDone(std::exchange(other.mDone, true))
, mCheckedDone(std::exchange(other.mCheckedDone, true))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
Binary &Binary::operator=(Binary &&other)
{
mHeaderParsed = std::exchange(other.mHeaderParsed, true);
mDone = std::exchange(other.mDone, true);
mCheckedDone = std::exchange(other.mCheckedDone, true);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
bool Binary::Done() bool Binary::Done()
{ {
if (!mHeaderParsed) { if (!mHeaderParsed) {
@ -649,6 +699,22 @@ namespace CBOR
, mDecoder(&decoder) , mDecoder(&decoder)
{} {}
String::String(String &&other)
: mHeaderParsed(std::exchange(other.mHeaderParsed, true))
, mDone(std::exchange(other.mDone, true))
, mCheckedDone(std::exchange(other.mCheckedDone, true))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
String &String::operator=(String &&other)
{
mHeaderParsed = std::exchange(other.mHeaderParsed, true);
mDone = std::exchange(other.mDone, true);
mCheckedDone = std::exchange(other.mCheckedDone, true);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
bool String::Done() bool String::Done()
{ {
if (!mHeaderParsed) { if (!mHeaderParsed) {
@ -718,6 +784,26 @@ namespace CBOR
, mDecoder(&decoder) , mDecoder(&decoder)
{} {}
Array::Array(Array &&other)
: mHeaderParsed(std::exchange(other.mHeaderParsed, true))
, mDone(std::exchange(other.mDone, true))
, mCheckedDone(std::exchange(other.mCheckedDone, true))
, mCurrent(std::exchange(other.mCurrent, 0))
, mSize(std::exchange(other.mSize, 0))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
Array &Array::operator=(Array &&other)
{
mHeaderParsed = std::exchange(other.mHeaderParsed, true);
mDone = std::exchange(other.mDone, true);
mCheckedDone = std::exchange(other.mCheckedDone, true);
mCurrent = std::exchange(other.mCurrent, 0);
mSize = std::exchange(other.mSize, 0);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
bool Array::Done() bool Array::Done()
{ {
if (!mHeaderParsed) { if (!mHeaderParsed) {
@ -782,6 +868,26 @@ namespace CBOR
, mDecoder(&decoder) , mDecoder(&decoder)
{} {}
Map::Map(Map &&other)
: mHeaderParsed(std::exchange(other.mHeaderParsed, true))
, mDone(std::exchange(other.mDone, true))
, mCheckedDone(std::exchange(other.mCheckedDone, true))
, mCurrent(std::exchange(other.mCurrent, 0))
, mSize(std::exchange(other.mSize, 0))
, mDecoder(std::exchange(other.mDecoder, nullptr))
{}
Map &Map::operator=(Map &&other)
{
mHeaderParsed = std::exchange(other.mHeaderParsed, true);
mDone = std::exchange(other.mDone, true);
mCheckedDone = std::exchange(other.mCheckedDone, true);
mCurrent = std::exchange(other.mCurrent, 0);
mSize = std::exchange(other.mSize, 0);
mDecoder = std::exchange(other.mDecoder, nullptr);
return *this;
}
bool Map::Done() bool Map::Done()
{ {
if (!mHeaderParsed) { if (!mHeaderParsed) {
@ -841,6 +947,18 @@ namespace CBOR
: mCurrent(0), mBuffer(buffer) : mCurrent(0), mBuffer(buffer)
{} {}
Decoder::Decoder(Decoder &&other)
: mCurrent(std::exchange(other.mCurrent, 0))
, mBuffer(std::exchange(other.mBuffer, {}))
{}
Decoder &Decoder::operator=(Decoder &&other)
{
mCurrent = std::exchange(other.mCurrent, 0);
mBuffer = std::exchange(other.mBuffer, {});
return *this;
}
MajorType Decoder::GetMajor() const MajorType Decoder::GetMajor() const
{ {
std::uint8_t header = Read1B(mBuffer, mCurrent); std::uint8_t header = Read1B(mBuffer, mCurrent);

View file

@ -1,6 +1,6 @@
#include "CBOR/Decoder.hpp" #include "CBOR/Decoder.hpp"
#include "CBOR/Encoder.hpp" #include "CBOR/Encoder.hpp"
#include "Cbor/Printer.hpp" #include "CBOR/Printer.hpp"
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>