Vaucanson 1.4
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, 2006, 2008, 2011 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 # include <cstring>
00037 
00038 namespace vcsn
00039 {
00040   namespace misc
00041   {
00042 
00045   /*-----------------------------------------------.
00046   | DeferrerDebugPart, with runtime tests enabled  |
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   | DeferrerDebugPart, with runtime tests *disabled* |
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   | Deferrer |
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 this->cast (data);
00132     }
00133 
00135   } // End of namespace misc.
00136 } // End of namespace vcsn.
00137 
00138 #endif // ! VCSN_MISC_DEFERRER_HXX