Compare commits

...

3 Commits

Author SHA1 Message Date
BodgeMaster 8594f4cbb0 NBT: Fix ambiguity between int types and bool when calling the ErrorOr<> constructor 2022-06-28 16:13:33 +02:00
BodgeMaster 83d606a2c7 Error: Add error codes to ErrorOr<> and add constructors
This allows us to handle functions that can fail in multiple different ways
2022-06-28 16:01:39 +02:00
BodgeMaster 081035db32 expose helpers in the header, rename NBT::helpers to NBT::helper 2022-06-28 15:19:47 +02:00
3 changed files with 68 additions and 5 deletions

View File

@ -15,22 +15,59 @@
#pragma once
#include <cstdint>
template <typename T>
struct ErrorOr {
bool isError;
uint8_t errorCode;
T value;
ErrorOr<T>();
ErrorOr<T>(T);
ErrorOr<T>(bool);
ErrorOr<T>(bool, uint8_t);
ErrorOr<T>(bool, uint8_t, T);
};
template <typename T>
ErrorOr<T>::ErrorOr() {
this->isError = false;
this->errorCode = 0;
}
template <typename T>
ErrorOr<T>::ErrorOr(T value) {
this->isError = false;
this->errorCode = 0;
this->value = value;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError) {
this->isError = isError;
this->errorCode = 0;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode) {
this->isError = isError;
this->errorCode = errorCode;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode, T value) {
this->isError = isError;
this->errorCode = errorCode;
this->value = value;
}
namespace ErrorCodes {
// These are all arbitrary values used to assign error codes to different
// kinds of errors.
// Using them is optional as ErrorOr<> accepts any uint8_t value as
// error code, but they are useful for readability.
// IndexOutOfRangeException equivalent
const uint8_t RANGE_ERROR = 1;
}

View File

@ -42,25 +42,25 @@ int endianness_example() {
}
namespace NBT {
namespace helpers {
namespace helper {
ErrorOr<int8_t> readInt8(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int8_t>(0);
return ErrorOr<int8_t>((int8_t) 0);
}
ErrorOr<int16_t> readInt16(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int16_t>(0);
return ErrorOr<int16_t>((int16_t) 0);
}
ErrorOr<int32_t> readInt32(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int32_t>(0);
return ErrorOr<int32_t>((int32_t) 0);
}
ErrorOr<int64_t> readInt64(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition) {
//TODO: implement
return ErrorOr<int64_t>(0);
return ErrorOr<int64_t>((int64_t) 0);
}
//FIXME: we just assume that float is a single-precision IEEE754

View File

@ -40,5 +40,31 @@
#include "error.h++"
namespace NBT {
namespace helper {
ErrorOr<int8_t> readInt8(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int16_t> readInt16(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int32_t> readInt32(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<int64_t> readInt64(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
//FIXME: we just assume that float is a single-precision IEEE754
// floating point number
ErrorOr<float> readFloat32(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
//FIXME: we just assume that double is a double-precision IEEE754
// floating point number
ErrorOr<double> readFloat64(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int8_t>> readInt8Array(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
//ErrorOr<> readString(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int32_t>> readInt32Array(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
ErrorOr<std::vector<int64_t>> readInt64Array(uint8_t* data[], uint64_t dataSize, uint64_t currentPosition);
}
bool validateRawNBTData(uint8_t* data[], int length);
}