197403Sobrien// nonstandard construct and destroy functions -*- C++ -*-
297403Sobrien
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
497403Sobrien//
597403Sobrien// This file is part of the GNU ISO C++ Library.  This library is free
697403Sobrien// software; you can redistribute it and/or modify it under the
797403Sobrien// terms of the GNU General Public License as published by the
897403Sobrien// Free Software Foundation; either version 2, or (at your option)
997403Sobrien// any later version.
1097403Sobrien
1197403Sobrien// This library is distributed in the hope that it will be useful,
1297403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of
1397403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1497403Sobrien// GNU General Public License for more details.
1597403Sobrien
1697403Sobrien// You should have received a copy of the GNU General Public License along
1797403Sobrien// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
1997403Sobrien// USA.
2097403Sobrien
2197403Sobrien// As a special exception, you may use this file as part of a free software
2297403Sobrien// library without restriction.  Specifically, if other files instantiate
2397403Sobrien// templates or use macros or inline functions from this file, or you compile
2497403Sobrien// this file and link it with other files to produce an executable, this
2597403Sobrien// file does not by itself cause the resulting executable to be covered by
2697403Sobrien// the GNU General Public License.  This exception does not however
2797403Sobrien// invalidate any other reasons why the executable file might be covered by
2897403Sobrien// the GNU General Public License.
2997403Sobrien
3097403Sobrien/*
3197403Sobrien *
3297403Sobrien * Copyright (c) 1994
3397403Sobrien * Hewlett-Packard Company
3497403Sobrien *
3597403Sobrien * Permission to use, copy, modify, distribute and sell this software
3697403Sobrien * and its documentation for any purpose is hereby granted without fee,
3797403Sobrien * provided that the above copyright notice appear in all copies and
3897403Sobrien * that both that copyright notice and this permission notice appear
3997403Sobrien * in supporting documentation.  Hewlett-Packard Company makes no
4097403Sobrien * representations about the suitability of this software for any
4197403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
4297403Sobrien *
4397403Sobrien *
4497403Sobrien * Copyright (c) 1996,1997
4597403Sobrien * Silicon Graphics Computer Systems, Inc.
4697403Sobrien *
4797403Sobrien * Permission to use, copy, modify, distribute and sell this software
4897403Sobrien * and its documentation for any purpose is hereby granted without fee,
4997403Sobrien * provided that the above copyright notice appear in all copies and
5097403Sobrien * that both that copyright notice and this permission notice appear
5197403Sobrien * in supporting documentation.  Silicon Graphics makes no
5297403Sobrien * representations about the suitability of this software for any
5397403Sobrien * purpose.  It is provided "as is" without express or implied warranty.
5497403Sobrien */
5597403Sobrien
5697403Sobrien/** @file stl_construct.h
5797403Sobrien *  This is an internal header file, included by other library headers.
5897403Sobrien *  You should not attempt to use it directly.
5997403Sobrien */
6097403Sobrien
61132720Skan#ifndef _STL_CONSTRUCT_H
62132720Skan#define _STL_CONSTRUCT_H 1
6397403Sobrien
64169691Skan#include <bits/cpp_type_traits.h>
6597403Sobrien#include <new>
6697403Sobrien
67169691Skan_GLIBCXX_BEGIN_NAMESPACE(std)
68169691Skan
6997403Sobrien  /**
7097403Sobrien   * @if maint
7197403Sobrien   * Constructs an object in existing memory by invoking an allocated
7297403Sobrien   * object's constructor with an initializer.
7397403Sobrien   * @endif
7497403Sobrien   */
75132720Skan  template<typename _T1, typename _T2>
7697403Sobrien    inline void
7797403Sobrien    _Construct(_T1* __p, const _T2& __value)
78132720Skan    {
79132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
80132720Skan      // 402. wrong new expression in [some_]allocator::construct
81132720Skan      ::new(static_cast<void*>(__p)) _T1(__value);
82132720Skan    }
83132720Skan
8497403Sobrien  /**
8597403Sobrien   * @if maint
8697403Sobrien   * Constructs an object in existing memory by invoking an allocated
8797403Sobrien   * object's default constructor (no initializers).
8897403Sobrien   * @endif
8997403Sobrien   */
90132720Skan  template<typename _T1>
9197403Sobrien    inline void
9297403Sobrien    _Construct(_T1* __p)
93132720Skan    {
94132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
95132720Skan      // 402. wrong new expression in [some_]allocator::construct
96132720Skan      ::new(static_cast<void*>(__p)) _T1();
97132720Skan    }
9897403Sobrien
9997403Sobrien  /**
10097403Sobrien   * @if maint
101132720Skan   * Destroy the object pointed to by a pointer type.
102132720Skan   * @endif
103132720Skan   */
104132720Skan  template<typename _Tp>
105132720Skan    inline void
106132720Skan    _Destroy(_Tp* __pointer)
107132720Skan    { __pointer->~_Tp(); }
108132720Skan
109132720Skan  /**
110132720Skan   * @if maint
111132720Skan   * Destroy a range of objects with nontrivial destructors.
11297403Sobrien   *
11397403Sobrien   * This is a helper function used only by _Destroy().
11497403Sobrien   * @endif
11597403Sobrien   */
116132720Skan  template<typename _ForwardIterator>
11797403Sobrien    inline void
118132720Skan    __destroy_aux(_ForwardIterator __first, _ForwardIterator __last,
119132720Skan		  __false_type)
120169691Skan    {
121169691Skan      for (; __first != __last; ++__first)
122169691Skan	std::_Destroy(&*__first);
123169691Skan    }
12497403Sobrien
12597403Sobrien  /**
12697403Sobrien   * @if maint
12797403Sobrien   * Destroy a range of objects with trivial destructors.  Since the destructors
12897403Sobrien   * are trivial, there's nothing to do and hopefully this function will be
12997403Sobrien   * entirely optimized away.
13097403Sobrien   *
13197403Sobrien   * This is a helper function used only by _Destroy().
13297403Sobrien   * @endif
13397403Sobrien   */
134132720Skan  template<typename _ForwardIterator>
13597403Sobrien    inline void
13697403Sobrien    __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type)
13797403Sobrien    { }
13897403Sobrien
13997403Sobrien  /**
14097403Sobrien   * @if maint
14197403Sobrien   * Destroy a range of objects.  If the value_type of the object has
14297403Sobrien   * a trivial destructor, the compiler should optimize all of this
14397403Sobrien   * away, otherwise the objects' destructors must be invoked.
14497403Sobrien   * @endif
14597403Sobrien   */
146132720Skan  template<typename _ForwardIterator>
14797403Sobrien    inline void
14897403Sobrien    _Destroy(_ForwardIterator __first, _ForwardIterator __last)
14997403Sobrien    {
15097403Sobrien      typedef typename iterator_traits<_ForwardIterator>::value_type
15197403Sobrien                       _Value_type;
152169691Skan      typedef typename std::__is_scalar<_Value_type>::__type
153169691Skan	               _Has_trivial_destructor;
15497403Sobrien
155132720Skan      std::__destroy_aux(__first, __last, _Has_trivial_destructor());
15697403Sobrien    }
15797403Sobrien
158169691Skan  /**
159169691Skan   * @if maint
160169691Skan   * Destroy a range of objects using the supplied allocator.  For
161169691Skan   * nondefault allocators we do not optimize away invocation of
162169691Skan   * destroy() even if _Tp has a trivial destructor.
163169691Skan   * @endif
164169691Skan   */
165169691Skan
166169691Skan  template <typename _Tp> class allocator;
167169691Skan
168169691Skan  template<typename _ForwardIterator, typename _Allocator>
169169691Skan    void
170169691Skan    _Destroy(_ForwardIterator __first, _ForwardIterator __last,
171169691Skan	     _Allocator __alloc)
172169691Skan    {
173169691Skan      for (; __first != __last; ++__first)
174169691Skan	__alloc.destroy(&*__first);
175169691Skan    }
176169691Skan
177169691Skan  template<typename _ForwardIterator, typename _Tp>
178169691Skan    inline void
179169691Skan    _Destroy(_ForwardIterator __first, _ForwardIterator __last,
180169691Skan	     allocator<_Tp>)
181169691Skan    {
182169691Skan      _Destroy(__first, __last);
183169691Skan    }
184169691Skan
185169691Skan_GLIBCXX_END_NAMESPACE
186169691Skan
187132720Skan#endif /* _STL_CONSTRUCT_H */
18897403Sobrien
189