Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Polymorphic clone utilities
- namespace HOEngine {
- template <typename T>
- class AbstractFunc;
- template <typename T>
- class VirtualInheritFrom : public virtual T {
- using T::T;
- };
- template <typename Derived, typename... Bases>
- class CloneInherit : public Bases... {
- public:
- virtual ~CloneInherit() = default;
- std::unique_ptr<Derived> Clone() const {
- return std::unique_ptr<Derived>(static_cast<Derived*>(this->CloneImpl()));
- }
- private:
- virtual Derived* CloneImpl() const override {
- return new Derived(static_cast<const Derived&>(*this));
- }
- };
- template <typename Derived, typename... Bases>
- class CloneInherit<AbstractFunc<Derived>, Bases...> : public Bases... {
- public:
- virtual ~CloneInherit() = default;
- std::unique_ptr<Derived> Clone() const {
- return std::unique_ptr<Derived>(static_cast<Derived*>(this->CloneImpl()));
- }
- private:
- virtual Derived* CloneImpl() const = 0;
- };
- template <typename Derived>
- class CloneInherit<Derived> {
- public:
- virtual ~CloneInherit() = default;
- std::unique_ptr<Derived> Clone() const {
- return std::unique_ptr<Derived>(static_cast<Derived*>(this->CloneImpl()));
- }
- private:
- virtual Derived* CloneImpl() const override {
- return new Derived(static_cast<const Derived&>(*this));
- }
- };
- template <typename Derived>
- class CloneInherit<AbstractFunc<Derived>> {
- public:
- virtual ~CloneInherit() = default;
- std::unique_ptr<Derived> Clone() const {
- return std::unique_ptr<Derived>(static_cast<Derived*>(this->CloneImpl()));
- }
- private:
- virtual Derived* CloneImpl() const = 0;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment