Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [C++] Memory Managed Pointers Class  (Read 916 times)

0 Members and 1 Guest are viewing this topic.
[C++] Memory Managed Pointers Class
« on: October 23, 2008, 07:16:55 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
Got bored, thought "!@#$% boost" and wrote this, it's rather simple but all it really does it keep track of how many times a reference is added and dropped. I didn't put in any +, - etc. operators because...well, what're you doing pointing at arbitrary locations in the memory anyway? ;)

Code: C++
  1. /********************************************************************************
  2.  
  3.   Copyright © 2008 Jason Morley
  4.  
  5.   -----------------------------------------------------------------------------
  6.  
  7.   This software is provided &#39;as-is&#39;, without any express or implied
  8.   warranty.  In no event will the authors be held liable for any damages
  9.   arising from the use of this software.
  10.  
  11.   Permission is granted to anyone to use this software for any purpose,
  12.   including commercial applications, and to alter it and redistribute it
  13.   freely, subject to the following restrictions:
  14.  
  15.   1. The origin of this software must not be misrepresented; you must not
  16.      claim that you wrote the original software. If you use this software
  17.      in a product, an acknowledgment in the product documentation would be
  18.      appreciated but is not required.
  19.   2. Altered source versions must be plainly marked as such, and must not be
  20.      misrepresented as being the original software.
  21.   3. This notice may not be removed or altered from any source distribution.
  22.  
  23. *********************************************************************************/
  24. namespace A2D
  25. {
  26.     namespace Base
  27.     {
  28.         template<class T> class MM_Pointer
  29.         {
  30.         public:
  31.             MM_Pointer()
  32.             {
  33.                 _mv_pnCount = 0;
  34.                 _mv_pPointer = 0;
  35.             };
  36.  
  37.             MM_Pointer(T* pT)
  38.             {
  39.                 _mv_pnCount = new unsigned int(0);
  40.                 _mv_pPointer = pT;
  41.                 AddRef();
  42.             }
  43.  
  44.             MM_Pointer(const MM_Pointer<T>& pT)
  45.             {
  46.                 _mv_pnCount = pT._mv_pnCount;
  47.                 _mv_pPointer = pT._mv_pPointer;
  48.                 AddRef();
  49.             }
  50.  
  51.             MM_Pointer& operator=(T* pT)
  52.             {
  53.                 Release();
  54.                 if ( _mv_pnCount == 0 ) _mv_pnCount = new unsigned int(0);
  55.                 _mv_pPointer = pT;
  56.                 AddRef();
  57.                 return *this;
  58.             }
  59.  
  60.             MM_Pointer& operator=(MM_Pointer<T>& pT)
  61.             {
  62.                 Release();
  63.                 _mv_pnCount = pT._mv_pnCount;
  64.                 _mv_pPointer = pT._mv_pPointer;
  65.                 AddRef();
  66.                 return *this;
  67.             }
  68.  
  69.             virtual ~MM_Pointer()
  70.             {
  71.                 if ( _mv_pPointer != 0 ) Release();
  72.             }
  73.  
  74.             void Drop()
  75.             {
  76.                 Release();
  77.                 _mv_pnCount = 0;
  78.                 _mv_pPointer = 0;
  79.             }
  80.  
  81.             T& operator*() const
  82.             {
  83.                 return *_mv_pPointer;
  84.             }
  85.  
  86.             T* operator->() const
  87.             {
  88.                 return _mv_pPointer;
  89.             }
  90.  
  91.             bool IsValid()
  92.             {
  93.                 return ( _mv_pnCount !=0);
  94.             }
  95.  
  96.             unsigned int GetRefCount()
  97.             {
  98.                 if ( !IsValid() ) return 0;
  99.                 return *_mv_pnCount;
  100.             }
  101.  
  102.             bool operator==(const T* p) const
  103.             {
  104.                 return ( _mv_pPointer == p );
  105.             }
  106.             bool operator!=(const T* p) const
  107.             {
  108.                 return ( _mv_pPointer != p );
  109.             }
  110.  
  111.             bool operator==(const MM_Pointer<T>& p) const
  112.             {
  113.                 return ( _mv_pPointer == p._mv_pPointer );
  114.             }
  115.             bool operator!=(const MM_Pointer<T>& p) const
  116.             {
  117.                 return ( _mv_pPointer != p._mv_pPointer );
  118.             }
  119.  
  120.         protected:
  121.             void AddRef()
  122.             {
  123.                 if ( IsValid() )
  124.                 {
  125.                     //std::cout << "Ref Added" << std::endl;
  126.                     *_mv_pnCount = *_mv_pnCount + 1;
  127.                 }
  128.             }
  129.  
  130.             void Release()
  131.             {
  132.                 if ( !IsValid() ) return;
  133.                 //if ( *_mv_pnCount == 0 ) std::cout << "Trying to delete gone" << std::endl;
  134.                 if ( *_mv_pnCount > 0 )
  135.                 {
  136.                     //std::cout << "Ref Removed" << std::endl;
  137.                     *_mv_pnCount -= 1;
  138.                     //std::cout << *_mv_pnCount << std::endl;
  139.                     if ( *_mv_pnCount == 0 ) DeleteThis();
  140.                 }
  141.             }
  142.  
  143.             void DeleteThis()
  144.             {
  145.                 //std::cout << "Deleting" << std::endl;
  146.                 if ( _mv_pnCount != 0 ) {delete _mv_pnCount;  _mv_pnCount = 0;}
  147.                 if ( _mv_pPointer != 0 ) {delete _mv_pPointer; _mv_pPointer = 0;}
  148.             }
  149.  
  150.  
  151.         private:
  152.             unsigned int* _mv_pnCount;
  153.             T* _mv_pPointer;
  154.         };
  155.     }
  156. }
  157.  

Rather simple and obviously can't be used with arrays, to use it you just do something like...

Code: Text
  1. class TestingClass
  2. {
  3. public:
  4.     TestingClass()
  5.     {
  6.         staticID++;
  7.         ID = staticID;
  8.         std::cout << "Creating " << ID << std::endl;
  9.     }
  10.     virtual ~TestingClass()
  11.     {
  12.         std::cout << "Destroying " << ID << std::endl;
  13.     }
  14.  
  15.     int GetID() { return ID; }
  16.  
  17. protected:
  18.     int ID;
  19.     static int staticID;
  20. private:
  21. };
  22. int TestingClass::staticID = 0;
  23.  
  24. void MM(A2D::Base::MM_Pointer<TestingClass> ProvePoint)
  25. {
  26.     std::cout << "ID: " << ProvePoint->GetID() << std::endl;
  27. }
  28.  
  29. void MM2(A2D::Base::MM_Pointer<TestingClass> ProvingMorePoints)
  30. {
  31.     std::cout << "ID: " << (*ProvingMorePoints).GetID() << std::endl;
  32. }
  33.  
  34. int main( int argc, char **argv )
  35. {
  36.     A2D::Base::MM_Pointer<TestingClass> tst1( new TestingClass() ); // Creating 1
  37.     A2D::Base::MM_Pointer<TestingClass> tst2 = new TestingClass(); // Creating 2
  38.     MM( tst1 ); // ID 1
  39.     MM2( tst2 ); // ID 2
  40.     MM( new TestingClass() ); // Creating 3, ID 3, Destroying 3
  41.     return 0; // Destroying 2, Destroying 1
  42. }

I'm aware Boost comes with one which is probably better than this, and this was done rather quickly, just thought I'd share it with y'all in case you find it useful...And am totally not doing so that the other programmers here can point out any flaws or anything useful I missed, nope, not at all.

EDIT: Realised checking both pointers was pointless, a managed pointer to a NULL may be useful, who knows? Also removed some In-House-Specific code I left in
« Last Edit: October 23, 2008, 07:32:21 pm by TheDarkJay »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.072 seconds with 37 queries.

anything