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 # include <cstring>
00037 
00038 namespace vcsn
00039 {
00040   namespace misc
00041   {
00042 
00045   
00046 
00047 
00048 
00049     template <class T, bool B>
00050     DeferrerDebugPart<T, B>::DeferrerDebugPart (void* ptr, bool is_valid) :
00051       is_valid_ (is_valid)
00052     {
00053       memset (ptr, 0, sizeof (T));
00054     }
00055 
00056     template <class T, bool B>
00057     void
00058     DeferrerDebugPart<T, B>::set_valid (bool b)
00059     {
00060       if (is_valid_)
00061         WARNING ("Modifying a valid Deferrer<T, true>.");
00062       is_valid_ = b;
00063     }
00064 
00065     template <class T, bool B>
00066     T&
00067     DeferrerDebugPart<T, B>::cast (void *ptr)
00068     {
00069       precondition (is_valid_);
00070       return *reinterpret_cast<T*> (ptr);
00071     }
00072 
00073     template <class T, bool B>
00074     const T&
00075     DeferrerDebugPart<T, B>::cast (const void *ptr) const
00076     {
00077       precondition (is_valid_);
00078       return *reinterpret_cast<const T*> (ptr);
00079     }
00080 
00081   
00082 
00083 
00084 
00085     template <class T>
00086     DeferrerDebugPart<T, false>::DeferrerDebugPart (void*, bool)
00087     {
00088     }
00089 
00090     template <class T>
00091     void
00092     DeferrerDebugPart<T, false>::set_valid (bool)
00093     {
00094     }
00095 
00096     template <class T>
00097     T&
00098     DeferrerDebugPart<T, false>::cast (void *ptr)
00099     {
00100       return *reinterpret_cast<T*> (ptr);
00101     }
00102 
00103     template <class T>
00104     const T&
00105     DeferrerDebugPart<T, false>::cast (const void *ptr) const
00106     {
00107       return *reinterpret_cast<const T*> (ptr);
00108     }
00109 
00110   
00111 
00112 
00113 
00114     template <class T, bool rt_checks>
00115     Deferrer<T, rt_checks>::Deferrer () : DeferrerDebugPart<T, rt_checks> (data)
00116     {
00117     }
00118 
00119     template <class T, bool rt_checks>
00120     Deferrer<T, rt_checks>&
00121     Deferrer<T, rt_checks>::operator= (const deferred_type& rhs)
00122     {
00123       new (data) deferred_type (rhs);
00124       this->set_valid (true);
00125       return *this;
00126     }
00127 
00128     template <class T, bool rt_checks>
00129     Deferrer<T, rt_checks>::operator T () const
00130     {
00131       return cast (data);
00132     }
00133 
00135   } 
00136 } 
00137 
00138 #endif // ! VCSN_MISC_DEFERRER_HXX