diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-12-04 12:18:14 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-12-04 12:18:14 +0100 |
commit | a14896f9ed209dd3f9597722e5a5697bd7dbf531 (patch) | |
tree | 089ca5cbbd206d1921f8f6b53292f5bc1902ca5c /modules/core/array.h | |
parent | 84ecdcbca9e55b1f57fbb832e12ff4fdbb86e7c9 (diff) |
meta: Renamed folder containing source
Diffstat (limited to 'modules/core/array.h')
-rw-r--r-- | modules/core/array.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/modules/core/array.h b/modules/core/array.h new file mode 100644 index 0000000..f82b8d6 --- /dev/null +++ b/modules/core/array.h @@ -0,0 +1,96 @@ +#pragma once + +#include <vector> + +namespace saw { +/** + * Array container avoiding exceptions + */ +template<typename T> +class array { +private: + std::vector<T> data_; +public: + array() = default; + + SAW_FORBID_COPY(array); + + T& at(size_t i) noexcept { + return data_.at(i); + } + + const T& at(size_t i) noexcept const { + return data_.at(i); + } + + size_t size() noexcept const { + return data_.size(); + } + + T& front() noexcept { + return data_.front(); + } + + const T& front() noexcept const { + return data_.front(); + } + + T& back() noexcept { + return data_.back(); + } + + const T& back() noexcept const { + return data_.back(); + } + + error_or<void> push(T val) noexcept { + try{ + data_.push_back(std::move(val)); + }catch(std::exception& e){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } + + error_or<void> pop() noexcept { + try{ + data_.pop_back(); + }catch(std::exception& e){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } + + error_or<void> resize(size_t i) noexcept { + try{ + data_.resize(i); + }catch(std::exception& e){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } + + error_or<void> reserve(size_t i) noexcept { + try{ + data_.reserve(i); + }catch(std::exception& e){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } + + error_or<void> shrink_to_fit() noexcept { + try{ + data_.shrink_to_fit(); + }catch(std::exception& e){ + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } +}; +} |