Skip to content

Commit

Permalink
RFC104: add reference counting to GDALArgDatasetValue::m_poDS
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 24, 2024
1 parent f8c03fc commit 5e99cf5
Show file tree
Hide file tree
Showing 24 changed files with 236 additions and 309 deletions.
14 changes: 9 additions & 5 deletions apps/gdalalg_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool GDALDispatcherAlgorithm<RasterDispatcher, VectorDispatcher>::

if (ok)
{
auto poDS = m_rasterDispatcher->GetDataset();
auto poDS = m_rasterDispatcher->GetDatasetRef();
// cppcheck-suppress knownConditionTrueFalse
if (poDS &&
(poDS->GetRasterCount() > 0 || poDS->GetMetadata("SUBDATASETS")))
Expand Down Expand Up @@ -112,11 +112,11 @@ bool GDALDispatcherAlgorithm<RasterDispatcher, VectorDispatcher>::
return false;
}

auto poDSFromRaster = m_rasterDispatcher->GetDataset();
auto poDSFromRaster = m_rasterDispatcher->GetDatasetRef();
// cppcheck-suppress knownConditionTrueFalse
if (poDSFromRaster)
{
m_vectorDispatcher->SetDataset(poDSFromRaster, false);
m_vectorDispatcher->SetDataset(poDSFromRaster);
}

std::vector<std::string> argsWithoutInput;
Expand Down Expand Up @@ -180,7 +180,9 @@ bool GDALDispatcherAlgorithm<RasterDispatcher, VectorDispatcher>::
return false;
}
m_rasterDispatcher = std::make_unique<RasterDispatcher>();
m_rasterDispatcher->SetDataset(poDS.release(), true);
auto poDSRaw = poDS.get();
m_rasterDispatcher->SetDataset(poDS.release());
poDSRaw->Release();
m_selectedSubAlg = m_rasterDispatcher.get();
std::vector<std::string> callPath(m_callPath);
callPath.push_back("raster");
Expand All @@ -192,7 +194,9 @@ bool GDALDispatcherAlgorithm<RasterDispatcher, VectorDispatcher>::
else if (poDS->GetLayerCount() != 0)
{
m_vectorDispatcher = std::make_unique<VectorDispatcher>();
m_vectorDispatcher->SetDataset(poDS.release(), true);
auto poDSRaw = poDS.get();
m_vectorDispatcher->SetDataset(poDS.release());
poDSRaw->Release();
m_selectedSubAlg = m_vectorDispatcher.get();
std::vector<std::string> callPath(m_callPath);
callPath.push_back("vector");
Expand Down
4 changes: 2 additions & 2 deletions apps/gdalalg_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class GDALDummyRasterPipelineAlgorithm final : public GDALAlgorithm
}

/* cppcheck-suppress functionStatic */
GDALDataset *GetDataset()
GDALDataset *GetDatasetRef()
{
return nullptr;
}

/* cppcheck-suppress functionStatic */
void SetDataset(GDALDataset *, bool)
void SetDataset(GDALDataset *)
{
}

Expand Down
6 changes: 3 additions & 3 deletions apps/gdalalg_raster_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ GDALRasterConvertAlgorithm::GDALRasterConvertAlgorithm(
bool GDALRasterConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
CPLAssert(m_inputDataset.GetDataset());
if (m_outputDataset.GetDataset())
CPLAssert(m_inputDataset.GetDatasetRef());
if (m_outputDataset.GetDatasetRef())
{
CPLError(CE_Failure, CPLE_NotSupported,
"gdal raster convert does not support outputting to an "
Expand Down Expand Up @@ -94,7 +94,7 @@ bool GDALRasterConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,

auto poOutDS = std::unique_ptr<GDALDataset>(GDALDataset::FromHandle(
GDALTranslate(m_outputDataset.GetName().c_str(),
GDALDataset::ToHandle(m_inputDataset.GetDataset()),
GDALDataset::ToHandle(m_inputDataset.GetDatasetRef()),
psOptions, nullptr)));
GDALTranslateOptionsFree(psOptions);
if (!poOutDS)
Expand Down
8 changes: 4 additions & 4 deletions apps/gdalalg_raster_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class GDALRasterConvertAlgorithm final : public GDALAlgorithm

explicit GDALRasterConvertAlgorithm(bool openForMixedRasterVector = false);

GDALDataset *GetDataset()
GDALDataset *GetDatasetRef()
{
return m_inputDataset.GetDataset();
return m_inputDataset.GetDatasetRef();
}

void SetDataset(GDALDataset *poDS, bool owned)
void SetDataset(GDALDataset *poDS)
{
auto arg = GetArg(GDAL_ARG_NAME_INPUT);
arg->Set(poDS, owned);
arg->Set(poDS);
arg->SetSkipIfAlreadySet();
}

Expand Down
4 changes: 2 additions & 2 deletions apps/gdalalg_raster_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ GDALRasterInfoAlgorithm::GDALRasterInfoAlgorithm(bool openForMixedRasterVector)

bool GDALRasterInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
{
CPLAssert(m_dataset.GetDataset());
CPLAssert(m_dataset.GetDatasetRef());

CPLStringList aosOptions;
if (m_format == "json")
Expand Down Expand Up @@ -128,7 +128,7 @@ bool GDALRasterInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
aosOptions.AddString(m_mdd.c_str());
}

GDALDatasetH hDS = GDALDataset::ToHandle(m_dataset.GetDataset());
GDALDatasetH hDS = GDALDataset::ToHandle(m_dataset.GetDatasetRef());
std::unique_ptr<GDALDataset> poSubDataset;

if (m_subDS > 0)
Expand Down
13 changes: 8 additions & 5 deletions apps/gdalalg_raster_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ class GDALRasterInfoAlgorithm final : public GDALAlgorithm

explicit GDALRasterInfoAlgorithm(bool openForMixedRasterVector = false);

GDALDataset *GetDataset()
GDALDataset *GetDatasetRef()
{
return m_dataset.GetDataset();
return m_dataset.GetDatasetRef();
}

void SetDataset(GDALDataset *poDS, bool owned)
void SetDataset(GDALDataset *poDS)
{
auto arg = GetArg(GDAL_ARG_NAME_INPUT);
arg->Set(poDS, owned);
arg->SetSkipIfAlreadySet();
if (arg)
{
arg->Set(poDS);
arg->SetSkipIfAlreadySet();
}
}

private:
Expand Down
7 changes: 4 additions & 3 deletions apps/gdalalg_vector_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ GDALVectorConvertAlgorithm::GDALVectorConvertAlgorithm()
bool GDALVectorConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
CPLAssert(m_inputDataset.GetDataset());
CPLAssert(m_inputDataset.GetDatasetRef());

CPLStringList aosOptions;
aosOptions.AddString("--invoked-from-gdal-vector-convert");
Expand Down Expand Up @@ -116,8 +116,9 @@ bool GDALVectorConvertAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress,
pProgressData);

GDALDatasetH hOutDS = GDALDataset::ToHandle(m_outputDataset.GetDataset());
GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDataset());
GDALDatasetH hOutDS =
GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
auto poRetDS = GDALDataset::FromHandle(
GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
&hSrcDS, psOptions, nullptr));
Expand Down
4 changes: 2 additions & 2 deletions apps/gdalalg_vector_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class GDALVectorConvertAlgorithm final : public GDALAlgorithm

GDALVectorConvertAlgorithm();

void SetDataset(GDALDataset *poDS, bool owned)
void SetDataset(GDALDataset *poDS)
{
auto arg = GetArg(GDAL_ARG_NAME_INPUT);
if (arg)
{
arg->Set(poDS, owned);
arg->Set(poDS);
arg->SetSkipIfAlreadySet();
}
}
Expand Down
8 changes: 4 additions & 4 deletions apps/gdalalg_vector_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ GDALVectorFilterAlgorithm::GDALVectorFilterAlgorithm()

bool GDALVectorFilterAlgorithm::RunImpl(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDataset());
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
CPLAssert(!m_outputDataset.GetDataset());
CPLAssert(!m_outputDataset.GetDatasetRef());

bool ret = true;
if (m_bbox.size() == 4)
Expand All @@ -67,7 +67,7 @@ bool GDALVectorFilterAlgorithm::RunImpl(GDALProgressFunc, void *)
const double ymin = m_bbox[1];
const double xmax = m_bbox[2];
const double ymax = m_bbox[3];
auto poSrcDS = m_inputDataset.GetDataset();
auto poSrcDS = m_inputDataset.GetDatasetRef();
const int nLayerCount = poSrcDS->GetLayerCount();
for (int i = 0; i < nLayerCount; ++i)
{
Expand All @@ -78,7 +78,7 @@ bool GDALVectorFilterAlgorithm::RunImpl(GDALProgressFunc, void *)
}
}

m_outputDataset.Set(m_inputDataset.GetDataset(), false);
m_outputDataset.Set(m_inputDataset.GetDatasetRef());

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions apps/gdalalg_vector_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm()

bool GDALVectorInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
{
CPLAssert(m_dataset.GetDataset());
CPLAssert(m_dataset.GetDatasetRef());

CPLStringList aosOptions;
if (m_format == "json")
Expand Down Expand Up @@ -106,8 +106,8 @@ bool GDALVectorInfoAlgorithm::RunImpl(GDALProgressFunc, void *)

GDALVectorInfoOptions *psInfo =
GDALVectorInfoOptionsNew(aosOptions.List(), nullptr);
char *ret =
GDALVectorInfo(GDALDataset::ToHandle(m_dataset.GetDataset()), psInfo);
char *ret = GDALVectorInfo(GDALDataset::ToHandle(m_dataset.GetDatasetRef()),
psInfo);
GDALVectorInfoOptionsFree(psInfo);
if (!ret)
return false;
Expand Down
4 changes: 2 additions & 2 deletions apps/gdalalg_vector_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class GDALVectorInfoAlgorithm final : public GDALAlgorithm

GDALVectorInfoAlgorithm();

void SetDataset(GDALDataset *poDS, bool owned)
void SetDataset(GDALDataset *poDS)
{
auto arg = GetArg(GDAL_ARG_NAME_INPUT);
if (arg)
{
arg->Set(poDS, owned);
arg->Set(poDS);
arg->SetSkipIfAlreadySet();
}
}
Expand Down
12 changes: 6 additions & 6 deletions apps/gdalalg_vector_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,17 @@ bool GDALVectorPipelineAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
auto &step = m_steps[i];
if (i > 0)
{
if (step->m_inputDataset.GetDataset())
if (step->m_inputDataset.GetDatasetRef())
{
// Shouldn't happen
ReportError(CE_Failure, CPLE_AppDefined,
"Step nr %d (%s) has already an input dataset",
static_cast<int>(i), step->GetName().c_str());
return false;
}
step->m_inputDataset.Set(poCurDS, false);
step->m_inputDataset.Set(poCurDS);
}
if (i + 1 < m_steps.size() && step->m_outputDataset.GetDataset())
if (i + 1 < m_steps.size() && step->m_outputDataset.GetDatasetRef())
{
// Shouldn't happen
ReportError(CE_Failure, CPLE_AppDefined,
Expand All @@ -420,7 +420,7 @@ bool GDALVectorPipelineAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
{
return false;
}
poCurDS = step->m_outputDataset.GetDataset();
poCurDS = step->m_outputDataset.GetDatasetRef();
if (!poCurDS)
{
ReportError(CE_Failure, CPLE_AppDefined,
Expand All @@ -430,9 +430,9 @@ bool GDALVectorPipelineAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
}
}

if (!m_outputDataset.GetDataset())
if (!m_outputDataset.GetDatasetRef())
{
m_outputDataset.Set(poCurDS, false);
m_outputDataset.Set(poCurDS);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion apps/gdalalg_vector_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class GDALVectorPipelineAlgorithm final : public GDALVectorPipelineStepAlgorithm
std::string GetUsageAsJSON() const override;

/* cppcheck-suppress functionStatic */
void SetDataset(GDALDataset *, bool)
void SetDataset(GDALDataset *)
{
}

Expand Down
8 changes: 4 additions & 4 deletions apps/gdalalg_vector_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ class GDALVectorReadAlgorithmDataset final : public GDALDataset

bool GDALVectorReadAlgorithm::RunImpl(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDataset());
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
CPLAssert(!m_outputDataset.GetDataset());
CPLAssert(!m_outputDataset.GetDatasetRef());

if (m_inputLayerNames.empty())
{
m_outputDataset.Set(m_inputDataset.GetDataset(), false);
m_outputDataset.Set(m_inputDataset.GetDatasetRef());
}
else
{
auto poSrcDS = m_inputDataset.GetDataset();
auto poSrcDS = m_inputDataset.GetDatasetRef();
auto poOutDS = std::make_unique<GDALVectorReadAlgorithmDataset>();
poOutDS->SetDescription(poSrcDS->GetDescription());
for (const auto &srcLayerName : m_inputLayerNames)
Expand Down
6 changes: 3 additions & 3 deletions apps/gdalalg_vector_reproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class GDALVectorReprojectAlgorithmDataset final : public GDALDataset

bool GDALVectorReprojectAlgorithm::RunImpl(GDALProgressFunc, void *)
{
CPLAssert(m_inputDataset.GetDataset());
CPLAssert(m_inputDataset.GetDatasetRef());
CPLAssert(m_outputDataset.GetName().empty());
CPLAssert(!m_outputDataset.GetDataset());
CPLAssert(!m_outputDataset.GetDatasetRef());

std::unique_ptr<OGRSpatialReference> poSrcCRS;
if (!m_srsCrs.empty())
Expand All @@ -99,7 +99,7 @@ bool GDALVectorReprojectAlgorithm::RunImpl(GDALProgressFunc, void *)
}
oDstCRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);

auto poSrcDS = m_inputDataset.GetDataset();
auto poSrcDS = m_inputDataset.GetDatasetRef();

auto reprojectedDataset =
std::make_unique<GDALVectorReprojectAlgorithmDataset>();
Expand Down
7 changes: 4 additions & 3 deletions apps/gdalalg_vector_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ GDALVectorWriteAlgorithm::GDALVectorWriteAlgorithm()
bool GDALVectorWriteAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
void *pProgressData)
{
CPLAssert(m_inputDataset.GetDataset());
CPLAssert(m_inputDataset.GetDatasetRef());

CPLStringList aosOptions;
aosOptions.AddString("--invoked-from-gdal-vector-convert");
Expand Down Expand Up @@ -86,8 +86,9 @@ bool GDALVectorWriteAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
GDALVectorTranslateOptionsSetProgress(psOptions, pfnProgress,
pProgressData);

GDALDatasetH hOutDS = GDALDataset::ToHandle(m_outputDataset.GetDataset());
GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDataset());
GDALDatasetH hOutDS =
GDALDataset::ToHandle(m_outputDataset.GetDatasetRef());
GDALDatasetH hSrcDS = GDALDataset::ToHandle(m_inputDataset.GetDatasetRef());
auto poRetDS = GDALDataset::FromHandle(
GDALVectorTranslate(m_outputDataset.GetName().c_str(), hOutDS, 1,
&hSrcDS, psOptions, nullptr));
Expand Down
Loading

0 comments on commit 5e99cf5

Please sign in to comment.