void clearOne(T* t) { if(!t) return; { CriticalBlock b(crit); typename std::vector<Linked<T> >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get() && it->get() == t) { it->clear(); break; } } } }
void clearAll() { CriticalBlock b(crit); typename std::vector<Linked<T> >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get()) { it->clear(); } } }
Linked<T> get(long timeout=0) { const long interval=1000; for(;;) { { CriticalBlock b(crit); typename std::vector<Linked<T> >::iterator it; for(it=resources.begin();it!=resources.end();it++) { if(it->get() == NULL) { Owned<T> e = factory->createResource(); if(e) { it->set(e.get()); return e; } } else if(!it->get()->IsShared()) { return *it; } } } long slp=timeout!=INFINITE && timeout<interval ? timeout : interval; if(slp<=0) break; long start=msTick(); long elapsed=sem.wait(slp) ? (msTick()-start) : slp; if(timeout!=INFINITE) timeout-=elapsed; } throw MakeStringException(1, "Run out of resources"); }
int main(int argc, char *argv[]) { typedef std::tr1::shared_ptr<AbstractTrack> TrackPtr; std::vector<TrackPtr> tracks; tracks.push_back(TrackPtr(new Track<int>(1, 1))); tracks.push_back(TrackPtr(new Track<float>(2, 2.0))); typename std::vector<TrackPtr>::iterator iter; for (iter = tracks.begin(); iter != tracks.end(); ++iter) { AbstractTrack *track = iter->get(); std::cout << track->getId() << ": "; //std::cout << track->getValue(); std::cout << std::endl; } return 0; }
void helper::unify_shared_vector(std::vector<boost::shared_ptr<T> > &v, const _Compare &c) { std::map<T *, std::vector<std::size_t>, _Compare> m(c); typename std::vector<boost::shared_ptr<T> >::const_iterator vit; typename std::vector<std::size_t>::const_iterator vit2; typename std::map<T *, std::vector<std::size_t>, _Compare>::const_iterator mit; std::size_t i; for (i=0, vit=v.begin(); vit!=v.end(); vit++, i++) { std::vector<std::size_t> &vv = m[vit->get()]; vv.push_back(i); } for (mit=m.begin(); mit!=m.end(); mit++) { const std::vector<std::size_t> &vv = mit->second; boost::shared_ptr<T> &p = v[vv[0]]; for (vit2=vv.begin()+1; vit2!=vv.end(); vit2++) { v[*vit2] = p; } } }