SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
ArcMap.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 
11 #ifndef smtk_graph_ArcMap_h
12 #define smtk_graph_ArcMap_h
13 
14 #include "smtk/PublicPointerDefs.h"
15 
16 #include "smtk/common/CompilerInformation.h"
17 #include "smtk/common/TypeContainer.h"
18 #include "smtk/common/TypeName.h"
19 #include "smtk/graph/ArcImplementation.h"
20 #include "smtk/string/Token.h"
21 
22 #include <set>
23 
24 namespace smtk
25 {
26 namespace graph
27 {
28 
37 class SMTKCORE_EXPORT ArcMap : public smtk::common::TypeContainer
38 {
39 public:
43 
44  ArcMap() {} // NOLINT . MSVC2019 barfs with C2580 on "= default;"
45 
46  template<typename List>
47  ArcMap()
48  {
49  this->insertArcImplementations<List>();
50  }
51 
52  template<typename List>
54  {
55  this->insertArcImplementations<List>();
56  }
57 
59  ArcMap(const ArcMap&) = delete;
60  ArcMap& operator=(const ArcMap&) = delete;
61 
62  ~ArcMap() override = default;
63 
70  template<typename ArcTraits>
72  {
73  if (this->contains<ArcImplementation<ArcTraits>>())
74  {
75  const auto& arcObject = this->get<ArcImplementation<ArcTraits>>();
76  return &arcObject;
77  }
78  return nullptr;
79  }
80 
81  template<typename ArcTraits>
83  {
84  if (this->contains<ArcImplementation<ArcTraits>>())
85  {
86  auto& arcObject = this->get<ArcImplementation<ArcTraits>>();
87  return &arcObject;
88  }
89  return nullptr;
90  }
92 
100  template<typename Tuple, typename Functor, typename... Args>
101  void invoke(Args&&... args) const
102  {
103  ArcMap::invokeFunctors<0, Tuple, Functor>(std::forward<Args>(args)...);
104  }
105 
106  template<typename Tuple, typename Functor, typename... Args>
107  void invoke(Args&&... args)
108  {
109  ArcMap::invokeFunctors<0, Tuple, Functor>(std::forward<Args>(args)...);
110  }
112 
114  const std::set<smtk::string::Token>& types() const { return m_types; }
115 
116 protected:
117  template<typename Type>
118  void insertArcImplementation()
119  {
120  ArcImplementation<Type> arcObject;
121  if (this->insert(arcObject))
122  {
123  smtk::string::Token arcTypeName = smtk::common::typeName<Type>();
124  m_types.insert(arcTypeName);
125  }
126  }
127 
128  // const version
129  template<typename Entry, typename Functor, typename... Args>
130  void invokeFunctor(Args&&... args) const
131  {
132  Functor f;
133  f(this->at<Entry>(), std::forward<Args>(args)...);
134  }
135 
136  // non-const version
137  template<typename Entry, typename Functor, typename... Args>
138  void invokeFunctor(Args&&... args)
139  {
140  Functor f;
141  f(this->at<Entry>(), std::forward<Args>(args)...);
142  }
143 
144  template<typename Tuple>
145  void insertArcImplementations()
146  {
147  ArcMap::insertArcImplementations<0, Tuple>();
148  }
149 
150 private:
151  template<std::size_t I, typename Tuple>
152  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type
153  insertArcImplementations()
154  {
155  this->insertArcImplementation<typename std::tuple_element<I, Tuple>::type>();
156  ArcMap::insertArcImplementations<I + 1, Tuple>();
157  }
158 
159  template<std::size_t I, typename Tuple>
160  inline typename std::enable_if<I == std::tuple_size<Tuple>::value>::type
161  insertArcImplementations()
162  {
163  }
164 
165  // const versions
166  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
167  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type invokeFunctors(
168  Args&&... args) const
169  {
170  this->invokeFunctor<typename std::tuple_element<I, Tuple>::type, Functor>(
171  std::forward<Args>(args)...);
172  ArcMap::invokeFunctors<I + 1, Tuple, Functor>(std::forward<Args>(args)...);
173  }
174 
175  // non-const version
176  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
177  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type invokeFunctors(
178  Args&&... args)
179  {
180  this->invokeFunctor<typename std::tuple_element<I, Tuple>::type, Functor>(
181  std::forward<Args>(args)...);
182  ArcMap::invokeFunctors<I + 1, Tuple, Functor>(std::forward<Args>(args)...);
183  }
184 
185  // This only needs a const version.
186  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
187  inline typename std::enable_if<I == std::tuple_size<Tuple>::value>::type invokeFunctors(
188  Args&&...) const
189  {
190  }
191 
192  std::set<smtk::string::Token> m_types;
193 };
194 
195 } // namespace graph
196 } // namespace smtk
197 
198 #endif // smtk_graph_ArcMap_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
PublicPointerDefs.h
smtk::graph::ArcMap::types
const std::set< smtk::string::Token > & types() const
Return the type-names of the arc types accepted by this ArcMap instance.
Definition: ArcMap.h:114
smtk::graph::ArcMap::at
const ArcImplementation< ArcTraits > * at() const
Return the arc's implementation object given its trait type.
Definition: ArcMap.h:71
smtk::graph::ArcMap::invoke
void invoke(Args &&... args) const
Invoke a Functor (which accepts args) on each arc type in the Tuple.
Definition: ArcMap.h:101
smtk::common::UUID
Definition: UUID.h:38
TypeName.h
Named type functions.
smtk::graph::ArcImplementation
A wrapper around arc type-traits classes that provides API.
Definition: ArcImplementation.h:43
smtkSuperclassMacro
#define smtkSuperclassMacro(...)
Add a typedef to the superclass of this class.
Definition: SharedFromThis.h:87
smtk::string::Token
A string token identified by an integer.
Definition: Token.h:30
smtk::graph::ArcMap
A container for arcs held by a resource.
Definition: ArcMap.h:37
smtkTypeMacroBase
#define smtkTypeMacroBase(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:55
smtk::common::TypeMap
TypeMap is a generalized map for storing and accessing data using a key.
Definition: TypeMap.h:51
smtk::identity
Embeds a type in another class so its type information can be passed as a parameter.
Definition: TupleTraits.h:340
smtk::common::TypeContainer
A container for caching and retrieving instances of types.
Definition: TypeContainer.h:37