Skip to content
Snippets Groups Projects
Commit 275d1d2a authored by Jan Uhlík's avatar Jan Uhlík Committed by Jan Trávníček
Browse files

Extended ext::map with insert_or_assign member funciton.

parent 9875d60b
No related branches found
No related tags found
No related merge requests found
......@@ -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 >
......
......@@ -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);
}
 
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment