Skip to content

Commit e692ff5

Browse files
committed
Adapt myPath() functionality
1 parent 908789d commit e692ff5

File tree

9 files changed

+27
-122
lines changed

9 files changed

+27
-122
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ set(CORE_SOURCE
456456
src/auxiliary/JSON.cpp
457457
src/backend/Attributable.cpp
458458
src/backend/BaseRecordComponent.cpp
459-
src/backend/Container.cpp
460459
src/backend/MeshRecordComponent.cpp
461460
src/backend/PatchRecord.cpp
462461
src/backend/PatchRecordComponent.cpp

include/openPMD/backend/Container.hpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,6 @@ namespace internal
8686
};
8787
} // namespace internal
8888

89-
namespace detail
90-
{
91-
/*
92-
* This converts the key (first parameter) to its string name within the
93-
* openPMD hierarchy.
94-
* If the key is found to be equal to RecordComponent::SCALAR, the parentKey
95-
* will be returned, adding RecordComponent::SCALAR to its back.
96-
* Reason: Scalar record components do not link their containing record as
97-
* parent, but rather the parent's parent, so the own key within the
98-
* "apparent" parent must be given as two steps.
99-
*/
100-
template <typename T>
101-
std::vector<std::string>
102-
keyAsString(T &&key, std::vector<std::string> const &parentKey)
103-
{
104-
(void)parentKey;
105-
return {std::to_string(std::forward<T>(key))};
106-
}
107-
108-
// moved to a *.cpp file so we don't need to include RecordComponent.hpp
109-
// here
110-
template <>
111-
std::vector<std::string> keyAsString<std::string const &>(
112-
std::string const &key, std::vector<std::string> const &parentKey);
113-
114-
template <>
115-
std::vector<std::string> keyAsString<std::string>(
116-
std::string &&key, std::vector<std::string> const &parentKey);
117-
} // namespace detail
118-
11989
/** @brief Map-like container that enforces openPMD requirements and handles IO.
12090
*
12191
* @see http://en.cppreference.com/w/cpp/container/map
@@ -328,8 +298,14 @@ class Container : virtual public Attributable
328298
T t = T();
329299
t.linkHierarchy(writable());
330300
auto &ret = container().insert({key, std::move(t)}).first->second;
331-
ret.writable().ownKeyWithinParent =
332-
detail::keyAsString(key, writable().ownKeyWithinParent);
301+
if constexpr (std::is_same_v<T_key, std::string>)
302+
{
303+
ret.writable().ownKeyWithinParent = key;
304+
}
305+
else
306+
{
307+
ret.writable().ownKeyWithinParent = std::to_string(key);
308+
}
333309
traits::GenerationPolicy<T> gen;
334310
gen(ret);
335311
return ret;
@@ -363,8 +339,15 @@ class Container : virtual public Attributable
363339
T t = T();
364340
t.linkHierarchy(writable());
365341
auto &ret = container().insert({key, std::move(t)}).first->second;
366-
ret.writable().ownKeyWithinParent = detail::keyAsString(
367-
std::move(key), writable().ownKeyWithinParent);
342+
if constexpr (std::is_same_v<T_key, std::string>)
343+
{
344+
ret.writable().ownKeyWithinParent = std::move(key);
345+
}
346+
else
347+
{
348+
ret.writable().ownKeyWithinParent =
349+
std::to_string(std::move(key));
350+
}
368351
traits::GenerationPolicy<T> gen;
369352
gen(ret);
370353
return ret;

include/openPMD/backend/Writable.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,10 @@ OPENPMD_private
131131
Writable *parent = nullptr;
132132
bool dirty = true;
133133
/**
134-
* If parent is not null, then this is a vector of keys such that:
135-
* &(*parent)[key_1]...[key_n] == this
136-
* (Notice that scalar record components do not link their direct parent,
137-
* but instead their parent's parent, hence a vector of keys)
134+
* If parent is not null, then this is a key such that:
135+
* &(*parent)[key] == this
138136
*/
139-
std::vector<std::string> ownKeyWithinParent;
137+
std::string ownKeyWithinParent;
140138
/**
141139
* @brief Whether a Writable has been written to the backend.
142140
*

src/Iteration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ Iteration::Iteration() : Attributable(NoInit())
4242
setTime(static_cast<double>(0));
4343
setDt(static_cast<double>(1));
4444
setTimeUnitSI(1);
45-
meshes.writable().ownKeyWithinParent = {"meshes"};
46-
particles.writable().ownKeyWithinParent = {"particles"};
45+
meshes.writable().ownKeyWithinParent = "meshes";
46+
particles.writable().ownKeyWithinParent = "particles";
4747
}
4848

4949
template <typename T>

src/ParticleSpecies.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace openPMD
3030
{
3131
ParticleSpecies::ParticleSpecies()
3232
{
33-
particlePatches.writable().ownKeyWithinParent = {"particlePatches"};
33+
particlePatches.writable().ownKeyWithinParent = "particlePatches";
3434
}
3535

3636
void ParticleSpecies::read()

src/Series.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void Series::init(
560560
std::make_shared<std::optional<std::unique_ptr<AbstractIOHandler>>>(
561561
std::move(ioHandler));
562562
series.iterations.linkHierarchy(writable());
563-
series.iterations.writable().ownKeyWithinParent = {"iterations"};
563+
series.iterations.writable().ownKeyWithinParent = "iterations";
564564

565565
series.m_name = input->name;
566566

src/backend/Attributable.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,7 @@ auto Attributable::myPath() const -> MyPath
197197
// so it's alright that this loop doesn't ask the key of the last found
198198
// Writable
199199

200-
// push these in reverse because we're building the list from the back
201-
for (auto it = findSeries->ownKeyWithinParent.rbegin();
202-
it != findSeries->ownKeyWithinParent.rend();
203-
++it)
204-
{
205-
res.group.push_back(*it);
206-
}
200+
res.group.push_back(findSeries->ownKeyWithinParent);
207201
findSeries = findSeries->parent;
208202
}
209203
std::reverse(res.group.begin(), res.group.end());

src/backend/Container.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

test/CoreTest.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,7 @@ TEST_CASE("myPath", "[core]")
193193
auto scalarMeshComponent = scalarMesh[RecordComponent::SCALAR];
194194
REQUIRE(
195195
pathOf(scalarMeshComponent) ==
196-
vec_t{
197-
"iterations",
198-
"1234",
199-
"meshes",
200-
"e_chargeDensity",
201-
RecordComponent::SCALAR});
196+
vec_t{"iterations", "1234", "meshes", "e_chargeDensity"});
202197
writeSomething(scalarMeshComponent);
203198

204199
auto vectorMesh = iteration.meshes["E"];
@@ -234,13 +229,7 @@ TEST_CASE("myPath", "[core]")
234229
auto speciesWeightingX = speciesWeighting[RecordComponent::SCALAR];
235230
REQUIRE(
236231
pathOf(speciesWeightingX) ==
237-
vec_t{
238-
"iterations",
239-
"1234",
240-
"particles",
241-
"e",
242-
"weighting",
243-
RecordComponent::SCALAR});
232+
vec_t{"iterations", "1234", "particles", "e", "weighting"});
244233
writeSomething(speciesWeightingX);
245234

246235
REQUIRE(

0 commit comments

Comments
 (0)