CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/GeneratorInterface/ThePEGInterface/src/Proxy.cc

Go to the documentation of this file.
00001 #include <map>
00002 
00003 #include <boost/thread.hpp>
00004 #include <boost/shared_ptr.hpp>
00005 #include <boost/weak_ptr.hpp>
00006 
00007 #include "GeneratorInterface/ThePEGInterface/interface/Proxy.h"
00008 
00009 using namespace ThePEG;
00010 
00011 static boost::mutex mutex;
00012 
00013 typedef std::map<ProxyBase::ProxyID, boost::weak_ptr<ProxyBase> > ProxyMap;
00014 
00015 static ProxyMap *getProxyMapInstance()
00016 {
00017         static struct Sentinel {
00018                 Sentinel() : instance(new ProxyMap) {}
00019                 ~Sentinel() { delete instance; instance = 0; }
00020 
00021                 ProxyMap        *instance;
00022         } sentinel;
00023 
00024         return sentinel.instance;
00025 }
00026 
00027 ProxyBase::ProxyBase(ProxyID id) :
00028         id(id)
00029 {
00030 }
00031 
00032 ProxyBase::~ProxyBase()
00033 {
00034         boost::mutex::scoped_lock scoped_lock(mutex);
00035 
00036         ProxyMap *map = getProxyMapInstance();
00037         if (map)
00038                 map->erase(id);
00039 }
00040 
00041 boost::shared_ptr<ProxyBase> ProxyBase::create(ctor_t ctor)
00042 {
00043         static ProxyBase::ProxyID nextProxyID = 0;
00044 
00045         boost::mutex::scoped_lock scoped_lock(mutex);
00046 
00047         boost::shared_ptr<ProxyBase> proxy(ctor(++nextProxyID));
00048 
00049         ProxyMap *map = getProxyMapInstance();
00050         if (map)
00051                 map->insert(ProxyMap::value_type(proxy->getID(), proxy));
00052 
00053         return proxy;
00054 }
00055 
00056 boost::shared_ptr<ProxyBase> ProxyBase::find(ProxyID id)
00057 {
00058         boost::mutex::scoped_lock scoped_lock(mutex);
00059 
00060         ProxyMap *map = getProxyMapInstance();
00061         if (!map)
00062                 return boost::shared_ptr<ProxyBase>();
00063 
00064         ProxyMap::const_iterator pos = map->find(id);
00065         if (pos == map->end())
00066                 return boost::shared_ptr<ProxyBase>();
00067 
00068         return boost::shared_ptr<ProxyBase>(pos->second);
00069 }