epoc32/include/stdapis/boost/multi_index/detail/auto_space.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
williamr@2
     1
/* Copyright 2003-2005 Joaquín M López Muñoz.
williamr@2
     2
 * Distributed under the Boost Software License, Version 1.0.
williamr@2
     3
 * (See accompanying file LICENSE_1_0.txt or copy at
williamr@2
     4
 * http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     5
 *
williamr@2
     6
 * See http://www.boost.org/libs/multi_index for library home page.
williamr@2
     7
 */
williamr@2
     8
williamr@2
     9
#ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
williamr@2
    10
#define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
williamr@2
    11
williamr@2
    12
#if defined(_MSC_VER)&&(_MSC_VER>=1200)
williamr@2
    13
#pragma once
williamr@2
    14
#endif
williamr@2
    15
williamr@2
    16
#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
williamr@2
    17
#include <algorithm>
williamr@2
    18
#include <boost/detail/allocator_utilities.hpp>
williamr@2
    19
#include <boost/noncopyable.hpp>
williamr@2
    20
#include <memory>
williamr@2
    21
williamr@2
    22
namespace boost{
williamr@2
    23
williamr@2
    24
namespace multi_index{
williamr@2
    25
williamr@2
    26
namespace detail{
williamr@2
    27
williamr@2
    28
/* auto_space provides uninitialized space suitably to store
williamr@2
    29
 * a given number of elements of a given type.
williamr@2
    30
 */
williamr@2
    31
williamr@2
    32
/* NB: it is not clear whether using an allocator to handle
williamr@2
    33
 * zero-sized arrays of elements is conformant or not. GCC 3.3.1
williamr@2
    34
 * and prior fail here, other stdlibs handle the issue gracefully.
williamr@2
    35
 * To be on the safe side, the case n==0 is given special treatment.
williamr@2
    36
 * References:
williamr@2
    37
 *   GCC Bugzilla, "standard allocator crashes when deallocating segment
williamr@2
    38
 *    "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
williamr@2
    39
 *   C++ Standard Library Defect Report List (Revision 28), issue 199
williamr@2
    40
 *     "What does allocate(0) return?",
williamr@2
    41
 *     http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#199
williamr@2
    42
 */
williamr@2
    43
williamr@2
    44
template<typename T,typename Allocator=std::allocator<T> >
williamr@2
    45
struct auto_space:private noncopyable
williamr@2
    46
{
williamr@2
    47
  explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
williamr@2
    48
  al_(al),n_(n),data_(n_?al_.allocate(n_):0)
williamr@2
    49
  {}
williamr@2
    50
williamr@2
    51
  ~auto_space()
williamr@2
    52
  {
williamr@2
    53
    if(n_)al_.deallocate(data_,n_);
williamr@2
    54
  }
williamr@2
    55
williamr@2
    56
  Allocator get_allocator()const{return al_;}
williamr@2
    57
williamr@2
    58
  T* data()const{return data_;}
williamr@2
    59
williamr@2
    60
  void swap(auto_space& x)
williamr@2
    61
  {
williamr@2
    62
    std::swap(n_,x.n_);
williamr@2
    63
    std::swap(data_,x.data_);
williamr@2
    64
  }
williamr@2
    65
    
williamr@2
    66
private:
williamr@2
    67
  typename boost::detail::allocator::rebind_to<
williamr@2
    68
    Allocator,T>::type                          al_;
williamr@2
    69
  std::size_t                                   n_;
williamr@2
    70
  T*                                            data_;
williamr@2
    71
};
williamr@2
    72
williamr@2
    73
template<typename T,typename Allocator>
williamr@2
    74
void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
williamr@2
    75
{
williamr@2
    76
  x.swap(y);
williamr@2
    77
}
williamr@2
    78
williamr@2
    79
} /* namespace multi_index::detail */
williamr@2
    80
williamr@2
    81
} /* namespace multi_index */
williamr@2
    82
williamr@2
    83
} /* namespace boost */
williamr@2
    84
williamr@2
    85
#endif