Guest User

Untitled

a guest
Jun 13th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <list>
  2.  
  3. template <typename T> void ProxyNoOp(T *) {}
  4. template <typename T> void ProxyDelete(T * ptrT) {delete ptrT;}
  5.  
  6. template <typename T, typename C, void (* Release)(T *) = ProxyNoOp<T>  >
  7. class Proxy
  8. {
  9. public:
  10.     class Container : public std::list<T *>
  11.     {
  12.     public:
  13.         Container() {}
  14.         ~Container() {clear();}
  15.  
  16.         void clear()
  17.         {
  18.             typename Container::iterator clsEnd = this->end();
  19.             for (typename Container::iterator clsCurrent = this->begin(); clsCurrent != clsEnd; ++clsCurrent)
  20.             {
  21.                 T * ptrT = *clsCurrent;
  22.                 static_cast<Proxy *>(ptrT)->m_ptrContainer = NULL;
  23.                 Release(ptrT);
  24.             }  
  25.         }
  26.     };
  27.  
  28. private:
  29.     typename Container::iterator m_clsPosition;
  30.     C *      m_ptrContainer;
  31.  
  32. public:
  33.     Proxy() : m_ptrContainer(NULL) {}
  34.     ~Proxy()
  35.     {
  36.         if ( m_ptrContainer != NULL )
  37.         {
  38.             Container * ptrContainer = static_cast<Container *>(m_ptrContainer);
  39.             static_cast<typename Container::Range *>(ptrContainer)->erase(m_clsPosition);
  40.         }
  41.     }
  42.  
  43.     C * GetContainer() const {return m_ptrContainer;}
  44. };
  45.  
  46. #if 0
  47. class Child;
  48. class Parent : public Proxy<Child, Parent>::Container {};
  49. class Child: public Proxy<Child, Parent> {};
  50. #endif
  51.  
  52. class Parent;
  53. class Child : public Proxy<Child, Parent> {};
  54. class Parent : public Proxy<Child, Parent>::Container {};
Advertisement
Add Comment
Please, Sign In to add comment