|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r48723 - sandbox/move/libs/move/doc
From: daniel_james_at_[hidden]
Date: 2008-09-11 03:10:31
Author: danieljames
Date: 2008-09-11 03:10:31 EDT (Thu, 11 Sep 2008)
New Revision: 48723
URL: http://svn.boost.org/trac/boost/changeset/48723
Log:
Move only example.
Text files modified:
sandbox/move/libs/move/doc/listing1.cpp | 49 ++++++++++++++++++++++++++++++++++++++-
sandbox/move/libs/move/doc/move.qbk | 20 ++++++++++++++++
2 files changed, 67 insertions(+), 2 deletions(-)
Modified: sandbox/move/libs/move/doc/listing1.cpp
==============================================================================
--- sandbox/move/libs/move/doc/listing1.cpp (original)
+++ sandbox/move/libs/move/doc/listing1.cpp 2008-09-11 03:10:31 EDT (Thu, 11 Sep 2008)
@@ -58,11 +58,56 @@
implementation* member;
};
-int main()
+void example1()
{
movable x(10);
movable y = x;
+}
+//]
+
+//[move_only
+class move_only : public boost::equality_comparable<move_only>
+{
+ move_only(move_only&);
+
+ public:
+
+ explicit move_only(int x = 0) : member(new implementation(x)) { }
+ ~move_only() { delete member; }
+
+ // Move Constructor
+ move_only(boost::move_from<move_only> rhs) : member(rhs.source.member)
+ { rhs.source.member = 0; }
+
+ // operator=() on a move_only type takes parameter by value and consumes it
+ // note that this type can't be copied, so operator= can only be called
+ // for a moved value.
+ move_only& operator=(move_only x)
+ { swap(*this, x); return *this; }
- return 0;
+ operator boost::move_from<move_only>()
+ { return boost::move_from<move_only>(*this); }
+
+ friend bool operator==(const move_only& x, const move_only &y)
+ { return *x.member == *y.member; }
+
+ friend void swap(move_only& x, move_only& y)
+ { swap(x.member, y.member); }
+
+ private:
+ implementation* member;
+};
+
+void example2()
+{
+ move_only x(20);
+ move_only y = boost::move(x);
}
//]
+
+int main() {
+ std::cout<<"Example 1:\n\n";
+ example1();
+ std::cout<<"\nExample 2:\n\n";
+ example2();
+}
Modified: sandbox/move/libs/move/doc/move.qbk
==============================================================================
--- sandbox/move/libs/move/doc/move.qbk (original)
+++ sandbox/move/libs/move/doc/move.qbk 2008-09-11 03:10:31 EDT (Thu, 11 Sep 2008)
@@ -32,11 +32,17 @@
[section Implementing a Movable Type]
+Boost.Move provides two methods to implement a movable, one for types that are both copyable and movable, and one for types that are movable but not copyable ('move only' types).
+
+[section Movable and Copyable]
+
+[/
A movable type models __concept_movable__. There are three components of a movable type:
* Satisfy the requirements of concept __concept_regular_type__.
* Implement a move-ctor using `move_from<>`.
* Modify the assignment operator to take the operand by value and consume it.
+]
A typical implementation of the move-ctor will simply extract the remote part, leaving the
source in a destructible state.
@@ -61,6 +67,20 @@
[endsect]
+[section Move Only]
+
+'''<example id="listing1.1">
+<title>Movable class</title>
+'''[move_only]'''
+<simpara>Output:</simpara>
+<screen>
+</screen>
+</example>'''
+
+[endsect]
+
+[endsect]
+
[section Returning a Movable Type]
We can return a movable type from a function by value and unnessary copies will be avoided as
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