VPTissue Reference Manual
FunctionMap.h
Go to the documentation of this file.
1 #ifndef INC_FUNCTION_MAP_H
2 #define INC_FUNCTION_MAP_H
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include <functional>
23 #include <initializer_list>
24 #include <list>
25 #include <map>
26 #include <string>
27 #include <utility>
28 
29 namespace SimPT_Sim {
30 namespace Util {
31 
35 template<typename S>
37 {
38 public:
39  // Type of function stored in the map.
40  using FunctionType = std::function<S>;
41 
42 private:
44  using MapType = std::map<const std::string, const FunctionType>;
45 
46 public:
49 
51  FunctionMap(std::initializer_list<typename MapType::value_type> l) : m_map(l) {}
52 
54  virtual ~FunctionMap() {}
55 
57  FunctionType Get(const std::string& name) const
58  {
59  return (m_map.count(name) != 0) ? m_map.at(name) : FunctionType();
60  }
61 
63  bool IsValid(const std::string& name) const
64  {
65  return (m_map.count(name) != 0) && (m_map.at(name));
66  }
67 
69  bool Register(const std::string& name, const FunctionType& f)
70  {
71  bool status = false;
72  if ((m_map.count(name) == 0) && (f)){
73  m_map.insert(std::make_pair(name, f));
74  status = true;
75  }
76  return status;
77  }
78 
80  std::list<std::string> List() const
81  {
82  std::list<std::string> l;
83  for (const auto& e : m_map) {
84  l.push_back(e.first);
85  }
86  return l;
87  }
88 
89 private:
90  MapType m_map;
91 };
92 
93 } // namespace
94 } // namespace
95 
96 #endif // end-of-include-guard
97 
virtual ~FunctionMap()
Destructor must be virtual.
Definition: FunctionMap.h:54
bool Register(const std::string &name, const FunctionType &f)
Register a function.
Definition: FunctionMap.h:69
Namespace for the core simulator.
bool IsValid(const std::string &name) const
Check whether function name is present.
Definition: FunctionMap.h:63
std::list< std::string > List() const
List the names.
Definition: FunctionMap.h:80
A map to hold std::functions.
Definition: FunctionMap.h:36
FunctionType Get(const std::string &name) const
Return function for given name.
Definition: FunctionMap.h:57
FunctionMap()
Construct an empty map.
Definition: FunctionMap.h:48
FunctionMap(std::initializer_list< typename MapType::value_type > l)
Construct map from initializer_list.
Definition: FunctionMap.h:51