1 // Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
26 mixin_counter() { ++counter; }
27 mixin_counter(mixin_counter const&) { ++counter; }
28 ~mixin_counter() { --counter; }
31 struct value_type : private mixin_counter
37 throwing_construction,
39 throwing_copy_assignment,
41 throwing_move_assignment,
45 value_type() = default;
47 explicit value_type(state_type state_)
50 throw_if(throwing_construction);
53 value_type(value_type const& other)
56 throw_if(throwing_copy);
60 operator=(value_type const& other)
63 throw_if(throwing_copy_assignment);
67 value_type(value_type&& other)
70 other.state = moved_from;
71 throw_if(throwing_move);
75 operator=(value_type&& other)
78 other.state = moved_from;
79 throw_if(throwing_move_assignment);
83 void throw_if(state_type match)
92 state_type state = zero;
98 using O = gdb::optional<value_type>;
99 using S = value_type::state_type;
100 auto const make = [](S s = S::zero) { return O { gdb::in_place, s }; };
102 enum outcome_type { nothrow, caught, bad_catch };
104 // Check copy/move assignment for disengaged optional
106 // From disengaged optional
134 // From engaged optional
138 O p = make(S::throwing_copy_assignment);
140 VERIFY( o && o->state == S::throwing_copy_assignment );
141 VERIFY( p && p->state == S::throwing_copy_assignment );
147 O p = make(S::throwing_move_assignment);
149 VERIFY( o && o->state == S::throwing_move_assignment );
150 VERIFY( p && p->state == S::moved_from );
154 outcome_type outcome {};
157 O p = make(S::throwing_copy);
163 catch(exception const&)
164 { outcome = caught; }
166 { outcome = bad_catch; }
168 VERIFY( outcome == caught );
170 VERIFY( p && p->state == S::throwing_copy );
174 outcome_type outcome {};
177 O p = make(S::throwing_move);
183 catch(exception const&)
184 { outcome = caught; }
186 { outcome = bad_catch; }
188 VERIFY( outcome == caught );
190 VERIFY( p && p->state == S::moved_from );
193 VERIFY( counter == 0 );
196 } // namespace assign_1