2015-06-21 17:31:19 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef MESSMER_CPP_UTILS_POINTER_UNIQUE_REF_H
|
|
|
|
#define MESSMER_CPP_UTILS_POINTER_UNIQUE_REF_H
|
2015-06-17 00:38:32 +02:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <boost/optional.hpp>
|
2015-06-21 17:31:19 +02:00
|
|
|
#include "../macros.h"
|
|
|
|
#include "cast.h"
|
2015-06-17 00:38:32 +02:00
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* unique_ref<T> behaves like unique_ptr<T>, but guarantees that the pointer points to a valid object.
|
|
|
|
* You can create objects using make_unique_ref (works like make_unique for unique_ptr).
|
|
|
|
*
|
|
|
|
* If you happen to already have a unique_ptr<T>, you can call nullcheck(unique_ptr),
|
|
|
|
* which returns optional<unique_ref<T>>.
|
|
|
|
* Take care that this should be used very rarely, since it circumvents parts of the guarantee.
|
|
|
|
* It still protects against null pointers, but it does not guarantee anymore that the pointer points
|
|
|
|
* to a valid object. It might hold an arbitrary non-null memory location.
|
|
|
|
*
|
|
|
|
* Caution: There is one way a unique_ref<T> can actually hold a nullptr.
|
|
|
|
* It will hold a nullptr after its value was moved to another unique_ref.
|
|
|
|
* Never use the old instance after moving!
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class unique_ref {
|
|
|
|
public:
|
|
|
|
|
|
|
|
unique_ref(unique_ref&& from): _target(std::move(from._target)) {}
|
2015-06-18 02:36:57 +02:00
|
|
|
// TODO Test this dynamic-cast-allowing move constructor
|
|
|
|
template<typename U> unique_ref(unique_ref<U>&& from): _target(std::move(from._target)) {}
|
2015-06-17 00:38:32 +02:00
|
|
|
|
|
|
|
unique_ref& operator=(unique_ref&& from) {
|
2015-06-18 01:29:02 +02:00
|
|
|
_target = std::move(from._target);
|
|
|
|
return *this;
|
2015-06-17 00:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typename std::add_lvalue_reference<T>::type operator*() const {
|
|
|
|
return *_target;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* get() const {
|
|
|
|
return _target.get();
|
|
|
|
}
|
|
|
|
|
2015-06-18 01:29:02 +02:00
|
|
|
void swap(unique_ref& rhs) {
|
|
|
|
std::swap(_target, rhs._target);
|
2015-06-17 00:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
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> friend boost::optional<unique_ref<U>> nullcheck(std::unique_ptr<U> ptr);
|
2015-06-18 02:36:57 +02:00
|
|
|
template<typename U> friend class unique_ref;
|
2015-06-21 17:31:19 +02:00
|
|
|
template<typename DST, typename SRC> friend boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source);
|
2015-06-18 19:35:30 +02:00
|
|
|
template<typename U> friend std::unique_ptr<U> to_unique_ptr(unique_ref<U> ref);
|
2015-06-17 00:38:32 +02:00
|
|
|
|
|
|
|
std::unique_ptr<T> _target;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(unique_ref);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline unique_ref<T> make_unique_ref(Args&&... args) {
|
|
|
|
return unique_ref<T>(std::make_unique<T>(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline boost::optional<unique_ref<T>> nullcheck(std::unique_ptr<T> ptr) {
|
|
|
|
if (ptr.get() != nullptr) {
|
|
|
|
return unique_ref<T>(std::move(ptr));
|
|
|
|
}
|
|
|
|
return boost::none;
|
|
|
|
}
|
|
|
|
|
2015-06-18 02:36:57 +02:00
|
|
|
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
|
|
|
|
template<typename DST, typename SRC>
|
2015-06-18 19:35:30 +02:00
|
|
|
inline boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source) {
|
|
|
|
return nullcheck<DST>(dynamic_pointer_move<DST>(source._target));
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO Write test cases for to_unique_ptr
|
|
|
|
template<typename T>
|
|
|
|
inline std::unique_ptr<T> to_unique_ptr(unique_ref<T> ref) {
|
|
|
|
return std::move(ref._target);
|
2015-06-18 02:36:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-18 01:29:02 +02:00
|
|
|
template<typename T>
|
|
|
|
inline bool operator==(const unique_ref<T> &lhs, const unique_ref<T> &rhs) {
|
|
|
|
return lhs.get() == rhs.get();
|
2015-06-17 00:38:32 +02:00
|
|
|
}
|
|
|
|
|
2015-06-18 01:29:02 +02:00
|
|
|
template<typename T>
|
|
|
|
inline bool operator!=(const unique_ref<T> &lhs, const unique_ref<T> &rhs) {
|
|
|
|
return !operator==(lhs, rhs);
|
2015-06-17 00:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template<typename T>
|
|
|
|
inline void swap(cpputils::unique_ref<T>& lhs, cpputils::unique_ref<T>& rhs) {
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void swap(cpputils::unique_ref<T>&& lhs, cpputils::unique_ref<T>& rhs) {
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void swap(cpputils::unique_ref<T>& lhs, cpputils::unique_ref<T>&& rhs) {
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
2015-06-18 01:29:02 +02:00
|
|
|
|
|
|
|
// Allow using it in std::unordered_set / std::unordered_map
|
|
|
|
template<typename T> struct hash<cpputils::unique_ref<T>> {
|
|
|
|
size_t operator()(const cpputils::unique_ref<T> &ref) const {
|
|
|
|
return (size_t)ref.get();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allow using it in std::map / std::set
|
|
|
|
template <typename T> struct less<cpputils::unique_ref<T>> {
|
|
|
|
bool operator()(const cpputils::unique_ref<T> &lhs, const cpputils::unique_ref<T> &rhs) const {
|
|
|
|
return lhs.get() < rhs.get();
|
|
|
|
}
|
|
|
|
};
|
2015-06-17 00:38:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|