Fix some issues in DataTree using OptionalOwnershipPointer
This commit is contained in:
parent
114284a58f
commit
59933036d6
34
src/fspp/utils/OptionalOwnershipPointer.h
Normal file
34
src/fspp/utils/OptionalOwnershipPointer.h
Normal 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
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user