1221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org/* 2221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 3221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * 4221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * Use of this source code is governed by a BSD-style license 5221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * that can be found in the LICENSE file in the root of the source 6221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * tree. An additional intellectual property rights grant can be found 7221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * in the file PATENTS. All contributing project authors may 8221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org */ 10221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org 11221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Borrowed from Chromium's src/base/move.h. 12221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org 13221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org#ifndef WEBRTC_SYSTEM_WRAPPERS_INTEFACE_MOVE_H_ 14221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org#define WEBRTC_SYSTEM_WRAPPERS_INTEFACE_MOVE_H_ 15221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org 16221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Macro with the boilerplate that makes a type move-only in C++03. 17221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 18221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// USAGE 19221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 20221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// This macro should be used instead of DISALLOW_COPY_AND_ASSIGN to create 21221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// a "move-only" type. Unlike DISALLOW_COPY_AND_ASSIGN, this macro should be 22221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// the first line in a class declaration. 23221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 24221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// A class using this macro must call .Pass() (or somehow be an r-value already) 25221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// before it can be: 26221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 27221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * Passed as a function argument 28221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * Used as the right-hand side of an assignment 29221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * Returned from a function 30221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 31221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Each class will still need to define their own "move constructor" and "move 32221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// operator=" to make this useful. Here's an example of the macro, the move 33221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// constructor, and the move operator= from the scoped_ptr class: 34221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 35221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// template <typename T> 36221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// class scoped_ptr { 37221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) 38221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// public: 39221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// scoped_ptr(RValue& other) : ptr_(other.release()) { } 40221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// scoped_ptr& operator=(RValue& other) { 41221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// swap(other); 42221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// return *this; 43221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// } 44221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// }; 45221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 46221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Note that the constructor must NOT be marked explicit. 47221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 48221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// For consistency, the second parameter to the macro should always be RValue 49221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// unless you have a strong reason to do otherwise. It is only exposed as a 50221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// macro parameter so that the move constructor and move operator= don't look 51221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// like they're using a phantom type. 52221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 53221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 54221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// HOW THIS WORKS 55221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 56221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// For a thorough explanation of this technique, see: 57221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 58221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Move_Constructor 59221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 60221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// The summary is that we take advantage of 2 properties: 61221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 62221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 1) non-const references will not bind to r-values. 63221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 2) C++ can apply one user-defined conversion when initializing a 64221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// variable. 65221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 66221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// The first lets us disable the copy constructor and assignment operator 67221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// by declaring private version of them with a non-const reference parameter. 68221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 69221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// For l-values, direct initialization still fails like in 70221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// DISALLOW_COPY_AND_ASSIGN because the copy constructor and assignment 71221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// operators are private. 72221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 73221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// For r-values, the situation is different. The copy constructor and 74221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// assignment operator are not viable due to (1), so we are trying to call 75221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// a non-existent constructor and non-existing operator= rather than a private 76221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// one. Since we have not committed an error quite yet, we can provide an 77221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// alternate conversion sequence and a constructor. We add 78221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 79221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * a private struct named "RValue" 80221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * a user-defined conversion "operator RValue()" 81221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// * a "move constructor" and "move operator=" that take the RValue& as 82221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// their sole parameter. 83221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 84221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Only r-values will trigger this sequence and execute our "move constructor" 85221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// or "move operator=." L-values will match the private copy constructor and 86221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// operator= first giving a "private in this context" error. This combination 87221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// gives us a move-only type. 88221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 89221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// For signaling a destructive transfer of data from an l-value, we provide a 90221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// method named Pass() which creates an r-value for the current instance 91221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// triggering the move constructor or move operator=. 92221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 93221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Other ways to get r-values is to use the result of an expression like a 94221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// function call. 95221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 96221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Here's an example with comments explaining what gets triggered where: 97221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 98221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// class Foo { 99221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// MOVE_ONLY_TYPE_FOR_CPP_03(Foo, RValue); 100221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 101221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// public: 102221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// ... API ... 103221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo(RValue other); // Move constructor. 104221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo& operator=(RValue rhs); // Move operator= 105221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// }; 106221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 107221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo MakeFoo(); // Function that returns a Foo. 108221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 109221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f; 110221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f_copy(f); // ERROR: Foo(Foo&) is private in this context. 111221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f_assign; 112221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// f_assign = f; // ERROR: operator=(Foo&) is private in this context. 113221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 114221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 115221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f(MakeFoo()); // R-value so alternate conversion executed. 116221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f_copy(f.Pass()); // R-value so alternate conversion executed. 117221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// f = f_copy.Pass(); // R-value so alternate conversion executed. 118221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 119221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 120221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// IMPLEMENTATION SUBTLETIES WITH RValue 121221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 122221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// The RValue struct is just a container for a pointer back to the original 123221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// object. It should only ever be created as a temporary, and no external 124221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// class should ever declare it or use it in a parameter. 125221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 126221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// It is tempting to want to use the RValue type in function parameters, but 127221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// excluding the limited usage here for the move constructor and move 128221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// operator=, doing so would mean that the function could take both r-values 129221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// and l-values equially which is unexpected. See COMPARED To Boost.Move for 130221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// more details. 131221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 132221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// An alternate, and incorrect, implementation of the RValue class used by 133221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Boost.Move makes RValue a fieldless child of the move-only type. RValue& 134221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// is then used in place of RValue in the various operators. The RValue& is 135221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// "created" by doing *reinterpret_cast<RValue*>(this). This has the appeal 136221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// of never creating a temporary RValue struct even with optimizations 137221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// disabled. Also, by virtue of inheritance you can treat the RValue 138221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// reference as if it were the move-only type itself. Unfortunately, 139221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// using the result of this reinterpret_cast<> is actually undefined behavior 140221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// due to C++98 5.2.10.7. In certain compilers (e.g., NaCl) the optimizer 141221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// will generate non-working code. 142221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 143221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// In optimized builds, both implementations generate the same assembly so we 144221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// choose the one that adheres to the standard. 145221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 146221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 1476958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// WHY HAVE typedef void MoveOnlyTypeForCPP03 1486958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// 1496958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// Callback<>/Bind() needs to understand movable-but-not-copyable semantics 1506958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// to call .Pass() appropriately when it is expected to transfer the value. 1516958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// The cryptic typedef MoveOnlyTypeForCPP03 is added to make this check 1526958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// easy and automatic in helper templates for Callback<>/Bind(). 1536958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// See IsMoveOnlyType template and its usage in base/callback_internal.h 1546958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// for more details. 1556958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// 1566958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org// 157221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// COMPARED TO C++11 158221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 159221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// In C++11, you would implement this functionality using an r-value reference 160221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// and our .Pass() method would be replaced with a call to std::move(). 161221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 162221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// This emulation also has a deficiency where it uses up the single 163221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// user-defined conversion allowed by C++ during initialization. This can 164221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// cause problems in some API edge cases. For instance, in scoped_ptr, it is 165221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// impossible to make a function "void Foo(scoped_ptr<Parent> p)" accept a 166221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// value of type scoped_ptr<Child> even if you add a constructor to 167221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// scoped_ptr<> that would make it look like it should work. C++11 does not 168221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// have this deficiency. 169221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 170221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 171221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// COMPARED TO Boost.Move 172221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 173221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Our implementation similar to Boost.Move, but we keep the RValue struct 174221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// private to the move-only type, and we don't use the reinterpret_cast<> hack. 175221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 176221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// In Boost.Move, RValue is the boost::rv<> template. This type can be used 177221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// when writing APIs like: 178221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 179221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// void MyFunc(boost::rv<Foo>& f) 180221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 181221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// that can take advantage of rv<> to avoid extra copies of a type. However you 182221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// would still be able to call this version of MyFunc with an l-value: 183221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 184221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Foo f; 185221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// MyFunc(f); // Uh oh, we probably just destroyed |f| w/o calling Pass(). 186221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 187221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// unless someone is very careful to also declare a parallel override like: 188221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 189221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// void MyFunc(const Foo& f) 190221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 191221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// that would catch the l-values first. This was declared unsafe in C++11 and 192221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// a C++11 compiler will explicitly fail MyFunc(f). Unfortunately, we cannot 193221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// ensure this in C++03. 194221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 195221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Since we have no need for writing such APIs yet, our implementation keeps 196221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// RValue private and uses a .Pass() method to do the conversion instead of 197221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// trying to write a version of "std::move()." Writing an API like std::move() 198221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// would require the RValue struct to be public. 199221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 200221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 201221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// CAVEATS 202221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 203221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// If you include a move-only type as a field inside a class that does not 204221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// explicitly declare a copy constructor, the containing class's implicit 205221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// copy constructor will change from Containing(const Containing&) to 206221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// Containing(Containing&). This can cause some unexpected errors. 207221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 208221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// http://llvm.org/bugs/show_bug.cgi?id=11528 209221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 210221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// The workaround is to explicitly declare your copy constructor. 211221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org// 21252ec7b651de9ce767c8143068de4c0991b78d45aandrew@webrtc.org#define WEBRTC_MOVE_ONLY_TYPE_FOR_CPP_03(type, rvalue_type) \ 213221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org private: \ 214221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org struct rvalue_type { \ 215221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org explicit rvalue_type(type* object) : object(object) {} \ 216221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org type* object; \ 217221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org }; \ 218221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org type(type&); \ 219221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org void operator=(type&); \ 220221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org public: \ 221221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org operator rvalue_type() { return rvalue_type(this); } \ 222221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org type Pass() { return type(rvalue_type(this)); } \ 2236958c0fa5ef13bb11a209e003aca5a2a32e28c06andrew@webrtc.org typedef void MoveOnlyTypeForCPP03; \ 224221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org private: 225221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org 226221798a7c4ad83ba7fb8b3ae39b99d44278bbe5aandrew@webrtc.org#endif // WEBRTC_SYSTEM_WRAPPERS_INTEFACE_MOVE_H_ 227