Difference between revisions of "LSObjectCollection"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
== Example == | == Example == | ||
− | class Foo | + | class Foo |
− | { | + | { |
− | public: | + | public: |
− | + | Foo() | |
− | + | { | |
− | + | } | |
− | + | ~Foo() | |
− | + | { | |
− | + | } | |
− | + | ||
− | + | char Name[64]; | |
− | }; | + | }; |
− | + | ||
− | class FooType : public LSTypeDefinition | + | class FooType : public LSTypeDefinition |
− | { | + | { |
− | public: | + | public: |
− | + | // yadda yadda, all the type stuff goes here, but here's the FreeVariable example. this gets called | |
− | + | // for each object in the collection when it needs to be destroyed | |
− | + | void FreeVariable(LSOBJECTDATA &ObjectData) | |
− | + | { | |
− | + | Foo *pPtr=(Foo *)ObjectData.Ptr; | |
− | + | delete pPtr; | |
− | + | } | |
− | + | ||
− | }; | + | }; |
− | + | ||
− | + | ||
− | LSObjectCollection MyCollection(pFooType,0);// our collection will store Foos, which does not use sub-types so we 0 it | + | LSObjectCollection MyCollection(pFooType,0);// our collection will store Foos, which does not use sub-types so we 0 it |
− | void FooBar() | + | void FooBar() |
− | { | + | { |
− | + | // we have some instance of Foo. if we allocate it, then we need to be allocating all of them. | |
− | + | // if we dont, then we need to not be allocating all of them. doesnt matter as long as our pFooType code | |
− | + | // handles deallocation if needed | |
− | + | Foo *pFoo=new Foo; | |
− | + | ||
− | + | // convert the pointer to LSOBJECTDATA, which can either be done with a funky typecast or using its union | |
− | + | LSOBJECTDATA ObjectData; | |
− | + | ObjectData.Ptr=pFoo; | |
− | + | ||
− | + | // use MyCollection.SetItem | |
− | + | MyCollection.SetItem(pFoo->Name,ObjectData); | |
− | } | + | } |
Latest revision as of 19:05, 30 September 2006
Just storing this example for later.
Example
class Foo { public: Foo() { } ~Foo() { } char Name[64]; }; class FooType : public LSTypeDefinition { public: // yadda yadda, all the type stuff goes here, but here's the FreeVariable example. this gets called // for each object in the collection when it needs to be destroyed void FreeVariable(LSOBJECTDATA &ObjectData) { Foo *pPtr=(Foo *)ObjectData.Ptr; delete pPtr; } }; LSObjectCollection MyCollection(pFooType,0);// our collection will store Foos, which does not use sub-types so we 0 it void FooBar() { // we have some instance of Foo. if we allocate it, then we need to be allocating all of them. // if we dont, then we need to not be allocating all of them. doesnt matter as long as our pFooType code // handles deallocation if needed Foo *pFoo=new Foo; // convert the pointer to LSOBJECTDATA, which can either be done with a funky typecast or using its union LSOBJECTDATA ObjectData; ObjectData.Ptr=pFoo; // use MyCollection.SetItem MyCollection.SetItem(pFoo->Name,ObjectData); }