/** \brief Force the pointer for the object in store to be a specific pointer. This can be useful for singleton values. These usually must be removed from the store explicitly. \pre No object with this value is already in the store. */ void set (std::shared_ptr <Value const> pointer) { // The value must not be in the store yet. assert (objects_.find (*pointer, boost::hash <Value>(), EqualToWeakPtr()) == objects_.end()); objects_.insert (std::move (pointer)); }
// Rvalue reference. std::shared_ptr <Value const> get (Value && value) { auto existing = objects_.find (value, boost::hash <Value>(), EqualToWeakPtr()); if (existing != objects_.end()) return existing->get(); // The value is not in the store yet; insert it. auto sole = SoleType::construct (*this, std::move (value)); Pointer newElement (sole); objects_.insert (std::move (newElement)); return sole; }
bool remap(ID source,ID target) // change ID from source to target { // source->target // target->new id if(!contains(source)) return false; // remap failed // find object Objects::iterator it=objects.find(source); // get pointer Object *object=it->second;//objects[source]; // remove objects.erase(it); if(contains(target)) // if <target> is valid object -> remap it to new ID remap(target,generator->generate(getTarget(objects[target]))); objects[target]=object; // set new ID object->localID=target; object->back=objects.find(target); return true; }
// called in Stored constructor // assignes new unique ID and back iterator virtual ID add(Target * object) { assert(generator); if(!valid(object->id())) object->localID=generator->generate(object); else if(contains(object->id())) { assert(false); } objects.insert(std::make_pair(object->id(),object)); Objects::iterator it=objects.find(object->id()); object->back=it; onAdd(object); // raise onAdd event. return object->id(); }
/** \brief Retrieve the sole object with value \a value. If an object with this value is already in the store, return a pointer to that object. If not, return a pointer to a newly allocated object. */ std::shared_ptr <Value const> get (Value const & value) { auto existing = objects_.find (value, boost::hash <Value>(), EqualToWeakPtr()); if (existing != objects_.end()) return existing->get(); // The value is not in the store yet; insert it. auto sole = SoleType::construct (*this, value); Pointer newElement (sole); // Note that this insertion involves a mere copy of newElement into // another place in memory. // No objects will therefore be deleted, and no objects will // therefore attempt to remove themselves from the store. objects_.insert (std::move (newElement)); return sole; }
bool contains(ID id) { return objects.find(id)!=objects.end(); }
void destroy(ID id) { Objects::iterator it=objects.find(id); if(it!=objects.end()) delete it->second; }