xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/bits/stl_construct.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino // nonstandard construct and destroy functions -*- C++ -*-
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4*e4b17023SJohn Marino // 2009, 2010, 2011
5*e4b17023SJohn Marino // Free Software Foundation, Inc.
6*e4b17023SJohn Marino //
7*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library.  This library is free
8*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the
9*e4b17023SJohn Marino // terms of the GNU General Public License as published by the
10*e4b17023SJohn Marino // Free Software Foundation; either version 3, or (at your option)
11*e4b17023SJohn Marino // any later version.
12*e4b17023SJohn Marino 
13*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful,
14*e4b17023SJohn Marino // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*e4b17023SJohn Marino // GNU General Public License for more details.
17*e4b17023SJohn Marino 
18*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
19*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
20*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
23*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
24*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino /*
28*e4b17023SJohn Marino  *
29*e4b17023SJohn Marino  * Copyright (c) 1994
30*e4b17023SJohn Marino  * Hewlett-Packard Company
31*e4b17023SJohn Marino  *
32*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
33*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
34*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
35*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
36*e4b17023SJohn Marino  * in supporting documentation.  Hewlett-Packard Company makes no
37*e4b17023SJohn Marino  * representations about the suitability of this software for any
38*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
39*e4b17023SJohn Marino  *
40*e4b17023SJohn Marino  *
41*e4b17023SJohn Marino  * Copyright (c) 1996,1997
42*e4b17023SJohn Marino  * Silicon Graphics Computer Systems, Inc.
43*e4b17023SJohn Marino  *
44*e4b17023SJohn Marino  * Permission to use, copy, modify, distribute and sell this software
45*e4b17023SJohn Marino  * and its documentation for any purpose is hereby granted without fee,
46*e4b17023SJohn Marino  * provided that the above copyright notice appear in all copies and
47*e4b17023SJohn Marino  * that both that copyright notice and this permission notice appear
48*e4b17023SJohn Marino  * in supporting documentation.  Silicon Graphics makes no
49*e4b17023SJohn Marino  * representations about the suitability of this software for any
50*e4b17023SJohn Marino  * purpose.  It is provided "as is" without express or implied warranty.
51*e4b17023SJohn Marino  */
52*e4b17023SJohn Marino 
53*e4b17023SJohn Marino /** @file bits/stl_construct.h
54*e4b17023SJohn Marino  *  This is an internal header file, included by other library headers.
55*e4b17023SJohn Marino  *  Do not attempt to use it directly. @headername{memory}
56*e4b17023SJohn Marino  */
57*e4b17023SJohn Marino 
58*e4b17023SJohn Marino #ifndef _STL_CONSTRUCT_H
59*e4b17023SJohn Marino #define _STL_CONSTRUCT_H 1
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino #include <new>
62*e4b17023SJohn Marino #include <bits/move.h>
63*e4b17023SJohn Marino #include <ext/alloc_traits.h>
64*e4b17023SJohn Marino 
_GLIBCXX_VISIBILITY(default)65*e4b17023SJohn Marino namespace std _GLIBCXX_VISIBILITY(default)
66*e4b17023SJohn Marino {
67*e4b17023SJohn Marino _GLIBCXX_BEGIN_NAMESPACE_VERSION
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino   /**
70*e4b17023SJohn Marino    * Constructs an object in existing memory by invoking an allocated
71*e4b17023SJohn Marino    * object's constructor with an initializer.
72*e4b17023SJohn Marino    */
73*e4b17023SJohn Marino #ifdef __GXX_EXPERIMENTAL_CXX0X__
74*e4b17023SJohn Marino   template<typename _T1, typename... _Args>
75*e4b17023SJohn Marino     inline void
76*e4b17023SJohn Marino     _Construct(_T1* __p, _Args&&... __args)
77*e4b17023SJohn Marino     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
78*e4b17023SJohn Marino #else
79*e4b17023SJohn Marino   template<typename _T1, typename _T2>
80*e4b17023SJohn Marino     inline void
81*e4b17023SJohn Marino     _Construct(_T1* __p, const _T2& __value)
82*e4b17023SJohn Marino     {
83*e4b17023SJohn Marino       // _GLIBCXX_RESOLVE_LIB_DEFECTS
84*e4b17023SJohn Marino       // 402. wrong new expression in [some_]allocator::construct
85*e4b17023SJohn Marino       ::new(static_cast<void*>(__p)) _T1(__value);
86*e4b17023SJohn Marino     }
87*e4b17023SJohn Marino #endif
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino   /**
90*e4b17023SJohn Marino    * Destroy the object pointed to by a pointer type.
91*e4b17023SJohn Marino    */
92*e4b17023SJohn Marino   template<typename _Tp>
93*e4b17023SJohn Marino     inline void
94*e4b17023SJohn Marino     _Destroy(_Tp* __pointer)
95*e4b17023SJohn Marino     { __pointer->~_Tp(); }
96*e4b17023SJohn Marino 
97*e4b17023SJohn Marino   template<bool>
98*e4b17023SJohn Marino     struct _Destroy_aux
99*e4b17023SJohn Marino     {
100*e4b17023SJohn Marino       template<typename _ForwardIterator>
101*e4b17023SJohn Marino         static void
102*e4b17023SJohn Marino         __destroy(_ForwardIterator __first, _ForwardIterator __last)
103*e4b17023SJohn Marino 	{
104*e4b17023SJohn Marino 	  for (; __first != __last; ++__first)
105*e4b17023SJohn Marino 	    std::_Destroy(std::__addressof(*__first));
106*e4b17023SJohn Marino 	}
107*e4b17023SJohn Marino     };
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino   template<>
110*e4b17023SJohn Marino     struct _Destroy_aux<true>
111*e4b17023SJohn Marino     {
112*e4b17023SJohn Marino       template<typename _ForwardIterator>
113*e4b17023SJohn Marino         static void
114*e4b17023SJohn Marino         __destroy(_ForwardIterator, _ForwardIterator) { }
115*e4b17023SJohn Marino     };
116*e4b17023SJohn Marino 
117*e4b17023SJohn Marino   /**
118*e4b17023SJohn Marino    * Destroy a range of objects.  If the value_type of the object has
119*e4b17023SJohn Marino    * a trivial destructor, the compiler should optimize all of this
120*e4b17023SJohn Marino    * away, otherwise the objects' destructors must be invoked.
121*e4b17023SJohn Marino    */
122*e4b17023SJohn Marino   template<typename _ForwardIterator>
123*e4b17023SJohn Marino     inline void
124*e4b17023SJohn Marino     _Destroy(_ForwardIterator __first, _ForwardIterator __last)
125*e4b17023SJohn Marino     {
126*e4b17023SJohn Marino       typedef typename iterator_traits<_ForwardIterator>::value_type
127*e4b17023SJohn Marino                        _Value_type;
128*e4b17023SJohn Marino       std::_Destroy_aux<__has_trivial_destructor(_Value_type)>::
129*e4b17023SJohn Marino 	__destroy(__first, __last);
130*e4b17023SJohn Marino     }
131*e4b17023SJohn Marino 
132*e4b17023SJohn Marino   /**
133*e4b17023SJohn Marino    * Destroy a range of objects using the supplied allocator.  For
134*e4b17023SJohn Marino    * nondefault allocators we do not optimize away invocation of
135*e4b17023SJohn Marino    * destroy() even if _Tp has a trivial destructor.
136*e4b17023SJohn Marino    */
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino   template <typename _Tp> class allocator;
139*e4b17023SJohn Marino 
140*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Allocator>
141*e4b17023SJohn Marino     void
142*e4b17023SJohn Marino     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
143*e4b17023SJohn Marino 	     _Allocator& __alloc)
144*e4b17023SJohn Marino     {
145*e4b17023SJohn Marino       typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
146*e4b17023SJohn Marino       for (; __first != __last; ++__first)
147*e4b17023SJohn Marino 	__traits::destroy(__alloc, std::__addressof(*__first));
148*e4b17023SJohn Marino     }
149*e4b17023SJohn Marino 
150*e4b17023SJohn Marino   template<typename _ForwardIterator, typename _Tp>
151*e4b17023SJohn Marino     inline void
152*e4b17023SJohn Marino     _Destroy(_ForwardIterator __first, _ForwardIterator __last,
153*e4b17023SJohn Marino 	     allocator<_Tp>&)
154*e4b17023SJohn Marino     {
155*e4b17023SJohn Marino       _Destroy(__first, __last);
156*e4b17023SJohn Marino     }
157*e4b17023SJohn Marino 
158*e4b17023SJohn Marino _GLIBCXX_END_NAMESPACE_VERSION
159*e4b17023SJohn Marino } // namespace
160*e4b17023SJohn Marino 
161*e4b17023SJohn Marino #endif /* _STL_CONSTRUCT_H */
162*e4b17023SJohn Marino 
163