ScalarField.h

Go to the documentation of this file.
00001 #ifndef SCALARFIELD_H_
00002 #define SCALARFIELD_H_
00003 
00004 #include <typeinfo>
00005 
00006 #include "Field.h"
00007 
00008 template<typename T, bool is_scalar >
00009 class ScalarFieldImpl : public Field
00010     {}
00011 ;
00012 
00013 template<typename T>
00014 class ScalarFieldImpl<T, true> : public Field
00015 {
00016 
00017 public:
00018     static ValueType Type;
00019 
00020     ScalarFieldImpl<T, true>()
00021     {}
00022     ;
00023 
00024     ScalarFieldImpl<T, true>(
00025         const string & name,
00026         const string & description,
00027         const int & offset,
00028         const string & units = "unspecific",
00029         const Field::AccessType & access = Field::readwrite,
00030         const double & lb = std::numeric_limits<double>::quiet_NaN(),
00031         const double & ub = std::numeric_limits<double>::quiet_NaN()
00032     )
00033     {
00034         this->name = name;
00035         this->description = description;
00036         this->units = units;
00037         this->access = access;
00038         this->lb = lb;
00039         this->ub = ub;
00040         this->offset = offset;
00041         this->size = sizeof(T);
00042         initValueType();
00043         Field::type = ScalarFieldImpl<T,true>::Type;
00044     };
00045 
00046     virtual ~ScalarFieldImpl<T, true>()
00047     {}
00048     ;
00049 
00050     virtual ScalarFieldImpl<T, true> *clone(void) const
00051     {
00052         return new ScalarFieldImpl<T, true>( *this );
00053     };
00054 
00055     virtual double getValue( SimObject *o ) const
00056     {
00057         return (double)( *( (T *)( ((char *)o)+offset ) ) );
00058     };
00059 
00060     virtual void setValue( SimObject *o, double v ) const
00061     {
00062         *( (T *)( ((char *)o)+offset ) ) = (T)(v);
00063     };
00064 
00066 
00070     static void initValueType()
00071     {
00072         if (typeid(double) == typeid(T))
00073             Type = Field::double_t;
00074         else if (typeid(float) == typeid(T))
00075             Type = Field::float_t;
00076         else if (typeid(int) == typeid(T))
00077             Type = Field::int_t;       
00078     };
00079     
00080 };
00081 
00082 template<typename T>
00083 Field::ValueType ScalarFieldImpl<T, true>::Type = Field::unknown_t;
00084 
00085 
00086 template<typename T>
00087 class ScalarFieldImpl<T, false> : public Field
00088 {
00089 public:
00090 
00091     ScalarFieldImpl<T, false >(
00092         const string & name,
00093         const string & description,
00094         const int & offset,
00095         const string & units = "unspecific",
00096         const Field::AccessType & access = Field::readwrite,
00097         const double & lb = std::numeric_limits<double>::quiet_NaN(),
00098         const double & ub = std::numeric_limits<double>::quiet_NaN()
00099     )
00100     {}
00101     ;
00102 
00103     virtual ~ScalarFieldImpl<T, false>()
00104     {}
00105     ;
00106 
00107     virtual ScalarFieldImpl<T, false> *clone(void) const
00108     {
00109         return new ScalarFieldImpl<T, false>( *this );
00110     };
00111 
00112     virtual double getValue( SimObject *o ) const
00113     {
00114         return std::numeric_limits<double>::quiet_NaN();
00115     };
00116 
00117     virtual void setValue( SimObject *o, double v ) const
00118     {
00119         /* NOOP */
00120     };
00121 
00122     virtual Field::ValueType valueType()
00123     {
00124         return Field::unknown_t;
00125     }
00126 
00127 };
00128 
00129 template<typename T>
00130 class ScalarField : public ScalarFieldImpl< T, std::numeric_limits<T>::is_specialized >
00131 {
00132 public:
00133     ScalarField(
00134         const string & name,
00135         const string & description,
00136         const int & offset,
00137         const string & units = "unspecific",
00138         const Field::AccessType & access = Field::readwrite,
00139         const double & lb = std::numeric_limits<double>::quiet_NaN(),
00140         const double & ub = std::numeric_limits<double>::quiet_NaN()
00141     ) : ScalarFieldImpl< T, std::numeric_limits<T>::is_specialized >( name, description, offset, units, access, lb, ub )
00142     {}
00143     ;
00144 };
00145 
00146 #endif /*SCALARFIELD_H_*/

Generated on Wed Jul 9 16:34:38 2008 for PCSIM by  doxygen 1.5.5