Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52793 - sandbox/boost0x/boost
From: bgreen0_at_[hidden]
Date: 2009-05-05 23:09:29


Author: bgreen
Date: 2009-05-05 23:09:26 EDT (Tue, 05 May 2009)
New Revision: 52793
URL: http://svn.boost.org/trac/boost/changeset/52793

Log:
conversion of any.hpp to use rvalue references

Text files modified:
   sandbox/boost0x/boost/any.hpp | 52 ++++++++++++++++++++++++++++++++++++---
   1 files changed, 47 insertions(+), 5 deletions(-)

Modified: sandbox/boost0x/boost/any.hpp
==============================================================================
--- sandbox/boost0x/boost/any.hpp (original)
+++ sandbox/boost0x/boost/any.hpp 2009-05-05 23:09:26 EDT (Tue, 05 May 2009)
@@ -12,6 +12,7 @@
 
 #include <algorithm>
 #include <typeinfo>
+#include <utility>
 
 #include "boost/config.hpp"
 #include <boost/type_traits/remove_reference.hpp>
@@ -31,8 +32,8 @@
         }
 
         template<typename ValueType>
- any(const ValueType & value)
- : content(new holder<ValueType>(value))
+ any(ValueType && value)
+ : content(new holder<typename remove_reference<ValueType>::type>(std::forward<ValueType>(value)))
         {
         }
 
@@ -41,6 +42,17 @@
         {
         }
 
+ any(any & other)
+ : content(other.content ? other.content->clone() : 0)
+ {
+ }
+
+ any(any && other)
+ : content(other.content)
+ {
+ other.content = 0;
+ }
+
         ~any()
         {
             delete content;
@@ -55,14 +67,22 @@
         }
 
         template<typename ValueType>
- any & operator=(const ValueType & rhs)
+ any & operator=(ValueType && rhs)
         {
- any(rhs).swap(*this);
+ any(std::forward<ValueType>(rhs)).swap(*this);
             return *this;
         }
 
- any & operator=(any rhs)
+ any & operator=(const any & rhs)
         {
+ any a(rhs);
+ a.swap(*this);
+ return *this;
+ }
+
+ any & operator=(any && rhs)
+ {
+ clear();
             rhs.swap(*this);
             return *this;
         }
@@ -74,6 +94,11 @@
             return !content;
         }
 
+ void clear() {
+ delete content;
+ content = 0;
+ }
+
         const std::type_info & type() const
         {
             return content ? content->type() : typeid(void);
@@ -111,6 +136,11 @@
             {
             }
 
+ holder(ValueType && value)
+ : held(std::forward<ValueType>(value))
+ {
+ }
+
         public: // queries
 
             virtual const std::type_info & type() const
@@ -197,6 +227,18 @@
     }
 
     template<typename ValueType>
+ ValueType any_cast(any && operand)
+ {
+ BOOST_STATIC_ASSERT(!is_reference<ValueType>::value);
+
+ ValueType * result = any_cast<ValueType>(&operand);
+ if(!result)
+ boost::throw_exception(bad_any_cast());
+
+ return std::move(*result);
+ }
+
+ template<typename ValueType>
     inline ValueType any_cast(const any & operand)
     {
         typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk