summaryrefslogtreecommitdiff
path: root/modules/codec/c++
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2024-06-13 17:34:22 +0200
committerClaudius "keldu" Holeksa <mail@keldu.de>2024-06-13 17:34:22 +0200
commit57f6eacfcdbdba31185eb66b9a573a8923eecf16 (patch)
tree1683da4209744fabbe87a949134701d617c0d5f9 /modules/codec/c++
parent0f317186de9fb11d336e564f808e4732386c4074 (diff)
Possible fix for transferring primitives to device without dropping STL
Diffstat (limited to 'modules/codec/c++')
-rw-r--r--modules/codec/c++/id_map.hpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/modules/codec/c++/id_map.hpp b/modules/codec/c++/id_map.hpp
index 84c04e7..18a331f 100644
--- a/modules/codec/c++/id_map.hpp
+++ b/modules/codec/c++/id_map.hpp
@@ -88,6 +88,29 @@ public:
}
/**
+ * Insert as data with associated id. This can fail when it doesn't adhere to the standard approach.
+ */
+ error_or<void> insert_as(data<T,Encoding,Storage> val, id<T> id) noexcept {
+ if(free_ids_.empty()){
+ if( id.get_value() != data_.size() ){
+ return make_error<err::invalid_state>("Can't insert_as with provided ID. Doesn't match.");
+ }
+ try {
+ data_.emplace_back(std::move(val));
+ }catch(std::exception& e){
+ return make_error<err::out_of_memory>();
+ }
+ return void_t{};
+ }
+
+ if(free_ids_.back() != id){
+ return make_error<err::invalid_state>("Can't insert_as with provided ID. Doesn't match next id.");
+ }
+ data_.at(id.get_value()) = std::move(val);
+ return void_t{};
+ }
+
+ /**
* Erase a value at this id. If this id isn't in the map, then it returns an error.
*/
error_or<void> erase(const id<T>& val) noexcept {
@@ -141,6 +164,16 @@ public:
}
/**
+ * Tries to return the next free id
+ */
+ id<T> next_free_id() const {
+ if(free_ids_.empty()){
+ return {data_.size()};
+ }
+ return free_ids_.back();
+ }
+
+ /**
* Tries to find a value based on an id.
* Returns an error on failure and returns
* a value pointer on success.