CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/GeneratorInterface/LHEInterface/src/LHEProxy.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/LHEInterface/interface/LHEProxy.h"
00008 
00009 using namespace lhef;
00010 
00011 static boost::mutex mutex;
00012 
00013 typedef std::map< LHEProxy::ProxyID, boost::weak_ptr<LHEProxy> > 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 LHEProxy::LHEProxy(ProxyID id) :
00028         id(id)
00029 {
00030 }
00031 
00032 LHEProxy::~LHEProxy()
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<LHEProxy> LHEProxy::create()
00042 {
00043         static LHEProxy::ProxyID nextProxyID = 0;
00044 
00045         boost::mutex::scoped_lock scoped_lock(mutex);
00046 
00047         boost::shared_ptr<LHEProxy> proxy(new LHEProxy(++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<LHEProxy> LHEProxy::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<LHEProxy>();
00063 
00064         ProxyMap::const_iterator pos = map->find(id);
00065         if (pos == map->end())
00066                 return boost::shared_ptr<LHEProxy>();
00067 
00068         return boost::shared_ptr<LHEProxy>(pos->second);
00069 }