//----------------------------------------------------------------------------- // Pointer_2.h (checking implementation) //----------------------------------------------------------------------------- #ifndef POINTER_2_H #define POINTER_2_H #include "__Pointer_Map.h" __Pointer_Map __pointer_map; #define New(p) p.Allocate (__LINE__, __FILE__) #define Delete(p) p.Deallocate (__LINE__, __FILE__) template class Pointer { public: Pointer () { timestamp = -1; } Pointer (void* a) { rep = reinterpret_cast (a); } Pointer& operator= (const void* a) { rep = reinterpret_cast (a); return *this; } bool operator== (const Pointer& a) { return (rep == a.rep) && (timestamp == a.timestamp); } bool operator== (const void* a) { return rep == reinterpret_cast (a); } bool operator!= (const Pointer& a) { return (rep != a.rep) || (timestamp != a.timestamp); } bool operator!= (const void* a) { return rep != reinterpret_cast (a); } void Allocate (long l, char* f) { rep = new T; __pointer_map.Add (rep, timestamp, l, f); } void Deallocate (long l, char* f) { if (rep != 0) { if (! __pointer_map.Contains (rep, timestamp)) { __pointer_map.Report_Error ( "Deleting dangling pointer", l, f ); } __pointer_map.Remove (rep); } delete rep; } T* operator-> () { if (rep == 0) { __pointer_map.Report_Error ( "Dereferencing null pointer using ->", 0, "" ); } if (! __pointer_map.Contains (rep, timestamp)) { __pointer_map.Report_Error ( "Dereferencing dangling pointer using ->", 0, "" ); } return rep; } T& operator* () { if (rep == 0) { __pointer_map.Report_Error ( "Dereferencing null pointer using *", 0, "" ); } if (! __pointer_map.Contains (rep, timestamp)) { __pointer_map.Report_Error ( "Dereferencing dangling pointer using *", 0, "" ); } return *rep; } private: T* rep; long timestamp; }; void Report_Storage_Allocation () { __pointer_map.Report_Allocation (); } #endif // POINTER_2_H