parameter_multiaccessor.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PARAMETER_MULTIACCESSOR_H
21 #define LIBMESH_PARAMETER_MULTIACCESSOR_H
22 
23 
24 // Local Includes
25 #include "libmesh/libmesh_common.h"
27 
28 // C++ Includes
29 #include <vector>
30 
31 namespace libMesh
32 {
33 
46 template <typename T=Number>
48 {
49 public:
54 
58  ParameterMultiAccessor(const ParameterAccessor<T> & param_accessor) :
59  _accessors(1, param_accessor.clone()) {}
60 
61  /*
62  * Destructor: delete our clones of sub-accessors
63  */
65  for (auto & accessor : _accessors)
66  delete accessor;
67  }
68 
72 #ifdef LIBMESH_ENABLE_DEPRECATED
73  virtual ParameterAccessor<T> &
74  operator= (T * /* new_ptr */) override
75  {
76  libmesh_error();
77  return *this;
78  }
79 #endif
80 
84  virtual void set (const T & new_value) override
85  {
86  libmesh_assert(!_accessors.empty());
87 #ifndef NDEBUG
88  // Compare other values to the last one we'll change
89  const T & val = _accessors.back()->get();
90 #endif
91  for (auto & accessor : _accessors)
92  {
93  // If you're already using inconsistent parameters we can't
94  // help you.
95  libmesh_assert_equal_to(accessor->get(), val);
96  accessor->set(new_value);
97  }
98  }
99 
103  virtual const T & get () const override
104  {
105  libmesh_assert(!_accessors.empty());
106  const T & val = _accessors[0]->get();
107 #ifndef NDEBUG
108  // If you're already using inconsistent parameters we can't help
109  // you.
110  for (std::size_t i=1; i < _accessors.size(); ++i)
111  libmesh_assert_equal_to(_accessors[i]->get(), val);
112 #endif
113  return val;
114  }
115 
119  virtual std::unique_ptr<ParameterAccessor<T>> clone() const override
120  {
122  for (auto & accessor : _accessors)
123  pmp->_accessors.push_back(accessor->clone().release());
124 
125  return std::unique_ptr<ParameterAccessor<T>>(pmp);
126  }
127 
128 
129  void push_back (const ParameterAccessor<T> & new_accessor) {
130  _accessors.push_back(new_accessor.clone().release());
131  }
132 
138  std::size_t size() const { return _accessors.size(); }
139 
140 private:
141  std::vector<ParameterAccessor<T> *> _accessors;
142 };
143 
144 } // namespace libMesh
145 
146 #endif // LIBMESH_PARAMETER_MULTIACCESSOR_H
ParameterMultiAccessor(const ParameterAccessor< T > &param_accessor)
virtual std::unique_ptr< ParameterAccessor< T > > clone() const override
virtual ParameterAccessor< T > & operator=(T *) override
void push_back(const ParameterAccessor< T > &new_accessor)
std::vector< ParameterAccessor< T > * > _accessors
Stores a user-provided pointer to a parameter.
virtual std::unique_ptr< ParameterAccessor< T > > clone() const =0
Base class for reading/writing sensitivity parameters.