@@ -119,6 +119,7 @@ protected:
119
119
# include < __locale>
120
120
# include < __type_traits/is_same.h>
121
121
# include < __utility/is_valid_range.h>
122
+ # include < __utility/scope_guard.h>
122
123
# include < climits>
123
124
# include < ios>
124
125
# include < iosfwd>
@@ -178,18 +179,27 @@ public:
178
179
// Get and put areas:
179
180
// 27.6.2.2.3 Get area:
180
181
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail () {
182
+ __check_invariants ();
183
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
184
+
181
185
if (gptr () < egptr ())
182
186
return static_cast <streamsize>(egptr () - gptr ());
183
187
return showmanyc ();
184
188
}
185
189
186
190
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc () {
191
+ __check_invariants ();
192
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
193
+
187
194
if (sbumpc () == traits_type::eof ())
188
195
return traits_type::eof ();
189
196
return sgetc ();
190
197
}
191
198
192
199
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc () {
200
+ __check_invariants ();
201
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
202
+
193
203
if (gptr () == egptr ())
194
204
return uflow ();
195
205
int_type __c = traits_type::to_int_type (*gptr ());
@@ -198,6 +208,9 @@ public:
198
208
}
199
209
200
210
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc () {
211
+ __check_invariants ();
212
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
213
+
201
214
if (gptr () == egptr ())
202
215
return underflow ();
203
216
return traits_type::to_int_type (*gptr ());
@@ -207,13 +220,19 @@ public:
207
220
208
221
// 27.6.2.2.4 Putback:
209
222
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputbackc (char_type __c) {
223
+ __check_invariants ();
224
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
225
+
210
226
if (eback () == gptr () || !traits_type::eq (__c, *(gptr () - 1 )))
211
227
return pbackfail (traits_type::to_int_type (__c));
212
228
this ->gbump (-1 );
213
229
return traits_type::to_int_type (*gptr ());
214
230
}
215
231
216
232
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sungetc () {
233
+ __check_invariants ();
234
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
235
+
217
236
if (eback () == gptr ())
218
237
return pbackfail ();
219
238
this ->gbump (-1 );
@@ -222,6 +241,9 @@ public:
222
241
223
242
// 27.6.2.2.5 Put area:
224
243
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputc (char_type __c) {
244
+ __check_invariants ();
245
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
246
+
225
247
if (pptr () == epptr ())
226
248
return overflow (traits_type::to_int_type (__c));
227
249
*pptr () = __c;
@@ -317,6 +339,9 @@ protected:
317
339
virtual streamsize showmanyc () { return 0 ; }
318
340
319
341
virtual streamsize xsgetn (char_type* __s, streamsize __n) {
342
+ __check_invariants ();
343
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
344
+
320
345
int_type __c;
321
346
streamsize __i = 0 ;
322
347
while (__i < __n) {
@@ -338,6 +363,9 @@ protected:
338
363
339
364
virtual int_type underflow () { return traits_type::eof (); }
340
365
virtual int_type uflow () {
366
+ __check_invariants ();
367
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
368
+
341
369
if (underflow () == traits_type::eof ())
342
370
return traits_type::eof ();
343
371
int_type __c = traits_type::to_int_type (*gptr ());
@@ -350,6 +378,9 @@ protected:
350
378
351
379
// 27.6.2.4.5 Put area:
352
380
virtual streamsize xsputn (const char_type* __s, streamsize __n) {
381
+ __check_invariants ();
382
+ auto __guard = std::__make_scope_guard ([this ] { this ->__check_invariants (); });
383
+
353
384
streamsize __i = 0 ;
354
385
while (__i < __n) {
355
386
if (pptr () >= epptr ()) {
@@ -370,6 +401,15 @@ protected:
370
401
371
402
virtual int_type overflow (int_type = traits_type::eof()) { return traits_type::eof (); }
372
403
404
+ // This function checks some invariants of the class (it isn't exhaustive).
405
+ _LIBCPP_HIDE_FROM_ABI void __check_invariants () const {
406
+ _LIBCPP_ASSERT_INTERNAL (pbase () <= pptr (), " this is an invariant of the class" );
407
+ _LIBCPP_ASSERT_INTERNAL (pptr () <= epptr (), " this is an invariant of the class" );
408
+
409
+ _LIBCPP_ASSERT_INTERNAL (eback () <= gptr (), " this is an invariant of the class" );
410
+ _LIBCPP_ASSERT_INTERNAL (gptr () <= egptr (), " this is an invariant of the class" );
411
+ }
412
+
373
413
private:
374
414
locale __loc_;
375
415
char_type* __binp_ = nullptr ;
0 commit comments