• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #ifndef BOOST_CONTRACT_TEST_CONSTRUCTOR_DECL_HPP_
3 #define BOOST_CONTRACT_TEST_CONSTRUCTOR_DECL_HPP_
4 
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
9 
10 // Test with and without pre, post, and inv declarations.
11 
12 #include "../detail/oteststream.hpp"
13 #include <boost/contract/constructor.hpp>
14 #include <boost/contract/base_types.hpp>
15 #include <boost/contract/check.hpp>
16 #include <boost/contract/assert.hpp>
17 
18 boost::contract::test::detail::oteststream out;
19 
20 bool c_pre = true, c_post = true;
21 bool c_entering_static_inv = true, c_entry_static_inv = true,
22         c_exit_static_inv = true;
23 bool c_exit_inv = true; // Only exit non-static inv for ctors.
24 struct c
25     #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
26         : private boost::contract::constructor_precondition<c>
27     #endif
28 {
29     #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
static_invariantc30         static void static_invariant() {
31             out << "c::static_inv" << std::endl;
32             if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv);
33             else BOOST_CONTRACT_ASSERT(c_exit_static_inv);
34             c_entering_static_inv = false;
35         }
36     #endif
37     #ifndef BOOST_CONTRACT_TEST_NO_C_INV
invariantc38         void invariant() const {
39             out << "c::inv" << std::endl;
40             BOOST_CONTRACT_ASSERT(c_exit_inv);
41         }
42     #endif
43 
cc44     c()
45         #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
46             : boost::contract::constructor_precondition<c>([] {
47                 out << "c::ctor::pre" << std::endl;
48                 BOOST_CONTRACT_ASSERT(c_pre);
49             })
50         #endif
51     {
52         boost::contract::check c = boost::contract::constructor(this)
__anondb49fb300202null53             .old([] { out << "c::ctor::old" << std::endl; })
54             #ifndef BOOST_CONTRACT_TEST_NO_C_POST
__anondb49fb300302null55                 .postcondition([] {
56                     out << "c::ctor::post" << std::endl;
57                     BOOST_CONTRACT_ASSERT(c_post);
58                 })
59             #endif
60         ;
61         out << "c::ctor::body" << std::endl;
62     }
63 };
64 
65 bool b_pre = true, b_post = true;
66 bool b_entering_static_inv = true, b_entry_static_inv = true,
67         b_exit_static_inv = true;
68 bool b_exit_inv = true; // Only exit non-static inv for ctors.
69 struct b
70     #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
71         #define BASES \
72             private boost::contract::constructor_precondition<b>, public c
73     #else
74         #define BASES public c
75     #endif
76     : BASES
77 {
78     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
79     #undef BASES
80 
81     #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
static_invariantb82         static void static_invariant() {
83             out << "b::static_inv" << std::endl;
84             if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv);
85             else BOOST_CONTRACT_ASSERT(b_exit_static_inv);
86             b_entering_static_inv = false;
87         }
88     #endif
89     #ifndef BOOST_CONTRACT_TEST_NO_B_INV
invariantb90         void invariant() const {
91             out << "b::inv" << std::endl;
92             BOOST_CONTRACT_ASSERT(b_exit_inv);
93         }
94     #endif
95 
bb96     b()
97         #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
98             : boost::contract::constructor_precondition<b>([] {
99                 out << "b::ctor::pre" << std::endl;
100                 BOOST_CONTRACT_ASSERT(b_pre);
101             })
102         #endif
103     {
104         boost::contract::check c = boost::contract::constructor(this)
__anondb49fb300502null105             .old([] { out << "b::ctor::old" << std::endl; })
106             #ifndef BOOST_CONTRACT_TEST_NO_B_POST
__anondb49fb300602null107                 .postcondition([] {
108                     out << "b::ctor::post" << std::endl;
109                     BOOST_CONTRACT_ASSERT(b_post);
110                 })
111             #endif
112         ;
113         out << "b::ctor::body" << std::endl;
114     }
115 };
116 
117 bool a_pre = true, a_post = true;
118 bool a_entering_static_inv = true, a_entry_static_inv = true,
119         a_exit_static_inv = true;
120 bool a_exit_inv = true; // Only exit non-static inv for ctors.
121 struct a
122     #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
123         #define BASES \
124             private boost::contract::constructor_precondition<a>, public b
125     #else
126         #define BASES public b
127     #endif
128     : BASES
129 {
130     typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
131     #undef BASES
132 
133     #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
static_invarianta134         static void static_invariant() {
135             out << "a::static_inv" << std::endl;
136             if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv);
137             else BOOST_CONTRACT_ASSERT(a_exit_static_inv);
138             a_entering_static_inv = false;
139         }
140     #endif
141     #ifndef BOOST_CONTRACT_TEST_NO_A_INV
invarianta142         void invariant() const {
143             out << "a::inv" << std::endl;
144             BOOST_CONTRACT_ASSERT(a_exit_inv);
145         }
146     #endif
147 
aa148     a()
149         #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
150             : boost::contract::constructor_precondition<a>([] {
151                 out << "a::ctor::pre" << std::endl;
152                 BOOST_CONTRACT_ASSERT(a_pre);
153             })
154         #endif
155     {
156         boost::contract::check c = boost::contract::constructor(this)
__anondb49fb300802null157             .old([] { out << "a::ctor::old" << std::endl; })
158             #ifndef BOOST_CONTRACT_TEST_NO_A_POST
__anondb49fb300902null159                 .postcondition([] {
160                     out << "a::ctor::post" << std::endl;
161                     BOOST_CONTRACT_ASSERT(a_post);
162                 })
163             #endif
164         ;
165         out << "a::ctor::body" << std::endl;
166     }
167 };
168 
169 #endif // #include guard
170 
171