Ticket #2243: swap_value_initialized.patch

File swap_value_initialized.patch, 3.3 KB (added by niels_dekker, 14 years ago)
  • boost/utility/value_init.hpp

     
    77// 21 Ago 2002 (Created) Fernando Cacciola
    88// 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
    99// 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
     10// 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
    1011//
    1112#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
    1213#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
     
    2223#include <boost/static_assert.hpp>
    2324#include <boost/type_traits/cv_traits.hpp>
    2425#include <boost/type_traits/alignment_of.hpp>
     26#include <boost/swap.hpp>
    2527#include <cstring>
    2628#include <new>
    2729
     
    9395      return wrapper_address()->data;
    9496    }
    9597
     98    void swap(value_initialized & arg)
     99    {
     100      ::boost::swap( this->data(), arg.data() );
     101    }
     102
    96103    operator T&() const { return this->data(); }
    97104
    98105} ;
     
    110117  return x.data() ;
    111118}
    112119
     120template<class T>
     121void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
     122{
     123  lhs.swap(rhs) ;
     124}
    113125
     126
    114127class initialized_value_t
    115128{
    116129  public :
  • libs/utility/value_init_test.cpp

     
    99// 21 Ago 2002 (Created) Fernando Cacciola
    1010// 15 Jan 2008 (Added tests regarding compiler issues) Fernando Cacciola, Niels Dekker
    1111// 23 May 2008 (Added tests regarding initialized_value) Niels Dekker
     12// 21 Ago 2008 (Added swap test) Niels Dekker
    1213
    1314#include <cstring>  // For memcmp.
    1415#include <iostream>
     
    181182};
    182183
    183184
     185//
     186// A struct that allows testing whether its customized swap function is called.
     187//
     188struct SwapFunctionCallTester
     189{
     190  bool is_custom_swap_called;
     191  int data;
     192
     193  SwapFunctionCallTester()
     194  : is_custom_swap_called(false), data(0) {}
     195
     196  SwapFunctionCallTester(const SwapFunctionCallTester & arg)
     197  : is_custom_swap_called(false), data(arg.data) {}
     198
     199  void swap(SwapFunctionCallTester & arg)
     200  {
     201    std::swap(data, arg.data);
     202    is_custom_swap_called = true;
     203    arg.is_custom_swap_called = true;
     204  }
     205};
     206
     207void swap(SwapFunctionCallTester & lhs, SwapFunctionCallTester & rhs)
     208{
     209  lhs.swap(rhs);
     210}
     211
     212
     213
    184214template<class T>
    185215void check_initialized_value ( T const& y )
    186216{
     
    323353  BOOST_CHECK ( ! get(copyFunctionCallTester3).is_copy_constructed);
    324354  BOOST_CHECK ( get(copyFunctionCallTester3).is_assignment_called);
    325355
     356  boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester1;
     357  boost::value_initialized<SwapFunctionCallTester> swapFunctionCallTester2;
     358  get(swapFunctionCallTester1).data = 1;
     359  get(swapFunctionCallTester2).data = 2;
     360  boost::swap(swapFunctionCallTester1, swapFunctionCallTester2);
     361  BOOST_CHECK( get(swapFunctionCallTester1).data == 2 );
     362  BOOST_CHECK( get(swapFunctionCallTester2).data == 1 );
     363  BOOST_CHECK( get(swapFunctionCallTester1).is_custom_swap_called );
     364  BOOST_CHECK( get(swapFunctionCallTester2).is_custom_swap_called );
     365
    326366  return 0;
    327367}
    328368
    329369
    330370unsigned int expected_failures = 0;
    331371
     372