// Array.h #if _MSC_VER >= 1000 #pragma once #endif // _MSC_VER >= 1000 #ifndef Arrayh #define Arrayh /* Creates an array of the specified type. The main point being that the destructor will delete the array elements correctly when the instance goes out of scope. You would only use this when the Array size isn't an fixed number on creation - otherwise int Table[123]; would be fine. Usage: Array Table(TableElementCount,true); Table[3]=7; int* TableIterator1=Table.Data; int* TableIterator2=Table(); I thought about keeping the Array Element Count in the struct, but it never seems to be useful: the count is generally known by whatever creates the array anyway. */ template struct Array { // Lest we should forget... T* Data; Array() : Data(0) {} Array(DWORD Count, bool Zero=false) : Data(0) {SetSize(Count,Zero);} virtual ~Array() {try{Empty();}catch(...){ASSERT(0);}} void Empty() {delete[] Data; Drop();} void Drop () {Data=0;} void Set(DWORD Count, T* Source) { delete[] Data; Data=new T[Count]; if(Data && Count) memcpy(Data, Source, Count * sizeof(T)); } void SetSize(DWORD Count, bool Zero=false) { delete[] Data; Data=new T[Count]; if(Zero && Data && Count) memset(Data, 0, Count * sizeof(T)); } void MoveTo(Array& it) {delete[] it.Data; it.Data=Data; Drop();} // Assignment without Copying Data T* operator()() {return Data;} // Shorthand access to the array T& operator[](int i) {return Data[i];} // Shorthand access to an element }; /*__________________________________________________________________ Unit Tester (only runs if you have included Tester.h in stdafx.h) */ #ifdef Assert static struct ArrayTester : Tester { ArrayTester() { Array Table(5,true); int* it=Table.Data; Assert(it); Assert(it==Table()); for(int i=0; i<5; ++i) { Assert(*it++==0); Table[i]=i+1; } for(int i=5; --i; Assert(Table[i]==i+1)); } } TheArrayTester; #endif // def Assert */ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> COMArray <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include #define COMArray(typ,src,dst) _variant_t V##dst(src); CCOMArray dst(V##dst); /* Usage: COMArray(double,GetKnots(),Knots); double K7=Knots[7]; or: COMArray(double, Spline->Evaluate(t), Coord); if(Coord.Data==0) return false; Point.x=Coord[0]; Point.y=Coord[1]; Point.z=Coord[2]; */ template class CCOMArray { // To retrieve arrays from COM _variant_t& V; public: T* Data; CCOMArray(_variant_t& V) : V(V), Data(0) {SafeArrayAccessData (V.parray, reinterpret_cast(&Data));} virtual ~CCOMArray() {try {if(Data) SafeArrayUnaccessData(V.parray);}catch(...){ASSERT(0);}} T* operator()() {return Data;} // Shorthand access to the array T& operator[](int i) const {return Data[i];} // Shorthand access to an element }; #endif // ndef Arrayh