Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

jot_vars.H

Go to the documentation of this file.
00001 #ifndef JOT_VARS_IN_DA_HOUSE
00002 #define JOT_VARS_IN_DA_HOUSE
00003 
00004 #include "std/support.H"
00005 #include "net/data_item.H"
00006 
00007 /* ---------------------- JOTvar classes ------------------------------ */
00008 //
00009 // this is a generic class for observing all JOTvars.  
00010 // Observers are notified with the DATA_ITEM corresponding to the
00011 // JOTvar that changed.  Observers are not expected to look at the
00012 // values in the JOTvar, but instead are only expected to call 
00013 // DATA_ITEM methods such as format or decode.
00014 //
00015 class JOTvar_obs;
00016 typedef ARRAYptrs<JOTvar_obs *> JOTvar_obs_list;
00017 class JOTvar_obs {
00018    static JOTvar_obs_list  *_jot_var_obs_list;
00019    JOTvar_obs_list *jot_var_obs_list(){ if (!_jot_var_obs_list) 
00020                                           _jot_var_obs_list=new JOTvar_obs_list;
00021                                          return _jot_var_obs_list; }
00022  public:
00023    virtual ~JOTvar_obs() {}
00024    static  void notify_jot_var_obs(DATA_ITEM *item)
00025                                  { if (!_jot_var_obs_list) return;
00026                                    for (int i=0;i<_jot_var_obs_list->num();i++) 
00027                                        (*_jot_var_obs_list)[i]->
00028                                              notify_jot_var(item); }
00029    virtual void notify_jot_var(DATA_ITEM *item) = 0;
00030    
00031            void jot_var_obs()     { jot_var_obs_list()->add_uniquely(this); }
00032            void unobs_jot_var()   { jot_var_obs_list()->rem         (this); }
00033 };
00034 
00035 
00036 //
00037 //  Special TAG for JOTvars.  This TAG is different from a normal TAG_val
00038 // because the TAG stores the object where the value is stored and ignores
00039 // the object passed to format/decode.  This is because the value in a JOTvar
00040 // is logically associated with the containing class, but is actually stored
00041 // in the "invisible" JOTvar class.  Therefore, the object that is passed to
00042 // format/decode is the containing class and isn't useful in helping find the
00043 // actual variable data.  
00044 //
00045 template <class T, class V>
00046 class TAG_var : public TAG {
00047      typedef V    (T::*value)() const;
00048      typedef void (T::*set_meth)(V t);
00049 
00050      set_meth  _smeth;
00051      value     _value;
00052      TAGformat _delim;
00053      T        *_var;
00054   public:
00055                 TAG_var( ) : _var(0),_value(0) { }
00056                 TAG_var(T *var, Cstr_ptr &field_name, value val, set_meth m): 
00057                     _var(var), _delim(field_name, 0), _value(val), _smeth(m) { }
00058     STDdstream &format(CDATA_ITEM *, STDdstream &d) 
00059                                       { _delim.set_stream(&d);
00060                                         _delim.id() << (_var->*_value)();
00061                                         return d; }
00062     STDdstream &decode(CDATA_ITEM *, STDdstream &d)
00063                                       {  V val;
00064                                         _delim.set_stream(&d); 
00065                                         _delim.read_id() >> val;
00066                                         (_var->*_smeth)(val);
00067                                         return d; }
00068     Cstr_ptr   &name()     const      { return _delim.name(); }
00069 };
00070 
00071 //
00072 // Generic JOTvar that enables a class to easily add a member variable
00073 // that will automatically be networked when it is set and that will 
00074 // provide TAGs for reading and writing the variable to a file.
00075 //
00076 template <class OBJ, class TYPE>
00077 class JOTvar : public DATA_ITEM {
00078       typedef void (OBJ::*set_meth)(TYPE t);
00079    protected:
00080       TAGlist   *_var_tags;   // tags for reading/writing the variables
00081       str_ptr    _vname;      // name of the variable
00082       str_ptr    _inst_name;  // variable instance name:  object::variable
00083 
00084       set_meth   _smeth;      // containing class method used to set variable
00085       OBJ       *_obj;        // containing instance
00086 
00087       TYPE       _val;        // variable value
00088 
00089       void       set_val(TYPE val)        { (_obj->*_smeth)(val); }
00090       str_ptr    inst_name()              { return _obj->name() + "::"+_vname; }
00091       virtual void      check_inst_name() { if (inst_name() != _inst_name) {
00092                                               // New instantiation name
00093                                               _inst_name = inst_name();
00094                                               DATA_ITEM::add_decoder(_inst_name,
00095                                                                      this);
00096                                             }
00097                                           }
00098 
00099    public:
00100                      // constructor: each JOTvar instance is a unique decoder!
00101                      JOTvar(TYPE v, OBJ *obj, Cstr_ptr &vname, set_meth m):
00102                           _vname(vname), _val(v), _obj(obj), _smeth(m), 
00103                           _var_tags(0)
00104                                           { _inst_name = inst_name(); 
00105                                        DATA_ITEM::add_decoder(_inst_name,this);}
00106 
00107          void         set(TYPE v)         { _val = v;// set val & notify net,etc
00108                                             check_inst_name();
00109                                           JOTvar_obs::notify_jot_var_obs(this);}
00110          TYPE         get()         const { return _val; }
00111 
00112  virtual Cstr_ptr    &obj_name()    const { return _obj->name();} 
00113  virtual Cstr_ptr    &var_name()    const { return _vname;} 
00114  virtual STAT_STR_RET class_name()  const { return _inst_name;}// instance name!
00115  virtual DATA_ITEM   *dup()         const { return  0; }
00116  virtual CTAGlist    &tags()        const {
00117                            if (!_var_tags) {
00118                               ((JOTvar<OBJ,TYPE> *)this)->_var_tags = new TAGlist;
00119                               *((JOTvar<OBJ,TYPE> *)this)->_var_tags += 
00120                                  new TAG_var<JOTvar<OBJ,TYPE>,TYPE>((JOTvar<OBJ,TYPE> *)this,_vname,
00121                                                            get,set_val);
00122                            }
00123                            return *_var_tags;
00124                         }
00125 };
00126 
00127 
00128 /*
00129 
00130 MAKE_PTR_SUBC(Rake,GEOM);
00131 class Rake : public GEOM {
00132     JOTvar<Rake,int>  _num_streams;
00133     JOTvar<Rake,int>  _num_rakes;
00134     TAGlist          *_tg_tags; // can't be static unless JOTvar tags are redone
00135 
00136     CTAGlist  &tags()   const   {
00137                             if (!_tg_tags) {
00138                                ((Rake  *)this)->_tg_tags  = new TAGlist;
00139                                *((Rake *)this)->_tg_tags += GEOM::tags();
00140                                *((Rake *)this)->_tg_tags += _num_rakes.tags();
00141                                *((Rake *)this)->_tg_tags += _num_streams.tags();
00142                             }
00143                             return *_tg_tags; 
00144                          }
00145 
00146    public:
00147 
00148     Rake():_tg_tags(0), _num_rakes  (0, this, "NumRakes",   set_num_rakes),
00149                         _num_streams(0, this, "NumStreams", set_num_streams) { }
00150 
00151     Rake(Cstr_ptr &n):GEOM(n),_tg_tags(0),
00152                         _num_rakes  (0, this, "NumRakes",   set_num_rakes),
00153                         _num_streams(0, this, "NumStreams", set_num_streams) { }
00154 
00155     int      num_rakes()            { return _num_rakes.get(); }
00156     void     set_num_rakes(int n)   { _num_rakes.set(n); }
00157 
00158     int      num_streams()          { return _num_streams.get(); }
00159     void     set_num_streams(int n) { _num_streams.set(n); }
00160 
00161     DEFINE_RTTI_METHODS2("Rake", GEOM, CDATA_ITEM *);
00162 
00163    virtual DATA_ITEM *dup()            const { return new Rake(_name); }
00164    virtual GEOMptr    dup(Cstr_ptr &n) const { cerr <<"Whaaa\n"; return 0;}
00165 };
00166 */
00167 
00168 #endif

Generated on Mon Sep 18 11:39:31 2006 for jot by  doxygen 1.4.4