1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* C++11-style, but C++98-usable, "move references" implementation. */
8 #ifndef mozilla_Move_h_
9 #define mozilla_Move_h_
16 * Some types can be copied much more efficiently if we know the original's
17 * value need not be preserved --- that is, if we are doing a "move", not a
18 * "copy". For example, if we have:
23 * the constructor for v must apply a copy constructor to each element of u ---
24 * taking time linear in the length of u. However, if we know we will not need u
25 * any more once v has been initialized, then we could initialize v very
26 * efficiently simply by stealing u's dynamically allocated buffer and giving it
27 * to v --- a constant-time operation, regardless of the size of u.
29 * Moves often appear in container implementations. For example, when we append
30 * to a vector, we may need to resize its buffer. This entails moving each of
31 * its extant elements from the old, smaller buffer to the new, larger buffer.
32 * But once the elements have been migrated, we're just going to throw away the
33 * old buffer; we don't care if they still have their values. So if the vector's
34 * element type can implement "move" more efficiently than "copy", the vector
35 * resizing should by all means use a "move" operation. Hash tables also need to
38 * The details of the optimization, and whether it's worth applying, vary from
39 * one type to the next. And while some constructor calls are moves, many really
40 * are copies, and can't be optimized this way. So we need:
42 * 1) a way for a particular invocation of a copy constructor to say that it's
43 * really a move, and that the value of the original isn't important
44 * afterwards (although it must still be safe to destroy); and
46 * 2) a way for a type (like Vector) to announce that it can be moved more
47 * efficiently than it can be copied, and provide an implementation of that
50 * The Move(T&) function takes a reference to a T, and returns a MoveRef<T>
51 * referring to the same value; that's 1). A MoveRef<T> is simply a reference
52 * to a T, annotated to say that a copy constructor applied to it may move that
53 * T, instead of copying it. Finally, a constructor that accepts an MoveRef<T>
54 * should perform a more efficient move, instead of a copy, providing 2).
56 * So, where we might define a copy constructor for a class C like this:
58 * C(const C& rhs) { ... copy rhs to this ... }
60 * we would declare a move constructor like this:
62 * C(MoveRef<C> rhs) { ... move rhs to this ... }
64 * And where we might perform a copy like this:
68 * we would perform a move like this:
72 * Note that MoveRef<T> implicitly converts to T&, so you can pass a MoveRef<T>
73 * to an ordinary copy constructor for a type that doesn't support a special
74 * move constructor, and you'll just get a copy. This means that templates can
75 * use Move whenever they know they won't use the original value any more, even
76 * if they're not sure whether the type at hand has a specialized move
77 * constructor. If it doesn't, the MoveRef<T> will just convert to a T&, and
78 * the ordinary copy constructor will apply.
80 * A class with a move constructor can also provide a move assignment operator,
81 * which runs this's destructor, and then applies the move constructor to
82 * *this's memory. A typical definition:
84 * C& operator=(MoveRef<C> rhs) {
90 * With that in place, one can write move assignments like this:
94 * This destroys c1, moves c1's value to c2, and leaves c1 in an undefined but
97 * This header file defines MoveRef and Move in the mozilla namespace. It's up
98 * to individual containers to annotate moves as such, by calling Move; and it's
99 * up to individual types to define move constructors.
101 * One hint: if you're writing a move constructor where the type has members
102 * that should be moved themselves, it's much nicer to write this:
104 * C(MoveRef<C> c) : x(c->x), y(c->y) { }
106 * than the equivalent:
108 * C(MoveRef<C> c) { new(&x) X(c->x); new(&y) Y(c->y); }
110 * especially since GNU C++ fails to notice that this does indeed initialize x
111 * and y, which may matter if they're const.
119 explicit MoveRef(T
& t
) : pointer(&t
) { }
120 T
& operator*() const { return *pointer
; }
121 T
* operator->() const { return pointer
; }
122 operator T
& () const { return *pointer
; }
129 return MoveRef
<T
>(t
);
136 return MoveRef
<T
>(const_cast<T
&>(t
));
139 /** Swap |t| and |u| using move-construction if possible. */
149 } // namespace mozilla
151 #endif // mozilla_Move_h_