Pool.h
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 
20 #pragma once
21 
22 #include <assert.h>
23 #include <memory>
24 #include <unordered_map>
25 
26 #include <momemta/any.h>
27 #include <momemta/impl/InputTag_fwd.h>
28 #include <momemta/Value.h>
29 
30 // A simple memory pool
31 
35 struct PoolContent {
36  momemta::any ptr;
37  bool valid;
38 };
39 
40 class Pool {
41  public:
42  Pool() = default;
43 
52  template<typename T, typename... Args> std::shared_ptr<T> put(const InputTag& tag, Args&&... args);
53 
54  template<typename T> Value<T> get(const InputTag& tag) const;
55 
56  void alias(const InputTag& from, const InputTag& to);
57 
65  bool exists(const InputTag& tag) const;
66 
67  private:
68  friend class MoMEMta;
69  friend class Module;
70 
71  template <typename U>
72  friend class ValueProxy;
73 
74  using PoolStorage = std::unordered_map<InputTag, PoolContent>;
75 
76  friend struct InputTag;
77 
78  void remove(const InputTag&, bool force = true);
79  void remove_if_invalid(const InputTag&);
80 
81  momemta::any reserve(const InputTag&);
82 
83  template<typename T> std::shared_ptr<const T> raw_get(const InputTag& tag) const;
84 
85  template<typename T, typename... Args> PoolStorage::iterator create(const InputTag& tag,
86  bool valid, Args&&... args) const;
87 
88  public:
89  class tag_not_found_error: public std::runtime_error {
90  using std::runtime_error::runtime_error;
91  };
92 
93  class duplicated_tag_error: public std::runtime_error {
94  using std::runtime_error::runtime_error;
95  };
96 
97  class constructor_tag_error: public std::runtime_error {
98  using std::runtime_error::runtime_error;
99  };
100 
101 
102 private:
103 
111  virtual void freeze() final;
112 
113  Pool(const Pool&) = delete;
114  Pool& operator=(const Pool&) = delete;
115 
116  bool m_frozen = false;
117 
118  mutable PoolStorage m_storage;
119 };
120 
121 using PoolPtr = std::shared_ptr<Pool>;
A MoMEMta instance.
Definition: MoMEMta.h:44
An identifier of a module&#39;s output.
Definition: InputTag_fwd.h:37
Parent class for all the modules.
Definition: Module.h:37
Definition: Pool.h:40
bool valid
Pointer to the memory allocated for this block.
Definition: Pool.h:37
A class representing a value produced by a module.
Definition: Value.h:30