Skip to content

Commit

Permalink
Remove emplace constructor from polymorphic wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Lebedev <[email protected]>
  • Loading branch information
lebdron committed Dec 19, 2017
1 parent 6b8253b commit 0a72bbe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
49 changes: 24 additions & 25 deletions shared_model/interfaces/polymorphic_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,14 @@ namespace shared_model {
/// Type of wrapped object
using WrappedType = T;

/**
* Value constructor
* @param value - pointer for wrapping
*/
template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
explicit PolymorphicWrapper(const Y *value)
: ptr_(std::shared_ptr<Y>(value)) {}

template <typename... Args>
explicit PolymorphicWrapper(Args &&... args)
: ptr_(std::make_shared<T>(std::forward<Args>(args)...)) {}

template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
PolymorphicWrapper(const PolymorphicWrapper<Y> &rhs)
: ptr_(std::shared_ptr<T>(rhs.ptr_->copy())) {}
PolymorphicWrapper() = delete;

template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
PolymorphicWrapper(PolymorphicWrapper<Y> &&rhs) noexcept
: ptr_(rhs.ptr_) {
rhs.ptr_ = nullptr;
}
/**
* Copy constructor that performs deep copy
* @param rhs - another wrapped value
*/
PolymorphicWrapper(const PolymorphicWrapper &rhs)
: ptr_(std::shared_ptr<T>(rhs.ptr_->copy())) {}
: ptr_(rhs.ptr_->copy()) {}

/**
* Move constructor
Expand All @@ -75,13 +53,34 @@ namespace shared_model {
std::swap(this->ptr_, rhs.ptr_);
}

/**
* Value constructor
* @param value - pointer for wrapping
*/
template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
explicit PolymorphicWrapper(Y *value)
: ptr_(value) {}

template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
PolymorphicWrapper(const PolymorphicWrapper<Y> &rhs)
: ptr_(rhs.ptr_->copy()) {}

template <typename Y,
typename = std::enable_if_t<std::is_base_of<T, Y>::value>>
PolymorphicWrapper(PolymorphicWrapper<Y> &&rhs) noexcept
: ptr_(std::move(rhs.ptr_)) {
rhs.ptr_ = nullptr;
}

/**
* Copy operator=
* @param rhs - another wrapped value
* @return *this
*/
PolymorphicWrapper &operator=(const PolymorphicWrapper &rhs) {
ptr_ = std::shared_ptr<T>(rhs.ptr_->copy());
ptr_ = rhs.ptr_->copy();
return *this;
}

Expand Down
3 changes: 2 additions & 1 deletion shared_model/utils/variant_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ namespace shared_model {
std::is_base_of<typename variant_head_type::WrappedType,
typename head_type::WrappedType>::value,
"variant_head_type is not base of head_type");
return V(head_type(std::forward<Archive>(ar)));
return V(head_type(new typename head_type::WrappedType(
std::forward<Archive>(ar))));
} else {
using type = typename boost::mpl::pop_front<S>::type;
using variant_type = typename boost::mpl::pop_front<T>::type;
Expand Down

0 comments on commit 0a72bbe

Please sign in to comment.