[ox/std] Make Vector::{front,back} return Result

This commit is contained in:
Gary Talent 2021-04-17 11:29:21 -05:00
parent 6610b501b3
commit 09383401e5
3 changed files with 39 additions and 24 deletions

View File

@ -28,7 +28,8 @@ class DataWalker {
public: public:
DataWalker(DescriptorType *type, T fieldHandler); DataWalker(DescriptorType *type, T fieldHandler);
[[nodiscard]] const DescriptorType *type() const noexcept; [[nodiscard]]
Result<const DescriptorType*> type() const noexcept;
Error read(const DescriptorField&, Reader *rdr); Error read(const DescriptorField&, Reader *rdr);
@ -49,8 +50,9 @@ DataWalker<Reader, T>::DataWalker(DescriptorType *type, T fieldHandler): m_field
} }
template<typename Reader, typename T> template<typename Reader, typename T>
const DescriptorType *DataWalker<Reader, T>::type() const noexcept { Result<const DescriptorType*> DataWalker<Reader, T>::type() const noexcept {
return m_typeStack.back(); oxRequire(out, m_typeStack.back());
return out;
} }
template<typename Reader, typename T> template<typename Reader, typename T>
@ -132,10 +134,7 @@ static Error parseField(const DescriptorField &field, Reader *rdr, DataWalker<Re
template<typename Reader, typename FH> template<typename Reader, typename FH>
Error model(Reader *rdr, DataWalker<Reader, FH> *walker) { Error model(Reader *rdr, DataWalker<Reader, FH> *walker) {
auto type = walker->type(); oxRequire(type, walker->type());
if (!type) {
return OxError(1);
}
auto typeName = type->typeName.c_str(); auto typeName = type->typeName.c_str();
auto &fields = type->fieldList; auto &fields = type->fieldList;
rdr->setTypeInfo(typeName, fields.size()); rdr->setTypeInfo(typeName, fields.size());

View File

@ -18,7 +18,7 @@ std::map<std::string, std::function<ox::Error()>> tests = {
"malloc", "malloc",
[] { [] {
ox::Vector<char> buff(ox::units::MB); ox::Vector<char> buff(ox::units::MB);
ox::heapmgr::initHeap(&buff.front(), &buff.back()); ox::heapmgr::initHeap(&buff[0], &buff[buff.size()-1]);
oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken"); oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken");
oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken"); oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken");
return OxError(0); return OxError(0);

View File

@ -26,11 +26,11 @@ class Vector {
public: public:
Vector() noexcept = default; Vector() noexcept = default;
explicit Vector(std::size_t size); explicit Vector(std::size_t size) noexcept;
Vector(const Vector &other); Vector(const Vector &other);
Vector(Vector &&other); Vector(Vector &&other) noexcept;
~Vector(); ~Vector();
@ -44,17 +44,17 @@ class Vector {
[[nodiscard]] const T &operator[](std::size_t i) const noexcept; [[nodiscard]] const T &operator[](std::size_t i) const noexcept;
[[nodiscard]] T &front() noexcept; [[nodiscard]] Result<T&> front() noexcept;
[[nodiscard]] const T &front() const noexcept; [[nodiscard]] Result<const T&> front() const noexcept;
[[nodiscard]] T &back() noexcept; [[nodiscard]] Result<T&> back() noexcept;
[[nodiscard]] const T &back() const noexcept; [[nodiscard]] Result<const T&> back() const noexcept;
[[nodiscard]] std::size_t size() const noexcept; [[nodiscard]] std::size_t size() const noexcept;
[[nodiscard]] bool empty() const; [[nodiscard]] bool empty() const noexcept;
void clear(); void clear();
@ -70,7 +70,7 @@ class Vector {
return m_items; return m_items;
} }
[[nodiscard]] bool contains(T) const; [[nodiscard]] bool contains(const T&) const;
void insert(std::size_t pos, const T &val); void insert(std::size_t pos, const T &val);
@ -100,7 +100,7 @@ class Vector {
}; };
template<typename T> template<typename T>
Vector<T>::Vector(std::size_t size) { Vector<T>::Vector(std::size_t size) noexcept {
m_size = size; m_size = size;
m_cap = m_size; m_cap = m_size;
m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]); m_items = bit_cast<T*>(new AllocAlias<T>[m_cap]);
@ -120,7 +120,7 @@ Vector<T>::Vector(const Vector<T> &other) {
} }
template<typename T> template<typename T>
Vector<T>::Vector(Vector<T> &&other) { Vector<T>::Vector(Vector<T> &&other) noexcept {
m_size = other.m_size; m_size = other.m_size;
m_cap = other.m_cap; m_cap = other.m_cap;
m_items = other.m_items; m_items = other.m_items;
@ -185,22 +185,38 @@ const T &Vector<T>::operator[](std::size_t i) const noexcept {
} }
template<typename T> template<typename T>
T &Vector<T>::front() noexcept { Result<T&> Vector<T>::front() noexcept {
if (!m_size) {
AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)};
}
return m_items[0]; return m_items[0];
} }
template<typename T> template<typename T>
const T &Vector<T>::front() const noexcept { Result<const T&> Vector<T>::front() const noexcept {
if (!m_size) {
AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)};
}
return m_items[0]; return m_items[0];
} }
template<typename T> template<typename T>
T &Vector<T>::back() noexcept { Result<T&> Vector<T>::back() noexcept {
if (!m_size) {
AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)};
}
return m_items[m_size - 1]; return m_items[m_size - 1];
} }
template<typename T> template<typename T>
const T &Vector<T>::back() const noexcept { Result<const T&> Vector<T>::back() const noexcept {
if (!m_size) {
AllocAlias<T> v;
return {*bit_cast<T*>(&v), OxError(1)};
}
return m_items[m_size - 1]; return m_items[m_size - 1];
} }
@ -210,7 +226,7 @@ std::size_t Vector<T>::size() const noexcept {
} }
template<typename T> template<typename T>
bool Vector<T>::empty() const { bool Vector<T>::empty() const noexcept {
return !m_size; return !m_size;
} }
@ -237,7 +253,7 @@ void Vector<T>::resize(std::size_t size) {
} }
template<typename T> template<typename T>
bool Vector<T>::contains(T v) const { bool Vector<T>::contains(const T &v) const {
for (std::size_t i = 0; i < m_size; i++) { for (std::size_t i = 0; i < m_size; i++) {
if (m_items[i] == v) { if (m_items[i] == v) {
return true; return true;