Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #ifndef __MutuallyAttachable_H__
- #define __MutuallyAttachable_H__
- // Usage: Suppose you want to have two classes A and B that are mutually connected to each other.
- //
- // Derive the class A from MutuallyAttachable<A, B> where A is the class you're deriving
- // and B is the other class you want to make mutual attachments with.
- // Derive the class B from MutuallyAttachable<B, A>.
- //
- // Call MutuallyAttachable<A, B>::CreateConnection(A, B) to create a connection.
- // Call MutuallyAttachable<A, B>::DestroyConnection(A, B) to destroy a connection.
- //
- // If A or B is destroyed while it has non-zero connections, it will automatically remove the connections.
- template <typename T, typename U> class MutuallyAttachable
- {
- public:
- auto GetAttached() -> const std::set<U *> &;
- static void CreateConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1);
- static void DestroyConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1);
- protected:
- MutuallyAttachable();
- virtual ~MutuallyAttachable();
- private:
- MutuallyAttachable(const MutuallyAttachable<T, U> &) = delete;
- MutuallyAttachable<T, U> & operator =(const MutuallyAttachable<T, U> &) = delete;
- void AttachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable);
- void DetachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable);
- std::set<U *> m_Attached;
- friend class MutuallyAttachable<U, T>;
- };
- template <typename T, typename U> MutuallyAttachable<T, U>::MutuallyAttachable()
- {
- }
- template <typename T, typename U> MutuallyAttachable<T, U>::~MutuallyAttachable()
- {
- for (auto & Attached : m_Attached)
- {
- static_cast<MutuallyAttachable<U, T> *>(Attached)->DetachWithoutReciprocating(*this);
- }
- }
- template <typename T, typename U> auto MutuallyAttachable<T, U>::GetAttached() -> const std::set<U *> &
- {
- return m_Attached;
- }
- template <typename T, typename U> void MutuallyAttachable<T, U>::CreateConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1)
- {
- MutuallyAttachable0.AttachWithoutReciprocating(MutuallyAttachable1);
- MutuallyAttachable1.AttachWithoutReciprocating(MutuallyAttachable0);
- }
- template <typename T, typename U> void MutuallyAttachable<T, U>::DestroyConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1)
- {
- MutuallyAttachable0.DetachWithoutReciprocating(MutuallyAttachable1);
- MutuallyAttachable1.DetachWithoutReciprocating(MutuallyAttachable0);
- }
- template <typename T, typename U> void MutuallyAttachable<T, U>::AttachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable)
- {
- m_Attached.insert(static_cast<U *>(&MutuallyAttachable));
- }
- template <typename T, typename U> void MutuallyAttachable<T, U>::DetachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable)
- {
- m_Attached.erase(static_cast<U *>(&MutuallyAttachable));
- }
- #endif // __MutuallyAttachable_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement