This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

MutuallyAttachable<T, U> class template

By: a guest on May 15th, 2012  |  syntax: C++  |  size: 3.07 KB  |  views: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #pragma once
  2. #ifndef __MutuallyAttachable_H__
  3. #define __MutuallyAttachable_H__
  4.  
  5. // Usage: Suppose you want to have two classes A and B that are mutually connected to each other.
  6. //
  7. //        Derive the class A from MutuallyAttachable<A, B> where A is the class you're deriving
  8. //        and B is the other class you want to make mutual attachments with.
  9. //        Derive the class B from MutuallyAttachable<B, A>.
  10. //
  11. //        Call MutuallyAttachable<A, B>::CreateConnection(A, B) to create a connection.
  12. //        Call MutuallyAttachable<A, B>::DestroyConnection(A, B) to destroy a connection.
  13. //
  14. //        If A or B is destroyed while it has non-zero connections, it will automatically remove the connections.
  15.  
  16. template <typename T, typename U> class MutuallyAttachable
  17. {
  18. public:
  19.         auto GetAttached() -> const std::set<U *> &;
  20.  
  21.         static void CreateConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1);
  22.         static void DestroyConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1);
  23.  
  24. protected:
  25.         MutuallyAttachable();
  26.         virtual ~MutuallyAttachable();
  27.  
  28. private:
  29.         MutuallyAttachable(const MutuallyAttachable<T, U> &) = delete;
  30.         MutuallyAttachable<T, U> & operator =(const MutuallyAttachable<T, U> &) = delete;
  31.  
  32.         void AttachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable);
  33.         void DetachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable);
  34.  
  35.         std::set<U *>           m_Attached;
  36.  
  37.         friend class MutuallyAttachable<U, T>;
  38. };
  39.  
  40. template <typename T, typename U> MutuallyAttachable<T, U>::MutuallyAttachable()
  41. {
  42. }
  43.  
  44. template <typename T, typename U> MutuallyAttachable<T, U>::~MutuallyAttachable()
  45. {
  46.         for (auto & Attached : m_Attached)
  47.         {
  48.                 static_cast<MutuallyAttachable<U, T> *>(Attached)->DetachWithoutReciprocating(*this);
  49.         }
  50. }
  51.  
  52. template <typename T, typename U> auto MutuallyAttachable<T, U>::GetAttached() -> const std::set<U *> &
  53. {
  54.         return m_Attached;
  55. }
  56.  
  57. template <typename T, typename U> void MutuallyAttachable<T, U>::CreateConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1)
  58. {
  59.         MutuallyAttachable0.AttachWithoutReciprocating(MutuallyAttachable1);
  60.         MutuallyAttachable1.AttachWithoutReciprocating(MutuallyAttachable0);
  61. }
  62.  
  63. template <typename T, typename U> void MutuallyAttachable<T, U>::DestroyConnection(MutuallyAttachable<T, U> & MutuallyAttachable0, MutuallyAttachable<U, T> & MutuallyAttachable1)
  64. {
  65.         MutuallyAttachable0.DetachWithoutReciprocating(MutuallyAttachable1);
  66.         MutuallyAttachable1.DetachWithoutReciprocating(MutuallyAttachable0);
  67. }
  68.  
  69. template <typename T, typename U> void MutuallyAttachable<T, U>::AttachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable)
  70. {
  71.         m_Attached.insert(static_cast<U *>(&MutuallyAttachable));
  72. }
  73.  
  74. template <typename T, typename U> void MutuallyAttachable<T, U>::DetachWithoutReciprocating(MutuallyAttachable<U, T> & MutuallyAttachable)
  75. {
  76.         m_Attached.erase(static_cast<U *>(&MutuallyAttachable));
  77. }
  78.  
  79. #endif // __MutuallyAttachable_H__
clone this paste RAW Paste Data