/// <summary> /// Adds an object to be tracked by this objecttype. /// </summary> /// <param name="existingObject">Object to track.</param> /// <param name="assumeNew">If false, ignores objects not ready to be saved (with id=-1). /// Else assign a new ID if the object has not been saved yet.</param> /// <returns>True on success. False if somehow the add failed.</returns> public virtual bool Add(ISaveable existingObject, bool assumeNew = true) { int id = existingObject.GetSaveID(); if (assumeNew && id == -1) { // SetSaveID is only called by two things: Loading, and this method. By locking here, this should be threadsafe. lock (this) { id = existingObject.GetSaveID(); if (id == -1) { id = NextFreeNumber(); //Note that this may get a lock on ItemDictionary also, and then the file stream. existingObject.SetSaveID(id); existingObject.Save(); } } } if (id >= 0) { Add(existingObject, id, false); return(true); } return(false); }
public override string GetValue(IModifiable source) { ISaveable value = Getter(source); if (value == null) { return("null"); } else { return(value.GetType().Name + " " + value.GetSaveID()); } }
/// <summary> /// Attempts to remove an object from this type. /// </summary> /// <param name="saveable"></param> /// <returns></returns> public virtual bool Remove(ISaveable saveable) { int id = saveable.GetSaveID(); if (id == -1) { return(false); } bool success; lock (ItemDictionary) { success = (ItemDictionary.Count > id && ItemDictionary[id] == saveable); if (success) { ItemDictionary[id] = null; ReturnIndex(id); } } return(success); }
public bool Contains(ISaveable obj) { return(Contains(obj.GetSaveID())); }