NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgSubject.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
19 #ifndef TG_SUBJECT_H
20 #define TG_SUBJECT_H
21 
29 // This application
30 #include "tgObserver.h"
31 // The C++ standard library
32 #include <vector>
33 
42 template <typename T>
43 class tgSubject
44 {
45 public:
46 
48  tgSubject() { }
49 
51  virtual ~tgSubject() { }
52 
58  void attach(tgObserver<T>* pObserver);
59 
66  void notifyStep(double dt);
67 
72  void notifySetup();
73 
78  void notifyTeardown();
79 
80 private:
81 
86  std::vector<tgObserver<T> * > m_observers;
87 };
88 
89 template <typename Subject>
91 {
92  if (pObserver) { m_observers.push_back(pObserver);
93  pObserver->onAttach(static_cast<Subject&>(*this));}
94 }
95 
96 template <typename Subject>
98 {
99  if (dt > 0)
100  {
101  const std::size_t n = m_observers.size();
102  for (std::size_t i = 0; i < n; ++i)
103  {
104  tgObserver<Subject>* const pObserver = m_observers[i];
105  if (pObserver) { pObserver->onStep(static_cast<Subject&>(*this), dt); }
106  }
107  }
108 }
109 
110 template <typename Subject>
112 {
113  const std::size_t n = m_observers.size();
114  for (std::size_t i = 0; i < n; ++i)
115  {
116  tgObserver<Subject>* const pObserver = m_observers[i];
117  if (pObserver) { pObserver->onSetup(static_cast<Subject&>(*this)); }
118  }
119 }
120 
121 template <typename Subject>
123 {
124  const std::size_t n = m_observers.size();
125  for (std::size_t i = 0; i < n; ++i)
126  {
127  tgObserver<Subject>* const pObserver = m_observers[i];
128  if (pObserver) { pObserver->onTeardown(static_cast<Subject&>(*this)); }
129  }
130 }
131 #endif // TG_SUBJECT_H
132 
tgSubject()
Definition: tgSubject.h:48
Definition of tgObserver class.
virtual ~tgSubject()
Definition: tgSubject.h:51
virtual void onStep(Subject &subject, double dt)=0
void notifySetup()
Definition: tgSubject.h:111
virtual void onTeardown(Subject &subject)
Definition: tgObserver.h:66
void notifyTeardown()
Definition: tgSubject.h:122
virtual void onSetup(Subject &subject)
Definition: tgObserver.h:60
virtual void onAttach(Subject &subject)
Definition: tgObserver.h:54
void attach(tgObserver< T > *pObserver)
Definition: tgSubject.h:90
void notifyStep(double dt)
Definition: tgSubject.h:97