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 Dec 18, 2023
1 parent 0177d4c commit 0713e05
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 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 @@ -240,6 +241,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 @@ -511,7 +515,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
29 changes: 25 additions & 4 deletions src/backend/Attributable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include <algorithm>
#include <complex>
#include <iostream>
#include <optional>
#include <set>
#include <stdexcept>

namespace openPMD
{
Expand Down Expand Up @@ -111,16 +113,35 @@ 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);
return Series{{seriesData, [](auto const *) {}}};
auto maybeSeriesData =
auxiliary::dynamic_cast_optional<internal::SeriesData>(
findSeries->attributable);
if (!maybeSeriesData.has_value())
{
return std::nullopt;
}
auto seriesData = *maybeSeriesData;
return {Series{{seriesData, [](auto const *) {}}}};
}

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
Expand Down

0 comments on commit 0713e05

Please sign in to comment.