gated.h

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #ifndef SAMPA_GATED_H
00020 #define SAMPA_GATED_H
00021 
00022 #include <string>
00023 #include "sampa/core/triggerable.h"
00024 #include "sampa/core/object.h"
00025 #include "sampa/core/time.h"
00026 #include "sampa/core/event.h"
00027 
00028 namespace Sampa {
00029 
00030   class Simulation;
00031 
00035   class GatedBase : public ContainerObject, public Triggerable { 
00036   // would have used protected Triggerable, but serialize doesn't work then...
00037   public:
00039     typedef ContainerObject::Name Name;
00040     GatedBase(const Name& nm);   
00042     void set_gate_event(EventListener& event); 
00043 
00044     bool check_binding();
00045   protected:
00046     int m_curr;
00047     void request_update();
00048   private:
00049     void trigger();
00050     // disabled
00051     GatedBase(const GatedBase&);
00052     GatedBase& operator=(const GatedBase&);
00053     // state
00054     EventListener* m_sync;
00055     bool           m_notified;
00056   public:
00057     SAMPA_PERSISTENT(GatedBase);
00059   };
00060 
00061   class Module;
00062 
00068   template<class UserType>
00069   class Gated : public GatedBase {
00070   public:
00071     typedef GatedBase::Name Name;
00072     Gated(const Name& name, const UserType& default_value);
00073     void schedule(const UserType& v);      
00074     void force(const UserType& v);         
00075     const UserType& read() const;          
00076     const UserType& get_next() const;      
00077     operator const UserType&() const;      
00078     UserType* operator->() { return &m_value[m_curr]; }
00079   private:
00081     // disabled
00082     Gated(const Gated&);
00083     Gated& operator=(const UserType& v);
00084     // state  
00085     UserType m_value[2];
00086   public:
00087     SAMPA_PERSISTENT(Gated);
00089   };
00090 
00091   inline void GatedBase::set_gate_event(EventListener& event)
00092   { 
00093     m_sync = &event; 
00094   }
00095 
00096   template<class UserType>
00097   inline Gated<UserType>::operator const UserType&() const                  
00098   { 
00099     return m_value[m_curr]; 
00100   }
00101 
00102   template<class UserType>
00103   inline const UserType& Gated<UserType>::read() const                  
00104   { 
00105     return m_value[m_curr]; 
00106   }
00107 
00108   template<class UserType>
00109   inline const UserType& Gated<UserType>::get_next() const                  
00110   { 
00111     return m_value[1-m_curr]; 
00112   }
00113 
00114 } // namespace Sampa
00115 
00116 #endif

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3