#include "stdafx.h" 
#include <iostream> 
#include <vector> 
#include <list> 
#include <algorithm> 
#include "SmartContainer.h"



// our client class
class foo
{
    int i_;

public:
    foo( int i ) : i_( i )
   {
      cout << "constructing : " << i_ << endl;
   }

   ~foo()
   {
      cout << "destructing: " << i_ << endl;
   }

   int bar()
   {
      return i_;
   }

};

// some helpers to use in our tests
struct PrintFooBar
{
   template< class T >
      void operator() ( T* p )
   {
      cout << p->bar() << endl;
   }
};


struct PrintPointerValue
{
   template< class T >
      void operator() ( T& p )
   {
      cout << p << endl;
   }
};

bool IsNull(foo* p)
{
   return p == 0;
}

void UseSmartContainerToDeleteElements()
{
   SmartContainer< foo* > smartFooVec;

   cout << "initialising  smartFooVec\n\n";
   for(int i=0; i <10; i++)
   {
      smartFooVec.push_back( new foo( i ) );
   }

   cout << "\n using smartFooVec\n\n";
   for_each( smartFooVec.begin(), smartFooVec.end(), PrintFooBar() );

   vector<foo*> dumbFooVec(5);
   SmartContainer< foo* >::iterator it = smartFooVec.begin();
   SmartContainer< foo* >::iterator itEndOfRange = it +5;

   copy(it, itEndOfRange, dumbFooVec.begin() );

   for_each(dumbFooVec.begin(), dumbFooVec.end(), PrintFooBar() );
   cout << "\n smartFooVec going out of scope... \n";
}

void UseSmartContainerToDeleteAndNullElementPointers()
{
   typedef SmartContainer< foo*,
      list<foo*>,
      false,
      ContainerDeleteAndNull
      > SmartFooNullingList;

   SmartFooNullingList smartFooNullingList;

   cout << "\n initialising smartFooNullingList\n\n";
   for(int i=0; i <10; i++)
   {
      smartFooNullingList.push_back( new foo( i ) );
   }

   cout << "\n using smartFooNullingList\n\n";
   for_each( smartFooNullingList.begin(), smartFooNullingList.end(), PrintFooBar() );

   cout << "\n\n deleting smartFooNullingList... \n";

   smartFooNullingList.delete_objects();

   for_each( smartFooNullingList.begin(), smartFooNullingList.end(), PrintPointerValue() );

   cout << "\n\n smartFooNullingList going out of scope... \n";
}

void UseSmartContainerToDeleteElementsAndSetBytePatern()
{

   typedef SmartContainer< foo*,
      list<foo*>,
      false,
      ContainerDeleteAndSetBytePattern
      > SmartFooByteSettingList;
   SmartFooByteSettingList smartFooByteSettingList;

   cout << "\n initialising smartFooByteSettingList\n\n";
   for(int i=0; i <10; i++)
   {
      smartFooByteSettingList.push_back( new foo( i ) );
   }

   cout << "\n using smartFooByteSettingList\n\n";
   for_each( smartFooByteSettingList.begin(), smartFooByteSettingList.end(), PrintFooBar() );

   cout << "\n\n deleting smartFooByteSettingList... \n";

   smartFooByteSettingList.delete_objects();

   for_each( smartFooByteSettingList.begin(), smartFooByteSettingList.end(), PrintPointerValue() );

   cout << "\n\n smartFooNullingList going out of scope... \n";

}

class InitialiseFooPtr
{
public:
   InitialiseFooPtr() : i_(0) {};
   void operator()(foo*& p)
   {
      p = new foo(i_);
      ++i_;
   }

private:
   int i_;
};


void UseSmartPOBVector()
{
   typedef SmartContainer< foo * , vector<foo*>, true, ContainerDeleteAndNull> SmartPOBVector;
   SmartPOBVector smartPOBVector(10);  // there can only be 10  note: no resize()

   for_each(smartPOBVector.begin(), smartPOBVector.end(), InitialiseFooPtr());

   cout << "\n delete one individual object\n";

   SmartPOBVector::iterator it = smartPOBVector.begin();
   ++it; // set to element 1
   delete *it;
   *it =0;

   cout << "\n find an unused element and create a new object\n";
   it =find_if(smartPOBVector.begin(), smartPOBVector.end(), IsNull);
   *it = new foo(666);
}

int main( int argc, char** argv )
{
// UseSmartContainerToDeleteElements();
// UseSmartContainerToDeleteAndNullElementPointers();
// UseSmartContainerToDeleteElementsAndSetBytePatern();
// UseSmartPOBVector();

   while(!cin.get() )
      ;
   return( 0 );
}


Home
Article
Hosted by www.Geocities.ws

1