23 #include "openPMD/backend/Attributable.hpp" 25 #include <initializer_list> 26 #include <type_traits> 32 #ifndef OPENPMD_protected 33 # define OPENPMD_protected protected 47 template<
typename U >
48 struct GenerationPolicy
50 template<
typename T >
67 typename T_key = std::string,
68 typename T_container = std::map< T_key, T >
72 static_assert(std::is_base_of< Attributable, T >::value,
"Type of container element must be derived from Writable");
73 using InternalContainer = T_container;
80 using key_type =
typename InternalContainer::key_type;
81 using mapped_type =
typename InternalContainer::mapped_type;
82 using value_type =
typename InternalContainer::value_type;
83 using size_type =
typename InternalContainer::size_type;
84 using difference_type =
typename InternalContainer::difference_type;
85 using allocator_type =
typename InternalContainer::allocator_type;
86 using reference =
typename InternalContainer::reference;
87 using const_reference =
typename InternalContainer::const_reference;
88 using pointer =
typename InternalContainer::pointer;
89 using const_pointer =
typename InternalContainer::const_pointer;
90 using iterator =
typename InternalContainer::iterator;
91 using const_iterator =
typename InternalContainer::const_iterator;
96 iterator begin() noexcept {
return m_container->begin(); }
97 const_iterator begin()
const noexcept {
return m_container->begin(); }
98 const_iterator cbegin()
const noexcept {
return m_container->cbegin(); }
100 iterator end() noexcept {
return m_container->end(); }
101 const_iterator end()
const noexcept {
return m_container->end(); }
102 const_iterator cend()
const noexcept {
return m_container->cend(); }
104 bool empty()
const noexcept {
return m_container->empty(); }
106 size_type size()
const noexcept {
return m_container->size(); }
116 throw std::runtime_error(
"Can not clear a container in a read-only Series.");
121 std::pair< iterator, bool > insert(value_type
const& value) {
return m_container->insert(value); }
123 std::pair< iterator, bool > insert(P&& value) {
return m_container->insert(value); }
124 iterator insert(const_iterator hint, value_type
const& value) {
return m_container->insert(hint, value); }
126 iterator insert(const_iterator hint, P&& value) {
return m_container->insert(hint, value); }
127 template<
class InputIt >
128 void insert(InputIt first, InputIt last) { m_container->insert(first, last); }
129 void insert(std::initializer_list< value_type > ilist) { m_container->insert(ilist); }
131 void swap(
Container & other) { m_container->swap(other.m_container); }
133 mapped_type& at(key_type
const& key) {
return m_container->at(key); }
134 mapped_type
const& at(key_type
const& key)
const {
return m_container->at(key); }
144 auto it = m_container->find(key);
145 if( it != m_container->end() )
152 throw std::out_of_range(out_of_range_msg(key));
156 t.linkHierarchy(m_writable);
157 auto& ret = m_container->insert({key, std::move(t)}).first->second;
171 auto it = m_container->find(key);
172 if( it != m_container->end() )
179 throw std::out_of_range(out_of_range_msg(key));
183 t.linkHierarchy(m_writable);
184 auto& ret = m_container->insert({std::move(key), std::move(t)}).first->second;
191 iterator find(key_type
const& key) {
return m_container->find(key); }
192 const_iterator find(key_type
const& key)
const {
return m_container->find(key); }
199 size_type
count(key_type
const& key)
const {
return m_container->count(key); }
206 bool contains(key_type
const& key)
const {
return m_container->find(key) != m_container->end(); }
215 virtual size_type
erase(key_type
const& key)
218 throw std::runtime_error(
"Can not erase from a container in a read-only Series.");
220 auto res = m_container->find(key);
221 if( res != m_container->end() && res->second.written() )
225 IOHandler->enqueue(
IOTask(&res->second, pDelete));
228 return m_container->erase(key);
232 virtual iterator
erase(iterator res)
235 throw std::runtime_error(
"Can not erase from a container in a read-only Series.");
237 if( res != m_container->end() && res->second.written() )
241 IOHandler->enqueue(
IOTask(&res->second, pDelete));
244 return m_container->erase(res);
249 template <
class... Args>
251 -> decltype(InternalContainer().emplace(std::forward<Args>(args)...))
253 return m_container->emplace(std::forward<Args>(args)...);
258 : m_container{std::make_shared< InternalContainer >()}
261 void clear_unchecked()
264 throw std::runtime_error(
"Clearing a written container not (yet) implemented.");
266 m_container->clear();
269 virtual void flush(std::string
const& path)
275 IOHandler->enqueue(
IOTask(
this, pCreate));
281 std::shared_ptr< InternalContainer > m_container;
Return an error string for read-only access.
Definition: OutOfRangeMsg.hpp:37
Self-contained description of a single IO operation.
Definition: IOTask.hpp:550
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:142
Container Element Creation Policy.
Definition: Attributable.hpp:46
Logical compilation of data from one snapshot (e.g.
Definition: Iteration.hpp:38
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:206
Definition: IOTask.hpp:223
virtual size_type erase(key_type const &key)
Remove a single element from the container and (if written) from disk.
Definition: Container.hpp:215
Root level of the openPMD hierarchy.
Definition: Series.hpp:64
Public definitions of openPMD-api.
Definition: Date.cpp:29
void clear()
Remove all objects from the container and (if written) from disk.
Definition: Container.hpp:113
auto emplace(Args &&... args) -> decltype(InternalContainer().emplace(std::forward< Args >(args)...))
Definition: Container.hpp:250
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:199
virtual iterator erase(iterator res)
Definition: Container.hpp:232
Definition: ParticleSpecies.hpp:34
Definition: IOTask.hpp:169
Map-like container that enforces openPMD requirements and handles IO.
Definition: Container.hpp:70
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:169
Layer to manage storage of attributes associated with file objects.
Definition: Attributable.hpp:65