Pool.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <momemta/Pool.h>
20 
21 #include <momemta/Logging.h>
22 
23 void Pool::remove(const InputTag& tag, bool force/* = true*/) {
24  auto it = m_storage.find(tag);
25  if (it == m_storage.end())
26  return;
27 
28  if (!force && it->second.valid)
29  return;
30 
31  m_storage.erase(it);
32 }
33 
34 void Pool::remove_if_invalid(const InputTag& tag) {
35  remove(tag, false);
36 }
37 
38 momemta::any Pool::reserve(const InputTag& tag) {
39  auto it = m_storage.find(tag);
40  if (it == m_storage.end()) {
41  // This tag do not exist currently in the pool
42  // Reserve a slot, but mark it as invalid.
43  // Once a module inform the pool it produces such a tag, the slot will
44  // be flagged as valid.
45  PoolContent content { momemta::any(), false };
46  it = m_storage.emplace(tag, content).first;
47  }
48 
49  return it->second.ptr;
50 }
51 
52 void Pool::alias(const InputTag& from, const InputTag& to) {
53  if (from.isIndexed() || to.isIndexed()) {
54  throw std::invalid_argument("Indexed input tag cannot be passed as argument of the pool. Use the `get` function of the input tag to retrieve its content.");
55  }
56 
57  auto from_it = m_storage.find(from);
58  if (from_it == m_storage.end())
59  throw tag_not_found_error("No such tag in pool: " + from.toString());
60 
61  auto to_it = m_storage.find(to);
62  if (to_it != m_storage.end())
63  throw duplicated_tag_error("A module already produced the tag '" + to.toString() + "'");
64 
65  m_storage[to] = m_storage[from];
66 }
67 
68 bool Pool::exists(const InputTag& tag) const {
69  auto it = m_storage.find(tag);
70  return it != m_storage.end();
71 }
72 
73 class invalid_state: public std::runtime_error {
74  using std::runtime_error::runtime_error;
75 };
76 
77 void Pool::freeze() {
78  m_frozen = true;
79 
80  // Iterate over the storage, and check if any block is invalid.
81  for (const auto& it: m_storage) {
82  if (!it.second.valid) {
83  LOG(fatal) << "Memory block '" << it.first.toString() << "' is flagged as invalid. This should not happen. Please open a bug report at <>.";
84  throw invalid_state("Memory pool state is invalid");
85  }
86  }
87 }
bool isIndexed() const
Definition: InputTag.cc:93
bool exists(const InputTag &tag) const
Check if input tag exists in the pool.
Definition: Pool.cc:68
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
std::string toString() const
Convert the InputTag in its string representation.
Definition: InputTag.cc:89