Vaucanson  1.4.1
contract.hh
Go to the documentation of this file.
1 // contract.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 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_CONTRACT_HH
18 # define VCSN_MISC_CONTRACT_HH
19 
25 # ifndef VCSN_NDEBUG
27 # include <vaucanson/misc/static.hh>
28 
29 # include <iostream>
30 # include <string>
31 
32 namespace vcsn {
33  namespace misc {
34  namespace contract {
35 
112 
113  void trap (const char *file, int line,
114  const char *location,
115  const std::string& message);
116 
117  template<typename T>
118  struct fail;
119  }
120  }
121 }
122 
123 # define vcsn_trap_(Message, Cond) \
124  vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (), \
125  std::string (Message) + ": " #Cond)
126 # define vcsn_trap__(Message, Cond, Explanation) \
127  vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (), \
128  std::string (Message) + ": " #Cond " // " + Explanation)
129 # define vcsn_trap_2(Message1, Message2) \
130  vcsn::misc::contract::trap (__FILE__, __LINE__, PRETTY_FUNCTION (), \
131  std::string (Message1) + ": " + Message2)
132 
133 # define assertion(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Assertion failed", Cond))
134 # define precondition(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Precondition failed", Cond))
135 # define postcondition(Cond) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap_ ("Postcondition failed", Cond))
136 
137 # define unreachable(Explanation) vcsn_trap_2 ("Unreachable code reached", Explanation)
138 
139 # define assertion_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Assertion failed", Cond, Explanation_if_false))
140 # define precondition_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Precondition failed", Cond, Explanation_if_false))
141 # define postcondition_(Cond, Explanation_if_false) static_cast<void> ((Cond) ? static_cast<void> (0) : vcsn_trap__ ("Postcondition failed", Cond, Explanation_if_false))
142 
143 # define result_not_computable_if(Cond) static_cast<void> ((Cond) ? vcsn_trap_ ("Result is not computable", Cond) : static_cast<void> (0))
144 # define result_not_computable(Message) vcsn_trap_2 ("Result is not computable", Message)
145 
146 # define pure_service_call(Service) vcsn_trap_ ("Pure abstract service called", Service)
147 
148 # define static_assertion(Cond, Message) \
149  { vcsn::misc::static_if<Cond, int, vcsn::misc::contract::fail<void> >::t Message; Message = 0; }
150 # define static_assertion_(Cond, Message) \
151  { typename vcsn::misc::static_if<Cond, int, vcsn::misc::contract::fail<void> >::t Message; Message = 0; }
152 
153 # define static_error(Message) \
154  { \
155  struct Message; \
156  vcsn::misc::contract::fail<Message> Message; \
157  }
158 
159 # ifndef INTERNAL_CHECKS
160 
161 # define recommendation(Cond) static_cast<void> (0)
162 # define deprecation(Feature) static_cast<void> (0)
163 # define weakness(Feature) static_cast<void> (0)
164 # define incompletion(Feature) static_cast<void> (0)
165 
166 # define WARNING(Message) static_cast<void> (0)
167 
168 # else // ! INTERNAL_CHECKS
169 
170 # ifdef STRICT
171 # define __inconsistency(Message1, Message2) vcsn_trap_2 (Message1, Message2)
172 # else // ! STRICT
173 # define __inconsistency(Message1, Message2) \
174  static_cast<void> (std::cerr << __FILE__ << ':' << __LINE__ << ": " \
175  << Message1 << ": " << Message2 << std::endl)
176 # endif // STRICT
177 
178 # define recommendation(Cond) \
179  static_cast<void> ((Cond) ? \
180  static_cast<void> (0) : \
181  __inconsistency ("Recommendation", #Cond " Failed."))
182 # define deprecation(Feature) __inconsistency ("Deprecated feature", Feature)
183 # define weakness(Feature) __inconsistency ("Weak feature", Feature)
184 # define incompletion(Feature) __inconsistency ("Incomplete implementation", Feature)
185 # define WARNING(Message) __inconsistency ("Warning", Message)
186 
187 # endif // INTERNAL_CHECKS
188 
189 # else // VCSN_NDEBUG
190 
191 # define static_assertion(Cond, Message) typedef void Message
192 # define static_assertion_(Cond, Message) typedef void Message
193 # define static_error(Message) typedef void Message
194 
195 # define assertion(Cond) static_cast<void> (0)
196 # define precondition(Cond) static_cast<void> (0)
197 # define postcondition(Cond) static_cast<void> (0)
198 # define assertion_(Cond, Explanation_if_false) static_cast<void> (0)
199 # define precondition_(Cond, Explanation_if_false) static_cast<void> (0)
200 # define postcondition_(Cond, Explanation_if_false) static_cast<void> (0)
201 
202 # define unreachable(Explanation) static_cast<void> (0)
203 
204 # define result_not_computable_if(Cond) static_cast<void> (0)
205 # define result_not_computable(Message) static_cast<void> (0)
206 
207 # define pure_service_call(Service) static_cast<void> (0)
208 
209 # define recommendation(Cond) static_cast<void> (0)
210 # define deprecation(Feature) static_cast<void> (0)
211 # define weakness(Feature) static_cast<void> (0)
212 # define incompletion(Feature) static_cast<void> (0)
213 
214 # define WARNING(Message) static_cast<void> (0)
215 
216 # endif // ! VCSN_NDEBUG
217 
218 #endif // ! VCSN_MISC_CONTRACT_HH