NTRT Simulator  Version: Master
 All Classes Namespaces 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 
30 // This application
31 #include "tgObserver.h"
32 // The C++ standard library
33 #include <vector>
34 
43 template <typename T>
44 class tgSubject
45 {
46 public:
47 
49  tgSubject() { }
50 
52  virtual ~tgSubject() { }
53 
59  void attach(tgObserver<T>* pObserver);
60 
67  void notifyStep(double dt);
68 
73  void notifySetup();
74 
79  void notifyTeardown();
80 
81 private:
82 
87  std::vector<tgObserver<T> * > m_observers;
88 };
89 
90 template <typename Subject>
92 {
93  if (pObserver) { m_observers.push_back(pObserver);
94  pObserver->onAttach(static_cast<Subject&>(*this));}
95 }
96 
97 template <typename Subject>
99 {
100  if (dt > 0)
101  {
102  const std::size_t n = m_observers.size();
103  for (std::size_t i = 0; i < n; ++i)
104  {
105  tgObserver<Subject>* const pObserver = m_observers[i];
106  if (pObserver) { pObserver->onStep(static_cast<Subject&>(*this), dt); }
107  }
108  }
109 }
110 
111 template <typename Subject>
113 {
114  const std::size_t n = m_observers.size();
115  for (std::size_t i = 0; i < n; ++i)
116  {
117  tgObserver<Subject>* const pObserver = m_observers[i];
118  if (pObserver) { pObserver->onSetup(static_cast<Subject&>(*this)); }
119  }
120 }
121 
122 template <typename Subject>
124 {
125  const std::size_t n = m_observers.size();
126  for (std::size_t i = 0; i < n; ++i)
127  {
128  tgObserver<Subject>* const pObserver = m_observers[i];
129  if (pObserver) { pObserver->onTeardown(static_cast<Subject&>(*this)); }
130  }
131 }
132 #endif // TG_SUBJECT_H
133 
tgSubject()
Definition: tgSubject.h:49
Definition of tgObserver class.
virtual ~tgSubject()
Definition: tgSubject.h:52
virtual void onStep(Subject &subject, double dt)=0
void notifySetup()
Definition: tgSubject.h:112
virtual void onTeardown(Subject &subject)
Definition: tgObserver.h:67
void notifyTeardown()
Definition: tgSubject.h:123
virtual void onSetup(Subject &subject)
Definition: tgObserver.h:61
virtual void onAttach(Subject &subject)
Definition: tgObserver.h:55
void attach(tgObserver< T > *pObserver)
Definition: tgSubject.h:91
void notifyStep(double dt)
Definition: tgSubject.h:98