deferrer.hxx

Go to the documentation of this file.
00001 // deferrer.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2004, 2005 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
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 utility
00038 {
00039 
00042   /*-----------------------------------------------.
00043   | DeferrerDebugPart, with runtime tests enabled  |
00044   `-----------------------------------------------*/
00045 
00046   template <class T, bool B>
00047   DeferrerDebugPart<T, B>::DeferrerDebugPart(void* ptr, bool is_valid) :
00048     is_valid_ (is_valid)
00049   {
00050     memset(ptr, 0, sizeof(T));
00051   }
00052 
00053   template <class T, bool B>
00054   void
00055   DeferrerDebugPart<T, B>::set_valid(bool b)
00056   {
00057     if (is_valid_)
00058       warning("Modifying a valid Deferrer<T, true>.");
00059     is_valid_ = b;
00060   }
00061 
00062   template <class T, bool B>
00063   T&
00064   DeferrerDebugPart<T, B>::cast(void *ptr)
00065   {
00066     precondition(is_valid_);
00067     return *reinterpret_cast<T*> (ptr);
00068   }
00069 
00070   template <class T, bool B>
00071   const T&
00072   DeferrerDebugPart<T, B>::cast(const void *ptr) const
00073   {
00074     precondition(is_valid_);
00075     return *reinterpret_cast<const T*> (ptr);
00076   }
00077 
00078   /*-------------------------------------------------.
00079   | DeferrerDebugPart, with runtime tests *disabled* |
00080   `-------------------------------------------------*/
00081 
00082   template <class T>
00083   DeferrerDebugPart<T, false>::DeferrerDebugPart(void*, bool)
00084   {
00085   }
00086 
00087   template <class T>
00088   void
00089   DeferrerDebugPart<T, false>::set_valid(bool)
00090   {
00091   }
00092 
00093   template <class T>
00094   T&
00095   DeferrerDebugPart<T, false>::cast(void *ptr)
00096   {
00097     return *reinterpret_cast<T*> (ptr);
00098   }
00099 
00100   template <class T>
00101   const T&
00102   DeferrerDebugPart<T, false>::cast(const void *ptr) const
00103   {
00104     return *reinterpret_cast<const T*> (ptr);
00105   }
00106 
00107   /*---------.
00108   | Deferrer |
00109   `---------*/
00110 
00111   template <class T, bool rt_checks>
00112   Deferrer<T, rt_checks>::Deferrer() : DeferrerDebugPart<T, rt_checks> (data)
00113   {
00114   }
00115 
00116   template <class T, bool rt_checks>
00117   Deferrer<T, rt_checks>&
00118   Deferrer<T, rt_checks>::operator = (const deferred_type& rhs)
00119   {
00120     new (data) deferred_type (rhs);
00121     this->set_valid(true);
00122     return *this;
00123   }
00124 
00125   template <class T, bool rt_checks>
00126   Deferrer<T, rt_checks>::operator T () const
00127   {
00128     return cast(data);
00129   }
00130 
00132 } // End of namespace utility.
00133 
00134 #endif // ! VCSN_MISC_DEFERRER_HXX

Generated on Fri Jul 28 12:18:30 2006 for Vaucanson by  doxygen 1.4.6