/* \file Test use of Owned in std::set container. */ #include <set> #include <algorithm> #include "Deriveds.hh" #include "OwnedPtr.hh" #include "BLink.hh" using namespace MangoPtr; namespace std { template <typename T> void swap( Owned<T>& a, Owned<T>& b) {a.swap(b);} } /* Test use of Owned and BLink with container of Owneds. Std::rotate() seems to require swap(const Owned, const Owned), which doens't really make sense. So above specialization doesn't get picked, and in turn this calls the default std::swap, which uses operator=, which is private for Owned. STL bug? */ int main() { std::cout << "Using std::set..." << std::endl; typedef Owned<Base*, OShared> PType; //typedef Base* PType; std::set<PType> owners; for (int i=0; i<5; i++) owners.insert( PType(new Derived1) ); std::cout << "Number of Base's created: " << Base::count << std::endl; for (std::set<PType>::const_iterator ii = owners.begin(); ii != owners.end(); ++ii) { BLink<Base> p( *ii ); std::cout << "Base " << p()->num() << " "; } std::cout << std::endl; } /* Output: Using std::set... Instantiating an 8Derived1# 1 Copying Base # 1 Destroying an 8Derived1# 1 Instantiating an 8Derived1# 3 Copying Base # 3 Destroying an 8Derived1# 3 Instantiating an 8Derived1# 5 Copying Base # 5 Destroying an 8Derived1# 5 Instantiating an 8Derived1# 7 Copying Base # 7 Destroying an 8Derived1# 7 Instantiating an 8Derived1# 9 Copying Base # 9 Destroying an 8Derived1# 9 Number of Base's created: 10 Base # 10 Base # 8 Base # 6 Base # 4 Base # 2 Destroying an 8Derived1# 2 Destroying an 8Derived1# 4 Destroying an 8Derived1# 6 Destroying an 8Derived1# 8 Destroying an 8Derived1# 10 */