diff options
Diffstat (limited to 'modules/codec')
-rw-r--r-- | modules/codec/c++/id_map.hpp | 33 |
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. |