VPTissue Reference Manual
SubjectNode.h
Go to the documentation of this file.
1 #ifndef VIEWER_SUBJECTNODE_H_INCLUDED
2 #define VIEWER_SUBJECTNODE_H_INCLUDED
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 "IViewerNode.h"
24 
25 #include <iostream>
26 #include <type_traits>
27 
28 namespace SimShell {
29 namespace Viewer {
30 
40 template <class SubjectType>
41 class SubjectNode : public IViewerNode
42 {
43 public:
44  SubjectNode(std::shared_ptr<Ws::MergedPreferences> p,
45  std::shared_ptr<SubjectType> s,
46  IViewerNode::ChildrenMap&& c);
47 
48  SubjectNode(std::shared_ptr<Ws::MergedPreferences> p,
49  std::shared_ptr<SubjectType> s);
50 
51  virtual ~SubjectNode() {}
52 
53  virtual void Disable();
54 
55  virtual void Enable();
56 
57  virtual bool IsEnabled() const;
58 
59  virtual bool IsParentEnabled() const;
60 
61  virtual IViewerNode::ChildrenMap::const_iterator begin() const;
62 
63  virtual IViewerNode::ChildrenMap::const_iterator end() const;
64 
65 private:
66  std::shared_ptr<Ws::MergedPreferences> m_preferences;
67  std::shared_ptr<SubjectType> m_subject;
68  IViewerNode::ChildrenMap m_children;
69  bool m_enabled;
70 };
71 
72 template <class FakeSubjectType>
73 class SubjectViewerNodeWrapper : public IViewerNodeWithParent<FakeSubjectType>
74 {
75 public:
76  SubjectViewerNodeWrapper(std::shared_ptr<IViewerNode> node)
77  : m_node(node) {}
78 
79  virtual ~SubjectViewerNodeWrapper() {}
80 
81  virtual void Disable() { m_node->Disable(); }
82 
83  virtual void Enable() { m_node->Enable(); }
84 
85  virtual bool IsEnabled() const { return m_node->IsEnabled(); }
86 
87  virtual bool IsParentEnabled() const { return m_node->IsParentEnabled(); }
88 
89  virtual IViewerNode::ChildrenMap::const_iterator begin() const { return m_node->begin(); }
90 
91  virtual IViewerNode::ChildrenMap::const_iterator end() const { return m_node->end(); }
92 
93 protected:
95  virtual void ParentDisabled() {}
96 
98  virtual void ParentEnabled(std::shared_ptr<FakeSubjectType>) {}
99 
100 private:
101  std::shared_ptr<IViewerNode> m_node;
102 };
103 
104 
105 template <class SubjectType>
106 SubjectNode<SubjectType>::SubjectNode(std::shared_ptr<Ws::MergedPreferences> p,
107  std::shared_ptr<SubjectType> s,
108  IViewerNode::ChildrenMap&& c)
109  : m_preferences(p),
110  m_subject(s),
111  m_children(c),
112  m_enabled(m_preferences->Get<bool>("enabled_at_startup"))
113 {
114  if (m_enabled)
115  Enable();
116 }
117 
118 template <class SubjectType>
119 SubjectNode<SubjectType>::SubjectNode(std::shared_ptr<Ws::MergedPreferences> p,
120  std::shared_ptr<SubjectType> s)
121  : m_preferences(p),
122  m_subject(s),
123  m_enabled(m_preferences->Get<bool>("enabled_at_startup"))
124 {
125  if (m_enabled)
126  Enable();
127 }
128 
129 template <class SubjectType>
131 {
132  // Disable children.
133  for (auto& child: m_children) {
134  std::static_pointer_cast<IViewerNodeWithParent<SubjectType>>
135  (child.second)->ParentDisabled();
136  }
137  m_enabled = false;
138 }
139 
140 template <class SubjectType>
142 {
143  m_enabled = true;
144  // Enable children.
145  for (auto& child: m_children) {
146  std::static_pointer_cast<IViewerNodeWithParent<SubjectType>>
147  (child.second)->ParentEnabled(m_subject);
148  }
149 }
150 
151 template <class SubjectType>
153 {
154  return m_enabled;
155 }
156 
157 template <class SubjectType>
159 {
160  return true;
161 }
162 
163 template <class SubjectType>
164 IViewerNode::ChildrenMap::const_iterator SubjectNode<SubjectType>::begin() const
165 {
166  return m_children.begin();
167 }
168 
169 template <class SubjectType>
170 IViewerNode::ChildrenMap::const_iterator SubjectNode<SubjectType>::end() const
171 {
172  return m_children.end();
173 }
174 
175 } // namespace
176 } // namespace
177 
178 #endif // end_of_inclde_guard
virtual IViewerNode::ChildrenMap::const_iterator begin() const
Get iterator pointing to first child.
Definition: SubjectNode.h:89
virtual void Enable()
Enable node.
Definition: SubjectNode.h:141
virtual void ParentEnabled(std::shared_ptr< FakeSubjectType >)
Definition: SubjectNode.h:98
Interface for a node in a hierarchical tree of viewers.
Definition: IViewerNode.h:31
virtual IViewerNode::ChildrenMap::const_iterator end() const
Get iterator pointing to one position after last child.
Definition: SubjectNode.h:91
virtual bool IsEnabled() const
Test whether node is enabled.
Definition: SubjectNode.h:152
virtual void Enable()
Enable node.
Definition: SubjectNode.h:83
virtual IViewerNode::ChildrenMap::const_iterator begin() const
Get iterator pointing to first child.
Definition: SubjectNode.h:164
virtual bool IsParentEnabled() const
Test whether parent node is enabled.
Definition: SubjectNode.h:158
virtual void Disable()
Disable node.
Definition: SubjectNode.h:81
virtual void Disable()
Disable node.
Definition: SubjectNode.h:130
Interface for MergedPreferences.
virtual bool IsEnabled() const
Test whether node is enabled.
Definition: SubjectNode.h:85
virtual IViewerNode::ChildrenMap::const_iterator end() const
Get iterator pointing to one position after last child.
Definition: SubjectNode.h:170
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
virtual bool IsParentEnabled() const
Test whether parent node is enabled.
Definition: SubjectNode.h:87
A viewer node that does not represent a viewer at all.
Definition: SubjectNode.h:41
Interface for IViewerNode.
A viewer node with a parent.
Definition: IViewerNode.h:64