GDB (xrefs)
1.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8 
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.
13 
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/>.
17 
18 namespace assign_1 {
19 
20 struct exception {};
21 
22 int counter = 0;
23 
25 {
29 };
30 
31 struct value_type : private mixin_counter
32 {
34  {
43  };
44 
45  value_type() = default;
46 
47  explicit value_type(state_type state_)
48  : state(state_)
49  {
51  }
52 
53  value_type(value_type const& other)
54  : state(other.state)
55  {
57  }
58 
59  value_type&
60  operator=(value_type const& other)
61  {
62  state = other.state;
64  return *this;
65  }
66 
68  : state(other.state)
69  {
70  other.state = moved_from;
72  }
73 
74  value_type&
76  {
77  state = other.state;
78  other.state = moved_from;
80  return *this;
81  }
82 
83  void throw_if(state_type match)
84  {
85  if(state == match)
86  {
87  state = threw;
88  throw exception {};
89  }
90  }
91 
93 };
94 
95 void test()
96 {
97  using O = gdb::optional<value_type>;
98  using S = value_type::state_type;
99  auto const make = [](S s = S::zero) { return O { gdb::in_place, s }; };
100 
101  enum outcome_type { nothrow, caught, bad_catch };
102 
103  // Check copy/move assignment for disengaged optional
104 
105  // From disengaged optional
106  {
107  O o;
108  VERIFY( !o );
109  O p;
110  o = p;
111  VERIFY( !o );
112  VERIFY( !p );
113  }
114 
115  {
116  O o;
117  VERIFY( !o );
118  O p;
119  o = std::move(p);
120  VERIFY( !o );
121  VERIFY( !p );
122  }
123 
124 #ifndef GDB_OPTIONAL
125  {
126  O o;
127  VERIFY( !o );
128  o = {};
129  VERIFY( !o );
130  }
131 #endif
132 
133  // From engaged optional
134  {
135  O o;
136  VERIFY( !o );
137  O p = make(S::throwing_copy_assignment);
138  o = p;
139  VERIFY( o && o->state == S::throwing_copy_assignment );
140  VERIFY( p && p->state == S::throwing_copy_assignment );
141  }
142 
143  {
144  O o;
145  VERIFY( !o );
146  O p = make(S::throwing_move_assignment);
147  o = std::move(p);
148  VERIFY( o && o->state == S::throwing_move_assignment );
149  VERIFY( p && p->state == S::moved_from );
150  }
151 
152  {
153  outcome_type outcome {};
154  O o;
155  VERIFY( !o );
156  O p = make(S::throwing_copy);
157 
158  try
159  {
160  o = p;
161  }
162  catch(exception const&)
163  { outcome = caught; }
164  catch(...)
165  { outcome = bad_catch; }
166 
167  VERIFY( outcome == caught );
168  VERIFY( !o );
169  VERIFY( p && p->state == S::throwing_copy );
170  }
171 
172  {
173  outcome_type outcome {};
174  O o;
175  VERIFY( !o );
176  O p = make(S::throwing_move);
177 
178  try
179  {
180  o = std::move(p);
181  }
182  catch(exception const&)
183  { outcome = caught; }
184  catch(...)
185  { outcome = bad_catch; }
186 
187  VERIFY( outcome == caught );
188  VERIFY( !o );
189  VERIFY( p && p->state == S::moved_from );
190  }
191 
192  VERIFY( counter == 0 );
193 }
194 
195 } // namespace assign_1
constexpr gdb::in_place_t in_place
Definition: gdb_optional.h:33
void test()
Definition: 1.cc:95
#define VERIFY
value_type & operator=(value_type &&other)
Definition: 1.cc:75
#define S(reg)
Definition: m32c-tdep.c:793
void throw_if(state_type match)
Definition: 1.cc:83
value_type(state_type state_)
Definition: 1.cc:47
value_type(value_type const &other)
Definition: 1.cc:53
Definition: 1.cc:18
int counter
Definition: 1.cc:22
value_type(value_type &&other)
Definition: 1.cc:67
state_type state
Definition: 1.cc:92
value_type & operator=(value_type const &other)
Definition: 1.cc:60
mixin_counter(mixin_counter const &)
Definition: 1.cc:27