00001
00040 #ifndef MANGO_CONTAINER_CLONER_H
00041 #define MANGO_CONTAINER_CLONER_H
00042
00043 #include "nullness.hh"
00044 #include "OwnedFWD.hh"
00045 #include "CntrClonerFWD.hh"
00046
00047 namespace MangoPtr
00048 {
00050 template <typename Container>
00051 struct CntrCloner
00052 {
00054 inline static void execute(Container& obj);
00055 };
00060 template <typename T>
00061 struct CntrCloner<T*>
00062 {
00063 static void execute(T*& ptr)
00064 {
00065 if (isNotNull(ptr))
00066 {
00067 T* const newPtr = new T(*ptr);
00068 ptr = newPtr;
00069 }
00070 }
00071 };
00074 template <typename T>
00075 struct CntrCloner<Owned<T,OStrict>* >
00076 {
00077 typedef Owned<T,OStrict> OT;
00078 static void execute(OT*& ptr)
00079 {
00080 if (isNotNull(ptr))
00081 {
00082 OT* const newPtr = new OT();
00083 assert( isNotNull(newPtr) );
00084 ptr->cloneObjNew( newPtr->cntr() );
00085 ptr = newPtr;
00086 }
00087 }
00088 };
00095 template <typename Container>
00096 struct CntrClonerSorted
00097 {
00099 template <typename X>
00100 static void xform(Container& obj, X* x)
00101 {
00102 X* tmp = new X(*x);
00103 obj.insert(tmp);
00104 }
00106 template <typename X, typename Y>
00107 static void xform(Container& obj, const std::pair<X*const,Y>& x)
00108 {
00109 X* tmp = new X(*(x.first));
00110 obj[tmp] = x.second;
00111 }
00113 static void execute(Container& obj)
00114 {
00115 Container tmpSet;
00116 std::swap(obj, tmpSet);
00117 for (typename Container::iterator ii = tmpSet.begin();
00118 ii != tmpSet.end(); ++ ii)
00119 {
00120 xform(obj, *ii);
00121 }
00122 }
00123 };
00124
00126 template <typename Container>
00127 inline void
00128 CntrCloner<Container>::execute(Container& obj)
00129 {
00130 for (typename Container::iterator ii = obj.begin();
00131 ii != obj.end(); ++ ii)
00132 CntrCloner<typename Container::value_type>::execute(*ii);
00133 }
00134
00135 }
00136
00137
00138 #endif // MANGO_CONTAINER_CLONER_H
00139