Added dynamic_pointer_move for unique_ref

This commit is contained in:
Sebastian Meßmer 2015-06-18 02:36:57 +02:00
parent 86a8683fa7
commit 590beac11c
3 changed files with 12 additions and 2 deletions

View File

@ -7,7 +7,6 @@ using std::ofstream;
using std::ifstream; using std::ifstream;
using std::ios; using std::ios;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;

View File

@ -4,7 +4,6 @@
using namespace cpputils; using namespace cpputils;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique;
class Parent { class Parent {
public: public:

View File

@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "macros.h" #include "macros.h"
#include "pointer.h"
namespace cpputils { namespace cpputils {
@ -26,6 +27,8 @@ class unique_ref {
public: public:
unique_ref(unique_ref&& from): _target(std::move(from._target)) {} unique_ref(unique_ref&& from): _target(std::move(from._target)) {}
// TODO Test this dynamic-cast-allowing move constructor
template<typename U> unique_ref(unique_ref<U>&& from): _target(std::move(from._target)) {}
unique_ref& operator=(unique_ref&& from) { unique_ref& operator=(unique_ref&& from) {
_target = std::move(from._target); _target = std::move(from._target);
@ -52,6 +55,8 @@ private:
unique_ref(std::unique_ptr<T> target): _target(std::move(target)) {} unique_ref(std::unique_ptr<T> target): _target(std::move(target)) {}
template<typename U, typename... Args> friend unique_ref<U> make_unique_ref(Args&&... args); template<typename U, typename... Args> friend unique_ref<U> make_unique_ref(Args&&... args);
template<typename U> friend boost::optional<unique_ref<U>> nullcheck(std::unique_ptr<U> ptr); template<typename U> friend boost::optional<unique_ref<U>> nullcheck(std::unique_ptr<U> ptr);
template<typename DST, typename SRC> friend unique_ref<DST> dynamic_pointer_move(unique_ref<SRC> &source);
template<typename U> friend class unique_ref;
std::unique_ptr<T> _target; std::unique_ptr<T> _target;
@ -71,6 +76,13 @@ inline boost::optional<unique_ref<T>> nullcheck(std::unique_ptr<T> ptr) {
return boost::none; return boost::none;
} }
//TODO Write test cases for dynamic_pointer_move
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
template<typename DST, typename SRC>
inline unique_ref<DST> dynamic_pointer_move(unique_ref<SRC> &source) {
return unique_ref<DST>(dynamic_pointer_move<DST>(source._target));
}
template<typename T> template<typename T>
inline bool operator==(const unique_ref<T> &lhs, const unique_ref<T> &rhs) { inline bool operator==(const unique_ref<T> &lhs, const unique_ref<T> &rhs) {
return lhs.get() == rhs.get(); return lhs.get() == rhs.get();