CMS 3D CMS Logo

Store.h
Go to the documentation of this file.
1 #ifndef DDI_Store_h
2 #define DDI_Store_h
3 
4 #include <map>
7 
8 //;
9 //FIXME: Store : implement readOnly-behaviour ..
10 namespace DDI {
11 
36  template <class N, class I, class K = I>
37  class Store {
38  public:
39  using name_type = N;
40  using pimpl_type = I;
41  using key_type = K;
43  using prep_type = Rep_type*;
44  using registry_type = std::map<name_type, prep_type>;
45  using iterator = typename registry_type::iterator;
46 
47  auto begin() { return reg_.begin(); }
48  auto end() { return reg_.end(); }
49  auto size() const { return reg_.size(); }
50 
51  // empty shell or fetch from registry
52  prep_type create(const name_type&);
53 
54  // full new object or replace existing with new one
56 
57  // clear all objects
58  void clear();
59 
60  // swap moves the registry from this guy to another of the same type
61  void swap(Store&);
62 
63  bool isDefined(const name_type& n) const;
64  void setReadOnly(bool b) { readOnly_ = b; }
65  bool readOnly() const { return readOnly_; }
66 
68  ~Store();
69 
70  protected:
71  std::map<name_type, prep_type> reg_;
72  Store(const Store&) = delete;
73  Store& operator=(const Store&) = delete;
74  bool readOnly_;
75  };
76 
77  template <class N, class I, class K>
79  prep_type tmp = nullptr;
80  auto result = reg_.emplace(n, tmp);
81  if (result.second) {
82  if (readOnly_)
83  throw cms::Exception("DetectorDescriptionStore")
84  << " Store has been locked. Illegal attempt to add " << n << " to a global store.";
85  // ELSE
86  result.first->second = new Rep_type(n, nullptr);
87  }
88  return result.first->second;
89  }
90 
91  template <class N, class I, class K>
93  if (readOnly_)
94  throw cms::Exception("DetectorDescriptionStore")
95  << " Store has been locked. Illegal attempt to add " << n << " to a global store.";
96  // ELSE
97  prep_type tmp = nullptr;
98  auto result = reg_.emplace(n, tmp);
99  if (!result.second) {
100  result.first->second->second.swap(p);
101  } else {
102  result.first->second = new Rep_type(n, std::move(p));
103  }
104  return result.first->second;
105  }
106 
107  template <typename I>
108  struct Finalize {
109  static void cleanup(I&& ptr) {}
110  };
111 
112  template <typename I>
113  struct Finalize<I*> {
114  static void cleanup(I* ptr) {
115  delete ptr;
116  ptr = nullptr;
117  }
118  };
119 
120  template <class N, class I, class K>
122  for (auto it : reg_) {
123  Finalize<I>::cleanup(std::move(it.second->second));
124  delete it.second;
125  it.second = nullptr;
126  }
127  }
128 
129  template <class N, class I, class K>
130  bool Store<N, I, K>::isDefined(const name_type& n) const {
131  if (readOnly_)
132  edm::LogWarning("DetectorDescriptionStore")
133  << " Store is locked and most likely empty. isDefined will be false.";
134  auto it = reg_.find(n);
135  bool result(false);
136  if (it != reg_.end()) {
137  if (it.second->second) {
138  result = true;
139  }
140  }
141  return result;
142  }
143 
144  template <class N, class I, class K>
146  reg_.swap(storeToSwap.reg_);
147  storeToSwap.readOnly_ = readOnly_;
148  }
149 
150 } // namespace DDI
151 
152 #endif
DDI::Store::isDefined
bool isDefined(const name_type &n) const
Definition: Store.h:130
DDI
Definition: DDCompactView.h:25
DDI::Store::reg_
std::map< name_type, prep_type > reg_
Definition: Store.h:71
MessageLogger.h
DDI::Store::create
prep_type create(const name_type &)
Definition: Store.h:78
funct::false
false
Definition: Factorize.h:29
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
DDI::Store< DDName, DDRotationMatrix * >::pimpl_type
DDRotationMatrix * pimpl_type
Definition: Store.h:40
DDName
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
DDI::Finalize
Definition: Store.h:108
DDI::Store::prep_type
Rep_type * prep_type
Definition: Store.h:43
DDI::Store
Definition: Store.h:37
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
DDI::Finalize< I * >::cleanup
static void cleanup(I *ptr)
Definition: Store.h:114
DDI::Store::swap
void swap(Store &)
Definition: Store.h:145
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
DDI::Store::Store
Store()
Definition: Store.h:67
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
DDI::Store::end
auto end()
Definition: Store.h:48
DDI::Store::clear
void clear()
Exhume::I
const std::complex< double > I
Definition: I.h:8
DDI::Store::operator=
Store & operator=(const Store &)=delete
DDI::Store::name_type
N name_type
Definition: Store.h:39
N
#define N
Definition: blowfish.cc:9
DDI::Store::begin
auto begin()
Definition: Store.h:47
b
double b
Definition: hdecay.h:118
DDI::rep_type
Definition: rep_type.h:30
DDI::Store::~Store
~Store()
Definition: Store.h:121
DDI::Store::size
auto size() const
Definition: Store.h:49
DDI::Store< DDName, DDRotationMatrix * >::registry_type
std::map< name_type, prep_type > registry_type
Definition: Store.h:44
DDI::Store::setReadOnly
void setReadOnly(bool b)
Definition: Store.h:64
DDI::Store< DDName, DDRotationMatrix * >::key_type
DDRotationMatrix * key_type
Definition: Store.h:41
DDI::Store< DDName, DDRotationMatrix * >::iterator
typename registry_type::iterator iterator
Definition: Store.h:45
eostools.move
def move(src, dest)
Definition: eostools.py:511
DDI::Store::readOnly
bool readOnly() const
Definition: Store.h:65
Exception
Definition: hltDiff.cc:245
mps_fire.result
result
Definition: mps_fire.py:311
rep_type.h
DDI::Store::readOnly_
bool readOnly_
Definition: Store.h:74
DDI::Finalize::cleanup
static void cleanup(I &&ptr)
Definition: Store.h:109