NEML2 1.4.0
Loading...
Searching...
No Matches
Storage.h
1// Copyright 2023, 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
96 iterator begin() { return iterator(_values.begin()); }
97 iterator end() { return iterator(_values.end()); }
98 const_iterator begin() const { return const_iterator(_values.begin()); }
99 const_iterator end() const { return const_iterator(_values.end()); }
101
112 T & operator[](const I & i) const
113 {
115 "Trying to access a null object. Make sure the storage was properly "
116 "initialized using set_pointer().");
117 return *pointer_value(i);
118 }
119 T & operator[](const I & i) { return std::as_const(*this)[i]; }
121
128 std::size_t size() const { return _values.size(); }
129
133 bool empty() const { return _values.empty(); }
134
138 bool has_key(const I & i) const { return _values.count(i); }
139
147 const T * query_value(const I & i) const { return has_key(i) ? pointer_value(i).get() : nullptr; }
148 T * query_value(const I & i)
149 {
150 return has_key(i) ? const_cast<T *>(std::as_const(*this).query_value(i)) : nullptr;
151 }
153
163 T * set_pointer(const I & i, std::unique_ptr<T> && ptr)
164 {
165 _values[i] = std::move(ptr);
166 return _values[i].get();
167 }
168
169private:
177 const std::unique_ptr<T> & pointer_value(const I & i) const { return _values.at(i); }
178
180 values_type _values;
181};
182} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition Storage.h:45
Storage(const Storage &)=delete
const_iterator begin() const
Definition Storage.h:98
T & operator[](const I &i) const
Definition Storage.h:112
T & operator[](const I &i)
Definition Storage.h:119
DereferenceIterator< typename values_type::const_iterator > const_iterator
Definition Storage.h:86
bool empty() const
Definition Storage.h:133
Storage()=default
Storage & operator=(const Storage &)=delete
const T * query_value(const I &i) const
Definition Storage.h:147
T * query_value(const I &i)
Definition Storage.h:148
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:163
bool has_key(const I &i) const
Definition Storage.h:138
DereferenceIterator< typename values_type::iterator > iterator
Definition Storage.h:85
iterator end()
Definition Storage.h:97
const_iterator end() const
Definition Storage.h:99
iterator begin()
Definition Storage.h:96
std::size_t size() const
Definition Storage.h:128
Storage(Storage &&)=default
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
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