1 // memory standard header
2 #pragma once
3 #ifndef _MEMORY_
4 #define _MEMORY_
5 #ifndef RC_INVOKED
6 #include <iterator>
7 #include <xmemory>
8
9 #ifdef _MSC_VER
10  #pragma pack(push,_CRT_PACKING)
11  #pragma warning(push,3)
12 #endif  /* _MSC_VER */
13 _STD_BEGIN
14
15         // TEMPLATE FUNCTION get_temporary_buffer
16 template<class _Ty> inline
17     pair<_Ty _FARQ *, _PDFT>
18         get_temporary_buffer(_PDFT _Count)
19     {    // get raw temporary buffer of up to _Count elements
20     _Ty _FARQ *_Pbuf;
21
22     if (_Count <= 0)
23         _Count = 0;
24     else if (((size_t)(-1) / _Count) < sizeof (_Ty))
25         _THROW_NCEE(std::bad_alloc, NULL);
26
27     for (_Pbuf = 0; 0 < _Count; _Count /= 2)
28         if ((_Pbuf = (_Ty _FARQ *)operator new(
29             (_SIZT)_Count * sizeof (_Ty), nothrow)) != 0)
30             break;
31
32     return (pair<_Ty _FARQ *, _PDFT>(_Pbuf, _Count));
33     }
34
35         // TEMPLATE FUNCTION return_temporary_buffer
36 template<class _Ty> inline
37     void return_temporary_buffer(_Ty *_Pbuf)
38     {    // delete raw temporary buffer
39     operator delete(_Pbuf);
40     }
41
42         // TEMPLATE FUNCTION uninitialized_copy
43 template<class _InIt,
44     class _FwdIt> inline
45     _FwdIt _Uninit_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
46         _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag)
47     {    // copy [_First, _Last) to raw _Dest, arbitrary type
48     _DEBUG_RANGE(_First, _Last);
49     _DEBUG_POINTER(_Dest);
50     _FwdIt _Next = _Dest;
51
52     _TRY_BEGIN
53     for (; _First != _Last; ++_Dest, ++_First)
54         _Construct(&*_Dest, *_First);
55     _CATCH_ALL
56     for (; _Next != _Dest; ++_Next)
57         _Destroy(&*_Next);
58     _RERAISE;
59     _CATCH_END
60     return (_Dest);
61     }
62
63 template<class _Ty1,
64     class _Ty2> inline
65     _Ty2 _Uninit_copy(_Ty1 _First, _Ty1 _Last, _Ty2 _Dest,
66         _Scalar_ptr_iterator_tag, _Range_checked_iterator_tag)
67     {    // copy [_First, _Last) to raw _Dest, scalar type
68     _DEBUG_RANGE(_First, _Last);
69     _DEBUG_POINTER(_Dest);
70     size_t _Count = (size_t)(_Last - _First);
71     _Ty2 _Result = _Dest + _Count;
72     if (_Count > 0)
73         _CRT_SECURE_MEMMOVE(&*_Dest,
74             _Count * sizeof (*_First), &*_First,
75             _Count * sizeof (*_First));    // NB: non-overlapping move
76     return (_Result);
77     }
78
79 #if _SECURE_SCL
80
81 template<class _InIt,
82     class _FwdIt> inline
83 _IF_CHK(_FwdIt) uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest)
84     {    // copy [_First, _Last) to raw _Dest
85     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest,
86         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
87     }
88
89 template<class _InIt, class _FwdElem, size_t _Size>
90 inline
91 _FwdElem* uninitialized_copy(_InIt _First, _InIt _Last, _FwdElem (&_Dest)[_Size])
92     {    // copy [_First, _Last) to raw _Dest
93     return (uninitialized_copy(_First, _Last,
94         _STDEXT make_checked_array_iterator(_Dest, _Size)).base());
95     }
96
97 template<class _InIt,
98     class _FwdIt> inline
99 _SCL_INSECURE_DEPRECATE
100 _IF_NOT_CHK(_FwdIt) uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest)
101     {    // copy [_First, _Last) to raw _Dest
102     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest,
103         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
104     }
105
106 #else
107
108 template<class _InIt,
109     class _FwdIt> inline
110     _FwdIt uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest)
111     {    // copy [_First, _Last) to raw _Dest
112     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest,
113         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
114     }
115
116 #endif
117
118         // TEMPLATE FUNCTION _Uninitialized_copy WITH ALLOCATOR
119 template<class _InIt,
120     class _FwdIt,
121     class _Alloc> inline
122     _FwdIt _Uninit_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
123         _Alloc& _Al, _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag)
124     {    // copy [_First, _Last) to raw _Dest, using _Al, arbitrary type
125     _DEBUG_RANGE(_First, _Last);
126     _DEBUG_POINTER(_Dest);
127     _FwdIt _Next = _Dest;
128
129     _TRY_BEGIN
130     for (; _First != _Last; ++_Dest, ++_First)
131         _Al.construct(_Dest, *_First);
132     _CATCH_ALL
133     for (; _Next != _Dest; ++_Next)
134         _Al.destroy(_Next);
135     _RERAISE;
136     _CATCH_END
137     return (_Dest);
138     }
139
140 template<class _InIt,
141     class _FwdIt,
142     class _Alloc> inline
143     _FwdIt _Uninit_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
144         _Alloc&, _Scalar_ptr_iterator_tag, _Range_checked_iterator_tag)
145     {    // copy [_First, _Last) to raw _Dest, scalar type
146     _DEBUG_RANGE(_First, _Last);
147     _DEBUG_POINTER(_Dest);
148     size_t _Count = (size_t)(_Last - _First);
149     _FwdIt _Result = _Dest + _Count;
150     if (_Count > 0)
151         _CRT_SECURE_MEMMOVE(&*_Dest, _Count * sizeof (*_First), &*_First, _Count * sizeof (*_First));    // NB: non-overlapping move
152     return (_Result);
153     }
154
155 #if _SECURE_SCL
156
157 template<class _InIt,
158     class _FwdIt,
159     class _Alloc> inline
160 _IF_CHK(_FwdIt) _Uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
161         _Alloc& _Al)
162     {    // copy [_First, _Last) to raw _Dest, using _Al
163     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Al,
164         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
165     }
166
167 template<class _InIt, class _FwdElem, class _Alloc, size_t _Size>
168 inline
169 _FwdElem* _Uninitialized_copy(_InIt _First, _InIt _Last, _FwdElem (&_Dest)[_Size],
170         _Alloc& _Al)
171     {    // copy [_First, _Last) to raw _Dest, using _Al
172     return (_Uninitialized_copy(_First, _Last,
173         _STDEXT make_checked_array_iterator(_Dest, _Size), _Al).base());
174     }
175
176 template<class _InIt,
177     class _FwdIt,
178     class _Alloc> inline
179 _SCL_INSECURE_DEPRECATE
180 _IF_NOT_CHK(_FwdIt) _Uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
181         _Alloc& _Al)
182     {    // copy [_First, _Last) to raw _Dest, using _Al
183     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Al,
184         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
185     }
186
187 #else
188
189 template<class _InIt,
190     class _FwdIt,
191     class _Alloc> inline
192     _FwdIt _Uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest,
193         _Alloc& _Al)
194     {    // copy [_First, _Last) to raw _Dest, using _Al
195     return (_Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Al,
196         _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
197     }
198
199 #endif
200
201         // TEMPLATE FUNCTION _Uninitialized_move WITH ALLOCATOR
202 template<class _InIt, class _FwdIt, class _Alloc, class _MoveCatTy>
203 inline
204     _FwdIt _Uninit_move(_InIt _First, _InIt _Last, _FwdIt _Dest,
205         _Alloc& _Al, _MoveCatTy, _Range_checked_iterator_tag)
206     {    // move defaults to copy if there is not a more effecient way
207     return (_STDEXT unchecked_uninitialized_copy(_First, _Last, _Dest, _Al));
208     }
209
210 template<class _InIt, class _FwdIt, class _Alloc>
211 inline
212     _FwdIt _Uninit_move(_InIt _First, _InIt _Last, _FwdIt _Dest,
213         _Alloc& _Al, _Swap_move_tag, _Range_checked_iterator_tag)
214     {    // use swap to instead of the copy constructor
215     _DEBUG_RANGE(_First, _Last);
216     _DEBUG_POINTER(_Dest);
217     _FwdIt _Next = _Dest;
218     // empty value used in the construction
219     typename _Alloc::value_type _Val;
220
221     _TRY_BEGIN
222     for (; _First != _Last; ++_Dest, ++_First)
223         {
224         _Al.construct(_Dest, _Val);
225         _STD _Swap_adl(*_Dest, *_First);
226         }
227     _CATCH_ALL
228     for (; _Next != _Dest; ++_Next)
229         _Al.destroy(_Next);
230     _RERAISE;
231     _CATCH_END
232     return (_Dest);
233     }
234
235 #if _SECURE_SCL
236
237 template<class _InIt, class _FwdIt, class _Alloc>
238 inline
239 _IF_CHK(_FwdIt) _Uninitialized_move(_InIt _First, _InIt _Last, _FwdIt _Dest,
240         _Alloc& _Al)
241     {    // move [_First, _Last) to raw _Dest, using _Al
242     return (_Uninit_move(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Al,
243         _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
244     }
245
246 template<class _InIt, class _FwdElem, class _Alloc, size_t _Size>
247 inline
248 _FwdElem* _Uninitialized_move(_InIt _First, _InIt _Last, _FwdElem (&_Dest)[_Size],
249         _Alloc& _Al)
250     {    // move [_First, _Last) to raw _Dest, using _Al
251     return (_Uninitialized_move(_First, _Last,
252         _STDEXT make_checked_array_iterator(_Dest, _Size), _Al).base());
253     }
254
255 template<class _InIt, class _FwdIt, class _Alloc>
256 inline
257 _SCL_INSECURE_DEPRECATE
258 _IF_NOT_CHK(_FwdIt) _Uninitialized_move(_InIt _First, _InIt _Last, _FwdIt _Dest,
259         _Alloc& _Al)
260     {    // move [_First, _Last) to raw _Dest, using _Al
261     return (_Uninit_move(_CHECKED_BASE(_First),
262         _CHECKED_BASE(_Last), _Dest, _Al,
263         _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
264     }
265
266 #else
267
268 template<class _InIt, class _FwdIt, class _Alloc>
269 inline
270     _FwdIt _Uninitialized_move(_InIt _First, _InIt _Last, _FwdIt _Dest,
271         _Alloc& _Al)
272     {    // move [_First, _Last) to raw _Dest, using _Al
273     return (_Uninit_move(_CHECKED_BASE(_First),
274         _CHECKED_BASE(_Last), _Dest, _Al,
275         _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
276     }
277
278 #endif
279
280       // TEMPLATE FUNCTION uninitialized_fill
281 template<class _FwdIt,
282     class _Tval> inline
283     void _Uninit_fill(_FwdIt _First, _FwdIt _Last, const _Tval& _Val,
284         _Nonscalar_ptr_iterator_tag)
285     {    // copy _Val throughout raw [_First, _Last), arbitrary type
286     _DEBUG_RANGE(_First, _Last);
287     _FwdIt _Next = _First;
288
289     _TRY_BEGIN
290     for (; _First != _Last; ++_First)
291         _Construct(&*_First, _Val);
292     _CATCH_ALL
293     for (; _Next != _First; ++_Next)
294         _Destroy(&*_Next);
295     _RERAISE;
296     _CATCH_END
297     }
298
299 template<class _Ty,
300     class _Tval> inline
301     void _Uninit_fill(_Ty *_First, _Ty *_Last, const _Tval& _Val,
302         _Scalar_ptr_iterator_tag)
303     {    // copy _Val throughout raw [_First, _Last), scalar type
304     std::fill(_First, _Last, _Val);
305     }
306
307 template<class _FwdIt,
308     class _Tval> inline
309     void uninitialized_fill(_FwdIt _First, _FwdIt _Last, const _Tval& _Val)
310     {    // copy _Val throughout raw [_First, _Last)
311     _Uninit_fill(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Val, _Ptr_cat(_First, _First));
312     }
313
314         // TEMPLATE FUNCTION uninitialized_fill_n
315 template<class _FwdIt,
316     class _Diff,
317     class _Tval> inline
318     void _Uninit_fill_n(_FwdIt _First, _Diff _Count, const _Tval& _Val,
319         _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag)
320     {    // copy _Count *_Val to raw _First, arbitrary type
321
322  #if _HAS_ITERATOR_DEBUGGING
323 //    if (_Count < 0)
324 //        _DEBUG_ERROR("negative count in uninitialized fill");
325  #endif /* _HAS_ITERATOR_DEBUGGING */
326
327     _FwdIt _Next = _First;
328
329     _TRY_BEGIN
330     for (; 0 < _Count; --_Count, ++_First)
331         _Construct(&*_First, _Val);
332     _CATCH_ALL
333     for (; _Next != _First; ++_Next)
334         _Destroy(&*_Next);
335     _RERAISE;
336     _CATCH_END
337     }
338
339 template<class _FwdIt,
340     class _Diff,
341     class _Tval> inline
342     void _Uninit_fill_n(_FwdIt _First, _Diff _Count, const _Tval& _Val,
343         _Scalar_ptr_iterator_tag, _Range_checked_iterator_tag)
344     {    // copy _Count *_Val to raw _First, scalar type
345     _STDEXT unchecked_fill_n(&*_First, _Count, _Val);
346     }
347
348 #if _SECURE_SCL
349
350 template<class _FwdIt,
351     class _Diff,
352     class _Tval> inline
353 _IF_CHK_(_FwdIt, void) uninitialized_fill_n(_FwdIt _First, _Diff _Count, const _Tval& _Val)
354     {    // copy _Count *_Val to raw _First
355     _Uninit_fill_n(_First, _Count, _Val, _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
356     }
357
358 template<class _FwdElem, class _Diff, class _Tval, size_t _Size>
359 inline
360 void uninitialized_fill_n(_FwdElem (&_First)[_Size], _Diff _Count, const _Tval& _Val)
361     {    // copy _Count *_Val to raw _First
362     uninitialized_fill_n(_STDEXT make_checked_array_iterator(_First, _Size), _Count, _Val);
363     }
364
365 template<class _FwdIt,
366     class _Diff,
367     class _Tval> inline
368 _SCL_INSECURE_DEPRECATE
369 _IF_NOT_CHK_(_FwdIt, void) uninitialized_fill_n(_FwdIt _First, _Diff _Count, const _Tval& _Val)
370     {    // copy _Count *_Val to raw _First
371     _Uninit_fill_n(_First, _Count, _Val, _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
372     }
373
374 #else
375
376 template<class _FwdIt,
377     class _Diff,
378     class _Tval> inline
379     void uninitialized_fill_n(_FwdIt _First, _Diff _Count, const _Tval& _Val)
380     {    // copy _Count *_Val to raw _First
381     _Uninit_fill_n(_First, _Count, _Val, _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
382     }
383
384 #endif
385
386         // TEMPLATE FUNCTION _Uninitialized_fill_n WITH ALLOCATOR
387 template<class _FwdIt,
388     class _Diff,
389     class _Tval,
390     class _Alloc> inline
391     void _Uninit_fill_n(_FwdIt _First, _Diff _Count,
392         const _Tval& _Val, _Alloc& _Al, _Nonscalar_ptr_iterator_tag, _Range_checked_iterator_tag)
393     {    // copy _Count *_Val to raw _First, using _Al, arbitrary type
394
395  #if _HAS_ITERATOR_DEBUGGING
396 //    if (_Count < 0)
397 //        _DEBUG_ERROR("negative count in uninitialized fill");
398  #endif /* _HAS_ITERATOR_DEBUGGING */
399
400     _FwdIt _Next = _First;
401
402     _TRY_BEGIN
403     for (; 0 < _Count; --_Count, ++_First)
404         _Al.construct(_First, _Val);
405     _CATCH_ALL
406     for (; _Next != _First; ++_Next)
407         _Al.destroy(_Next);
408     _RERAISE;
409     _CATCH_END
410     }
411
412 template<class _FwdIt,
413     class _Diff,
414     class _Tval,
415     class _Alloc> inline
416     void _Uninit_fill_n(_FwdIt _First, _Diff _Count,
417         const _Tval& _Val, _Alloc&, _Scalar_ptr_iterator_tag, _Range_checked_iterator_tag)
418     {    // copy _Count *_Val to raw _First, using _Al, scalar type
419         _STDEXT unchecked_fill_n(_First, _Count, _Val);
420     }
421
422 #if _SECURE_SCL
423
424 template<class _FwdIt,
425     class _Diff,
426     class _Tval,
427     class _Alloc> inline
428 _IF_CHK_(_FwdIt, void) _Uninitialized_fill_n(_FwdIt _First, _Diff _Count,
429         const _Tval& _Val, _Alloc& _Al)
430     {    // copy _Count *_Val to raw _First, using _Al
431     _Uninit_fill_n(_First, _Count, _Val, _Al,
432         _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
433     }
434
435 template<class _FwdElem, class _Diff, class _Tval, class _Alloc, size_t _Size>
436 inline
437 void _Uninitialized_fill_n(_FwdElem (&_First)[_Size], _Diff _Count,
438         const _Tval& _Val, _Alloc& _Al)
439     {    // copy _Count *_Val to raw _First, using _Al
440     _Uninitialized_fill_n(_STDEXT make_checked_array_iterator(_First, _Size), _Count, _Val, _Al);
441     }
442
443 template<class _FwdIt,
444     class _Diff,
445     class _Tval,
446     class _Alloc> inline
447 _SCL_INSECURE_DEPRECATE
448 _IF_NOT_CHK_(_FwdIt, void) _Uninitialized_fill_n(_FwdIt _First, _Diff _Count,
449         const _Tval& _Val, _Alloc& _Al)
450     {    // copy _Count *_Val to raw _First, using _Al
451     _Uninit_fill_n(_First, _Count, _Val, _Al,
452         _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
453     }
454
455 #else
456
457 template<class _FwdIt,
458     class _Diff,
459     class _Tval,
460     class _Alloc> inline
461     void _Uninitialized_fill_n(_FwdIt _First, _Diff _Count,
462         const _Tval& _Val, _Alloc& _Al)
463     {    // copy _Count *_Val to raw _First, using _Al
464     _Uninit_fill_n(_First, _Count, _Val, _Al,
465         _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
466     }
467
468 #endif
469
470         // TEMPLATE CLASS raw_storage_iterator
471 template<class _FwdIt,
472     class _Ty>
473     class raw_storage_iterator
474         : public _Outit
475     {    // wrap stores to raw buffer as output iterator
476 public:
477     typedef _FwdIt iterator_type;    // retained
478     typedef _FwdIt iter_type;    // retained
479     typedef _Ty element_type;    // retained
480
481     explicit raw_storage_iterator(_FwdIt _First)
482         : _Next(_First)
483         {    // construct with iterator
484         }
485
486     raw_storage_iterator<_FwdIt, _Ty>& operator*()
487         {    // pretend to return designated value
488         return (*this);
489         }
490
491     raw_storage_iterator<_FwdIt, _Ty>& operator=(const _Ty& _Val)
492         {    // construct value designated by stored iterator
493         _Construct(&*_Next, _Val);
494         return (*this);
495         }
496
497     raw_storage_iterator<_FwdIt, _Ty>& operator++()
498         {    // preincrement
499         ++_Next;
500         return (*this);
501         }
502
503     raw_storage_iterator<_FwdIt, _Ty> operator++(int)
504         {    // postincrement
505         raw_storage_iterator<_FwdIt, _Ty> _Ans = *this;
506         ++_Next;
507         return (_Ans);
508         }
509
510 private:
511     _FwdIt _Next;    // the stored iterator
512     };
513
514         // TEMPLATE CLASS _Temp_iterator
515 template<class _Ty>
516     class _Temp_iterator
517         : public _Outit
518     {    // wrap stores to temporary buffer as output iterator
519 public:
520     typedef _Ty _FARQ *_Pty;
521
522 #if _SECURE_SCL
523     typedef _Range_checked_iterator_tag _Checked_iterator_category;
524 #endif
525
526     _Temp_iterator(_PDFT _Count = 0)
527         {    // construct from desired temporary buffer size
528         _Buf._Begin = 0;
529         _Buf._Current = 0;
530         _Buf._Hiwater = 0;
531         _Buf._Size = _Count;    // memorize size for lazy allocation
532         _Pbuf = &_Buf;
533         }
534
535     _Temp_iterator(const _Temp_iterator<_Ty>& _Right)
536         {    // construct from _Right (share active buffer)
537         _Buf._Begin = 0;    // clear stored buffer, for safe destruction
538         _Buf._Current = 0;
539         _Buf._Hiwater = 0;
540         _Buf._Size = 0;
541         *this = _Right;
542         }
543
544     ~_Temp_iterator()
545         {    // destroy the object
546         if (_Buf._Begin != 0)
547             {    // destroy any constructed elements in buffer
548             for (_Pty _Next = _Buf._Begin;
549                 _Next != _Buf._Hiwater; ++_Next)
550                 _Destroy(&*_Next);
551             std::return_temporary_buffer(_Buf._Begin);
552             }
553         }
554
555     _Temp_iterator<_Ty>& operator=(const _Temp_iterator<_Ty>& _Right)
556         {    // assign _Right (share active buffer)
557         _Pbuf = _Right._Pbuf;
558         return (*this);
559         }
560
561     _Temp_iterator<_Ty>& operator=(const _Ty& _Val)
562         {    // assign or construct value into active buffer, and increment
563         if (_Pbuf->_Current < _Pbuf->_Hiwater)
564             *_Pbuf->_Current++ = _Val;    // below high water mark, assign
565         else
566             {    // above high water mark, construct
567             _SCL_SECURE_VALIDATE((_Pbuf->_Current - _Pbuf->_Begin) < _Pbuf->_Size);
568             _Pty _Ptr = &*_Pbuf->_Current;
569             _Construct(_Ptr, _Val);
570             _Pbuf->_Hiwater = ++_Pbuf->_Current;
571             }
572         return (*this);
573         }
574
575     _Temp_iterator<_Ty>& operator*()
576         {    // pretend to return designated value
577         return (*this);
578         }
579
580     _Temp_iterator<_Ty>& operator++()
581         {    // pretend to preincrement
582         return (*this);
583         }
584
585     _Temp_iterator<_Ty>& operator++(int)
586         {    // pretend to postincrement
587         return (*this);
588         }
589
590     _Temp_iterator<_Ty>& _Init()
591         {    // set pointer at beginning of buffer
592         _Pbuf->_Current = _Pbuf->_Begin;
593         return (*this);
594         }
595
596     _Pty _First() const
597         {    // return pointer to beginning of buffer
598         return (_Pbuf->_Begin);
599         }
600
601     _Pty _Last() const
602         {    // return pointer past end of buffer contents
603         return (_Pbuf->_Current);
604         }
605
606     _PDFT _Maxlen()
607         {    // return size of buffer
608         if (_Pbuf->_Begin == 0 && 0 < _Pbuf->_Size)
609             {    // allocate buffer on first size query
610             pair<_Pty, _PDFT> _Pair =
611                 std::get_temporary_buffer<_Ty>(_Pbuf->_Size);
612
613             _Pbuf->_Begin = _Pair.first;
614             _Pbuf->_Current = _Pair.first;
615             _Pbuf->_Hiwater = _Pair.first;
616             _Pbuf->_Size = _Pair.second;
617             }
618         return (_Pbuf->_Size);
619         }
620
621     static void _Xinvarg()
622         {    // report an invalid_argument error
623         _THROW(invalid_argument, "invalid _Temp_iterator<T> argument");
624         }
625
626 private:
627     struct _Bufpar
628         {    // control information for a temporary buffer
629         _Pty _Begin;    // pointer to beginning of buffer
630         _Pty _Current;    // pointer to next available element
631         _Pty _Hiwater;    // pointer to first unconstructed element
632         _PDFT _Size;    // length of buffer
633         };
634     _Bufpar _Buf;    // buffer control stored in iterator
635     _Bufpar *_Pbuf;    // pointer to active buffer control
636     };
637
638         // TEMPLATE CLASS auto_ptr
639 template<class _Ty>
640     class auto_ptr;
641
642 template<class _Ty>
643     struct auto_ptr_ref
644         {    // proxy reference for auto_ptr copying
645     explicit auto_ptr_ref(_Ty *_Right)
646         : _Ref(_Right)
647         {    // construct from generic pointer to auto_ptr ptr
648         }
649
650     _Ty *_Ref;    // generic pointer to auto_ptr ptr
651     };
652
653 template<class _Ty>
654     class auto_ptr
655         {    // wrap an object pointer to ensure destruction
656 public:
657     typedef _Ty element_type;
658
659     explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()
660         : _Myptr(_Ptr)
661         {    // construct from object pointer
662         }
663
664     auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()
665         : _Myptr(_Right.release())
666         {    // construct by assuming pointer from _Right auto_ptr
667         }
668
669     auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
670         {    // construct by assuming pointer from _Right auto_ptr_ref
671         _Ty *_Ptr = _Right._Ref;
672         _Right._Ref = 0;    // release old
673         _Myptr = _Ptr;    // reset this
674         }
675
676     template<class _Other>
677         operator auto_ptr<_Other>() _THROW0()
678         {    // convert to compatible auto_ptr
679         return (auto_ptr<_Other>(*this));
680         }
681
682     template<class _Other>
683         operator auto_ptr_ref<_Other>() _THROW0()
684         {    // convert to compatible auto_ptr_ref
685         _Other *_Cvtptr = _Myptr;    // test implicit conversion
686         auto_ptr_ref<_Other> _Ans(_Cvtptr);
687         _Myptr = 0;    // pass ownership to auto_ptr_ref
688         return (_Ans);
689         }
690
691
692     template<class _Other>
693         auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
694         {    // assign compatible _Right (assume pointer)
695         reset(_Right.release());
696         return (*this);
697         }
698
699     template<class _Other>
700         auto_ptr(auto_ptr<_Other>& _Right) _THROW0()
701         : _Myptr(_Right.release())
702         {    // construct by assuming pointer from _Right
703         }
704
705     auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
706         {    // assign compatible _Right (assume pointer)
707         reset(_Right.release());
708         return (*this);
709         }
710
711     auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
712         {    // assign compatible _Right._Ref (assume pointer)
713         _Ty *_Ptr = _Right._Ref;
714         _Right._Ref = 0;    // release old
715         reset(_Ptr);    // set new
716         return (*this);
717         }
718
719     ~auto_ptr()
720         {    // destroy the object
721         delete _Myptr;
722         }
723
724     _Ty& operator*() const _THROW0()
725         {    // return designated value
726
727  #if _HAS_ITERATOR_DEBUGGING
728         if (_Myptr == 0)
729             _DEBUG_ERROR("auto_ptr not dereferencable");
730  #endif /* _HAS_ITERATOR_DEBUGGING */
731
732         __analysis_assume(_Myptr);
733
734         return (*get());
735         }
736
737     _Ty *operator->() const _THROW0()
738         {    // return pointer to class object
739
740  #if _HAS_ITERATOR_DEBUGGING
741         if (_Myptr == 0)
742             _DEBUG_ERROR("auto_ptr not dereferencable");
743  #endif /* _HAS_ITERATOR_DEBUGGING */
744
745         return (get());
746         }
747
748     _Ty *get() const _THROW0()
749         {    // return wrapped pointer
750         return (_Myptr);
751         }
752
753     _Ty *release() _THROW0()
754         {    // return wrapped pointer and give up ownership
755         _Ty *_Tmp = _Myptr;
756         _Myptr = 0;
757         return (_Tmp);
758         }
759
760     void reset(_Ty* _Ptr = 0)
761         {    // destroy designated object and store new pointer
762         if (_Ptr != _Myptr)
763             delete _Myptr;
764         _Myptr = _Ptr;
765         }
766
767 private:
768     _Ty *_Myptr;    // the wrapped object pointer
769     };
770 _STD_END
771
772 _STDEXT_BEGIN
773
774 template<class _InIt,
775     class _FwdIt> inline
776     _FwdIt unchecked_uninitialized_copy(_InIt _First, _InIt _Last,
777         _FwdIt _Dest)
778     {    // copy [_First, _Last) to raw _Dest
779         return (_STD _Uninit_copy(_CHECKED_BASE(_First),
780             _CHECKED_BASE(_Last), _Dest,
781             _STD _Ptr_cat(_First, _Dest),
782             _STD _Range_checked_iterator_tag()));
783     }
784
785 template<class _InIt,
786     class _FwdIt> inline
787     _IF_CHK(_FwdIt) checked_uninitialized_copy(_InIt _First, _InIt _Last,
788         _FwdIt _Dest)
789     {    // copy [_First, _Last) to raw _Dest
790     return (_STD _Uninit_copy(_CHECKED_BASE(_First),
791             _CHECKED_BASE(_Last), _Dest,
792             _STD _Ptr_cat(_First, _Dest),
793             _STD _Range_checked_iterator_tag()));
794     }
795
796 template<class _InIt, class _FwdElem, size_t _Size> inline
797     _FwdElem *checked_uninitialized_copy(_InIt _First, _InIt _Last,
798         _FwdElem (&_Dest)[_Size])
799     {    // copy [_First, _Last) to raw _Dest
800     return (checked_uninitialized_copy(_First, _Last,
801         _STDEXT make_checked_array_iterator(_Dest, _Size)).base());
802     }
803
804 template<class _InIt,
805     class _FwdIt> inline
806     _SCL_CHECKED_ALGORITHM_WARN    _IF_NOT_CHK(_FwdIt)
807         checked_uninitialized_copy(_InIt _First, _InIt _Last, _FwdIt _Dest)
808     {    // copy [_First, _Last) to raw _Dest
809     return (_STD _Uninit_copy(_CHECKED_BASE(_First),
810         _CHECKED_BASE(_Last), _Dest,
811         _STD _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
812     }
813
814 template<class _InIt,
815     class _FwdIt,
816     class _Alloc> inline
817     _FwdIt unchecked_uninitialized_copy(_InIt _First, _InIt _Last,
818         _FwdIt _Dest, _Alloc& _Al)
819     {    // copy [_First, _Last) to raw _Dest, using _Al
820     return (_STD _Uninit_copy(_CHECKED_BASE(_First),
821         _CHECKED_BASE(_Last), _Dest, _Al,
822         _STD _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
823     }
824
825 template<class _InIt,
826     class _FwdIt,
827     class _Alloc> inline
828     _IF_CHK(_FwdIt) checked_uninitialized_copy(_InIt _First, _InIt _Last,
829         _FwdIt _Dest, _Alloc& _Al)
830     {    // copy [_First, _Last) to raw _Dest, using _Al
831     return (_STD _Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last),
832         _Dest, _Al,
833         _STD _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
834     }
835
836 template<class _InIt, class _FwdElem, class _Alloc, size_t _Size> inline
837     _FwdElem *checked_uninitialized_copy(_InIt _First, _InIt _Last,
838         _FwdElem (&_Dest)[_Size], _Alloc& _Al)
839     {    // copy [_First, _Last) to raw _Dest, using _Al
840     return (checked_uninitialized_copy(_First, _Last,
841         _STDEXT make_checked_array_iterator(_Dest, _Size), _Al).base());
842     }
843
844 template<class _InIt,
845     class _FwdIt,
846     class _Alloc> inline
847     _SCL_CHECKED_ALGORITHM_WARN _IF_NOT_CHK(_FwdIt)
848         checked_uninitialized_copy(_InIt _First, _InIt _Last,
849         _FwdIt _Dest, _Alloc& _Al)
850     {    // copy [_First, _Last) to raw _Dest, using _Al
851     return (_STD _Uninit_copy(_CHECKED_BASE(_First), _CHECKED_BASE(_Last),
852         _Dest, _Al,
853         _STD _Ptr_cat(_First, _Dest), _STD _Range_checked_iterator_tag()));
854     }
855
856 template<class _InIt,
857     class _FwdIt,
858     class _Alloc> inline
859     _FwdIt _Unchecked_uninitialized_move(_InIt _First, _InIt _Last,
860         _FwdIt _Dest, _Alloc& _Al)
861     {    // move [_First, _Last) to raw _Dest, using _Al
862     return (_STD _Uninit_move(_CHECKED_BASE(_First),
863         _CHECKED_BASE(_Last), _Dest, _Al,
864         _STD _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
865     }
866
867 template<class _InIt, class _FwdIt, class _Alloc> inline
868     _IF_CHK(_FwdIt) _Checked_uninitialized_move(_InIt _First, _InIt _Last,
869         _FwdIt _Dest, _Alloc& _Al)
870     {    // move [_First, _Last) to raw _Dest, using _Al
871     return (_STD _Uninit_move(_CHECKED_BASE(_First), _CHECKED_BASE(_Last),
872         _Dest, _Al,
873         _STD _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
874     }
875
876 template<class _InIt, class _FwdElem, class _Alloc, size_t _Size> inline
877     _FwdElem *_Checked_uninitialized_move(_InIt _First, _InIt _Last,
878         _FwdElem (&_Dest)[_Size], _Alloc& _Al)
879     {    // move [_First, _Last) to raw _Dest, using _Al
880     return (_Checked_uninitialized_move(_First, _Last,
881         _STDEXT make_checked_array_iterator(_Dest, _Size), _Al).base());
882     }
883
884 template<class _InIt, class _FwdIt, class _Alloc> inline
885     _SCL_CHECKED_ALGORITHM_WARN _IF_NOT_CHK(_FwdIt)
886         _Checked_uninitialized_move(_InIt _First, _InIt _Last,
887             _FwdIt _Dest, _Alloc& _Al)
888     {    // move [_First, _Last) to raw _Dest, using _Al
889     return (_STD _Uninit_move(_CHECKED_BASE(_First), _CHECKED_BASE(_Last),
890         _Dest, _Al,
891         _STD _Move_cat(_Dest), _STD _Range_checked_iterator_tag()));
892     }
893
894 template<class _FwdIt,
895     class _Diff,
896     class _Tval> inline
897     void unchecked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
898         const _Tval& _Val)
899     {    // copy _Count *_Val to raw _First
900     _STD _Uninit_fill_n(_First, _Count, _Val,
901         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
902     }
903
904 template<class _FwdIt,
905     class _Diff,
906     class _Tval> inline
907     _IF_CHK_(_FwdIt, void)
908         checked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
909             const _Tval& _Val)
910     {    // copy _Count *_Val to raw _First
911     _STD _Uninit_fill_n(_First, _Count, _Val,
912         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
913     }
914
915 template<class _FwdElem, class _Diff, class _Tval, size_t _Size> inline
916     void checked_uninitialized_fill_n(_FwdElem (&_First)[_Size],
917         _Diff _Count, const _Tval& _Val)
918     {    // copy _Count *_Val to raw _First
919     checked_uninitialized_fill_n(
920         _STDEXT make_checked_array_iterator(_First, _Size), _Count, _Val);
921     }
922
923 template<class _FwdIt,
924     class _Diff,
925     class _Tval> inline
926     _SCL_CHECKED_ALGORITHM_WARN _IF_NOT_CHK_(_FwdIt, void)
927         checked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
928             const _Tval& _Val)
929     {    // copy _Count *_Val to raw _First
930     _STD _Uninit_fill_n(_First, _Count, _Val,
931         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
932     }
933
934 template<class _FwdIt,
935     class _Diff,
936     class _Tval,
937     class _Alloc> inline
938     void unchecked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
939         const _Tval& _Val, _Alloc& _Al)
940     {    // copy _Count *_Val to raw _First, using _Al
941     _STD _Uninit_fill_n(_First, _Count, _Val, _Al,
942         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
943     }
944
945 template<class _FwdIt,
946     class _Diff,
947     class _Tval,
948     class _Alloc> inline
949     _IF_CHK_(_FwdIt, void)
950         checked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
951             const _Tval& _Val, _Alloc& _Al)
952     {    // copy _Count *_Val to raw _First, using _Al
953     _STD _Uninit_fill_n(_First, _Count, _Val, _Al,
954         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
955     }
956
957 template<class _FwdElem,
958     class _Diff,
959     class _Tval,
960     class _Alloc,
961     size_t _Size> inline
962     void checked_uninitialized_fill_n(_FwdElem (&_First)[_Size],
963         _Diff _Count, const _Tval& _Val, _Alloc& _Al)
964     {    // copy _Count *_Val to raw _First, using _Al
965     checked_uninitialized_fill_n(
966         _STDEXT make_checked_array_iterator(_First, _Size),
967         _Count, _Val, _Al);
968     }
969
970 template<class _FwdIt,
971     class _Diff,
972     class _Tval,
973     class _Alloc> inline
974     _SCL_CHECKED_ALGORITHM_WARN _IF_NOT_CHK_(_FwdIt, void)
975         checked_uninitialized_fill_n(_FwdIt _First, _Diff _Count,
976             const _Tval& _Val, _Alloc& _Al)
977     {    // copy _Count *_Val to raw _First, using _Al
978     _STD _Uninit_fill_n(_First, _Count, _Val, _Al,
979         _STD _Ptr_cat(_First, _First), _STD _Range_checked_iterator_tag());
980     }
981
982 _STDEXT_END
983
984  #if _HAS_TR1
985  #ifndef _XSTD2
986   #define _XSTD2
987  #endif /* _XSTD2 */
988
989  #include <exception>
990  #include <typeinfo>
991
992 #ifndef _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
993 extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedIncrement(volatile long *);
994 extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedDecrement(volatile long *);
995 extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange(volatile long *,
996     long, long);
997
998  #pragma intrinsic(_InterlockedIncrement)
999  #pragma intrinsic(_InterlockedDecrement)
1000  #pragma intrinsic(_InterlockedCompareExchange)
1001 #endif /* _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY */
1002
1003  #define _MT_INCR(mtx, x)    _InterlockedIncrement(&x)
1004  #define _MT_DECR(mtx, x)    _InterlockedDecrement(&x)
1005  #define _MT_CMPX(x, y, z)    _InterlockedCompareExchange(&x, y, z)
1006
1007 _STD_BEGIN
1008     namespace tr1 {    // TR1 additions
1009
1010     // CLASS bad_weak_ptr
1011 class bad_weak_ptr
1012     : public _XSTD exception
1013     {    // exception type for invalid use of expired weak_ptr object
1014 public:
1015     explicit bad_weak_ptr(const char * = 0)
1016         {    // construct with ignored message
1017         }
1018
1019     virtual const char *__CLR_OR_THIS_CALL what() const _THROW0()
1020         {    // return pointer to message string
1021         return ("tr1::bad_weak_ptr");
1022         }
1023     };
1024
1025 _CRTIMP2_PURE __declspec(noreturn) void __CLRCALL_PURE_OR_CDECL _Xweak();
1026
1027     // CLASS _Ref_count_base
1028 class _Ref_count_base
1029     {    // common code for reference counting
1030 private:
1031     virtual void _Destroy() = 0;
1032     virtual void _Delete_this() = 0;
1033
1034     const volatile void *_Ptr;
1035     long _Uses;
1036     long _Weaks;
1037
1038 protected:
1039     _Ref_count_base(const volatile void *_Px)
1040         : _Ptr(_Px), _Uses(0), _Weaks(1)
1041         {    // construct
1042         }
Lines 1043 ... 1776 are skipped.
1777         }
1778
1779     enable_shared_from_this& operator=(const enable_shared_from_this&)
1780         {    // assign (do nothing)
1781         return (*this);
1782         }
1783
1784     ~enable_shared_from_this()
1785         {    // destroy (do nothing)
1786         }
1787
1788 private:
1789     template<class _Ty1,
1790         class _Ty2>
1791         friend void _Do_enable(
1792             _Ty1 *,
1793             enable_shared_from_this<_Ty2>*,
1794             _Ref_count_base *);
1795
1796     mutable weak_ptr<_Ty> _Wptr;
1797     };
1798
1799 template<class _Ty1,
1800     class _Ty2>
1801     inline void _Do_enable(
1802         _Ty1 *_Ptr,
1803         enable_shared_from_this<_Ty2> *_Es,
1804         _Ref_count_base *_Refptr)
1805     {    // reset internal weak pointer
1806     _Es->_Wptr._Resetw(_Ptr, _Refptr);
1807     }
1808     }    // namespace tr1
1809
1810 template<class _Ty>
1811     class _Move_operation_category<tr1::shared_ptr<_Ty> >
1812     {    // shared_ptr implements a performant swap
1813 public:
1814     typedef _Swap_move_tag _Move_cat;
1815     };
1816
1817 template<class _Ty>
1818     class _Move_operation_category<tr1::weak_ptr<_Ty> >
1819     {    // weak_ptr implements a performant swap
1820 public:
1821     typedef _Swap_move_tag _Move_cat;
1822     };
1823 _STD_END
1824  #endif /* _HAS_TR1 */
1825
1826 #ifdef _MSC_VER
1827  #pragma warning(pop)
1828  #pragma pack(pop)
1829 #endif  /* _MSC_VER */
1830
1831 #endif /* RC_INVOKED */
1832 #endif /* _MEMORY_ */
1833
1834
1835 /*
1836  * Copyright (c) 1992-2008 by P.J. Plauger.  ALL RIGHTS RESERVED.
1837  * Consult your license regarding permissions and restrictions.
1838  */
1839
1840 /*
1841  * This file is derived from software bearing the following
1842  * restrictions:
1843  *
1844  * Copyright (c) 1994
1845  * Hewlett-Packard Company
1846  *
1847  * Permission to use, copy, modify, distribute and sell this
1848  * software and its documentation for any purpose is hereby
1849  * granted without fee, provided that the above copyright notice
1850  * appear in all copies and that both that copyright notice and
1851  * this permission notice appear in supporting documentation.
1852  * Hewlett-Packard Company makes no representations about the
1853  * suitability of this software for any purpose. It is provided
1854  * "as is" without express or implied warranty.
1855  V5.05:0009 */
1856
1857
1858