Fix some issues in DataTree using OptionalOwnershipPointer

This commit is contained in:
Sebastian Messmer 2015-01-23 18:32:26 +01:00
parent 114284a58f
commit 59933036d6
2 changed files with 39 additions and 4 deletions

View File

@ -0,0 +1,34 @@
#pragma once
#ifndef FSPP_UTILS_OPTIONALOWNERSHIPPOINTER_H_
#define FSPP_UTILS_OPTIONALOWNERSHIPPOINTER_H_
#include <memory>
namespace fspp {
namespace ptr {
//TODO Test cases
template<typename T>
using optional_ownership_ptr = std::unique_ptr<T, std::function<void(T*)>>;
template<typename T>
optional_ownership_ptr<T> WithOwnership(std::unique_ptr<T> obj) {
auto deleter = obj.get_deleter();
return optional_ownership_ptr<T>(obj.release(), [deleter](T* obj){deleter(obj);});
}
template<typename T>
optional_ownership_ptr<T> WithoutOwnership(T *obj) {
return optional_ownership_ptr<T>(obj, [](T*){});
}
template<typename T>
optional_ownership_ptr<T> null() {
return WithoutOwnership<T>(nullptr);
}
}
}
#endif

View File

@ -8,11 +8,12 @@ namespace fspp {
template<typename DST, typename SRC>
inline std::unique_ptr<DST> dynamic_pointer_move(std::unique_ptr<SRC> &source) {
DST *casted = dynamic_cast<DST*>(source.get());
if (casted != nullptr) {
//TODO Deleter
DST *casted = dynamic_cast<DST*>(source.get());
if (casted != nullptr) {
source.release();
}
return std::unique_ptr<DST>(casted);
}
return std::unique_ptr<DST>(casted);
}
}