- Fix dynamic_pointer_move for unique_ref

- Add workaround to use unique_ref with boost::optional in gtest
This commit is contained in:
Sebastian Meßmer 2015-06-18 19:35:30 +02:00
parent 590beac11c
commit 667151e6ed
2 changed files with 30 additions and 3 deletions

View File

@ -55,8 +55,9 @@ 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 DST, typename SRC> friend boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source);
template<typename U> friend class unique_ref; template<typename U> friend class unique_ref;
template<typename U> friend std::unique_ptr<U> to_unique_ptr(unique_ref<U> ref);
std::unique_ptr<T> _target; std::unique_ptr<T> _target;
@ -79,8 +80,14 @@ inline boost::optional<unique_ref<T>> nullcheck(std::unique_ptr<T> ptr) {
//TODO Write test cases for dynamic_pointer_move //TODO Write test cases for dynamic_pointer_move
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work //TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
template<typename DST, typename SRC> template<typename DST, typename SRC>
inline unique_ref<DST> dynamic_pointer_move(unique_ref<SRC> &source) { inline boost::optional<unique_ref<DST>> dynamic_pointer_move(unique_ref<SRC> &source) {
return unique_ref<DST>(dynamic_pointer_move<DST>(source._target)); 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);
} }
template<typename T> template<typename T>

View File

@ -0,0 +1,20 @@
#ifndef CRYFS_UNIQUE_REF_BOOST_OPTIONAL_GTEST_WORKAROUND_H
#define CRYFS_UNIQUE_REF_BOOST_OPTIONAL_GTEST_WORKAROUND_H
/**
* This is a workaround for using boost::optional<unique_ref<T>> in gtest.
* Without including this file, the linker will fail.
*/
#include "unique_ref.h"
#include <boost/optional/optional_io.hpp>
//gtest/boost::optional workaround for working with optional<unique_ref<T>>
namespace boost {
template<typename T>
inline std::ostream& operator<<(std::ostream& out, const cpputils::unique_ref<T> &ref) {
out << ref.get();
return out;
}
}
#endif //CRYFS_UNIQUE_REF_BOOST_OPTIONAL_GTEST_WORKAROUND_H