From abf805dd6b61dec138871087ee12bd27ed716a95 Mon Sep 17 00:00:00 2001 From: Wenqing Wang <wenqing.wang@ufz.de> Date: Fri, 23 Dec 2016 15:31:25 +0100 Subject: [PATCH] [Base] Added a map inserting function --- BaseLib/uniqueInsert.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/BaseLib/uniqueInsert.h b/BaseLib/uniqueInsert.h index 25aa7e28479..de6b3488dc3 100644 --- a/BaseLib/uniqueInsert.h +++ b/BaseLib/uniqueInsert.h @@ -27,6 +27,20 @@ void uniquePushBack(Container& container, typename Container::value_type const& container.push_back(element); } +//! Inserts the given \c key with the given \c value into the \c map if an entry with the +//! given \c key does not yet exist; otherwise an \c error_message is printed and the +//! program is aborted. +template<typename Map, typename Key, typename Value> +void insertMapIfKeyUniqueElseError( + Map& map, Key const& key, Value&& value, + std::string const& error_message) +{ + auto const inserted = map.emplace(key, std::forward<Value>(value)); + if (!inserted.second) { // insertion failed, i.e., key already exists + OGS_FATAL("Failed insert %s. Key already exists.", + error_message.c_str()); + } +} //! Inserts the given \c key with the given \c value into the \c map if an entry with the //! given \c key does not yet exist; otherwise an \c error_message is printed and the -- GitLab