Skip to content

Commit

Permalink
Add upgrade notice for old API call
Browse files Browse the repository at this point in the history
We cannot upgrade users silently in this place
  • Loading branch information
franzpoeschel committed Feb 14, 2024
1 parent be3dbbd commit 29352d5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
25 changes: 24 additions & 1 deletion include/openPMD/auxiliary/DerefDynamicCast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/
#pragma once

#include <exception>
#include <optional>
#include <stdexcept>

namespace openPMD
{
Expand All @@ -47,5 +48,27 @@ namespace auxiliary
return *tmp_ptr;
}

/** Returns a value reference stored in a dynamically casted pointer
*
* Safe version of *dynamic_cast< New_Type* >( some_ptr ); This function
* will throw as dynamic_cast and will furthermore throw if the result
* of the dynamic_cast is a nullptr.
*
* @tparam New_Type new type to cast to
* @tparam Old_Type old type to cast from
* @param[in] ptr and input pointer type
* @return value reference of a dereferenced, dynamically casted ptr to
* New_Type*
*/
template <typename New_Type, typename Old_Type>
inline std::optional<New_Type *>
dynamic_cast_optional(Old_Type *ptr) noexcept
{
auto const tmp_ptr = dynamic_cast<New_Type *>(ptr);
if (tmp_ptr == nullptr)
return std::nullopt;
return {tmp_ptr};
}

} // namespace auxiliary
} // namespace openPMD
3 changes: 3 additions & 0 deletions include/openPMD/backend/Attributable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <exception>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -245,6 +246,8 @@ class Attributable
OPENPMD_protected
// clang-format on

std::optional<Series> retrieveSeries_optional() const;

Series retrieveSeries() const;

/** Returns the corresponding Iteration
Expand Down
32 changes: 20 additions & 12 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ double Mesh::gridUnitSI() const
Mesh &Mesh::setGridUnitSI(double gusi)
{
setAttribute("gridUnitSI", gusi);
if (auto series_opt = retrieveSeries_optional(); series_opt.has_value())
{
if (auto version = series_opt->openPMD(); version >= "2.")
{
std::cerr << "[Mesh::setGridUnitSI] Warning: Setting a scalar "
"`gridUnitSI` in a file with openPMD version '" +
version +
"'. Consider specifying a vector instead in order to "
"specify "
"the gridUnitSI per axis (ref.: "
"https://github.com/openPMD/openPMD-standard/pull/193)."
<< std::endl;
}
}
return *this;
}

Expand Down Expand Up @@ -220,17 +234,7 @@ namespace

std::vector<double> Mesh::gridUnitSIPerDimension() const
{
Attribute rawAttribute = getAttribute("gridUnitSI");
if (isVector(rawAttribute.dtype))
{
return rawAttribute.get<std::vector<double>>();
}
else
{
double scalarValue = rawAttribute.get<double>();
uint64_t dimensionality = retrieveMeshDimensionality(*this);
return std::vector<double>(dimensionality, scalarValue);
}
return getAttribute("gridUnitSI").get<std::vector<double>>();
}

Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> gridUnitSI)
Expand Down Expand Up @@ -510,7 +514,11 @@ void Mesh::read()
aRead.name = "gridUnitSI";
IOHandler()->enqueue(IOTask(this, aRead));
IOHandler()->flush(internal::defaultFlushParams);
if (isVector(*aRead.dtype))
auto series = retrieveSeries();
/* @todo remove second if condition (currently enabled since it allows a
* sneak peek into openPMD 2.0 features)
*/
if (series.openPMD() >= "2." || isVector(*aRead.dtype))
{
if (auto val =
Attribute(*aRead.resource).getOptional<std::vector<double>>();
Expand Down
27 changes: 24 additions & 3 deletions src/backend/Attributable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <algorithm>
#include <complex>
#include <iostream>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>

namespace openPMD
{
Expand Down Expand Up @@ -119,21 +121,40 @@ void Attributable::seriesFlush(std::string backendConfig)
writable().seriesFlush(std::move(backendConfig));
}

Series Attributable::retrieveSeries() const
std::optional<Series> Attributable::retrieveSeries_optional() const
{
Writable const *findSeries = &writable();
while (findSeries->parent)
{
findSeries = findSeries->parent;
}
auto seriesData = &auxiliary::deref_dynamic_cast<internal::SeriesData>(
findSeries->attributable);
auto maybeSeriesData =
auxiliary::dynamic_cast_optional<internal::SeriesData>(
findSeries->attributable);
if (!maybeSeriesData.has_value())
{
return std::nullopt;
}
auto seriesData = *maybeSeriesData;
Series res;
res.setData(
std::shared_ptr<internal::SeriesData>{seriesData, [](auto const *) {}});
return res;
}

Series Attributable::retrieveSeries() const
{
if (auto maybeSeries = retrieveSeries_optional(); maybeSeries.has_value())
{
return *maybeSeries;
}
else
{
throw std::runtime_error(
"[Attributable::retrieveSeries] Was not able to retrieve Series.");
}
}

Iteration const &Attributable::containingIteration() const
{
std::vector<Writable const *> searchQueue;
Expand Down

0 comments on commit 29352d5

Please sign in to comment.