23 #include "openPMD/backend/Attributable.hpp" 25 #include <initializer_list> 30 #include <type_traits> 34 #ifndef OPENPMD_protected 35 #define OPENPMD_protected protected 49 struct GenerationPolicy
74 std::vector<std::string>
75 keyAsString(
T &&key, std::vector<std::string>
const &parentKey)
78 return {std::to_string(std::forward<T>(key))};
84 std::vector<std::string> keyAsString<std::string const &>(
85 std::string
const &key, std::vector<std::string>
const &parentKey);
88 std::vector<std::string> keyAsString<std::string>(
89 std::string &&key, std::vector<std::string>
const &parentKey);
103 typename T_key = std::string,
104 typename T_container = std::map<T_key, T> >
108 std::is_base_of<AttributableInterface, T>::value,
109 "Type of container element must be derived from Writable");
118 using InternalContainer = T_container;
121 using key_type =
typename InternalContainer::key_type;
122 using mapped_type =
typename InternalContainer::mapped_type;
123 using value_type =
typename InternalContainer::value_type;
124 using size_type =
typename InternalContainer::size_type;
125 using difference_type =
typename InternalContainer::difference_type;
126 using allocator_type =
typename InternalContainer::allocator_type;
127 using reference =
typename InternalContainer::reference;
128 using const_reference =
typename InternalContainer::const_reference;
129 using pointer =
typename InternalContainer::pointer;
130 using const_pointer =
typename InternalContainer::const_pointer;
131 using iterator =
typename InternalContainer::iterator;
132 using const_iterator =
typename InternalContainer::const_iterator;
137 iterator begin() noexcept
139 return m_container->begin();
141 const_iterator begin()
const noexcept
143 return m_container->begin();
145 const_iterator cbegin()
const noexcept
147 return m_container->cbegin();
150 iterator end() noexcept
152 return m_container->end();
154 const_iterator end()
const noexcept
156 return m_container->end();
158 const_iterator cend()
const noexcept
160 return m_container->cend();
163 bool empty()
const noexcept
165 return m_container->empty();
168 size_type size()
const noexcept
170 return m_container->size();
182 throw std::runtime_error(
183 "Can not clear a container in a read-only Series.");
188 std::pair<iterator, bool> insert(value_type
const &value)
190 return m_container->insert(value);
193 std::pair<iterator, bool> insert(P &&value)
195 return m_container->insert(value);
197 iterator insert(const_iterator hint, value_type
const &value)
199 return m_container->insert(hint, value);
202 iterator insert(const_iterator hint, P &&value)
204 return m_container->insert(hint, value);
206 template <
class InputIt>
207 void insert(InputIt first, InputIt last)
209 m_container->insert(first, last);
211 void insert(std::initializer_list<value_type> ilist)
213 m_container->insert(ilist);
218 m_container->swap(other.m_container);
221 mapped_type &at(key_type
const &key)
223 return m_container->at(key);
225 mapped_type
const &at(key_type
const &key)
const 227 return m_container->at(key);
242 auto it = m_container->find(key);
243 if (it != m_container->end())
250 throw std::out_of_range(out_of_range_msg(key));
254 t.linkHierarchy(writable());
255 auto &ret = m_container->insert({key, std::move(t)}).first->second;
256 ret.writable().ownKeyWithinParent =
257 detail::keyAsString(key, writable().ownKeyWithinParent);
275 auto it = m_container->find(key);
276 if (it != m_container->end())
283 throw std::out_of_range(out_of_range_msg(key));
287 t.linkHierarchy(writable());
288 auto &ret = m_container->insert({key, std::move(t)}).first->second;
289 ret.writable().ownKeyWithinParent = detail::keyAsString(
290 std::move(key), writable().ownKeyWithinParent);
297 iterator find(key_type
const &key)
299 return m_container->find(key);
301 const_iterator find(key_type
const &key)
const 303 return m_container->find(key);
311 size_type
count(key_type
const &key)
const 313 return m_container->count(key);
324 return m_container->find(key) != m_container->end();
335 virtual size_type
erase(key_type
const &key)
338 throw std::runtime_error(
339 "Can not erase from a container in a read-only Series.");
341 auto res = m_container->find(key);
342 if (res != m_container->end() && res->second.written())
346 IOHandler()->enqueue(
IOTask(&res->second, pDelete));
347 IOHandler()->flush(internal::defaultFlushParams);
349 return m_container->erase(key);
353 virtual iterator
erase(iterator res)
356 throw std::runtime_error(
357 "Can not erase from a container in a read-only Series.");
359 if (res != m_container->end() && res->second.written())
363 IOHandler()->enqueue(
IOTask(&res->second, pDelete));
364 IOHandler()->flush(internal::defaultFlushParams);
366 return m_container->erase(res);
371 template <
class... Args>
373 -> decltype(InternalContainer().emplace(std::forward<Args>(args)...))
375 return m_container->emplace(std::forward<Args>(args)...);
380 : m_container{std::make_shared< InternalContainer >()}
383 void clear_unchecked()
386 throw std::runtime_error(
387 "Clearing a written container not (yet) implemented.");
389 m_container->clear();
399 IOHandler()->enqueue(
IOTask(
this, pCreate));
402 flushAttributes(flushParams);
405 std::shared_ptr<InternalContainer> m_container;
417 std::set<key_type> m_accessedKeys;
429 : m_originalContainer(container_in)
432 template <
typename K>
433 mapped_type &operator[](K &&k)
435 m_accessedKeys.insert(k);
436 return m_originalContainer[std::forward<K>(k)];
439 template <
typename K>
440 mapped_type &at(K &&k)
442 m_accessedKeys.insert(k);
443 return m_originalContainer.at(std::forward<K>(k));
451 template <
typename K>
454 m_accessedKeys.erase(std::forward<K>(k));
459 auto &map = *m_originalContainer.m_container;
460 using iterator_t =
typename InternalContainer::const_iterator;
461 std::vector<iterator_t> deleteMe;
462 deleteMe.reserve(map.size() - m_accessedKeys.size());
463 for (iterator_t it = map.begin(); it != map.end(); ++it)
465 auto lookup = m_accessedKeys.find(it->first);
466 if (lookup == m_accessedKeys.end())
468 deleteMe.push_back(it);
471 for (
auto &it : deleteMe)
Return an error string for read-only access.
Definition: OutOfRangeMsg.hpp:36
Self-contained description of a single IO operation.
Definition: IOTask.hpp:615
This class wraps a Container and forwards operator[]() and at() to it.
Definition: Container.hpp:415
virtual mapped_type & operator[](key_type const &key)
Access the value that is mapped to a key equivalent to key, creating it if such key does not exist al...
Definition: Container.hpp:240
Container Element Creation Policy.
Definition: Attributable.hpp:46
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:40
auto emplace(Args &&...args) -> decltype(InternalContainer().emplace(std::forward< Args >(args)...))
Definition: Container.hpp:372
bool contains(key_type const &key) const
Checks if there is an element with a key equivalent to an exiting key in the container.
Definition: Container.hpp:322
Definition: IOTask.hpp:230
void forget(K &&k)
Remove key from the list of accessed keys.
Definition: Container.hpp:452
virtual size_type erase(key_type const &key)
Remove a single element from the container and (if written) from disk.
Definition: Container.hpp:335
Root level of the openPMD hierarchy.
Definition: Series.hpp:537
Definition: Container.cpp:50
Public definitions of openPMD-api.
Definition: Date.cpp:28
void clear()
Remove all objects from the container and (if written) from disk.
Definition: Container.hpp:179
Data members for Series.
Definition: Series.hpp:63
Parameters recursively passed through the openPMD hierarchy when flushing.
Definition: AbstractIOHandler.hpp:103
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:110
size_type count(key_type const &key) const
This returns either 1 if the key is found in the container of 0 if not.
Definition: Container.hpp:311
virtual iterator erase(iterator res)
Definition: Container.hpp:353
Definition: ParticleSpecies.hpp:33
Definition: IOTask.hpp:176
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:105
Definition: Attributable.hpp:444
open series as read-only, fails if series is not found
virtual mapped_type & operator[](key_type &&key)
Access the value that is mapped to a key equivalent to key, creating it if such key does not exist al...
Definition: Container.hpp:273