Gamma  0.9.5
Generic Synthesis Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
/Users/ljp/code/gamma/trunk/Gamma/Sync.h
00001 #ifndef GAMMA_SYNC_H_INC
00002 #define GAMMA_SYNC_H_INC
00003 
00004 /*  Gamma - Generic processing library
00005     See COPYRIGHT file for authors and license information */
00006 
00007 #include "Gamma/Node.h"
00008 
00009 namespace gam{
00010 
00011 class Sync;
00012 
00013 
00015 class Synced1{
00016 public:
00017 
00018     virtual ~Synced1(){}
00019 
00020     double spu() const { return 1.; }       
00021     double ups() const { return 1.; }       
00022 
00024 
00027     virtual void onResync(double ratioSPU){}
00028     
00029     void spu(double val){}          
00030     void ups(double val){}          
00031 
00032 protected:
00033     void initSynced(){ onResync(1); }
00034 };
00035 
00036 
00037 
00039 
00045 class Synced : public Node2<Synced> {
00046 public:
00047 
00048     Synced();
00049     
00051     
00054     Synced(const Synced& rhs);
00055 
00056     virtual ~Synced(){}
00057 
00058     double scaleSPU() const;    
00059     double spu() const;         
00060     double ups() const;         
00061     const Sync * sync() const;  
00062 
00064     
00067     virtual void onResync(double ratioSPU){}
00068 
00069     void scaleSPU(double v);    
00070     void scaleUPS(double v);    
00071     void spu(double v);         
00072     void sync(Sync& src);       
00073     void ups(double v);         
00074 
00075     Synced& operator= (const Synced& rhs);
00076 
00077 protected:
00078     void initSynced(); 
00079 
00080 private:
00081     friend class Sync;
00082     Synced(bool zeroLinks, Sync& src);
00083 
00084     Sync * mSync;   // Reference to my Sync
00085     double mSPU;    // Local samples/unit
00086     double mUPS;    // Local units/sample
00087 };
00088 
00089 
00090 
00091 
00092 
00094 class Sync{
00095 public:
00096 
00097     Sync();
00098 
00100     Sync(double spu);
00101 
00102     ~Sync();
00103 
00104     Sync& operator<< (Synced& synced);  
00105     void notifySynceds(double r);       
00106     void spu(double v);                 
00107     void ups(double v);                 
00108 
00109     bool hasBeenSet() const;            
00110     double spu() const;                 
00111     double ups() const;                 
00112 
00114     static Sync& master(){
00115         static Sync * s = new Sync;
00116         return *s;
00117     }
00118 
00119 protected:
00120     double mSPU, mUPS;
00121     Synced mHeadSynced;     // Head of Synced doubly-linked list.
00122     bool mHasBeenSet;
00123 
00124 friend class Synced;
00125     void addSynced(Synced& synced);
00126 };
00127 
00128 
00129 
00130 
00131 // Implementation_______________________________________________________________
00132 
00133 // Sync
00134 
00135 inline bool Sync::hasBeenSet() const { return mHasBeenSet; }
00136 inline double Sync::spu() const { return mSPU; }
00137 inline double Sync::ups() const { return mUPS; }
00138 
00139 
00140 // Synced
00141 
00142 #define SYNCED_INIT mSync(0), mSPU(1), mUPS(1)
00143 
00144 inline Synced::Synced()
00145 :   Node2<Synced>(), SYNCED_INIT
00146 {
00147     //printf("Synced::Synced() - %p\n", this);
00148     sync(Sync::master());
00149 }
00150 
00151 // This ctor is used to create the head Synced in a Sync
00152 inline Synced::Synced(bool zeroLinks, Sync& src)
00153 :   Node2<Synced>(zeroLinks), SYNCED_INIT
00154 {
00155     sync(src);
00156 }
00157 
00158 inline Synced::Synced(const Synced& rhs)
00159 :   Node2<Synced>(), SYNCED_INIT
00160 {
00161     //printf("Synced::Synced(const Synced&) - %p\n", this);
00162     Sync& s = rhs.mSync ? *rhs.mSync : Sync::master();
00163     sync(s);
00164 }
00165 
00166 inline void Synced::initSynced(){ if(sync()) spu(sync()->spu()); }
00167 inline double Synced::scaleSPU() const { return sync() ? spu() / sync()->spu() : 1; }
00168 inline void Synced::spu(double v){ scaleSPU(v * ups()); }
00169 inline void Synced::ups(double v){ scaleUPS(v * spu()); }
00170 inline double Synced::spu() const { return mSPU; }
00171 inline double Synced::ups() const { return mUPS; }
00172 
00173 inline const Sync * Synced::sync() const { return mSync; }
00174 
00175 } // gam::
00176 
00177 #endif
00178