Exchange the states of *this and rhs.
| 180 | |
| 181 | /// Exchange the states of *this and rhs. |
| 182 | void swap(any& rhs) noexcept |
| 183 | { |
| 184 | if(this->vtable != rhs.vtable) |
| 185 | { |
| 186 | any tmp(std::move(rhs)); |
| 187 | |
| 188 | // move from *this to rhs. |
| 189 | rhs.vtable = this->vtable; |
| 190 | if(this->vtable != nullptr) |
| 191 | { |
| 192 | this->vtable->move(this->storage, rhs.storage); |
| 193 | //this->vtable = nullptr; -- unneeded, see below |
| 194 | } |
| 195 | |
| 196 | // move from tmp (previously rhs) to *this. |
| 197 | this->vtable = tmp.vtable; |
| 198 | if(tmp.vtable != nullptr) |
| 199 | { |
| 200 | tmp.vtable->move(tmp.storage, this->storage); |
| 201 | tmp.vtable = nullptr; |
| 202 | } |
| 203 | } |
| 204 | else // same types |
| 205 | { |
| 206 | if(this->vtable != nullptr) |
| 207 | this->vtable->swap(this->storage, rhs.storage); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private: // Storage and Virtual Method Table |
| 212 |