#include <list>
#include <algorithm>
#include <iostream>
using std::cout;
using std::endl;
#include "Deriveds.hh"
#include "DynObj.hh"
#include "DynTmp.hh"
#include "RRef.hh"
using namespace NoPtr;
typedef std::list<DynObj<Base>::InValueContainerOpt2> List;
void outputList(const List& owners)
{
for (List::const_iterator ii = owners.begin();
ii != owners.end(); ++ii)
{
RRef<Base> p( *ii );
std::cout << "Base " << p().num() << " ";
}
std::cout << std::endl;
}
int main()
{
std::cout << "Using std::list..." << std::endl;
RRef<Base> rref;
List owners;
for (int i=0; i<5; i++)
{
owners.push_back( makeDynTmp(new Derived1) );
if (i==2)
{
cout << "Creating RRef to DynObj<Base> #3" << endl;
rref = owners.back();
}
}
std::cout << "Number of Base's created: " << Base::count << std::endl;
cout << "List: " << endl;
outputList(owners);
cout << "Rotating list by two" << endl;
List::iterator begin = owners.begin();
List::iterator middle = ++ (++ owners.begin());
List::iterator end = owners.end();
std::rotate( begin, middle, end);
cout << "Rotated list: " << endl;
outputList(owners);
cout << "Erasing first two items in list by calling list::erase():" << endl;
assert(rref.isNotNull());
owners.erase(owners.begin());
cout << "Asserting that RRef for #3 knows that DynObj #3 is dead" << endl;
assert(rref.isNull());
owners.erase(owners.begin());
cout << "Ending program, should see remaining deriveds being destroyed: "
<< endl;
}