2 #ifndef BOOST_CONTRACT_CALL_IF_HPP_
3 #define BOOST_CONTRACT_CALL_IF_HPP_
5 // Copyright (C) 2008-2018 Lorenzo Caminiti
6 // Distributed under the Boost Software License, Version 1.0 (see accompanying
7 // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
8 // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
11 Statically disable compilation and execution of functor calls.
13 @note These facilities allow to emulate C++17 <c>if constexpr</c> statements
14 when used together with functor templates (and C++14 generic lambdas).
15 Therefore, they are not useful on C++17 compilers where
16 <c> if constexpr</c> can be directly used instead.
19 #include <boost/contract/detail/none.hpp>
20 #include <boost/make_shared.hpp>
21 #include <boost/shared_ptr.hpp>
22 #include <boost/utility/enable_if.hpp>
23 #include <boost/config.hpp>
29 // Boost.ResultOf not always able to deduce lambda result type (on MSVC).
30 #ifndef BOOST_NO_CXX11_DECLTYPE
31 #include <boost/utility/declval.hpp>
32 #define BOOST_CONTRACT_CALL_IF_RESULT_OF_(F) \
33 decltype(boost::declval<F>()())
35 #include <boost/utility/result_of.hpp>
36 #define BOOST_CONTRACT_CALL_IF_RESULT_OF_(F) \
37 typename boost::result_of<F()>::type
44 namespace boost { namespace contract {
47 Select compilation and execution of functor template calls using a static
48 boolean predicate (not needed on C++17 compilers, use <c>if constexpr</c>
51 This class template has no members because it is never used directly, it is only
52 used via its specializations.
53 Usually this class template is instantiated only via the return value of
54 @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
56 @see @RefSect{extras.assertion_requirements__templates_,
57 Assertion Requirements}
59 @tparam Pred Static boolean predicate that selects which functor template
60 call to compile and execute.
61 @tparam Then Type of the functor template to call if the static predicate
63 @tparam ThenResult Return type of then-branch functor template call (this is
64 usually automatically deduced by this library so it is never explicitly
65 specified by the user, and that is why it is often marked as
66 @c internal_type in this documentation).
68 template<bool Pred, typename Then, typename ThenResult =
69 #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
70 boost::contract::detail::none
75 struct call_if_statement {}; // Empty so cannot be used (but copyable).
78 Template specialization to dispatch between then-branch functor template calls
79 that return void and the ones that return non-void (not needed on C++17
80 compilers, use <c>if constexpr</c> instead).
83 The base class is a call-if statement so the else and else-if statements can be
85 Usually this class template is instantiated only via the return value of
86 @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
88 @note The <c>result_of<Then()>::type</c> expression needs be evaluated only
89 when the static predicate is already checked to be @c true (because
90 @c Then() is required to compile only in that case).
91 Thus, this template specialization introduces an extra level of
92 indirection necessary for proper lazy evaluation of this result-of
95 @see @RefSect{extras.assertion_requirements__templates_,
96 Assertion Requirements}
98 @tparam Then Type of functor template to call when the static predicate is
99 @c true (as it is for this template specialization).
101 template<typename Then>
102 struct call_if_statement<true, Then,
103 #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
104 boost::contract::detail::none
109 call_if_statement<true, Then,
110 #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
111 BOOST_CONTRACT_CALL_IF_RESULT_OF_(Then)
113 typename result_of<Then()>::type
116 { // Copyable (as its base).
118 Construct this object with the then-branch functor template.
120 @param f Then-branch nullary functor template.
121 The functor template call @c f() is compiled and called for this
122 template specialization (because the if-statement static
123 predicate is @c true).
124 The return type of @c f() must be the same as (or implicitly
125 convertible to) the return type of all other functor template
126 calls specified for this call-if object.
128 explicit call_if_statement(Then f) : call_if_statement<true, Then,
129 BOOST_CONTRACT_CALL_IF_RESULT_OF_(Then)>(f) {}
133 Template specialization to handle static predicates that are @c true for
134 then-branch functor template calls that do not return void (not needed on C++17
135 compilers, use <c>if constexpr</c> instead).
138 Usually this class template is instantiated only via the return value of
139 @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
141 @see @RefSect{extras.assertion_requirements__templates_,
142 Assertion Requirements}
144 @tparam Then Type of functor template to call when the static predicate is
145 @c true (as it is for this template specialization).
146 @tparam ThenResult Non-void return type of the then-branch functor template
149 template<typename Then, typename ThenResult>
150 struct call_if_statement<true, Then, ThenResult> { // Copyable (as *).
152 Construct this object with the then-branch functor template.
154 @param f Then-branch nullary functor template.
155 The functor template call @c f() is actually compiled and
156 executed for this template specialization (because the
157 if-statement static predicate is @c true).
158 The return type of @c f() must be the same as (or implicitly
159 convertible to) the @p ThenResult type.
161 explicit call_if_statement(Then f) :
162 r_(boost::make_shared<ThenResult>(f())) {}
165 This implicit type conversion returns a copy of the value returned by the
166 call to the then-branch functor template.
168 operator ThenResult() const { return *r_; }
171 Specify the else-branch functor template.
173 @param f Else-branch nullary functor template.
174 The functor template call @c f() is never compiled or executed
175 for this template specialization (because the if-statement
176 static predicate is @c true).
177 The return type of @c f() must be the same as (or implicitly
178 convertible to) the @p ThenResult type.
180 @return A copy of the value returned by the call to the then-branch functor
181 template (because the else-branch functor template call is not
184 template<typename Else>
185 ThenResult else_(Else const&
186 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
189 ) const { return *r_; }
192 Specify an else-if-branch functor template (using a static boolean
195 @param f Else-if-branch nullary functor template.
196 The functor template call @c f() is never compiled or executed
197 for this template specialization (because the if-statement
198 static predicate is @c true).
199 The return type of @c f() must be the same as (or implicitly
200 convertible to) the @p ThenResult type.
202 @tparam ElseIfPred Static boolean predicate selecting which functor
203 template call to compile and execute.
205 @return A call-if statement so the else statement and additional else-if
206 statements can be specified if needed.
207 Eventually, it will be the return value of the then-branch functor
208 template call for this template specialization (because the
209 if-statement static predicate is @c true).
211 template<bool ElseIfPred, typename ElseIfThen>
212 call_if_statement<true, Then, ThenResult> else_if_c(
214 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
217 ) const { return *this; }
220 Specify an else-if-branch functor template (using a nullary boolean
223 @param f Else-if-branch nullary functor template.
224 The functor template call @c f() is never compiled or executed
225 for this template specialization (because the if-statement
226 static predicate is @c true).
227 The return type of @c f() must be the same as (or implicitly
228 convertible to) the @p ThenResult type.
230 @tparam ElseIfPred Nullary boolean meta-function selecting which functor
231 template call to compile and execute.
233 @return A call-if statement so the else statement and additional else-if
234 statements can be specified if needed.
235 Eventually, it will be the return value of the then-branch functor
236 template call for this template specialization (because the
237 if-statement static predicate is @c true).
239 template<class ElseIfPred, typename ElseIfThen>
240 call_if_statement<true, Then, ThenResult> else_if(
242 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
245 ) const { return *this; }
248 boost::shared_ptr<ThenResult> r_;
252 Template specialization to handle static predicates that are @c true for
253 then-branch functor template calls that return void (not needed on C++17
254 compilers, use <c>if constexpr</c> instead).
257 Usually this class template is instantiated only via the return value of
258 @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
260 @see @RefSect{extras.assertion_requirements__templates_,
261 Assertion Requirements}
263 @tparam Then Type of functor template to call when the static predicate if
264 @c true (as it is for this template specialization).
266 template<typename Then>
267 struct call_if_statement<true, Then, void> { // Copyable (no data).
269 Construct this object with the then-branch functor template.
271 @param f Then-branch nullary functor template.
272 The functor template call @c f() is actually compiled and
273 executed for this template specialization (because the
274 if-statement static predicate is @c true).
275 The return type of @c f() must be @c void for this template
276 specialization (because the then-branch functor template calls
279 explicit call_if_statement(Then f) { f(); }
281 // Cannot provide `operator ThenResult()` here, because ThenResult is void.
284 Specify the else-branch functor template.
286 @param f Else-branch nullary functor template.
287 The functor template call @c f() is never compiled or executed
288 for this template specialization (because the if-statement
289 static predicate is @c true).
290 The return type of @c f() must be @c void for this template
291 specialization (because the then-branch functor template calls
294 template<typename Else>
295 void else_(Else const&
296 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
302 Specify an else-if-branch functor template (using a static boolean
305 @param f Else-if-branch nullary functor template.
306 The functor template call @c f() is never compiled or executed
307 for this template specialization (because the if-statement
308 static predicate is @c true).
309 The return type of @c f() must be @c void for this template
310 specialization (because the then-branch functor template calls
313 @tparam ElseIfPred Static boolean predicate selecting which functor
314 template call to compile and execute.
316 @return A call-if statement so the else statement and additional else-if
317 statements can be specified if needed.
318 Eventually, it will return void for this template specialization
319 (because the then-branch functor template calls return void).
321 template<bool ElseIfPred, typename ElseIfThen>
322 call_if_statement<true, Then, void> else_if_c(
324 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
327 ) const { return *this; }
330 Specify an else-if-branch functor template (using a nullary boolean
333 @param f Else-if-branch nullary functor template.
334 The functor template call @c f() is never compiled or executed
335 for this template specialization (because the if-statement
336 static predicate is @c true).
337 The return type of @c f() must be @c void for this template
338 specialization (because the then-branch functor template calls
341 @tparam ElseIfPred Nullary boolean meta-function selecting which functor
342 template call to compile and execute.
344 @return A call-if statement so the else statement and additional else-if
345 statements can be specified if needed.
346 Eventually, it will return void for this template specialization
347 (because the then-branch functor template calls return void).
349 template<class ElseIfPred, typename ElseIfThen>
350 call_if_statement<true, Then, void> else_if(
352 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
355 ) const { return *this; }
359 Template specialization to handle static predicates that are @c false (not
360 needed on C++17 compilers, use <c>if constexpr</c> instead).
362 This template specialization handles all else-branch functor template calls
363 (whether they return void or not).
364 Usually this class template is instantiated only via the return value of
365 @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
367 @see @RefSect{extras.assertion_requirements__templates_,
368 Assertion Requirements}
370 @tparam Then Type of functor template to call when the static predicate is
371 @c true (never the case for this template specialization).
373 template<typename Then> // Copyable (no data).
374 struct call_if_statement<false, Then,
375 #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
376 boost::contract::detail::none
382 Construct this object with the then-branch functor template.
384 @param f Then-branch nullary functor template.
385 The functor template call @c f() is never compiled or executed
386 for this template specialization (because the if-statement
387 static predicate is @c false).
388 The return type of @c f() must be the same as (or implicitly
389 convertible to) the return type of all other functor template
390 calls specified for this call-if object.
392 explicit call_if_statement(Then const&
393 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
398 // Do not provide `operator result_type()` here, require else_ instead.
401 Specify the else-branch functor template.
403 @note The <c>result_of<Else()>::type</c> expression needs be evaluated
404 only when the static predicate is already checked to be @c false
405 (because @c Else() is required to compile only in that case).
406 Thus, this result-of expression is evaluated lazily and only in
407 instantiations of this template specialization.
409 @param f Else-branch nullary functor template.
410 The functor template call @c f() is actually compiled and
411 executed for this template specialization (because the
412 if-statement static predicate is @c false).
413 The return type of @c f() must be the same as (or implicitly
414 convertible to) the return type of all other functor template
415 calls specified for this call-if object.
417 @return A copy of the value returned by the call to the else-branch functor
420 template<typename Else>
421 #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
422 BOOST_CONTRACT_CALL_IF_RESULT_OF_(Else)
424 typename result_of<Else()>::type
426 else_(Else f) const { return f(); }
429 Specify an else-if-branch functor template (using a static boolean
432 @param f Else-if-branch nullary functor template.
433 The functor template call @c f() is actually compiled and
434 executed if and only if @c ElseIfPred is @c true (because the
435 if-statement static predicate is already @c false for this
436 template specialization).
437 The return type of @c f() must be the same as (or implicitly
438 convertible to) the return type of all other functor template
439 calls specified for this call-if object.
441 @tparam ElseIfPred Static boolean predicate selecting which functor
442 template call to compile and execute.
444 @return A call-if statement so the else statement and additional else-if
445 statements can be specified if needed.
446 Eventually, this will be the return value of the one functor
447 template call being compiled and executed.
449 template<bool ElseIfPred, typename ElseIfThen>
450 call_if_statement<ElseIfPred, ElseIfThen> else_if_c(ElseIfThen f) const {
451 return call_if_statement<ElseIfPred, ElseIfThen>(f);
455 Specify an else-if-branch functor template (using a nullary boolen
458 @param f Else-if-branch nullary functor template.
459 The functor template call @c f() is actually compiled and
460 executed if and only if @c ElseIfPred::value is @c true (because
461 the if-statement static predicate is already @c false for this
462 template specialization).
463 The return type of @c f() must be the same as (or implicitly
464 convertible to) the return type of all other functor template
465 calls specified for this call-if object.
467 @tparam ElseIfPred Nullary boolean meta-function selecting which functor
468 template call to compile and execute.
470 @return A call-if statement so the else statement and additional else-if
471 statements can be specified if needed.
472 Eventually, this will be the return value of the one functor
473 template call being compiled and executed.
475 template<class ElseIfPred, typename ElseIfThen>
476 call_if_statement<ElseIfPred::value, ElseIfThen> else_if(ElseIfThen f)
478 return call_if_statement<ElseIfPred::value, ElseIfThen>(f);
483 Select compilation and execution of functor template calls using a static
484 boolean predicate (not needed on C++17 compilers, use <c>if constexpr</c>
487 Create a call-if object with the specified then-branch functor template:
490 boost::contract::call_if_c<Pred1>(
491 then_functor_template1
492 ).template else_if_c<Pred2>( // Optional.
493 then_functor_template2
494 ) // Optionally, other `else_if_c` or
496 .else_( // Optional for `void` functors,
497 else_functor_template // but required for non `void`.
501 Optional functor templates for else-if-branches and the else-branch can be
502 specified as needed (the else-branch function template is required if @c f
505 @see @RefSect{extras.assertion_requirements__templates_,
506 Assertion Requirements}
508 @param f Then-branch nullary functor template.
509 The functor template call @c f() is compiled and executed if and
510 only if @c Pred is @c true.
511 The return type of other functor template calls specified for this
512 call-if statement (else-branch, else-if-branches, etc.) must be the
513 same as (or implicitly convertible to) the return type of
514 then-branch functor call @c f().
516 @tparam Pred Static boolean predicate selecting which functor template call
517 to compile and execute.
519 @return A call-if statement so else and else-if statements can be specified if
521 Eventually, this will be the return value of the one functor template
522 call being compiled and executed (which could also be @c void).
524 template<bool Pred, typename Then>
525 call_if_statement<Pred, Then> call_if_c(Then f) {
526 return call_if_statement<Pred, Then>(f);
530 Select compilation and execution of functor template calls using a nullary
531 boolean meta-function (not needed on C++17 compilers, use <c>if constexpr</c>
534 This is equivalent to <c>boost::contract::call_if_c<Pred::value>(f)</c>.
535 Create a call-if object with the specified then-branch functor template:
538 boost::contract::call_if<Pred1>(
539 then_functor_template1
540 ).template else_if<Pred2>( // Optional.
541 then_functor_template2
542 ) // Optionally, other `else_if` or
544 .else_( // Optional for `void` functors,
545 else_functor_template // but required for non `void`.
549 Optional functor templates for else-if-branches and the else-branch can be
550 specified as needed (the else-branch functor template is required if @c f
553 @see @RefSect{extras.assertion_requirements__templates_,
554 Assertion Requirements}
556 @param f Then-branch nullary functor template.
557 The functor template call @c f() is compiled and executed if and
558 only if @c Pred::value is @c true.
559 The return type of other functor template calls specified for this
560 call-if statement (else-branch, else-if-branches, etc.) must be the
561 same as (or implicitly convertible to) the return type of
562 then-branch functor template call @c f().
564 @tparam Pred Nullary boolean meta-function selecting which functor template
565 call to compile and execute.
567 @return A call-if statement so else and else-if statements can be specified if
569 Eventually, this will be the return value of the one functor template
570 call being compiled and executed (which could also be @c void).
572 template<class Pred, typename Then>
573 call_if_statement<Pred::value, Then> call_if(Then f) {
574 return call_if_statement<Pred::value, Then>(f);
578 Select compilation and execution of a boolean functor template condition using a
579 static boolean predicate (not needed on C++17 compilers, use
580 <c>if constexpr</c> instead).
582 Compile and execute the nullary boolean functor template call @c f() if and only
583 if the specified static boolean predicate @p Pred is @c true, otherwise
584 trivially return @p else_ (@c true by default) at run-time.
586 A call to <c>boost::contract::condition_if_c<Pred>(f, else_)</c> is logically
587 equivalent to <c>boost::contract::call_if_c<Pred>(f, [] { return else_; })</c>
588 (but its internal implementation is optimized and it does not actually use
591 @see @RefSect{extras.assertion_requirements__templates_,
592 Assertion Requirements}
594 @param f Nullary boolean functor template.
595 The functor template call @c f() is compiled and executed if and
596 only if @c Pred is @c true.
598 @tparam Pred Static boolean predicate selecting when the functor template
599 call @c f() should be compiled and executed.
600 @param else_ Boolean value to return when @c Pred is @c false (instead of
601 compiling and executing the functor template call @c f()).
603 @return Boolean value returned by @c f() if the static predicate @c Pred is
604 @c true. Otherwise, trivially return @p else_.
606 #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
607 template<bool Pred, typename Then>
608 bool condition_if_c(Then f, bool else_ = true);
610 // NOTE: condition_if is a very simple special case of call_if so it can be
611 // trivially implemented using enable_if instead of call_if as done below.
613 template<bool Pred, typename Then>
614 typename boost::enable_if_c<Pred, bool>::type
615 condition_if_c(Then f, bool /* else_ */ = true) { return f(); }
617 template<bool Pred, typename Then>
618 typename boost::disable_if_c<Pred, bool>::type
619 condition_if_c(Then /* f */, bool else_ = true) { return else_; }
623 Select compilation and execution of a boolean functor template condition using a
624 nullary boolean meta-function (not needed on C++17 compilers, use
625 <c>if constexpr</c> instead).
627 This is equivalent to
628 <c>boost::contract::condition_if_c<Pred::value>(f, else_)</c>.
629 Compile and execute the nullary boolean functor template call @c f() if and only
630 if the specified nullary boolean meta-function @p Pred::value is @c true,
631 otherwise trivially return @p else_ (@c true by default) at run-time.
633 @see @RefSect{extras.assertion_requirements__templates_,
634 Assertion Requirements}
636 @param f Nullary boolean functor template.
637 The functor template call @c f() is compiled and executed if and
638 only if @c Pred::value is @c true.
639 @param else_ Boolean value to return when @c Pred::value is @c false (instead
640 of compiling and executing the functor template call @c f()).
642 @tparam Pred Nullary boolean meta-function selecting when the functor
643 template call @c f() should be compiled and executed.
645 @return Boolean value returned by @c f() if the static predicate @c Pred::value
646 is @c true. Otherwise, trivially return @p else_.
648 template<class Pred, typename Then>
649 bool condition_if(Then f, bool else_ = true) {
650 return condition_if_c<Pred::value>(f, else_);
655 #endif // #include guard