iMSTK
Interactive Medical Simulation Toolkit
imstkTaskNode.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include <atomic>
10 #include <functional>
11 #include <string>
12 
13 namespace imstk
14 {
20 class TaskNode
21 {
22 public:
23  TaskNode() : m_globalId(TaskNode::getUniqueID()) { }
24 
25  TaskNode(std::function<void()> func, std::string name = "none", bool isCritical = false) :
26  m_name(name), m_isCritical(isCritical),
27  m_computeTime(0.0), m_func(func),
28  m_globalId(TaskNode::getUniqueID())
29  {
30  }
31 
32  TaskNode(TaskNode& other)
33  {
34  // When copied from another take all tis values
35  // but its global id
36  m_name = other.m_name;
37  m_enabled = other.m_enabled;
38  m_isCritical = other.m_isCritical;
39  m_computeTime = other.m_computeTime;
40  m_enableTiming = other.m_enableTiming;
41  m_func = other.m_func;
42  m_globalId = getUniqueID();
43  }
44 
45  void operator=(const TaskNode& other)
46  {
47  // When set equal to another take all its values but
48  // its global id
49  m_name = other.m_name;
50  m_enabled = other.m_enabled;
51  m_isCritical = other.m_isCritical;
52  m_computeTime = other.m_computeTime;
53  m_enableTiming = other.m_enableTiming;
54  m_func = other.m_func;
55  m_globalId = getUniqueID();
56  }
57 
58  virtual ~TaskNode() = default;
59 
60  void setFunction(std::function<void()> func) { this->m_func = func; }
61  void setEnabled(bool enabled) { this->m_enabled = enabled; }
62 
66  bool isFunctional() const { return m_func != nullptr; }
67 
71  virtual void execute();
72 
76  size_t getGlobalId() const { return m_globalId; }
77 
81  static size_t getNumGlobalIds() { return s_numGlobalIds; }
82 
83 protected:
84  static size_t getUniqueID()
85  {
86  const size_t idx = s_numGlobalIds;
87  s_numGlobalIds++;
88  return idx;
89  }
90 
91 public:
92  std::string m_name = "none";
93  bool m_enabled = true;
94  bool m_isCritical = false;
95  double m_computeTime = 0.0;
96  bool m_enableTiming = false;
97 
98 protected:
99  std::function<void()> m_func = nullptr;
100 
102  size_t m_globalId = static_cast<size_t>(-1);
103  static std::atomic<size_t> s_numGlobalIds;
104 };
105 } // namespace imstk
size_t getGlobalId() const
Get the global (unique) index of the geometry.
Definition: imstkTaskNode.h:76
Base class for TaskGraph nodes.
Definition: imstkTaskNode.h:20
bool isFunctional() const
Returns true if function is nullptr.
Definition: imstkTaskNode.h:66
size_t m_globalId
Mutex lock for thread-safe counter update.
Compound Geometry.
static size_t getNumGlobalIds()
Get number of ids/taskNodes.
Definition: imstkTaskNode.h:81
std::function< void()> m_func
Don&#39;t allow user to call directly (must use execute)
Definition: imstkTaskNode.h:99
virtual void execute()
Calls the function pointer provided if node enabled.