RFC104: add reference counting to GDALArgDatasetValue::m_poDS
This commit is contained in:
parent
ef8d0f6baa
commit
0de6b1e157
|
@ -88,7 +88,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")))
|
||||
|
@ -117,11 +117,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;
|
||||
|
@ -185,7 +185,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");
|
||||
|
@ -197,7 +199,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");
|
||||
|
|
|
@ -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 *)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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 "
|
||||
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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");
|
||||
|
@ -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));
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -78,7 +78,7 @@ bool GDALVectorFilterAlgorithm::RunImpl(GDALProgressFunc, void *)
|
|||
}
|
||||
}
|
||||
|
||||
m_outputDataset.Set(m_inputDataset.GetDataset(), false);
|
||||
m_outputDataset.Set(m_inputDataset.GetDatasetRef());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ GDALVectorInfoAlgorithm::GDALVectorInfoAlgorithm()
|
|||
|
||||
bool GDALVectorInfoAlgorithm::RunImpl(GDALProgressFunc, void *)
|
||||
{
|
||||
CPLAssert(m_dataset.GetDataset());
|
||||
CPLAssert(m_dataset.GetDatasetRef());
|
||||
|
||||
CPLStringList aosOptions;
|
||||
if (m_format == "json")
|
||||
|
@ -113,8 +113,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;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -397,7 +397,7 @@ 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,
|
||||
|
@ -405,9 +405,9 @@ bool GDALVectorPipelineAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
|
|||
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,
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
|
|
@ -95,7 +95,7 @@ class GDALVectorPipelineAlgorithm final : public GDALVectorPipelineStepAlgorithm
|
|||
std::string GetUsageAsJSON() const override;
|
||||
|
||||
/* cppcheck-suppress functionStatic */
|
||||
void SetDataset(GDALDataset *, bool)
|
||||
void SetDataset(GDALDataset *)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
@ -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>();
|
||||
|
|
|
@ -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");
|
||||
|
@ -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));
|
||||
|
|
|
@ -249,7 +249,7 @@ TEST_F(test_gdal_algorithm, GDALAlgorithmArg_Set)
|
|||
{
|
||||
CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
|
||||
CPLErrorReset();
|
||||
arg.Set(static_cast<GDALDataset *>(nullptr), false);
|
||||
arg.Set(static_cast<GDALDataset *>(nullptr));
|
||||
EXPECT_EQ(CPLGetLastErrorType(), CE_Failure);
|
||||
EXPECT_STREQ(arg.Get<std::string>().c_str(), "foo");
|
||||
}
|
||||
|
@ -310,25 +310,19 @@ TEST_F(test_gdal_algorithm, GDALAlgorithmArg_Set)
|
|||
GDALArgDatasetValue val;
|
||||
auto arg = GDALAlgorithmArg(
|
||||
GDALAlgorithmArgDecl("", 0, "", GAAT_DATASET), &val);
|
||||
|
||||
arg.Set(poMEMDS.get(), false);
|
||||
EXPECT_EQ(val.GetDataset(), poMEMDS.get());
|
||||
EXPECT_FALSE(val.IsOwned());
|
||||
auto poMEMDSRaw = poMEMDS.get();
|
||||
|
||||
arg.Set(poMEMDS.release(), true);
|
||||
EXPECT_EQ(val.GetDataset(), poMEMDSRaw);
|
||||
EXPECT_TRUE(val.IsOwned());
|
||||
arg.Set(poMEMDS.release());
|
||||
EXPECT_EQ(val.GetDatasetRef(), poMEMDSRaw);
|
||||
|
||||
bool isOwned = false;
|
||||
poMEMDS.reset(val.BorrowDataset(isOwned));
|
||||
EXPECT_TRUE(isOwned);
|
||||
poMEMDS.reset(val.BorrowDataset());
|
||||
EXPECT_EQ(poMEMDS.get(), poMEMDSRaw);
|
||||
EXPECT_EQ(val.GetDataset(), nullptr);
|
||||
EXPECT_EQ(val.GetDatasetRef(), nullptr);
|
||||
|
||||
EXPECT_TRUE(arg.Set(std::move(poMEMDS)));
|
||||
EXPECT_EQ(val.GetDataset(), poMEMDSRaw);
|
||||
EXPECT_TRUE(val.IsOwned());
|
||||
EXPECT_EQ(val.GetDatasetRef(), poMEMDSRaw);
|
||||
|
||||
poMEMDSRaw->ReleaseRef();
|
||||
|
||||
arg.SetDatasetName("foo");
|
||||
EXPECT_STREQ(val.GetName().c_str(), "foo");
|
||||
|
@ -349,13 +343,7 @@ TEST_F(test_gdal_algorithm, GDALAlgorithmArg_Set)
|
|||
{
|
||||
CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
|
||||
CPLErrorReset();
|
||||
arg.Set(static_cast<GDALDataset *>(nullptr), false);
|
||||
EXPECT_EQ(CPLGetLastErrorType(), CE_Failure);
|
||||
}
|
||||
{
|
||||
CPLErrorStateBackuper oBackuper(CPLQuietErrorHandler);
|
||||
CPLErrorReset();
|
||||
arg.Set(std::unique_ptr<GDALDataset>(nullptr));
|
||||
arg.Set(static_cast<GDALDataset *>(nullptr));
|
||||
EXPECT_EQ(CPLGetLastErrorType(), CE_Failure);
|
||||
}
|
||||
}
|
||||
|
@ -662,27 +650,18 @@ TEST_F(test_gdal_algorithm, GDALArgDatasetValue)
|
|||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
GDALArgDatasetValue value(poDS.release(), true);
|
||||
EXPECT_NE(value.GetDataset(), nullptr);
|
||||
EXPECT_TRUE(value.IsOwned());
|
||||
}
|
||||
{
|
||||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
GDALArgDatasetValue value(poDS.get(), false);
|
||||
EXPECT_EQ(value.GetDataset(), poDS.get());
|
||||
EXPECT_FALSE(value.IsOwned());
|
||||
EXPECT_STREQ(value.GetName().c_str(), poDS->GetDescription());
|
||||
auto poDSRaw = poDS.get();
|
||||
GDALArgDatasetValue value(poDS.release());
|
||||
EXPECT_EQ(value.GetDatasetRef(), poDSRaw);
|
||||
EXPECT_STREQ(value.GetName().c_str(), poDSRaw->GetDescription());
|
||||
|
||||
GDALArgDatasetValue value2;
|
||||
value2 = std::move(value);
|
||||
EXPECT_STREQ(value2.GetName().c_str(), poDS->GetDescription());
|
||||
EXPECT_STREQ(value2.GetName().c_str(), poDSRaw->GetDescription());
|
||||
|
||||
poDSRaw->ReleaseRef();
|
||||
}
|
||||
{
|
||||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
GDALArgDatasetValue value("foo");
|
||||
EXPECT_STREQ(value.GetName().c_str(), "foo");
|
||||
|
||||
|
@ -690,15 +669,6 @@ TEST_F(test_gdal_algorithm, GDALArgDatasetValue)
|
|||
value2 = std::move(value);
|
||||
EXPECT_STREQ(value2.GetName().c_str(), "foo");
|
||||
}
|
||||
{
|
||||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
auto poDSRaw = poDS.get();
|
||||
GDALArgDatasetValue value(std::move(poDS));
|
||||
EXPECT_EQ(value.GetDataset(), poDSRaw);
|
||||
EXPECT_TRUE(value.IsOwned());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(test_gdal_algorithm, bool_flag)
|
||||
|
@ -977,7 +947,7 @@ TEST_F(test_gdal_algorithm, dataset)
|
|||
MyAlgorithm alg;
|
||||
EXPECT_TRUE(alg.ParseCommandLineArguments(
|
||||
{"--val=" GCORE_DATA_DIR "byte.tif"}));
|
||||
EXPECT_NE(alg.m_val.GetDataset(), nullptr);
|
||||
EXPECT_NE(alg.m_val.GetDatasetRef(), nullptr);
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -986,8 +956,9 @@ TEST_F(test_gdal_algorithm, dataset)
|
|||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
auto poDSRaw = poDS.get();
|
||||
alg.GetArg("val")->Set(poDS.release(), true);
|
||||
EXPECT_EQ(alg.m_val.GetDataset(), poDSRaw);
|
||||
alg.GetArg("val")->Set(poDS.release());
|
||||
EXPECT_EQ(alg.m_val.GetDatasetRef(), poDSRaw);
|
||||
poDSRaw->ReleaseRef();
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -1053,8 +1024,8 @@ TEST_F(test_gdal_algorithm, input_update)
|
|||
EXPECT_TRUE(!alg.GetUsageAsJSON().empty());
|
||||
EXPECT_TRUE(
|
||||
alg.ParseCommandLineArguments({"--update", osTmpFilename}));
|
||||
ASSERT_NE(alg.m_input.GetDataset(), nullptr);
|
||||
EXPECT_EQ(alg.m_input.GetDataset()->GetAccess(), GA_Update);
|
||||
ASSERT_NE(alg.m_input.GetDatasetRef(), nullptr);
|
||||
EXPECT_EQ(alg.m_input.GetDatasetRef()->GetAccess(), GA_Update);
|
||||
|
||||
alg.Finalize();
|
||||
|
||||
|
@ -1099,10 +1070,11 @@ TEST_F(test_gdal_algorithm, same_input_output_dataset_sqlite)
|
|||
MyAlgorithm alg;
|
||||
EXPECT_TRUE(alg.ParseCommandLineArguments(
|
||||
{"--update", osTmpFilename, osTmpFilename}));
|
||||
ASSERT_NE(alg.m_input.GetDataset(), nullptr);
|
||||
EXPECT_NE(alg.m_output.GetDataset(), nullptr);
|
||||
EXPECT_EQ(alg.m_input.GetDataset(), alg.m_output.GetDataset());
|
||||
EXPECT_EQ(alg.m_input.GetDataset()->GetAccess(), GA_Update);
|
||||
ASSERT_NE(alg.m_input.GetDatasetRef(), nullptr);
|
||||
EXPECT_NE(alg.m_output.GetDatasetRef(), nullptr);
|
||||
EXPECT_EQ(alg.m_input.GetDatasetRef(),
|
||||
alg.m_output.GetDatasetRef());
|
||||
EXPECT_EQ(alg.m_input.GetDatasetRef()->GetAccess(), GA_Update);
|
||||
|
||||
alg.Finalize();
|
||||
|
||||
|
@ -1415,7 +1387,7 @@ TEST_F(test_gdal_algorithm, vector_dataset)
|
|||
EXPECT_TRUE(alg.ParseCommandLineArguments(
|
||||
{"--val=" GCORE_DATA_DIR "byte.tif"}));
|
||||
ASSERT_EQ(alg.m_val.size(), 1U);
|
||||
EXPECT_NE(alg.m_val[0].GetDataset(), nullptr);
|
||||
EXPECT_NE(alg.m_val[0].GetDatasetRef(), nullptr);
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -1425,7 +1397,7 @@ TEST_F(test_gdal_algorithm, vector_dataset)
|
|||
EXPECT_FALSE(alg.ParseCommandLineArguments({"--val=non_existing.tif"}));
|
||||
EXPECT_EQ(CPLGetLastErrorType(), CE_Failure);
|
||||
ASSERT_EQ(alg.m_val.size(), 1U);
|
||||
EXPECT_EQ(alg.m_val[0].GetDataset(), nullptr);
|
||||
EXPECT_EQ(alg.m_val[0].GetDatasetRef(), nullptr);
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -1506,8 +1478,8 @@ TEST_F(test_gdal_algorithm, vector_input)
|
|||
{"--update", "--oo=LIST_ALL_TABLES=YES", "--if=GPKG",
|
||||
osTmpFilename}));
|
||||
ASSERT_EQ(alg.m_input.size(), 1U);
|
||||
ASSERT_NE(alg.m_input[0].GetDataset(), nullptr);
|
||||
EXPECT_EQ(alg.m_input[0].GetDataset()->GetAccess(), GA_Update);
|
||||
ASSERT_NE(alg.m_input[0].GetDatasetRef(), nullptr);
|
||||
EXPECT_EQ(alg.m_input[0].GetDatasetRef()->GetAccess(), GA_Update);
|
||||
|
||||
alg.Finalize();
|
||||
|
||||
|
@ -2995,8 +2967,7 @@ TEST_F(test_gdal_algorithm, algorithm_c_api)
|
|||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
GDALArgDatasetValueSetDatasetWithoutOwnership(hVal, poDS.get());
|
||||
GDALArgDatasetValueSetDatasetWithOwnership(hVal, poDS.release());
|
||||
GDALArgDatasetValueSetDataset(hVal, poDS.release());
|
||||
}
|
||||
|
||||
GDALAlgorithmArgSetAsDatasetValue(hArg, hVal);
|
||||
|
@ -3004,27 +2975,32 @@ TEST_F(test_gdal_algorithm, algorithm_c_api)
|
|||
|
||||
hVal = GDALAlgorithmArgGetAsDatasetValue(hArg);
|
||||
ASSERT_NE(hVal, nullptr);
|
||||
EXPECT_NE(GDALArgDatasetValueGetDataset(hVal), nullptr);
|
||||
auto hDS = GDALArgDatasetValueGetDatasetRef(hVal);
|
||||
EXPECT_NE(hDS, nullptr);
|
||||
{
|
||||
auto hDS2 = GDALArgDatasetValueGetDatasetIncreaseRefCount(hVal);
|
||||
EXPECT_EQ(hDS2, hDS);
|
||||
GDALReleaseDataset(hDS2);
|
||||
}
|
||||
GDALArgDatasetValueRelease(hVal);
|
||||
|
||||
GDALAlgorithmArgSetDatasetWithoutOwnership(hArg, nullptr);
|
||||
GDALAlgorithmArgSetDataset(hArg, nullptr);
|
||||
|
||||
hVal = GDALAlgorithmArgGetAsDatasetValue(hArg);
|
||||
ASSERT_NE(hVal, nullptr);
|
||||
EXPECT_EQ(GDALArgDatasetValueGetDataset(hVal), nullptr);
|
||||
EXPECT_EQ(GDALArgDatasetValueGetDatasetRef(hVal), nullptr);
|
||||
GDALArgDatasetValueRelease(hVal);
|
||||
|
||||
{
|
||||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
GetGDALDriverManager()->GetDriverByName("MEM")->Create(
|
||||
"", 1, 1, 1, GDT_Byte, nullptr));
|
||||
GDALAlgorithmArgSetDatasetWithoutOwnership(hArg, poDS.get());
|
||||
GDALAlgorithmArgSetDatasetWithOwnership(hArg, poDS.release());
|
||||
GDALAlgorithmArgSetDataset(hArg, poDS.release());
|
||||
}
|
||||
|
||||
hVal = GDALAlgorithmArgGetAsDatasetValue(hArg);
|
||||
ASSERT_NE(hVal, nullptr);
|
||||
EXPECT_NE(GDALArgDatasetValueGetDataset(hVal), nullptr);
|
||||
EXPECT_NE(GDALArgDatasetValueGetDatasetRef(hVal), nullptr);
|
||||
GDALArgDatasetValueRelease(hVal);
|
||||
|
||||
GDALAlgorithmArgRelease(hArg);
|
||||
|
|
|
@ -163,22 +163,14 @@ def test_algorithm_dataset_value(tmp_path):
|
|||
|
||||
in_ds = input_arg_value.GetDataset()
|
||||
assert in_ds is not None
|
||||
assert input_arg_value.IsDatasetOwned()
|
||||
|
||||
out_ds = output_arg_value.GetDataset()
|
||||
assert out_ds is not None
|
||||
assert output_arg_value.IsDatasetOwned()
|
||||
assert out_ds.GetRasterBand(1).Checksum() == 4672
|
||||
|
||||
output_arg_value.SetDatasetWithoutOwnership(None)
|
||||
output_arg_value.SetDatasetWithOwnership(None)
|
||||
output_arg_value.SetDataset(None)
|
||||
with pytest.raises(
|
||||
Exception,
|
||||
match="Dataset object 'output' is created by algorithm and cannot be set as an input",
|
||||
):
|
||||
output_arg.SetDatasetWithoutOwnership(None)
|
||||
with pytest.raises(
|
||||
Exception,
|
||||
match="Dataset object 'output' is created by algorithm and cannot be set as an input",
|
||||
):
|
||||
output_arg.SetDatasetWithOwnership(None)
|
||||
output_arg.SetDataset(None)
|
||||
|
|
|
@ -76,7 +76,7 @@ def test_gdalalg_info_invalid_arg():
|
|||
def test_gdalalg_info_run_cannot_be_run():
|
||||
info = get_info_alg()
|
||||
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
with pytest.raises(Exception, match="method should not be called directly"):
|
||||
info.Run()
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ def test_gdalalg_raster_convert_append(tmp_vsimem):
|
|||
def test_gdalalg_raster_convert_error_output_already_set():
|
||||
convert = get_convert_alg()
|
||||
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
|
||||
convert.GetArg("output").Get().SetDatasetWithoutOwnership(ds)
|
||||
convert.GetArg("output").Get().SetDataset(ds)
|
||||
assert convert.ParseCommandLineArguments(["data/utmsmall.tif"])
|
||||
with pytest.raises(
|
||||
Exception,
|
||||
|
|
|
@ -58,7 +58,7 @@ def test_gdalalg_raster_info_mm_checksum():
|
|||
def test_gdalalg_raster_info_stats():
|
||||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(["--stats"])
|
||||
output_string = info.GetArg("output-string").Get()
|
||||
j = json.loads(output_string)
|
||||
|
@ -68,7 +68,7 @@ def test_gdalalg_raster_info_stats():
|
|||
def test_gdalalg_raster_info_approx_stats():
|
||||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(["--approx-stats"])
|
||||
output_string = info.GetArg("output-string").Get()
|
||||
j = json.loads(output_string)
|
||||
|
@ -78,7 +78,7 @@ def test_gdalalg_raster_info_approx_stats():
|
|||
def test_gdalalg_raster_info_hist():
|
||||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(["--hist"])
|
||||
output_string = info.GetArg("output-string").Get()
|
||||
j = json.loads(output_string)
|
||||
|
@ -88,7 +88,7 @@ def test_gdalalg_raster_info_hist():
|
|||
def test_gdalalg_raster_info_no_options():
|
||||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(
|
||||
["--no-gcp", "--no-md", "--no-ct", "--no-fl", "--no-nodata", "--no-mask"]
|
||||
)
|
||||
|
@ -98,7 +98,7 @@ def test_gdalalg_raster_info_list_mdd():
|
|||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(["--list-mdd"])
|
||||
output_string = info.GetArg("output-string").Get()
|
||||
j = json.loads(output_string)
|
||||
|
@ -109,7 +109,7 @@ def test_gdalalg_raster_info_mdd_all():
|
|||
info = get_info_alg()
|
||||
ds = gdal.Translate("", "../gcore/data/byte.tif", format="MEM")
|
||||
ds.SetMetadataItem("foo", "bar", "MY_DOMAIN")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
assert info.ParseRunAndFinalize(["--mdd=all"])
|
||||
output_string = info.GetArg("output-string").Get()
|
||||
j = json.loads(output_string)
|
||||
|
@ -149,7 +149,7 @@ def test_gdalalg_raster_info_list_subdataset_error_cannot_open_subdataset():
|
|||
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
|
||||
ds.SetMetadataItem("SUBDATASET_1_DESC", "desc", "SUBDATASETS")
|
||||
ds.SetMetadataItem("SUBDATASET_1_NAME", "i_do_not_exist", "SUBDATASETS")
|
||||
info.GetArg("input").SetDatasetWithoutOwnership(ds)
|
||||
info.GetArg("input").SetDataset(ds)
|
||||
with pytest.raises(
|
||||
Exception,
|
||||
match="i_do_not_exist",
|
||||
|
|
|
@ -295,7 +295,7 @@ bool GDALAlgorithmArg::Set(double value)
|
|||
return SetInternal(value);
|
||||
}
|
||||
|
||||
bool GDALAlgorithmArg::Set(GDALDataset *ds, bool owned)
|
||||
bool GDALAlgorithmArg::Set(GDALDataset *ds)
|
||||
{
|
||||
if (m_decl.GetType() != GAAT_DATASET)
|
||||
{
|
||||
|
@ -316,7 +316,7 @@ bool GDALAlgorithmArg::Set(GDALDataset *ds, bool owned)
|
|||
return false;
|
||||
}
|
||||
m_explicitlySet = true;
|
||||
val.Set(ds, owned);
|
||||
val.Set(ds);
|
||||
return RunAllActions();
|
||||
}
|
||||
|
||||
|
@ -566,20 +566,12 @@ GDALInConstructionAlgorithmArg &GDALInConstructionAlgorithmArg::SetPositional()
|
|||
/* GDALArgDatasetValue::GDALArgDatasetValue() */
|
||||
/************************************************************************/
|
||||
|
||||
GDALArgDatasetValue::GDALArgDatasetValue(GDALDataset *poDS, bool owned)
|
||||
: m_poDS(poDS), m_owned(owned),
|
||||
m_name(m_poDS ? m_poDS->GetDescription() : std::string()), m_nameSet(true)
|
||||
{
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValue::GDALArgDatasetValue() */
|
||||
/************************************************************************/
|
||||
|
||||
GDALArgDatasetValue::GDALArgDatasetValue(std::unique_ptr<GDALDataset> poDS)
|
||||
: m_poDS(poDS.release()), m_owned(true),
|
||||
m_name(m_poDS ? m_poDS->GetDescription() : std::string()), m_nameSet(true)
|
||||
GDALArgDatasetValue::GDALArgDatasetValue(GDALDataset *poDS)
|
||||
: m_poDS(poDS), m_name(m_poDS ? m_poDS->GetDescription() : std::string()),
|
||||
m_nameSet(true)
|
||||
{
|
||||
if (m_poDS)
|
||||
m_poDS->Reference();
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
|
@ -603,7 +595,6 @@ void GDALArgDatasetValue::Set(std::unique_ptr<GDALDataset> poDS)
|
|||
{
|
||||
Close();
|
||||
m_poDS = poDS.release();
|
||||
m_owned = true;
|
||||
m_name = m_poDS ? m_poDS->GetDescription() : std::string();
|
||||
m_nameSet = true;
|
||||
if (m_ownerArg)
|
||||
|
@ -614,17 +605,32 @@ void GDALArgDatasetValue::Set(std::unique_ptr<GDALDataset> poDS)
|
|||
/* GDALArgDatasetValue::Set() */
|
||||
/************************************************************************/
|
||||
|
||||
void GDALArgDatasetValue::Set(GDALDataset *poDS, bool owned)
|
||||
void GDALArgDatasetValue::Set(GDALDataset *poDS)
|
||||
{
|
||||
Close();
|
||||
m_poDS = poDS;
|
||||
m_owned = owned;
|
||||
if (m_poDS)
|
||||
m_poDS->Reference();
|
||||
m_name = m_poDS ? m_poDS->GetDescription() : std::string();
|
||||
m_nameSet = true;
|
||||
if (m_ownerArg)
|
||||
m_ownerArg->NotifyValueSet();
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValue::SetFrom() */
|
||||
/************************************************************************/
|
||||
|
||||
void GDALArgDatasetValue::SetFrom(const GDALArgDatasetValue &other)
|
||||
{
|
||||
Close();
|
||||
m_name = other.m_name;
|
||||
m_nameSet = other.m_nameSet;
|
||||
m_poDS = other.m_poDS;
|
||||
if (m_poDS)
|
||||
m_poDS->Reference();
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValue::~GDALArgDatasetValue() */
|
||||
/************************************************************************/
|
||||
|
@ -641,7 +647,7 @@ GDALArgDatasetValue::~GDALArgDatasetValue()
|
|||
bool GDALArgDatasetValue::Close()
|
||||
{
|
||||
bool ret = true;
|
||||
if (m_owned && m_poDS)
|
||||
if (m_poDS && m_poDS->Dereference() == 0)
|
||||
{
|
||||
ret = m_poDS->Close() == CE_None;
|
||||
delete m_poDS;
|
||||
|
@ -658,30 +664,38 @@ GDALArgDatasetValue &GDALArgDatasetValue::operator=(GDALArgDatasetValue &&other)
|
|||
{
|
||||
Close();
|
||||
m_poDS = other.m_poDS;
|
||||
m_owned = other.m_owned;
|
||||
m_name = other.m_name;
|
||||
m_nameSet = other.m_nameSet;
|
||||
m_type = other.m_type;
|
||||
m_inputFlags = other.m_inputFlags;
|
||||
m_outputFlags = other.m_outputFlags;
|
||||
other.m_poDS = nullptr;
|
||||
other.m_owned = false;
|
||||
other.m_name.clear();
|
||||
other.m_nameSet = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValue::GetDataset() */
|
||||
/************************************************************************/
|
||||
|
||||
GDALDataset *GDALArgDatasetValue::GetDatasetIncreaseRefCount()
|
||||
{
|
||||
if (m_poDS)
|
||||
m_poDS->Reference();
|
||||
return m_poDS;
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValue(GDALArgDatasetValue &&other) */
|
||||
/************************************************************************/
|
||||
|
||||
GDALArgDatasetValue::GDALArgDatasetValue(GDALArgDatasetValue &&other)
|
||||
: m_poDS(other.m_poDS), m_owned(other.m_owned), m_name(other.m_name),
|
||||
m_nameSet(other.m_nameSet), m_type(other.m_type),
|
||||
m_inputFlags(other.m_inputFlags), m_outputFlags(other.m_outputFlags)
|
||||
: m_poDS(other.m_poDS), m_name(other.m_name), m_nameSet(other.m_nameSet),
|
||||
m_type(other.m_type), m_inputFlags(other.m_inputFlags),
|
||||
m_outputFlags(other.m_outputFlags)
|
||||
{
|
||||
other.m_poDS = nullptr;
|
||||
other.m_owned = false;
|
||||
other.m_name.clear();
|
||||
}
|
||||
|
||||
|
@ -1334,14 +1348,14 @@ bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
|
|||
overwriteArg->GetType() == GAAT_BOOLEAN && overwriteArg->Get<bool>());
|
||||
auto outputArg = algForOutput->GetArg(GDAL_ARG_NAME_OUTPUT);
|
||||
auto &val = arg->Get<GDALArgDatasetValue>();
|
||||
if (!val.GetDataset() && !val.IsNameSet())
|
||||
if (!val.GetDatasetRef() && !val.IsNameSet())
|
||||
{
|
||||
ReportError(CE_Failure, CPLE_AppDefined,
|
||||
"Argument '%s' has no dataset object or dataset name.",
|
||||
arg->GetName().c_str());
|
||||
ret = false;
|
||||
}
|
||||
else if (!val.GetDataset() &&
|
||||
else if (!val.GetDatasetRef() &&
|
||||
(!arg->IsOutput() || (arg == outputArg && update && !overwrite)))
|
||||
{
|
||||
int flags = val.GetType();
|
||||
|
@ -1353,7 +1367,7 @@ bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
|
|||
outputArg && outputArg->GetType() == GAAT_DATASET)
|
||||
{
|
||||
auto &outputVal = outputArg->Get<GDALArgDatasetValue>();
|
||||
if (!outputVal.GetDataset() &&
|
||||
if (!outputVal.GetDatasetRef() &&
|
||||
outputVal.GetName() == val.GetName() &&
|
||||
(outputVal.GetInputFlags() & GADV_OBJECT) != 0)
|
||||
{
|
||||
|
@ -1382,9 +1396,9 @@ bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
|
|||
CPLStringList(ifArg->Get<std::vector<std::string>>());
|
||||
}
|
||||
|
||||
auto poDS = std::unique_ptr<GDALDataset>(
|
||||
auto poDS =
|
||||
GDALDataset::Open(val.GetName().c_str(), flags,
|
||||
aosAllowedDrivers.List(), aosOpenOptions.List()));
|
||||
aosAllowedDrivers.List(), aosOpenOptions.List());
|
||||
if (poDS)
|
||||
{
|
||||
if (assignToOutputArg)
|
||||
|
@ -1401,11 +1415,11 @@ bool GDALAlgorithm::ProcessDatasetArg(GDALAlgorithmArg *arg,
|
|||
EQUAL(poDriver->GetDescription(), "SQLite") ||
|
||||
EQUAL(poDriver->GetDescription(), "GPKG")))
|
||||
{
|
||||
outputArg->Get<GDALArgDatasetValue>().Set(poDS.get(),
|
||||
false);
|
||||
outputArg->Get<GDALArgDatasetValue>().Set(poDS);
|
||||
}
|
||||
}
|
||||
val.Set(std::move(poDS));
|
||||
val.Set(poDS);
|
||||
poDS->ReleaseRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -1528,7 +1542,7 @@ bool GDALAlgorithm::ValidateArguments()
|
|||
auto &listVal = arg->Get<std::vector<GDALArgDatasetValue>>();
|
||||
for (auto &val : listVal)
|
||||
{
|
||||
if (!val.GetDataset() && val.GetName().empty())
|
||||
if (!val.GetDatasetRef() && val.GetName().empty())
|
||||
{
|
||||
ReportError(
|
||||
CE_Failure, CPLE_AppDefined,
|
||||
|
@ -1536,7 +1550,7 @@ bool GDALAlgorithm::ValidateArguments()
|
|||
arg->GetName().c_str());
|
||||
ret = false;
|
||||
}
|
||||
else if (!val.GetDataset())
|
||||
else if (!val.GetDatasetRef())
|
||||
{
|
||||
int flags = val.GetType() | GDAL_OF_VERBOSE_ERROR;
|
||||
|
||||
|
@ -2110,10 +2124,10 @@ bool GDALAlgorithm::Run(GDALProgressFunc pfnProgress, void *pProgressData)
|
|||
|
||||
bool GDALAlgorithm::Finalize()
|
||||
{
|
||||
if (m_selectedSubAlg)
|
||||
return m_selectedSubAlg->Finalize();
|
||||
|
||||
bool ret = true;
|
||||
if (m_selectedSubAlg)
|
||||
ret = m_selectedSubAlg->Finalize();
|
||||
|
||||
for (auto &arg : m_args)
|
||||
{
|
||||
if (arg->GetType() == GAAT_DATASET)
|
||||
|
@ -3686,10 +3700,10 @@ bool GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg,
|
|||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALAlgorithmArgSetDatasetWithoutOwnership() */
|
||||
/* GDALAlgorithmArgSetDataset() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Set dataset object, but without transferring ownership to hArg.
|
||||
/** Set dataset object, increasing its reference counter.
|
||||
*
|
||||
* @param hArg Handle to an argument. Must NOT be null.
|
||||
* @param hDS Dataset object. May be null.
|
||||
|
@ -3697,30 +3711,10 @@ bool GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg,
|
|||
* @since 3.11
|
||||
*/
|
||||
|
||||
bool GDALAlgorithmArgSetDatasetWithoutOwnership(GDALAlgorithmArgH hArg,
|
||||
GDALDatasetH hDS)
|
||||
bool GDALAlgorithmArgSetDataset(GDALAlgorithmArgH hArg, GDALDatasetH hDS)
|
||||
{
|
||||
VALIDATE_POINTER1(hArg, __func__, false);
|
||||
return hArg->ptr->Set(GDALDataset::FromHandle(hDS), false);
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALAlgorithmArgSetDatasetWithOwnership() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Set dataset object, and transfer its ownership to hArg.
|
||||
*
|
||||
* @param hArg Handle to an argument. Must NOT be null.
|
||||
* @param hDS Dataset object. May be null.
|
||||
* @return true if success.
|
||||
* @since 3.11
|
||||
*/
|
||||
|
||||
bool GDALAlgorithmArgSetDatasetWithOwnership(GDALAlgorithmArgH hArg,
|
||||
GDALDatasetH hDS)
|
||||
{
|
||||
VALIDATE_POINTER1(hArg, __func__, false);
|
||||
return hArg->ptr->Set(GDALDataset::FromHandle(hDS), false);
|
||||
return hArg->ptr->Set(GDALDataset::FromHandle(hDS));
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
|
@ -3834,34 +3828,39 @@ const char *GDALArgDatasetValueGetName(GDALArgDatasetValueH hValue)
|
|||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValueGetDataset() */
|
||||
/* GDALArgDatasetValueGetDatasetRef() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Return the dataset component of the GDALArgDatasetValue
|
||||
/** Return the dataset component of the GDALArgDatasetValue.
|
||||
*
|
||||
* This does not modify the reference counter, hence the lifetime of the
|
||||
* returned object is not guaranteed to exceed the one of hValue.
|
||||
*
|
||||
* @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
|
||||
* @since 3.11
|
||||
*/
|
||||
GDALDatasetH GDALArgDatasetValueGetDataset(GDALArgDatasetValueH hValue)
|
||||
GDALDatasetH GDALArgDatasetValueGetDatasetRef(GDALArgDatasetValueH hValue)
|
||||
{
|
||||
VALIDATE_POINTER1(hValue, __func__, nullptr);
|
||||
return GDALDataset::ToHandle(hValue->ptr->GetDataset());
|
||||
return GDALDataset::ToHandle(hValue->ptr->GetDatasetRef());
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValueIsDatasetOwned() */
|
||||
/* GDALArgDatasetValueGetDatasetIncreaseRefCount() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Returns whether the dataset object referenced in this object is owned
|
||||
* by this object (and thus released by it when it is destroyed).
|
||||
/** Return the dataset component of the GDALArgDatasetValue, and increase its
|
||||
* reference count if not null. Once done with the dataset, the caller should
|
||||
* call GDALReleaseDataset().
|
||||
*
|
||||
* @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
|
||||
* @since 3.11
|
||||
*/
|
||||
bool GDALArgDatasetValueIsDatasetOwned(GDALArgDatasetValueH hValue)
|
||||
GDALDatasetH
|
||||
GDALArgDatasetValueGetDatasetIncreaseRefCount(GDALArgDatasetValueH hValue)
|
||||
{
|
||||
VALIDATE_POINTER1(hValue, __func__, false);
|
||||
return hValue->ptr->IsOwned();
|
||||
VALIDATE_POINTER1(hValue, __func__, nullptr);
|
||||
return GDALDataset::ToHandle(hValue->ptr->GetDatasetIncreaseRefCount());
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
|
@ -3950,37 +3949,19 @@ void GDALArgDatasetValueSetName(GDALArgDatasetValueH hValue,
|
|||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValueSetDatasetWithoutOwnership() */
|
||||
/* GDALArgDatasetValueSetDataset() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Set dataset object, but without transferring ownership to hValue.
|
||||
/** Set dataset object, increasing its reference counter.
|
||||
*
|
||||
* @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
|
||||
* @param hDS Dataset object. May be null.
|
||||
* @since 3.11
|
||||
*/
|
||||
|
||||
void GDALArgDatasetValueSetDatasetWithoutOwnership(GDALArgDatasetValueH hValue,
|
||||
GDALDatasetH hDS)
|
||||
void GDALArgDatasetValueSetDataset(GDALArgDatasetValueH hValue,
|
||||
GDALDatasetH hDS)
|
||||
{
|
||||
VALIDATE_POINTER0(hValue, __func__);
|
||||
hValue->ptr->Set(GDALDataset::FromHandle(hDS), false);
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/* GDALArgDatasetValueSetDatasetWithOwnership() */
|
||||
/************************************************************************/
|
||||
|
||||
/** Set dataset object, and transfer its ownership to hValue.
|
||||
*
|
||||
* @param hValue Handle to a GDALArgDatasetValue. Must NOT be null.
|
||||
* @param hDS Dataset object. May be null.
|
||||
* @since 3.11
|
||||
*/
|
||||
|
||||
void GDALArgDatasetValueSetDatasetWithOwnership(GDALArgDatasetValueH hValue,
|
||||
GDALDatasetH hDS)
|
||||
{
|
||||
VALIDATE_POINTER0(hValue, __func__);
|
||||
hValue->ptr->Set(GDALDataset::FromHandle(hDS), true);
|
||||
hValue->ptr->Set(GDALDataset::FromHandle(hDS));
|
||||
}
|
||||
|
|
|
@ -192,11 +192,7 @@ bool CPL_DLL GDALAlgorithmArgSetAsString(GDALAlgorithmArgH, const char *);
|
|||
bool CPL_DLL GDALAlgorithmArgSetAsDatasetValue(GDALAlgorithmArgH hArg,
|
||||
GDALArgDatasetValueH value);
|
||||
|
||||
bool CPL_DLL GDALAlgorithmArgSetDatasetWithoutOwnership(GDALAlgorithmArgH hArg,
|
||||
GDALDatasetH);
|
||||
|
||||
bool CPL_DLL GDALAlgorithmArgSetDatasetWithOwnership(GDALAlgorithmArgH hArg,
|
||||
GDALDatasetH);
|
||||
bool CPL_DLL GDALAlgorithmArgSetDataset(GDALAlgorithmArgH hArg, GDALDatasetH);
|
||||
|
||||
bool CPL_DLL GDALAlgorithmArgSetAsInteger(GDALAlgorithmArgH, int);
|
||||
|
||||
|
@ -220,9 +216,10 @@ void CPL_DLL GDALArgDatasetValueRelease(GDALArgDatasetValueH);
|
|||
|
||||
const char CPL_DLL *GDALArgDatasetValueGetName(GDALArgDatasetValueH);
|
||||
|
||||
GDALDatasetH CPL_DLL GDALArgDatasetValueGetDataset(GDALArgDatasetValueH);
|
||||
GDALDatasetH CPL_DLL GDALArgDatasetValueGetDatasetRef(GDALArgDatasetValueH);
|
||||
|
||||
bool CPL_DLL GDALArgDatasetValueIsDatasetOwned(GDALArgDatasetValueH hValue);
|
||||
GDALDatasetH
|
||||
CPL_DLL GDALArgDatasetValueGetDatasetIncreaseRefCount(GDALArgDatasetValueH);
|
||||
|
||||
/** Bit indicating that the name component of GDALArgDatasetValue is accepted. */
|
||||
#define GADV_NAME (1 << 0)
|
||||
|
@ -243,11 +240,7 @@ int CPL_DLL GDALArgDatasetValueGetOutputFlags(GDALArgDatasetValueH);
|
|||
|
||||
void CPL_DLL GDALArgDatasetValueSetName(GDALArgDatasetValueH, const char *);
|
||||
|
||||
void CPL_DLL GDALArgDatasetValueSetDatasetWithoutOwnership(GDALArgDatasetValueH,
|
||||
GDALDatasetH);
|
||||
|
||||
void CPL_DLL GDALArgDatasetValueSetDatasetWithOwnership(GDALArgDatasetValueH,
|
||||
GDALDatasetH);
|
||||
void CPL_DLL GDALArgDatasetValueSetDataset(GDALArgDatasetValueH, GDALDatasetH);
|
||||
|
||||
CPL_C_END
|
||||
|
||||
|
@ -328,61 +321,55 @@ class CPL_DLL GDALArgDatasetValue final
|
|||
{
|
||||
}
|
||||
|
||||
/** Constructor by dataset instance. */
|
||||
explicit GDALArgDatasetValue(GDALDataset *poDS, bool owned);
|
||||
|
||||
/** Constructor by dataset instance. */
|
||||
explicit GDALArgDatasetValue(std::unique_ptr<GDALDataset> poDS);
|
||||
/** Constructor by dataset instance, increasing its reference counter */
|
||||
explicit GDALArgDatasetValue(GDALDataset *poDS);
|
||||
|
||||
/** Move constructor */
|
||||
GDALArgDatasetValue(GDALArgDatasetValue &&other);
|
||||
|
||||
/** Destructor. Free m_poDS if m_owned is true. */
|
||||
/** Destructor. Decrease m_poDS reference count, and destroy it if no
|
||||
* longer referenced. */
|
||||
~GDALArgDatasetValue();
|
||||
|
||||
/** Close the dataset if it is owned and return an error if an error
|
||||
* occurred during dataset closing */
|
||||
/** Dereference the dataset object and close it if no longer referenced.
|
||||
* Return an error if an error occurred during dataset closing. */
|
||||
bool Close();
|
||||
|
||||
/** Move-assignment operator */
|
||||
GDALArgDatasetValue &operator=(GDALArgDatasetValue &&other);
|
||||
|
||||
/** Get GDALDataset* instance (may be null) */
|
||||
GDALDataset *GetDataset()
|
||||
{
|
||||
return m_poDS;
|
||||
}
|
||||
|
||||
/** Get GDALDataset* instance (may be null) */
|
||||
const GDALDataset *GetDataset() const
|
||||
{
|
||||
return m_poDS;
|
||||
}
|
||||
|
||||
/** Returns whether the dataset object referenced in this object is owned
|
||||
* by this object (and thus released by it when it is destroyed).
|
||||
/** Get the GDALDataset* instance (may be null), and increase its reference
|
||||
* count if not null. Once done with the dataset, the caller should call
|
||||
* GDALDataset::Release().
|
||||
*/
|
||||
bool IsOwned() const
|
||||
GDALDataset *GetDatasetIncreaseRefCount();
|
||||
|
||||
/** Get a GDALDataset* instance (may be null). This does not modify the
|
||||
* reference counter, hence the lifetime of the returned object is not
|
||||
* guaranteed to exceed the one of this instance.
|
||||
*/
|
||||
GDALDataset *GetDatasetRef()
|
||||
{
|
||||
return m_owned;
|
||||
return m_poDS;
|
||||
}
|
||||
|
||||
/** Borrow the GDALDataset* instance (may be null), with its ownership
|
||||
* status.*/
|
||||
GDALDataset *BorrowDataset(bool &owned)
|
||||
/** Borrow the GDALDataset* instance (may be null), leaving its reference
|
||||
* counter unchanged.
|
||||
*/
|
||||
GDALDataset *BorrowDataset()
|
||||
{
|
||||
owned = m_owned;
|
||||
m_owned = false;
|
||||
GDALDataset *ret = m_poDS;
|
||||
m_poDS = nullptr;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** Borrow the GDALDataset* instance from another GDALArgDatasetValue */
|
||||
/** Borrow the GDALDataset* instance from another GDALArgDatasetValue,
|
||||
* leaving its reference counter unchange.
|
||||
*/
|
||||
void BorrowDatasetFrom(GDALArgDatasetValue &other)
|
||||
{
|
||||
Close();
|
||||
m_poDS = other.BorrowDataset(m_owned);
|
||||
m_poDS = other.BorrowDataset();
|
||||
m_name = other.m_name;
|
||||
}
|
||||
|
||||
|
@ -430,21 +417,17 @@ class CPL_DLL GDALArgDatasetValue final
|
|||
/** Set dataset name */
|
||||
void Set(const std::string &name);
|
||||
|
||||
/** Transfer dataset to this instance. */
|
||||
/** Transfer dataset to this instance (does not affect is reference
|
||||
* counter). */
|
||||
void Set(std::unique_ptr<GDALDataset> poDS);
|
||||
|
||||
/** Set dataset object. */
|
||||
void Set(GDALDataset *poDS, bool owned);
|
||||
/** Set dataset object, increasing its reference counter. */
|
||||
void Set(GDALDataset *poDS);
|
||||
|
||||
/** Set from other value, referencing other.m_poDS. */
|
||||
void SetFrom(const GDALArgDatasetValue &other)
|
||||
{
|
||||
Close();
|
||||
m_name = other.m_name;
|
||||
m_nameSet = other.m_nameSet;
|
||||
m_poDS = other.m_poDS;
|
||||
m_owned = false;
|
||||
}
|
||||
/** Set from other value, increasing the reference counter of the
|
||||
* GDALDataset object.
|
||||
*/
|
||||
void SetFrom(const GDALArgDatasetValue &other);
|
||||
|
||||
/** Set which components among name and dataset are accepted as
|
||||
* input, when this argument serves as an input.
|
||||
|
@ -496,13 +479,9 @@ class CPL_DLL GDALArgDatasetValue final
|
|||
/** The owner argument (may be nullptr for freestanding objects) */
|
||||
GDALAlgorithmArg *m_ownerArg = nullptr;
|
||||
|
||||
/** Dataset object. Owned by this instance if m_owned is true. */
|
||||
/** Dataset object. */
|
||||
GDALDataset *m_poDS = nullptr;
|
||||
|
||||
/** Whether m_poDS should be destroyed at destruction of this
|
||||
* GDALArgDatasetValue instance. */
|
||||
bool m_owned = false;
|
||||
|
||||
/** Dataset name */
|
||||
std::string m_name{};
|
||||
|
||||
|
@ -1286,12 +1265,13 @@ class CPL_DLL GDALAlgorithmArg /* non-final */
|
|||
/** Set the value for a GAAT_REAL argument */
|
||||
bool Set(double value);
|
||||
|
||||
/** Set the value for a GAAT_DATASET argument.
|
||||
/** Set the value for a GAAT_DATASET argument, increasing ds' reference
|
||||
* counter if ds is not null.
|
||||
* It cannot be called several times for a given argument.
|
||||
* Validation checks and other actions are run.
|
||||
* Return true if success.
|
||||
*/
|
||||
bool Set(GDALDataset *ds, bool owned);
|
||||
bool Set(GDALDataset *ds);
|
||||
|
||||
/** Set the value for a GAAT_DATASET argument.
|
||||
* It cannot be called several times for a given argument.
|
||||
|
|
|
@ -223,12 +223,8 @@ public:
|
|||
return GDALAlgorithmArgSetAsDoubleList(self, nList, pList);
|
||||
}
|
||||
|
||||
void SetDatasetWithoutOwnership(GDALDatasetShadow* ds) {
|
||||
GDALAlgorithmArgSetDatasetWithoutOwnership(self, ds);
|
||||
}
|
||||
|
||||
void SetDatasetWithOwnership(GDALDatasetShadow* ds) {
|
||||
GDALAlgorithmArgSetDatasetWithOwnership(self, ds);
|
||||
void SetDataset(GDALDatasetShadow* ds) {
|
||||
GDALAlgorithmArgSetDataset(self, ds);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -385,12 +381,9 @@ public:
|
|||
return GDALArgDatasetValueGetName(self);
|
||||
}
|
||||
|
||||
%newobject GetDataset;
|
||||
GDALDatasetShadow* GetDataset() {
|
||||
return GDALArgDatasetValueGetDataset(self);
|
||||
}
|
||||
|
||||
bool IsDatasetOwned() {
|
||||
return GDALArgDatasetValueIsDatasetOwned(self);
|
||||
return GDALArgDatasetValueGetDatasetIncreaseRefCount(self);
|
||||
}
|
||||
|
||||
int GetInputFlags() {
|
||||
|
@ -405,12 +398,8 @@ public:
|
|||
GDALArgDatasetValueSetName(self, name);
|
||||
}
|
||||
|
||||
void SetDatasetWithoutOwnership(GDALDatasetShadow* ds) {
|
||||
GDALArgDatasetValueSetDatasetWithoutOwnership(self, ds);
|
||||
}
|
||||
|
||||
void SetDatasetWithOwnership(GDALDatasetShadow* ds) {
|
||||
GDALArgDatasetValueSetDatasetWithOwnership(self, ds);
|
||||
void SetDataset(GDALDatasetShadow* ds) {
|
||||
GDALArgDatasetValueSetDataset(self, ds);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue