NEML2 1.4.0
Loading...
Searching...
No Matches
Storage.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <memory>
28#include <vector>
29#include <utility>
30
31#include "neml2/misc/error.h"
32
33namespace neml2
34{
43template <typename I, typename T>
45{
46public:
47 Storage() = default;
48 Storage(Storage &&) = default;
49 Storage(const Storage &) = delete;
50 Storage & operator=(const Storage &) = delete;
51
55 template <class BaseIterator>
56 struct DereferenceIterator : public BaseIterator
57 {
58 DereferenceIterator(const BaseIterator & it)
59 : BaseIterator(it)
60 {
61 }
62
63 using key_type = typename BaseIterator::value_type::first_type;
64 using value_type = typename BaseIterator::value_type::second_type::element_type;
65
66 std::pair<key_type, value_type &> operator*() const
67 {
68 auto & [key, val] = BaseIterator::operator*();
69 neml_assert_dbg(val.get(),
70 "Trying to dereference a null object. Make sure the storage was properly "
71 "initialized using set_pointer().");
72 return {key, *val};
73 }
74 std::pair<key_type, value_type *> operator->() const
75 {
76 auto & [key, val] = BaseIterator::operator*();
77 neml_assert_dbg(val.get(),
78 "Trying to dereference a null object. Make sure the storage was properly "
79 "initialized using set_pointer().");
80 return {key, val.get()};
81 }
82 };
83
84 using values_type = typename std::map<I, std::unique_ptr<T>>;
87
97 iterator begin() { return iterator(_values.begin()); }
98 iterator end() { return iterator(_values.end()); }
99 const_iterator begin() const { return const_iterator(_values.begin()); }
100 const_iterator end() const { return const_iterator(_values.end()); }
102
104
113 T & operator[](const I & i) const
114 {
116 "Trying to access a null object. Make sure the storage was properly "
117 "initialized using set_pointer().");
118 return *pointer_value(i);
119 }
120 T & operator[](const I & i) { return std::as_const(*this)[i]; }
122
129 std::size_t size() const { return _values.size(); }
130
134 bool empty() const { return _values.empty(); }
135
139 bool has_key(const I & i) const { return _values.count(i); }
140
142
148 const T * query_value(const I & i) const { return has_key(i) ? pointer_value(i).get() : nullptr; }
149 T * query_value(const I & i)
150 {
151 return has_key(i) ? const_cast<T *>(std::as_const(*this).query_value(i)) : nullptr;
152 }
154
164 T * set_pointer(const I & i, std::unique_ptr<T> && ptr)
165 {
166 _values[i] = std::move(ptr);
167 return _values[i].get();
168 }
169
170private:
178 const std::unique_ptr<T> & pointer_value(const I & i) const { return _values.at(i); }
179
181 values_type _values;
182};
183} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
Definition Storage.h:45
Storage(const Storage &)=delete
const_iterator begin() const
Definition Storage.h:99
T & operator[](const I &i) const
Definition Storage.h:113
T & operator[](const I &i)
Definition Storage.h:120
DereferenceIterator< typename values_type::const_iterator > const_iterator
Definition Storage.h:86
bool empty() const
Definition Storage.h:134
Storage()=default
Storage & operator=(const Storage &)=delete
const T * query_value(const I &i) const
Definition Storage.h:148
T * query_value(const I &i)
Definition Storage.h:149
typename std::map< I, std::unique_ptr< T > > values_type
Definition Storage.h:84
T * set_pointer(const I &i, std::unique_ptr< T > &&ptr)
Definition Storage.h:164
bool has_key(const I &i) const
Definition Storage.h:139
DereferenceIterator< typename values_type::iterator > iterator
Definition Storage.h:85
iterator end()
Definition Storage.h:98
const_iterator end() const
Definition Storage.h:100
iterator begin()
Definition Storage.h:97
std::size_t size() const
Definition Storage.h:129
Storage(Storage &&)=default
Definition CrossRef.cxx:30
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:76
typename BaseIterator::value_type::first_type key_type
Definition Storage.h:63
typename BaseIterator::value_type::second_type::element_type value_type
Definition Storage.h:64
std::pair< key_type, value_type & > operator*() const
Definition Storage.h:66
std::pair< key_type, value_type * > operator->() const
Definition Storage.h:74
DereferenceIterator(const BaseIterator &it)
Definition Storage.h:58