// mccontext.h // // Context for managing mcThings // Provides basic searching functions #ifndef _MCCONTEXT_H_ #define _MCCONTEXT_H_ #include "mcthing.h" #include "numstring.h" class mcContext { // Storage for mcThings // - by ID (designated as master) // BUGBUG Would it be better to have a common ID across all things? CMapStringToPtr idStorage[mcThing::TYPE_COUNT]; // - by name // Note: where a direct name is not available, some unique derived // string should be used instead eg where (in the database design) // two FK columns define a uniqueness constraint the name should be // something like the concatenation of the string representations of // the FK IDs. CMapStringToPtr nameStorage[mcThing::TYPE_COUNT]; public: mcContext() { } ~mcContext() { for (int i = 0; i < mcThing::TYPE_COUNT; i++) { // Only need to clear out ID map, since others are only caching // copies. mcThing *mctPtr; CString idKey; for (POSITION mctp = idStorage[i].GetStartPosition(); mctp != NULL; ) { idStorage[i].GetNextAssoc(mctp, idKey, (void *&)mctPtr); delete mctPtr; } } } // Add and remove methods void addThing(mcThing *thing) { mcThing::type thingType = thing->getType(); idStorage[thingType].SetAt(CNumString(thing->getId()), thing); nameStorage[thingType].SetAt(thing->getName(), thing); } void removeThing(mcThing *thing) { // Needs to check that deletion is possible if (thing->deletable()) { idStorage[thing->getType()].RemoveKey(CNumString(thing->getId())); nameStorage[thing->getType()].RemoveKey(thing->getName()); delete thing; } } // Search methods - by ID and 'name' // Note: type must always be known before you start mcThing *findThing(mcThing::type t, int id) { mcThing *result = 0; idStorage[t].Lookup(CNumString(id), (void *&)result); return result; } mcThing *findThing(mcThing::type t, const CString &name) { mcThing *result = 0; nameStorage[t].Lookup(name, (void *&)result); return result; } // Replacement method - needed by line segment to allow replacement of // unresolved name with correct name // Note: this is *only* supplied for name, since that could change void remapThingName(const CString &oldName, mcThing *thing) { nameStorage[thing->getType()].RemoveKey(oldName); nameStorage[thing->getType()].SetAt(thing->getName(), thing); } // Set recovery method - returns map of things of given type const CMapStringToPtr &getThings(mcThing::type t) { return idStorage[t]; } }; #endif // _CONTEXT_H_