Vaucanson  1.4.1
deferrer.hxx
Go to the documentation of this file.
1 // deferrer.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2004, 2005, 2006, 2008, 2011 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_MISC_DEFERRER_HXX
18 # define VCSN_MISC_DEFERRER_HXX
19 
33 
34 # include <new>
35 # include <cstdlib>
36 # include <cstring>
37 
38 namespace vcsn
39 {
40  namespace misc
41  {
42 
45  /*-----------------------------------------------.
46  | DeferrerDebugPart, with runtime tests enabled |
47  `-----------------------------------------------*/
48 
49  template <class T, bool B>
50  DeferrerDebugPart<T, B>::DeferrerDebugPart (void* ptr, bool is_valid) :
51  is_valid_ (is_valid)
52  {
53  memset (ptr, 0, sizeof (T));
54  }
55 
56  template <class T, bool B>
57  void
59  {
60  if (is_valid_)
61  WARNING ("Modifying a valid Deferrer<T, true>.");
62  is_valid_ = b;
63  }
64 
65  template <class T, bool B>
66  T&
67  DeferrerDebugPart<T, B>::cast (void *ptr)
68  {
69  precondition (is_valid_);
70  return *reinterpret_cast<T*> (ptr);
71  }
72 
73  template <class T, bool B>
74  const T&
75  DeferrerDebugPart<T, B>::cast (const void *ptr) const
76  {
77  precondition (is_valid_);
78  return *reinterpret_cast<const T*> (ptr);
79  }
80 
81  /*-------------------------------------------------.
82  | DeferrerDebugPart, with runtime tests *disabled* |
83  `-------------------------------------------------*/
84 
85  template <class T>
87  {
88  }
89 
90  template <class T>
91  void
92  DeferrerDebugPart<T, false>::set_valid (bool)
93  {
94  }
95 
96  template <class T>
97  T&
98  DeferrerDebugPart<T, false>::cast (void *ptr)
99  {
100  return *reinterpret_cast<T*> (ptr);
101  }
102 
103  template <class T>
104  const T&
105  DeferrerDebugPart<T, false>::cast (const void *ptr) const
106  {
107  return *reinterpret_cast<const T*> (ptr);
108  }
109 
110  /*---------.
111  | Deferrer |
112  `---------*/
113 
114  template <class T, bool rt_checks>
116  {
117  }
118 
119  template <class T, bool rt_checks>
121  Deferrer<T, rt_checks>::operator= (const deferred_type& rhs)
122  {
123  new (data) deferred_type (rhs);
124  this->set_valid (true);
125  return *this;
126  }
127 
128  template <class T, bool rt_checks>
130  {
131  return this->cast (data);
132  }
133 
135  } // End of namespace misc.
136 } // End of namespace vcsn.
137 
138 #endif // ! VCSN_MISC_DEFERRER_HXX