Field.h

Go to the documentation of this file.
00001 #ifndef FIELD_H_
00002 #define FIELD_H_
00003 
00004 #include "PCSIMException.h"
00005 
00006 class SimObject;
00007 
00008 #include <string>
00009 using std::string;
00010 
00012 
00013 class Field
00014 {
00015 
00016 public:
00017     typedef enum { readonly=0, readwrite=1  } AccessType;
00018 
00019     typedef unsigned char ValueType;
00020 
00021     static const ValueType double_t = 0;
00022     static const ValueType float_t = 1;
00023     static const ValueType int_t = 2;
00024     static const ValueType unknown_t = 100;
00025 
00026     Field(void)
00027     {
00028         offset = -1;
00029     };
00030     virtual ~Field()
00031     {}
00032     ;
00033 
00034     virtual Field *clone(void) const = 0;
00035 
00036 
00038     string name;
00039 
00041     string description;
00042 
00044     string units;
00045 
00047     AccessType access;
00048 
00050     double lb;
00051 
00053     double ub;
00054 
00055     virtual double getValue( SimObject *o ) const = 0;
00056 
00057     virtual void setValue( SimObject *o, double v ) const = 0;
00058 
00059     int getOffset() const
00060     {
00061         return offset;
00062     };
00063 
00064     ValueType getType() const
00065     {
00066         return type;
00067     }
00068 
00069     static double getValue( void const * const field_ptr, const ValueType &type);
00070     
00071     static void setValue(void * const field_ptr, const ValueType &type, const double v);
00072 
00073     void * getPtr( SimObject *o ) const
00074     {
00075         return reinterpret_cast<void *>(reinterpret_cast<char *>(o)+offset);
00076     };
00077 
00078 protected:
00082 
00084 
00085     int offset;
00086 
00088     int size;
00089 
00091     ValueType type;
00092 
00094 };
00095 
00096 inline double Field::getValue( void const * const field_ptr, const ValueType &type)
00097 {
00098     switch (type) {
00099     case Field::double_t:
00100         return *(double *)field_ptr;
00101     case Field::float_t:
00102         return (float)(*(float *)field_ptr);
00103     case Field::int_t:
00104         return (int)(*(int *)field_ptr);
00105     default:
00106         /* throw exception */
00107         throw(
00108             PCSIM::Exception(
00109                 "static getValue(field_ptr, type)",
00110                 "Cannot get value from an field of an unknwon type.  Only double, float and int fields are supported."
00111             ) );
00112     }
00113 }
00114 
00115 inline void Field::setValue(void * const field_ptr, const ValueType &type, const double v)
00116 {
00117     switch (type) {
00118     case Field::double_t:
00119         *(double *)field_ptr = v;
00120         break;
00121     case Field::float_t:
00122         (*(float *)field_ptr) = (float)v;
00123         break;
00124     case Field::int_t:
00125         (*(int *)field_ptr) = (int)v;
00126         break;
00127     default:
00128         throw(
00129             PCSIM::Exception(
00130                 "static setValue(field_ptr, type)",
00131                 "Don't know how to set value to an unknown field type . Only double, float and int fields are supported."
00132             ) );
00133     }
00134 }
00135 
00136 
00137 
00138 #endif /*FIELD_H_*/

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