00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 #ifndef VCSN_MISC_DEFERRER_HXX
00018 # define VCSN_MISC_DEFERRER_HXX
00019  
00031 # include <vaucanson/misc/deferrer.hh>
00032 # include <vaucanson/misc/contract.hh>
00033 
00034 # include <new>
00035 # include <cstdlib>
00036 
00037 namespace vcsn
00038 {
00039   namespace misc
00040   {
00041 
00044   
00045 
00046 
00047 
00048     template <class T, bool B>
00049     DeferrerDebugPart<T, B>::DeferrerDebugPart (void* ptr, bool is_valid) :
00050       is_valid_ (is_valid)
00051     {
00052       memset (ptr, 0, sizeof (T));
00053     }
00054 
00055     template <class T, bool B>
00056     void
00057     DeferrerDebugPart<T, B>::set_valid (bool b)
00058     {
00059       if (is_valid_)
00060         WARNING ("Modifying a valid Deferrer<T, true>.");
00061       is_valid_ = b;
00062     }
00063 
00064     template <class T, bool B>
00065     T&
00066     DeferrerDebugPart<T, B>::cast (void *ptr)
00067     {
00068       precondition (is_valid_);
00069       return *reinterpret_cast<T*> (ptr);
00070     }
00071 
00072     template <class T, bool B>
00073     const T&
00074     DeferrerDebugPart<T, B>::cast (const void *ptr) const
00075     {
00076       precondition (is_valid_);
00077       return *reinterpret_cast<const T*> (ptr);
00078     }
00079 
00080   
00081 
00082 
00083 
00084     template <class T>
00085     DeferrerDebugPart<T, false>::DeferrerDebugPart (void*, bool)
00086     {
00087     }
00088 
00089     template <class T>
00090     void
00091     DeferrerDebugPart<T, false>::set_valid (bool)
00092     {
00093     }
00094 
00095     template <class T>
00096     T&
00097     DeferrerDebugPart<T, false>::cast (void *ptr)
00098     {
00099       return *reinterpret_cast<T*> (ptr);
00100     }
00101 
00102     template <class T>
00103     const T&
00104     DeferrerDebugPart<T, false>::cast (const void *ptr) const
00105     {
00106       return *reinterpret_cast<const T*> (ptr);
00107     }
00108 
00109   
00110 
00111 
00112 
00113     template <class T, bool rt_checks>
00114     Deferrer<T, rt_checks>::Deferrer () : DeferrerDebugPart<T, rt_checks> (data)
00115     {
00116     }
00117 
00118     template <class T, bool rt_checks>
00119     Deferrer<T, rt_checks>&
00120     Deferrer<T, rt_checks>::operator= (const deferred_type& rhs)
00121     {
00122       new (data) deferred_type (rhs);
00123       this->set_valid (true);
00124       return *this;
00125     }
00126 
00127     template <class T, bool rt_checks>
00128     Deferrer<T, rt_checks>::operator T () const
00129     {
00130       return cast (data);
00131     }
00132 
00134   } 
00135 } 
00136 
00137 #endif // ! VCSN_MISC_DEFERRER_HXX