- Fix dynamic_pointer_move for unique_ref
- Add workaround to use unique_ref with boost::optional in gtest
This commit is contained in:
parent
590beac11c
commit
667151e6ed
13
unique_ref.h
13
unique_ref.h
@ -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>
|
||||||
|
20
unique_ref_boost_optional_gtest_workaround.h
Normal file
20
unique_ref_boost_optional_gtest_workaround.h
Normal 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
|
Loading…
Reference in New Issue
Block a user