/* 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 POLY_H #define POLY_H #include #include #include #include #include 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 static_cast(delegate_->ptr()); } }; } // end namespace detail class bad_poly_construction : std::exception { public: bad_poly_construction() noexcept = default; const char *what() const noexcept override { return "Dynamic and static type mismatch in poly " "construction"; } }; template class poly; template struct is_poly : std::false_type { }; template struct is_poly> : std::true_type { }; //////////////////////////////////////////////////////////////////////////////// // `poly` class definition //////////////////////////////////////////////////////////////////////////////// template class poly { static_assert(!std::is_union::value, ""); static_assert(std::is_class::value, ""); template friend class poly; template friend poly make_poly(Ts &&... ts); template friend poly make_poly(Ts &&... ts); template friend poly poly_cast(poly p); T *ptr_ = nullptr; std::unique_ptr> cb_; public: // // Destructor // ~poly() = default; // // Constructors // poly() { } template, class D = detail::default_delete, class V = std::enable_if_t::value>> explicit poly(U *u, C copier = C{}, D deleter = D{}) { if (!u) { return; } if (std::is_same>::value && std::is_same>::value && typeid(*u) != typeid(U)) throw bad_poly_construction(); std::unique_ptr p(u, std::move(deleter)); cb_ = std::make_unique>( std::move(p), std::move(copier)); ptr_ = u; } // // Copy-constructors // poly(const poly &p) { if (!p) { return; } auto tmp_cb = p.cb_->clone(); ptr_ = tmp_cb->ptr(); cb_ = std::move(tmp_cb); } // // Move-constructors // poly(poly &&p) noexcept { ptr_ = p.ptr_; cb_ = std::move(p.cb_); p.ptr_ = nullptr; } // // Converting constructors // template::value && std::is_convertible::value>> poly(const poly &p) { poly tmp(p); ptr_ = tmp.ptr_; cb_ = std::make_unique>( std::move(tmp.cb_)); } template::value && std::is_convertible::value>> poly(poly &&p) { ptr_ = p.ptr_; cb_ = std::make_unique>( std::move(p.cb_)); p.ptr_ = nullptr; } // // Forwarding constructor // template *, T *>::value && !is_poly>::value>> poly(U &&u) : cb_(std::make_unique< detail::direct_control_block>>( std::forward(u))) { ptr_ = cb_->ptr(); } // // Assignment // poly &operator=(const poly &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 // poly &operator=(poly &&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(poly &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_; } }; // // poly creation // template poly make_poly(Ts &&... ts) { poly p; p.cb_ = std::make_unique>( std::forward(ts)...); p.ptr_ = p.cb_->ptr(); return std::move(p); } template poly make_poly(Ts &&... ts) { poly p; p.cb_ = std::make_unique>( std::forward(ts)...); p.ptr_ = p.cb_->ptr(); return std::move(p); } template const T *poly_view(const poly &p) { if (p) { return dynamic_cast(&*p); } else { return nullptr; } } template T *poly_view(poly &p) { if (p) { return dynamic_cast(&*p); } else { return nullptr; } } template poly poly_cast(poly p) { poly ret; if (T *ptr = dynamic_cast(&*p)) { ret.cb_ = std::make_unique>( std::move(p.cb_)); ret.ptr_ = ret.cb_->ptr(); } return ret; } #endif // POLY_H