From 57f6eacfcdbdba31185eb66b9a573a8923eecf16 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 13 Jun 2024 17:34:22 +0200 Subject: Possible fix for transferring primitives to device without dropping STL --- modules/codec/c++/id_map.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'modules/codec') 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 @@ -87,6 +87,29 @@ public: return make_error(); } + /** + * Insert as data with associated id. This can fail when it doesn't adhere to the standard approach. + */ + error_or insert_as(data val, id id) noexcept { + if(free_ids_.empty()){ + if( id.get_value() != data_.size() ){ + return make_error("Can't insert_as with provided ID. Doesn't match."); + } + try { + data_.emplace_back(std::move(val)); + }catch(std::exception& e){ + return make_error(); + } + return void_t{}; + } + + if(free_ids_.back() != id){ + return make_error("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. */ @@ -140,6 +163,16 @@ public: return void_t{}; } + /** + * Tries to return the next free id + */ + id 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 -- cgit v1.2.3