NEML2 2.0.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
52 template <class BaseIterator>
53 struct DereferenceIterator : public BaseIterator
54 {
55 DereferenceIterator(const BaseIterator & it)
56 : BaseIterator(it)
57 {
58 }
59
60 using key_type = typename BaseIterator::value_type::first_type;
61 using value_type = typename BaseIterator::value_type::second_type::element_type;
62
63 std::pair<key_type, value_type &> operator*() const
64 {
65 auto & [key, val] = BaseIterator::operator*();
66 neml_assert_dbg(val.get(),
67 "Trying to dereference a null object. Make sure the storage was properly "
68 "initialized using set_pointer().");
69 return {key, *val};
70 }
71 std::pair<key_type, value_type *> operator->() const
72 {
73 auto & [key, val] = BaseIterator::operator*();
74 neml_assert_dbg(val.get(),
75 "Trying to dereference a null object. Make sure the storage was properly "
76 "initialized using set_pointer().");
77 return {key, val.get()};
78 }
79 };
80
81 using values_type = typename std::map<I, std::unique_ptr<T>>;
84
94 iterator begin() { return iterator(_values.begin()); }
95 iterator end() { return iterator(_values.end()); }
96 const_iterator begin() const { return const_iterator(_values.begin()); }
97 const_iterator end() const { return const_iterator(_values.end()); }
99
101
110 T & operator[](const I & i) const
111 {
113 "Trying to access a non-existent value with name '" + utils::stringify(i) + "'.");
114 return *pointer_value(i);
115 }
116 T & operator[](const I & i) { return std::as_const(*this)[i]; }
118
125 std::size_t size() const { return _values.size(); }
126
130 bool empty() const { return _values.empty(); }
131
135 bool has_key(const I & i) const { return _values.count(i); }
136
138
144 const T * query_value(const I & i) const { return has_key(i) ? pointer_value(i).get() : nullptr; }
145 T * query_value(const I & i)
146 {
147 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
148 return has_key(i) ? const_cast<T *>(std::as_const(*this).query_value(i)) : nullptr;
149 }
151
161 T * set_pointer(const I & i, std::unique_ptr<T> && ptr)
162 {
163 _values[i] = std::move(ptr);
164 return _values[i].get();
165 }
166
167private:
175 const std::unique_ptr<T> & pointer_value(const I & i) const { return _values.at(i); }
176
178 values_type _values;
179};
180} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Definition Storage.h:45
const_iterator begin() const
Definition Storage.h:96
T & operator[](const I &i) const
Definition Storage.h:110
T & operator[](const I &i)
Definition Storage.h:116
DereferenceIterator< typename values_type::const_iterator > const_iterator
Definition Storage.h:83
bool empty() const
Definition Storage.h:130
Storage()=default
const T * query_value(const I &i) const
Definition Storage.h:144
T * query_value(const I &i)
Definition Storage.h:145
typename std::map< I, std::unique_ptr< T > > values_type
Definition Storage.h:81
T * set_pointer(const I &i, std::unique_ptr< T > &&ptr)
Definition Storage.h:161
bool has_key(const I &i) const
Definition Storage.h:135
DereferenceIterator< typename values_type::iterator > iterator
Definition Storage.h:82
iterator end()
Definition Storage.h:95
const_iterator end() const
Definition Storage.h:97
iterator begin()
Definition Storage.h:94
std::size_t size() const
Definition Storage.h:125
std::string stringify(const T &t)
Definition utils.h:317
Definition CrossRef.cxx:31
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:76
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64
typename BaseIterator::value_type::first_type key_type
Definition Storage.h:60
typename BaseIterator::value_type::second_type::element_type value_type
Definition Storage.h:61
std::pair< key_type, value_type & > operator*() const
Definition Storage.h:63
std::pair< key_type, value_type * > operator->() const
Definition Storage.h:71
DereferenceIterator(const BaseIterator &it)
Definition Storage.h:55