Skip to content

Commit

Permalink
OGRGeomFieldDefn copy cstor: avoid false positive warnings with cppch…
Browse files Browse the repository at this point in the history
…eck master, and add testing
  • Loading branch information
rouault committed Jan 19, 2025
1 parent 93878ec commit fe25f91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
30 changes: 30 additions & 0 deletions autotest/cpp/test_ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,36 @@ TEST_F(test_ogr, feature_defn_geomfields_iterator)
EXPECT_EQ(i, oFDefn.GetGeomFieldCount());
}

// Test OGRGeomFieldDefn copy constructor
TEST_F(test_ogr, geom_field_defn_copy_constructor)
{
{
OGRGeomFieldDefn oGeomFieldDefn("field1", wkbPoint);
oGeomFieldDefn.SetNullable(false);
OGRGeomFieldDefn oGeomFieldDefn2("field2", wkbLineString);
oGeomFieldDefn2 = oGeomFieldDefn;
EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn));
}

{
OGRSpatialReference oSRS;
oSRS.SetFromUserInput("WGS84");
EXPECT_EQ(oSRS.GetReferenceCount(), 1);
OGRGeomFieldDefn oGeomFieldDefn("field1", wkbPoint);
oGeomFieldDefn.SetSpatialRef(&oSRS);
EXPECT_EQ(oSRS.GetReferenceCount(), 2);
OGRGeomFieldDefn oGeomFieldDefn2("field2", wkbLineString);
oGeomFieldDefn2 = oGeomFieldDefn;
EXPECT_EQ(oSRS.GetReferenceCount(), 3);
EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn));

// oGeomFieldDefn2 already points to oSRS
oGeomFieldDefn2 = oGeomFieldDefn;
EXPECT_EQ(oSRS.GetReferenceCount(), 3);
EXPECT_TRUE(oGeomFieldDefn2.IsSame(&oGeomFieldDefn));
}
}

// Test GDALDataset QueryLoggerFunc callback
TEST_F(test_ogr, GDALDatasetSetQueryLoggerFunc)
{
Expand Down
13 changes: 9 additions & 4 deletions ogr/ogrgeomfielddefn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,15 @@ OGRGeomFieldDefn &OGRGeomFieldDefn::operator=(const OGRGeomFieldDefn &oOther)
{
if (&oOther != this)
{
SetName(oOther.pszName);
SetType(oOther.eGeomType);
SetSpatialRef(oOther.poSRS);
SetNullable(oOther.bNullable);
CPLFree(pszName);
pszName = CPLStrdup(oOther.pszName);
eGeomType = oOther.eGeomType;
if (oOther.poSRS)
const_cast<OGRSpatialReference *>(oOther.poSRS)->Reference();
if (poSRS)
const_cast<OGRSpatialReference *>(poSRS)->Dereference();
poSRS = oOther.poSRS;
bNullable = oOther.bNullable;
m_oCoordPrecision = oOther.m_oCoordPrecision;
m_bSealed = oOther.m_bSealed;
bIgnore = oOther.bIgnore;
Expand Down

0 comments on commit fe25f91

Please sign in to comment.