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

Control.h

Go to the documentation of this file.
00001 
00009 #include "defines.h"
00010 #ifdef CONTROL
00011 #ifndef CONTROL_H
00012 #define CONTROL_H
00013 
00014 #include <map>
00015 #include <list>
00016 #include <string>
00017 
00018 //struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0;}};
00019 
00020 struct SDL_mutex;
00021 namespace annie
00022 {
00023 typedef std::string ValueKey;
00024 //typedef const char *ValueKey;
00025 class PublicValues; //FW
00026 class Value;    //FW
00030 class Value {
00031 public:
00032     enum { VISIBLE=1, DEFAULT=VISIBLE};
00033     unsigned flags; //<! we'd like to concentrate most of the flags used in listeners so that Listener doesn't have to hold per'value data and also that the flags are portable between listeners
00034     typedef real val_t;
00035     static const double DISPLAY_THRESHOLD;  //< value won't be re-displayer more often then this (seconds)
00036 
00037     void set(val_t to);
00038     val_t get() const { return _current; }
00039     val_t getLast() const { return _last; }
00040     Value &operator= (val_t val)    { set(val); return *this; }
00041     val_t operator()() const    { return get(); }
00042     operator val_t () const { return get(); }
00043 
00044     bool isVisible() const { return flags & VISIBLE; }
00045     Value(const std::string &name);
00046 
00053     struct Time {
00055         Time();
00060         double diffReal(const Time &t) { Time tmp = (*this) - t; return tmp.toSec(); }
00061         Time operator-(const Time &t) const;
00062         Time &operator-=(const Time &t) { (*this) = (*this) - t; return (*this); }
00063 
00064         double toSec() const;
00065         
00069         void current();
00070 
00071         timeval _realCalendar;
00072       protected:
00073         Time(const timeval &realCalendar) : _realCalendar(realCalendar) {}
00074     };
00075     std::string name() const    { return _name; }
00076     
00082     bool enoughTimePassed() const;
00083 
00084     double timeFromLastChange() const   {
00085         Time current;
00086         current -= _time;
00087         return current.toSec();
00088     }
00089     
00090 private:
00091     real _current,  //< current value
00092     _last;      //< previous value
00093     Time _time, _lastTime;  //<time when current/last were set
00094     mutable Time    _lastTimeDisplayed;
00095     const std::string _name;
00096 };
00097 
00106 class PublicReal;
00107 struct ValueUpdateListener;
00108 class PublicValues
00109 {
00110     friend class PublicReal;
00111 public:
00112     PublicValues();
00113     ~PublicValues();
00119     void change(const ValueKey &name, real newVal);
00120 
00121     void inc(const ValueKey &name);
00122 
00127     void setFlags(const ValueKey &name, unsigned flags);
00128 
00132     //const Value &operator[](const std::string &name) const throw(Exception) { return (*this)[name]; }
00133     PublicReal operator[](const std::string &name) throw(Exception);
00134 
00138     PublicReal get(const std::string &name);
00139 
00143     PublicReal init(const std::string &name, real init=0.);
00144     
00146     void addListener(ValueUpdateListener *l);
00147 
00149     void removeListener(ValueUpdateListener *l);
00150 
00152     void addListener(ValueUpdateListener &l) { addListener(&l); }
00153     void removeListener(ValueUpdateListener &l) { removeListener(&l); }
00154 
00156     void triggerAll() const;
00157 private:
00159     void hookValue(const Value &v);
00160     void unHookValue(const Value &v)    { /*TODO*/}
00161 
00162     //direct change - the Value must be present in this PublicValues
00163     void change(Value &v, real newVal);
00164 
00168     void _updated(const Value &v) const;
00169 
00174     Value &_exist(const ValueKey &name);
00175 
00176     typedef std::map<std::string, Value> Values;
00177     Values _values;
00178     typedef std::list<ValueUpdateListener *> Listeners;
00179     Listeners _listeners;
00180 #ifdef THREADS_ENABLED
00181     SDL_mutex *valuesLock;
00182 #endif
00183 };
00184 
00185 PublicValues &getDefaultControl();
00186 #define defaultControl  getDefaultControl()
00187 
00193 struct ValueUpdateListener  {
00195     ValueUpdateListener(PublicValues &ctrl=defaultControl);
00200     virtual void valueChanged(const Value &val) = 0;
00201 
00203     PublicValues &getPublicValues() const;
00204 
00206     virtual ~ValueUpdateListener();
00207   protected:
00208     PublicValues &control;
00209 };
00210 
00211 
00212 
00220 //TODO: templatize parameter && enable other-than-real vars?
00221 class PublicReal    {
00222 public:
00223     friend class PublicValues;
00224     //TODO: also enable 'anon' (unnamed), so we can replace all reals eventually
00225     operator real() const { return get(); }
00226 #ifdef FP_ENABLED
00227     int toInt() const { return  get().toInt(); }
00228     double toDouble() const { return    get().toDouble(); }
00229 #endif
00230     //operator const real() const   {   return get(); }
00231     real operator=(real r) { set(r); return r; }
00232     real operator++ ()  {   set(get() + 1.); return get();  }
00233     real operator++(int)    { real r = get(); set(r + 1); return r; }
00234     
00235     real operator-(real r) const { return get() - r; }
00236     real operator+(real r) const { return get() + r; }
00237     real operator*(real r) const { return get() * r; }
00238     
00239     real operator*=(real r) { set(get() * r); return get(); }
00240     real operator+=(real r) { set(get() + r); return get(); }
00241     real operator-=(real r) { set(get() - r); return get(); }
00242 
00243     double timeFromLastChange() const { return _val.timeFromLastChange(); }
00244 
00245 #ifdef CONTROL
00246     //we rely on never-lasting Values in control, which is not good. But we can count it on Value --> OK ..
00247     Value &_val;
00248     PublicValues &_control;
00249 #else
00250     //ordinary real (+maybe name, if even..)
00251 #endif
00252     ~PublicReal()   { _control.unHookValue(_val); }
00253     PublicReal(const PublicReal &p);
00254   private:
00255     void set(real r) { _control.change(_val, r); }
00256     real get() const { return _val.get(); }
00257     PublicReal(Value &v, PublicValues &c) : _val(v), _control(c) { _control.hookValue(_val); }
00258     PublicReal &operator=(const PublicReal &p); //never implemented
00259 };
00260 
00261 typedef PublicReal Creal;
00262 
00263 }; //namespace annie
00264 #endif // define _EXCEPTION_H
00265 #endif // CONTROL

Generated on Fri Jun 18 13:18:10 2004 for Annie by doxygen 1.3.5