| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | |
| 4 * | |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | |
| 6 * it under the terms of the GNU General Public License version 3 as | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 /** | |
| 19 * Allows construction of an object in one block and usage in another. | |
| 20 * | |
| 21 * Splitting construction and usage is not necessary in all cases, | |
| 22 * but there are many when it is. | |
| 23 * In any relevant case it's necessary to define something in a context that con tains both blocks. | |
| 24 * One is when the class is not default-constructible. | |
| 25 * You might be able to construct an initial value if you know the class, | |
| 26 * but that's not the case in general for templates. | |
| 27 * Another is when there's no copy assignment available, | |
| 28 * such as std::unique_ptr. | |
| 29 * | |
| 30 * A 'Placeholder' can be initialized with either a copy or move constructor. | |
| 31 * We can't use assignment to initialize, because (in general) assignment assume s | |
| 32 * that the lvalue target is already initialized. | |
| 33 */ | |
| 34 template<class T> | |
| 35 class Placeholder | |
|
sergei
2015/10/01 15:50:07
The destructor is missing.
Eric
2015/10/08 21:05:41
What would such a destructor do?
| |
| 36 { | |
| 37 /** | |
| 38 * We 'allocate' memory for the object in the instance itself; | |
| 39 * there's no use of the heap. | |
| 40 * | |
| 41 * Note that it's a normative standard that sizeof(char)==1 | |
| 42 */ | |
| 43 char storage[sizeof(T)]; | |
| 44 | |
| 45 public: | |
| 46 /** | |
| 47 * Initialize by copying a glvalue into our private storage. | |
| 48 */ | |
| 49 void Construct(const T& x) | |
| 50 { | |
| 51 /* | |
| 52 * We use placement-new syntax to use our private storage. | |
| 53 * The constructor expression invokes the copy constructor. | |
| 54 */ | |
| 55 new (&storage[0]) T(x); | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Initialize by moving an rvalue into our private storage. | |
| 60 */ | |
| 61 void Construct(T&& x) | |
| 62 { | |
| 63 /* | |
| 64 * We use placement-new syntax to use our private storage. | |
| 65 * We explicitly cast to an rvalue to ensure use of the move constructor. | |
| 66 */ | |
| 67 new (&storage[0]) T(std::move(x)); | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Access the object in storage. | |
| 72 */ | |
| 73 T& Object() | |
| 74 { | |
| 75 return *reinterpret_cast<T *>(&storage[0]); | |
| 76 } | |
| 77 }; | |
| 78 | |
| OLD | NEW |