CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Proxy.cc
Go to the documentation of this file.
1 #include <map>
2 #include <mutex>
3 
5 
6 using namespace ThePEG;
7 
9 
10 typedef std::map<ProxyBase::ProxyID, std::weak_ptr<ProxyBase> > ProxyMap;
11 
13  static struct Sentinel {
14  Sentinel() : instance(new ProxyMap) {}
15  ~Sentinel() {
16  delete instance;
17  instance = nullptr;
18  }
19 
21  } sentinel;
22 
23  return sentinel.instance;
24 }
25 
27 
29  std::scoped_lock scoped_lock(mutex);
30 
32  if (map)
33  map->erase(id);
34 }
35 
36 std::shared_ptr<ProxyBase> ProxyBase::create(ctor_t ctor) {
37  static ProxyBase::ProxyID nextProxyID = 0;
38 
39  std::scoped_lock scoped_lock(mutex);
40 
41  std::shared_ptr<ProxyBase> proxy(ctor(++nextProxyID));
42 
44  if (map)
45  map->insert(ProxyMap::value_type(proxy->getID(), proxy));
46 
47  return proxy;
48 }
49 
50 std::shared_ptr<ProxyBase> ProxyBase::find(ProxyID id) {
51  std::scoped_lock scoped_lock(mutex);
52 
54  if (!map)
55  return std::shared_ptr<ProxyBase>();
56 
57  ProxyMap::const_iterator pos = map->find(id);
58  if (pos == map->end())
59  return std::shared_ptr<ProxyBase>();
60 
61  return std::shared_ptr<ProxyBase>(pos->second);
62 }
static PFTauRenderPlugin instance
std::map< ProxyBase::ProxyID, std::weak_ptr< ProxyBase > > ProxyMap
Definition: Proxy.cc:10
static std::mutex mutex
Definition: Proxy.cc:8
static ProxyMap * getProxyMapInstance()
Definition: Proxy.cc:12
ProxyBase(const ProxyBase &orig)=delete
static std::shared_ptr< ProxyBase > find(ProxyID id)
Definition: Proxy.cc:50
static std::shared_ptr< ProxyBase > create(ctor_t ctor)
Definition: Proxy.cc:36
virtual ~ProxyBase()
Definition: Proxy.cc:28
unsigned long ProxyID
Definition: Proxy.h:16