portManager.hpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23  \ *---------------------------------------------------------------------------*/
24 
25 #ifndef PORT_MANAGER_HPP
26 #define PORT_MANAGER_HPP
27 
28 #include "portDefinitions.hpp"
29 #include <unordered_map>
30 #include <cassert>
31 
32 
33 namespace mimmo{
34 
35 
44 struct InfoPort{
45 
46  long id;
47  std::string container;
48  std::string datatype;
49  std::string file;
52  id=0;
53  container="";
54  datatype="";
55  file = "";
56  };
58  virtual ~InfoPort(){};
59 };
60 
71 class PortManager {
72 
73 private:
75  PortManager(){};
77  PortManager(const PortManager&);
79  PortManager& operator=(const PortManager&);
81  ~PortManager() {};
82 
83 public:
85  static PortManager& instance(){
86  static PortManager manager;
87  return manager;
88  }
89 
99  long addPort(const std::string name, const std::string container_name, const std::string datatype_name, const std::string fileregistration)
100  {
101 
102  if(containsPort(name) > 0) {
103 
104  std::string orcont =ports[name].container;
105  std::string ordata =ports[name].datatype;
106  if( orcont != container_name || ordata != datatype_name){
107  std::cerr<<"PORT REGISTRATION ERROR: ================"<<std::endl;
108  std::cerr<<"Failed Registration of Port "<<name<<" - "<<container_name<<" - "<<datatype_name<<" in file :"<<fileregistration<<std::endl;
109  std::cerr<<"It already exists as: "<<name<<" - "<<orcont<<" - "<<ordata<<" in file :"<<fileregistration<<std::endl;
110  std::cerr<<"========================================="<<std::endl;
111  assert(false && "Port is already registered with different container or datatype or both");
112  }
113  return ports[name].id;
114  }
115  InfoPort temp;
116  temp.container = container_name;
117  temp.datatype = datatype_name;
118  temp.file = fileregistration;
119  long idC=long(containers.size()), idD=long(datatypes.size());
120 
121 
122  temp.id = long(ports.size());
123  ports[name]= temp;
124 
125  if(!containsContainer(container_name)){
126  containers[container_name]= idC;
127  }
128 
129  if(!containsDatatype(datatype_name)){
130  datatypes[datatype_name]= idD;
131  }
132 
133  return (long) temp.id;
134  }
135 
140  bool containsPort(const std::string name){
141  return ports.count(name) > 0;
142  }
143 
148  bool containsContainer(const std::string name){
149  return containers.count(name) > 0;
150  }
151 
156  bool containsDatatype(const std::string name){
157  return datatypes.count(name) > 0;
158  }
159 
162  std::unordered_map<std::string, InfoPort> mapRegisteredPorts(){
163  return ports;
164  }
165 
168  std::unordered_map<std::string, long> mapRegisteredContainers(){
169  return containers;
170  }
171 
174  std::unordered_map<std::string, long> mapRegisteredDataTypes(){
175  return datatypes;
176  }
177 
183  InfoPort getPortData(const std::string name){
184  assert(containsPort(name));
185  return ports[name];
186  }
187 
188 
189 private:
190  std::unordered_map<std::string, InfoPort> ports;
191  std::unordered_map<std::string, long> containers;
192  std::unordered_map<std::string, long> datatypes;
193 };
194 
195 };
196 
197 
198 
211 #define REGISTER_PORT(Name, Container, Datatype, ManipBlock) \
212 /* Register a Port in the mimmo::portManager*/ \
213 __attribute__((unused)) static long port_##Name##_##Container##_##Datatype##_##ManipBlock = mimmo::PortManager::instance().addPort(Name, Container, Datatype, __FILE__);\
214 
215 /*static long port_##Name##_##Container##_##Datatype##_##ManipBlock = mimmo::PortManager::instance().addPort(Name, Container, Datatype, __FILE__);\ */
216 
217 
224 inline bool isPORT_REGISTERED(const std::string name){
226 }
227 
228 
229 #endif
std::unordered_map< std::string, long > mapRegisteredContainers()
std::string datatype
Definition: portManager.hpp:48
std::string container
Definition: portManager.hpp:47
static PortManager & instance()
Definition: portManager.hpp:85
std::unordered_map< std::string, InfoPort > mapRegisteredPorts()
std::unordered_map< std::string, long > mapRegisteredDataTypes()
long addPort(const std::string name, const std::string container_name, const std::string datatype_name, const std::string fileregistration)
Definition: portManager.hpp:99
InfoPort getPortData(const std::string name)
Basic singleton for managing Ports declaration in mimmo.
Definition: portManager.hpp:71
virtual ~InfoPort()
Definition: portManager.hpp:58
std::string file
Definition: portManager.hpp:49
bool isPORT_REGISTERED(const std::string name)
collection of data functional to a port registration.
Definition: portManager.hpp:44
bool containsContainer(const std::string name)
bool containsDatatype(const std::string name)
bool containsPort(const std::string name)