From 275d1d2a94fd5252d262c8dea74a8fc378ed48af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Uhl=C3=ADk?= <jan@uhlik.me>
Date: Thu, 29 Mar 2018 20:42:09 +0200
Subject: [PATCH] Extended ext::map with insert_or_assign member funciton.

---
 alib2std/src/extensions/map.hpp          | 17 +++++++++++++++++
 alib2std/test-src/extensions/MapTest.cpp |  4 ++++
 alib2std/test-src/extensions/MapTest.h   | 10 ++++++++++
 3 files changed, 31 insertions(+)

diff --git a/alib2std/src/extensions/map.hpp b/alib2std/src/extensions/map.hpp
index 173c3de12b..e0b905e4a7 100644
--- a/alib2std/src/extensions/map.hpp
+++ b/alib2std/src/extensions/map.hpp
@@ -42,6 +42,23 @@ public:
 
 	using std::map < T, R, Cmp, Alloc >::operator =;
 #endif
+	using iterator = typename std::map<T, R, Cmp, Alloc>::iterator;
+	using key_type = typename std::map<T, R, Cmp, Alloc>::key_type;
+
+	// Follow interface from C++17
+	// TODO: Remove this member function after move to C++17
+	template < typename M >
+	pair < iterator, bool > insert_or_assign ( const key_type & k, M && obj ) {
+		iterator pos = this->find ( k );
+		if ( pos == this->end ( ) ) {
+			pos = this->insert ( ext::make_pair ( k, std::forward < M > ( obj ) ) ).first;
+			return ext::make_pair ( pos, true );
+		} else {
+			pos->second = std::forward < M > ( obj );
+			return ext::make_pair ( pos, false );
+		}
+	}
+
 };
 
 template< class T, class R, class ... Ts >
diff --git a/alib2std/test-src/extensions/MapTest.cpp b/alib2std/test-src/extensions/MapTest.cpp
index c13f5cff4c..05fcb83615 100644
--- a/alib2std/test-src/extensions/MapTest.cpp
+++ b/alib2std/test-src/extensions/MapTest.cpp
@@ -23,6 +23,10 @@ void MapTest::test3() {
 		map2.insert(std::move(moveablePair));
 	}
 
+	bool res = map2.insert_or_assign ( MapTest::Moveable ( moves, copies ), MapTest::Moveable ( moves, copies ) ).second;
+
+	CPPUNIT_ASSERT ( res == false );
+
 	CPPUNIT_ASSERT(copies == 0);
 }
 
diff --git a/alib2std/test-src/extensions/MapTest.h b/alib2std/test-src/extensions/MapTest.h
index 708fc34111..a43132aecc 100644
--- a/alib2std/test-src/extensions/MapTest.h
+++ b/alib2std/test-src/extensions/MapTest.h
@@ -28,6 +28,16 @@ public:
 		m_moves++;
 	}
 
+	Moveable & operator = ( const Moveable & ) {
+		m_copies ++;
+		return * this;
+	}
+
+	Moveable & operator = ( Moveable && ) {
+		m_moves ++;
+		return * this;
+	}
+
 	bool operator<(const Moveable&) const {
 		return false;
 	}
-- 
GitLab