$extrastylesheet
parameter_accessor.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_PARAMETER_ACCESSOR_H
00021 #define LIBMESH_PARAMETER_ACCESSOR_H
00022 
00023 
00024 // Local Includes -----------------------------------
00025 #include "libmesh/libmesh_common.h"
00026 #include "libmesh/compare_types.h" // remove_const
00027 
00028 namespace libMesh
00029 {
00030 
00031 // Forward declarations
00032 template <typename T>
00033 class ParameterProxy;
00034 
00035 template <typename T>
00036 class ConstParameterProxy;
00037 
00038 
00047 template <typename T=Number>
00048 class ParameterAccessor
00049 {
00050 public:
00055   virtual ~ParameterAccessor() {}
00056 
00060   virtual void set (const T & new_value) = 0;
00061 
00065   virtual const T& get () const = 0;
00066 
00072   virtual void operator= (T * /* new_ptr */) { libmesh_error(); }
00073 
00080   ParameterProxy<T> operator* () { return ParameterProxy<T>(*this); }
00081 
00082   ConstParameterProxy<T> operator* () const { return ConstParameterProxy<T>(*this); }
00083 };
00084 
00085 template <typename T=Number>
00086 class ParameterProxy
00087 {
00088 public:
00092   ParameterProxy (ParameterAccessor<T>& accessor)
00093     : _accessor(accessor) {}
00094 
00098   ParameterProxy& operator = (const T & new_value) { _accessor.set(new_value); return *this; }
00099 
00103   ParameterProxy& operator = (const ParameterProxy<T> & new_value) { _accessor.set(new_value.get()); }
00104 
00108   ParameterProxy& operator = (const ConstParameterProxy<T> & new_value) { _accessor.set(new_value.get()); return *this; }
00109 
00113   ParameterProxy& operator += (const T & value_increment) { _accessor.set(_accessor.get() + value_increment); return *this; }
00114 
00118   ParameterProxy& operator -= (const T & value_decrement) { _accessor.set(_accessor.get() - value_decrement); return *this; }
00119 
00123   ParameterProxy& operator *= (const T & value_multiplier) { _accessor.set(_accessor.get() * value_multiplier); return *this; }
00124 
00128   ParameterProxy& operator /= (const T & value_divisor) { _accessor.set(_accessor.get() / value_divisor); return *this; }
00129 
00133   operator T () const { return _accessor.get(); }
00134 
00135 private:
00136   ParameterAccessor<T>& _accessor;
00137 };
00138 
00139 
00140 template <typename T=Number>
00141 class ConstParameterProxy
00142 {
00143 public:
00147   ConstParameterProxy (const ParameterAccessor<T>& accessor)
00148     : _accessor(accessor) {}
00149 
00153   operator T () const { return _accessor.get(); }
00154 
00158   T get() const { return _accessor.get(); }
00159 
00160 private:
00161   const ParameterAccessor<T>& _accessor;
00162 };
00163 
00164 
00165 } // namespace libMesh
00166 
00167 #endif // LIBMESH_PARAMETER_ACCESSOR_H