QMap: fix insert() rvalue overloads
Receiving an rvalue still requires to check whether the parameter is detached, otherwise we can't steal its backing std::map. Change-Id: Ie88dbf39fd777112ad7bb20a46d5c2d65be8eb3d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
14090760a8
commit
4fc539bd84
@ -655,6 +655,12 @@ public:
|
||||
if (!map.d || map.d->m.empty())
|
||||
return;
|
||||
|
||||
if (map.d.isShared()) {
|
||||
// fall back to a regular copy
|
||||
insert(map);
|
||||
return;
|
||||
}
|
||||
|
||||
detach();
|
||||
|
||||
#ifdef __cpp_lib_node_extract
|
||||
@ -1292,6 +1298,12 @@ public:
|
||||
if (!map.d || map.d->m.empty())
|
||||
return;
|
||||
|
||||
if (map.d.isShared()) {
|
||||
// fall back to a regular copy
|
||||
insert(map);
|
||||
return;
|
||||
}
|
||||
|
||||
detach();
|
||||
|
||||
#ifdef __cpp_lib_node_extract
|
||||
|
@ -1205,6 +1205,100 @@ void tst_QMap::insert()
|
||||
}
|
||||
}
|
||||
|
||||
template <template <typename K, typename T> typename Map>
|
||||
void testDetachWhenInsert()
|
||||
{
|
||||
const Map<int, int> referenceSource = {
|
||||
{ 0, 0 },
|
||||
{ 1, 1 },
|
||||
{ 2, 2 }
|
||||
};
|
||||
|
||||
const Map<int, int> referenceDestination = {
|
||||
{ 0, 0 },
|
||||
{ 1, 1 },
|
||||
{ 2, 2 },
|
||||
{ 3, 3 }
|
||||
};
|
||||
|
||||
// copy insertion of non-shared map
|
||||
{
|
||||
Map<int, int> source;
|
||||
source.insert(0, 0);
|
||||
source.insert(1, 1);
|
||||
source.insert(2, 2);
|
||||
|
||||
Map<int, int> dest;
|
||||
dest.insert(3, 3);
|
||||
Map<int, int> destCopy = dest;
|
||||
|
||||
dest.insert(source);
|
||||
|
||||
QCOMPARE(source, referenceSource);
|
||||
QCOMPARE(dest, referenceDestination);
|
||||
|
||||
QCOMPARE(destCopy.count(), 1); // unchanged
|
||||
}
|
||||
|
||||
// copy insertion of shared map
|
||||
{
|
||||
Map<int, int> source;
|
||||
source.insert(0, 0);
|
||||
source.insert(1, 1);
|
||||
source.insert(2, 2);
|
||||
Map<int, int> sourceCopy = source;
|
||||
|
||||
Map<int, int> dest;
|
||||
dest.insert(3, 3);
|
||||
Map<int, int> destCopy = dest;
|
||||
|
||||
dest.insert(source);
|
||||
|
||||
QCOMPARE(source, referenceSource);
|
||||
QCOMPARE(sourceCopy, referenceSource);
|
||||
|
||||
QCOMPARE(dest, referenceDestination);
|
||||
QCOMPARE(destCopy.count(), 1); // unchanged
|
||||
}
|
||||
|
||||
// move insertion of non-shared map
|
||||
{
|
||||
Map<int, int> source;
|
||||
source.insert(0, 0);
|
||||
source.insert(1, 1);
|
||||
source.insert(2, 2);
|
||||
|
||||
Map<int, int> dest;
|
||||
dest.insert(3, 3);
|
||||
Map<int, int> destCopy = dest;
|
||||
|
||||
dest.insert(source);
|
||||
|
||||
QCOMPARE(dest, referenceDestination);
|
||||
QCOMPARE(destCopy.count(), 1); // unchanged
|
||||
}
|
||||
|
||||
// move insertion of shared map
|
||||
{
|
||||
Map<int, int> source;
|
||||
source.insert(0, 0);
|
||||
source.insert(1, 1);
|
||||
source.insert(2, 2);
|
||||
Map<int, int> sourceCopy = source;
|
||||
|
||||
Map<int, int> dest;
|
||||
dest.insert(3, 3);
|
||||
Map<int, int> destCopy = dest;
|
||||
|
||||
dest.insert(std::move(source));
|
||||
|
||||
QCOMPARE(sourceCopy, referenceSource);
|
||||
|
||||
QCOMPARE(dest, referenceDestination);
|
||||
QCOMPARE(destCopy.count(), 1); // unchanged
|
||||
}
|
||||
};
|
||||
|
||||
void tst_QMap::insertMap()
|
||||
{
|
||||
{
|
||||
@ -1298,6 +1392,9 @@ void tst_QMap::insertMap()
|
||||
|
||||
QCOMPARE(map.count(), 1);
|
||||
}
|
||||
|
||||
testDetachWhenInsert<QMap>();
|
||||
testDetachWhenInsert<QMultiMap>();
|
||||
}
|
||||
|
||||
void tst_QMap::checkMostLeftNode()
|
||||
|
Loading…
Reference in New Issue
Block a user