#include "stdafx.h" #include "V8DataModel/CollectionService.h" #include "V8DataModel/Configuration.h" namespace RBX { const char* const sCollectionService = "CollectionService"; REFLECTION_BEGIN(); static Reflection::BoundFuncDesc(std::string)> func_GetCollection(&CollectionService::getCollection, "GetCollection", "class", Security::None); Reflection::EventDesc)> event_collectionItemAdded(&CollectionService::itemAddedSignal, "ItemAdded", "instance"); Reflection::EventDesc)> event_collectionItemRemoved(&CollectionService::itemRemovedSignal, "ItemRemoved", "instance"); REFLECTION_END(); CollectionService::CollectionService() :DescribedNonCreatable(sCollectionService) { } shared_ptr CollectionService::getCollection(const Name& className) { return getCollection(className.toString()); } shared_ptr CollectionService::getCollection(std::string className) { const CollectionMap::iterator iter = collections.find(className); if(iter != collections.end()){ return iter->second->read(); } return shared_ptr(); } void CollectionService::removeInstance(shared_ptr instance) { const std::string& className = instance->getDescriptor().name.toString(); CollectionMap::iterator collectionIter = collections.find(className); RBXASSERT(collectionIter != collections.end()); Instances::iterator iter; shared_ptr c(collectionIter->second->write()); iter = std::find( c->begin(), c->end(), instance); if(iter != c->end()){ // Fast-remove. This can make a huge speed improvement over regular remove *iter = c->back(); c->pop_back(); itemRemovedSignal(instance); } else{ RBXASSERT(0); } } void CollectionService::addInstance(shared_ptr instance) { const std::string& className = instance->getDescriptor().name.toString(); CollectionMap::iterator collectionIter = collections.find(className); if(collectionIter == collections.end()){ collections[className].reset(new copy_on_write_ptr()); } shared_ptr c(collections[className]->write()); c->push_back(instance); itemAddedSignal(instance); } }