Sunshine master
Self-hosted game stream host for Moonlight.
move_by_copy.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <utility>
9
18 template<class T>
19 class MoveByCopy {
20 public:
24 typedef T move_type;
25
26 private:
27 move_type _to_move;
28
29 public:
35 explicit MoveByCopy(move_type &&to_move):
36 _to_move(std::move(to_move)) {
37 }
38
44 MoveByCopy(MoveByCopy &&other) = default;
45
51 MoveByCopy(const MoveByCopy &other) {
52 *this = other;
53 }
54
61 MoveByCopy &operator=(MoveByCopy &&other) = default;
62
70 this->_to_move = std::move(const_cast<MoveByCopy &>(other)._to_move);
71
72 return *this;
73 }
74
78 operator move_type() {
79 return std::move(_to_move);
80 }
81 };
82
89 template<class T>
90 MoveByCopy<T> cmove(T &movable) {
91 return MoveByCopy<T>(std::move(movable));
92 }
93
94 // Do NOT use this unless you are absolutely certain the object to be moved is no longer used by the caller
101 template<class T>
102 MoveByCopy<T> const_cmove(const T &movable) {
103 return MoveByCopy<T>(std::move(const_cast<T &>(movable)));
104 }
105} // namespace move_by_copy_util
Definition move_by_copy.h:19
T move_type
Wrapped type moved through copy-shaped APIs.
Definition move_by_copy.h:24
MoveByCopy(const MoveByCopy &other)
Copy by moving the stored object out of another wrapper.
Definition move_by_copy.h:51
MoveByCopy & operator=(MoveByCopy &&other)=default
Move-assign the wrapped object from another wrapper.
MoveByCopy & operator=(const MoveByCopy &other)
Copy-assign by moving the wrapped object out of the source wrapper.
Definition move_by_copy.h:69
MoveByCopy(move_type &&to_move)
Store a move-only value for transfer through copy-only call sites.
Definition move_by_copy.h:35
MoveByCopy(MoveByCopy &&other)=default
Move the stored object from another wrapper.
Contains utilities for moving objects by copying them.
Definition move_by_copy.h:13
MoveByCopy< T > const_cmove(const T &movable)
Copy-shape wrapper for moving from a const reference.
Definition move_by_copy.h:102
MoveByCopy< T > cmove(T &movable)
Copy a move-only value by moving from the source reference.
Definition move_by_copy.h:90