Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PdbClassMap.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PdbClassMap.h
1 #ifndef PDBCAL_BASE_PDBCLASSMAP_H
2 #define PDBCAL_BASE_PDBCLASSMAP_H
3 
4 //
5 // This class is a singleton wrapper around the STL-map.
6 // It ensures EXACTLY one existing instance of 'classmap'.
7 //
8 // Author: Matthias Messer 6/22/99
9 
10 #include <cstring>
11 #include <map>
12 #include <string>
13 
14 //
15 // The char* comparison operator, required by the STL map
16 //
17 template <typename T>
18 struct strless :
19  std::binary_function<T, T, bool> {
20  bool operator()(const T& x, const T& y) const
21  {
22  return strcmp(x,y) < 0;
23  }
24 };
25 
26 template <typename T>
28 {
29 public:
30 
31  static PdbClassMap *instance();
32  virtual ~PdbClassMap();
33 
34  T*& operator [] (const char * className) { return _map[className]; }
35  typename std::map<const char*, T*, strless<const char*> >::iterator find(const char * className) { return _map.find(className); }
36  typename std::map<const char*, T*, strless<const char*> >::iterator end() { return _map.end(); }
37  void erase(const char * className);
38 
39 protected:
40  PdbClassMap();
41 
42 private:
44  std::map<const char*, T*, strless<const char*> > _map;
45 };
46 
47 template <typename T>
49 {
50 }
51 
52 template <typename T>
53 void PdbClassMap<T>::erase(const char * className)
54 {
55  _map.erase(_map.find(className));
56  // if we have no more entries - delete ourselves
57  if (_map.size() == 0)
58  {
59  delete _instance;
60  _instance = 0;
61  }
62 }
63 
64 template <typename T>
66 {
67  while( _map.begin() != _map.end())
68  {
69  delete _map.begin()->second;
70  _map.erase(_map.begin());
71  }
72 }
73 
74 template <typename T>
76 {
77  if (!_instance)
78  {
79  _instance = new PdbClassMap<T>();
80  }
81  return _instance;
82 }
83 
84 template <typename T> PdbClassMap<T>* PdbClassMap<T>::_instance = 0;
85 
86 #endif /* PDBCAL_BASE_PDBCLASSMAP_H */