2015-02-16 21:31:07 +01:00
|
|
|
#pragma once
|
2015-06-21 17:31:19 +02:00
|
|
|
#ifndef MESSMER_CPP_UTILS_POINTER_CAST_H_
|
|
|
|
#define MESSMER_CPP_UTILS_POINTER_CAST_H_
|
2015-02-16 21:31:07 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace cpputils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dynamic_cast implementation for unique_ptr (moving unique_ptr into a unique_ptr of different type)
|
|
|
|
*/
|
2015-04-27 22:13:28 +02:00
|
|
|
//TODO Also allow passing a rvalue reference, otherwise dynamic_pointer_move(func()) won't work
|
2015-02-16 21:31:07 +01:00
|
|
|
template<typename DST, typename SRC>
|
|
|
|
inline std::unique_ptr<DST> dynamic_pointer_move(std::unique_ptr<SRC> &source) {
|
|
|
|
//TODO Deleter
|
|
|
|
DST *casted = dynamic_cast<DST*>(source.get());
|
|
|
|
if (casted != nullptr) {
|
|
|
|
source.release();
|
|
|
|
}
|
|
|
|
return std::unique_ptr<DST>(casted);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|