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/graph/ArcImplementation.h"
19 
20 namespace smtk
21 {
22 namespace graph
23 {
24 
33 class SMTKCORE_EXPORT ArcMap : public smtk::common::TypeContainer
34 {
35 public:
39 
40  ArcMap() {} // NOLINT . MSVC2019 barfs with C2580 on "= default;"
41 
42  template<typename List>
43  ArcMap()
44  {
45  this->insertArcImplementations<List>();
46  }
47 
48  template<typename List>
50  {
51  this->insertArcImplementations<List>();
52  }
53 
55  ArcMap(const ArcMap&) = delete;
56  ArcMap& operator=(const ArcMap&) = delete;
57 
58  ~ArcMap() override = default;
59 
66  template<typename ArcTraits>
68  {
69  if (this->contains<ArcImplementation<ArcTraits>>())
70  {
71  const auto& arcObject = this->get<ArcImplementation<ArcTraits>>();
72  return &arcObject;
73  }
74  return nullptr;
75  }
76 
77  template<typename ArcTraits>
79  {
80  if (this->contains<ArcImplementation<ArcTraits>>())
81  {
82  auto& arcObject = this->get<ArcImplementation<ArcTraits>>();
83  return &arcObject;
84  }
85  return nullptr;
86  }
88 
96  template<typename Tuple, typename Functor, typename... Args>
97  void invoke(Args&&... args) const
98  {
99  ArcMap::invokeFunctors<0, Tuple, Functor>(std::forward<Args>(args)...);
100  }
101 
102  template<typename Tuple, typename Functor, typename... Args>
103  void invoke(Args&&... args)
104  {
105  ArcMap::invokeFunctors<0, Tuple, Functor>(std::forward<Args>(args)...);
106  }
108 
109 protected:
110  template<typename Type>
111  void insertArcImplementation()
112  {
113  ArcImplementation<Type> arcObject;
114  this->insert(arcObject);
115  }
116 
117  // const version
118  template<typename Entry, typename Functor, typename... Args>
119  void invokeFunctor(Args&&... args) const
120  {
121  Functor f;
122  f(this->at<Entry>(), std::forward<Args>(args)...);
123  }
124 
125  // non-const version
126  template<typename Entry, typename Functor, typename... Args>
127  void invokeFunctor(Args&&... args)
128  {
129  Functor f;
130  f(this->at<Entry>(), std::forward<Args>(args)...);
131  }
132 
133  template<typename Tuple>
134  void insertArcImplementations()
135  {
136  ArcMap::insertArcImplementations<0, Tuple>();
137  }
138 
139 private:
140  template<std::size_t I, typename Tuple>
141  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type
142  insertArcImplementations()
143  {
144  this->insertArcImplementation<typename std::tuple_element<I, Tuple>::type>();
145  ArcMap::insertArcImplementations<I + 1, Tuple>();
146  }
147 
148  template<std::size_t I, typename Tuple>
149  inline typename std::enable_if<I == std::tuple_size<Tuple>::value>::type
150  insertArcImplementations()
151  {
152  }
153 
154  // const versions
155  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
156  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type invokeFunctors(
157  Args&&... args) const
158  {
159  this->invokeFunctor<typename std::tuple_element<I, Tuple>::type, Functor>(
160  std::forward<Args>(args)...);
161  ArcMap::invokeFunctors<I + 1, Tuple, Functor>(std::forward<Args>(args)...);
162  }
163 
164  // non-const version
165  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
166  inline typename std::enable_if<I != std::tuple_size<Tuple>::value>::type invokeFunctors(
167  Args&&... args)
168  {
169  this->invokeFunctor<typename std::tuple_element<I, Tuple>::type, Functor>(
170  std::forward<Args>(args)...);
171  ArcMap::invokeFunctors<I + 1, Tuple, Functor>(std::forward<Args>(args)...);
172  }
173 
174  // This only needs a const version.
175  template<std::size_t I, typename Tuple, typename Functor, typename... Args>
176  inline typename std::enable_if<I == std::tuple_size<Tuple>::value>::type invokeFunctors(
177  Args&&...) const
178  {
179  }
180 };
181 
182 } // namespace graph
183 } // namespace smtk
184 
185 #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::at
const ArcImplementation< ArcTraits > * at() const
Return the arc's implementation object given its trait type.
Definition: ArcMap.h:67
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:97
smtk::common::UUID
Definition: UUID.h:38
smtk::graph::ArcImplementation
A wrapper around arc type-traits classes that provides API.
Definition: ArcImplementation.h:42
smtkSuperclassMacro
#define smtkSuperclassMacro(...)
Add a typedef to the superclass of this class.
Definition: SharedFromThis.h:87
smtk::graph::ArcMap
A container for arcs held by a resource.
Definition: ArcMap.h:33
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:35