/* Copyright (c) 2016 Jonathan B. Coe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef JBCOE_POLYMORPHIC_VALUE_H_INCLUDED #define JBCOE_POLYMORPHIC_VALUE_H_INCLUDED #include #include #include #include #include namespace jbcoe { namespace detail { //////////////////////////////////////////////////////////////////////////// // Implementation detail classes //////////////////////////////////////////////////////////////////////////// template struct default_copy { T* operator()(const T& t) const { return new T(t); } }; template struct default_delete { void operator()(const T* t) const { delete t; } }; template struct control_block { virtual ~control_block() = default; virtual std::unique_ptr clone() const = 0; virtual T* ptr() = 0; }; template class direct_control_block : public control_block { static_assert(!std::is_reference::value, ""); U u_; public: template explicit direct_control_block(Ts&&... ts) : u_(U(std::forward(ts)...)) { } std::unique_ptr> clone() const override { return std::make_unique(*this); } T* ptr() override { return std::addressof(u_); } }; template , class D = default_delete> class pointer_control_block : public control_block, public C { std::unique_ptr p_; public: explicit pointer_control_block(U* u, C c = C{}, D d = D{}) : C(std::move(c)), p_(u, std::move(d)) { } explicit pointer_control_block(std::unique_ptr p, C c = C{}) : C(std::move(c)), p_(std::move(p)) { } std::unique_ptr> clone() const override { assert(p_); return std::make_unique( C::operator()(*p_), static_cast(*this), p_.get_deleter()); } T* ptr() override { return p_.get(); } }; template class delegating_control_block : public control_block { std::unique_ptr> delegate_; public: explicit delegating_control_block(std::unique_ptr> b) : delegate_(std::move(b)) { } std::unique_ptr> clone() const override { return std::make_unique(delegate_->clone()); } T* ptr() override { return delegate_->ptr(); } }; } // end namespace detail class bad_polymorphic_value_construction : public std::exception { public: bad_polymorphic_value_construction() noexcept = default; const char* what() const noexcept override { return "Dynamic and static type mismatch in polymorphic_value " "construction"; } }; template class polymorphic_value; template struct is_polymorphic_value : std::false_type { }; template struct is_polymorphic_value> : std::true_type { }; //////////////////////////////////////////////////////////////////////////////// // `polymorphic_value` class definition //////////////////////////////////////////////////////////////////////////////// template class polymorphic_value { static_assert(!std::is_union::value, ""); static_assert(std::is_class::value, ""); template friend class polymorphic_value; template friend polymorphic_value make_polymorphic_value(Ts&&... ts); template friend polymorphic_value make_polymorphic_value(Ts&&... ts); T* ptr_ = nullptr; std::unique_ptr> cb_; public: // // Destructor // ~polymorphic_value() = default; // // Constructors // polymorphic_value() { } template , class D = detail::default_delete, class V = std::enable_if_t::value>> explicit polymorphic_value(U* u, C copier = C{}, D deleter = D{}) { if (!u) { return; } #ifndef POLYMORPHIC_VALUE_NO_RTTI if (std::is_same>::value && std::is_same>::value && typeid(*u) != typeid(U)) throw bad_polymorphic_value_construction(); #endif std::unique_ptr p(u, std::move(deleter)); cb_ = std::make_unique>( std::move(p), std::move(copier)); ptr_ = u; } // // Copy-constructors // polymorphic_value(const polymorphic_value& p) { if (!p) { return; } auto tmp_cb = p.cb_->clone(); ptr_ = tmp_cb->ptr(); cb_ = std::move(tmp_cb); } // // Move-constructors // polymorphic_value(polymorphic_value&& p) noexcept { ptr_ = p.ptr_; cb_ = std::move(p.cb_); p.ptr_ = nullptr; } // // Converting constructors // template ::value && std::is_convertible::value>> explicit polymorphic_value(const polymorphic_value& p) { polymorphic_value tmp(p); ptr_ = tmp.ptr_; cb_ = std::make_unique>( std::move(tmp.cb_)); } template ::value && std::is_convertible::value>> explicit polymorphic_value(polymorphic_value&& p) { ptr_ = p.ptr_; cb_ = std::make_unique>( std::move(p.cb_)); p.ptr_ = nullptr; } // // Forwarding constructor // template *, T*>::value && !is_polymorphic_value>::value>> explicit polymorphic_value(U&& u) : cb_(std::make_unique< detail::direct_control_block>>( std::forward(u))) { ptr_ = cb_->ptr(); } // // Assignment // polymorphic_value& operator=(const polymorphic_value& p) { if (std::addressof(p) == this) { return *this; } if (!p) { cb_.reset(); ptr_ = nullptr; return *this; } auto tmp_cb = p.cb_->clone(); ptr_ = tmp_cb->ptr(); cb_ = std::move(tmp_cb); return *this; } // // Move-assignment // polymorphic_value& operator=(polymorphic_value&& p) noexcept { if (std::addressof(p) == this) { return *this; } cb_ = std::move(p.cb_); ptr_ = p.ptr_; p.ptr_ = nullptr; return *this; } // // Modifiers // void swap(polymorphic_value& p) noexcept { using std::swap; swap(ptr_, p.ptr_); swap(cb_, p.cb_); } // // Observers // explicit operator bool() const { return bool (cb_); } const T* operator->() const { assert(ptr_); return ptr_; } const T& operator*() const { assert(*this); return *ptr_; } T* operator->() { assert(*this); return ptr_; } T& operator*() { assert(*this); return *ptr_; } }; // // polymorphic_value creation // template polymorphic_value make_polymorphic_value(Ts&&... ts) { polymorphic_value p; p.cb_ = std::make_unique>( std::forward(ts)...); p.ptr_ = p.cb_->ptr(); return p; } template polymorphic_value make_polymorphic_value(Ts&&... ts) { polymorphic_value p; p.cb_ = std::make_unique>( std::forward(ts)...); p.ptr_ = p.cb_->ptr(); return p; } // // non-member swap // template void swap(polymorphic_value& t, polymorphic_value& u) noexcept { t.swap(u); } } // end namespace jbcoe #endif