Cafu Engine
ObserverPattern.hpp
Go to the documentation of this file.
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MODELEDITOR_OBSERVER_PATTERN_HPP_INCLUDED
8 #define CAFU_MODELEDITOR_OBSERVER_PATTERN_HPP_INCLUDED
9 
10 /// \file
11 /// This file provides the classes for the Observer pattern as described in the book by the GoF.
12 /// Note that implementations of ObserverT normally maintain a pointer to the subject(s) that they observe,
13 /// e.g. in order to be able to redraw themselves also outside of and independent from the NotifySubjectChanged() method.
14 /// This however in turn creates problems when the life of the observer begins before or ends after the life of the observed subject(s).
15 /// In fact, it seems that the observers have to maintain lists of valid, observed subjects as the subjects maintain lists of observers.
16 /// Fortunately, these possibly tough problems can (and apparently must) be addressed by the concrete implementation classes of
17 /// observers and subjects, not by the base classes that the Observer pattern describes and provided herein.
18 
19 #include "ElementTypes.hpp"
20 #include "Templates/Array.hpp"
21 
22 
23 namespace ModelEditor
24 {
25  class SubjectT;
26 
27 
28  class ObserverT
29  {
30  public:
31 
32  /// This method is called whenever the selection of a model changed.
33  /// @param Subject The model document in which the selection has changed.
34  /// @param Type The type of the elements in a model whose selection changed (joints, meshes or anims).
35  /// @param OldSel Array of the previously selected elements.
36  /// @param NewSel Array of the new selected elements.
37  virtual void Notify_SelectionChanged(SubjectT* Subject, ModelElementTypeT Type, const ArrayT<unsigned int>& OldSel, const ArrayT<unsigned int>& NewSel) { }
38 
39  /// This method is called when new elements have been created and were added to the model.
40  /// @param Subject The model document to which elements were added.
41  /// @param Type The type of the added elements (joints, meshes or anims).
42  /// @param Indices The array indices at which the new elements were inserted.
43  virtual void Notify_Created(SubjectT* Subject, ModelElementTypeT Type, const ArrayT<unsigned int>& Indices) { }
44 
45  /// This method is called when new elements were deleted from the model.
46  /// @param Subject The model document from which elements were deleted.
47  /// @param Type The type of the deleted elements (joints, meshes or anims).
48  /// @param Indices The array indices at which the elements were deleted.
49  virtual void Notify_Deleted(SubjectT* Subject, ModelElementTypeT Type, const ArrayT<unsigned int>& Indices) { }
50 
51  /// Notifies the observer that a joint has changed.
52  /// @param Subject The model document with the model in which the joint has changed.
53  /// @param JointNr The number of the joint that has changed.
54  virtual void Notify_JointChanged(SubjectT* Subject, unsigned int JointNr) { }
55 
56  /// Notifies the observer that a mesh has changed.
57  /// @param Subject The model document with the model in which the mesh has changed.
58  /// @param MeshNr The number of the mesh that has changed.
59  virtual void Notify_MeshChanged(SubjectT* Subject, unsigned int MeshNr) { }
60 
61  /// Notifies the observer that a skin has changed.
62  /// @param Subject The model document with the model in which the skin has changed.
63  /// @param SkinNr The number of the skin that has changed.
64  virtual void Notify_SkinChanged(SubjectT* Subject, unsigned int SkinNr) { }
65 
66  /// Notifies the observer that a GUI fixture has changed.
67  /// @param Subject The model document with the model in which the GUI fixture has changed.
68  /// @param GuiFixtureNr The number of the GUI fixture that has changed.
69  virtual void Notify_GuiFixtureChanged(SubjectT* Subject, unsigned int GuiFixtureNr) { }
70 
71  /// Notifies the observer that an animation sequence has changed.
72  /// @param Subject The model document with the model in which the anim has changed.
73  /// @param AnimNr The number of the anim sequence that has changed.
74  virtual void Notify_AnimChanged(SubjectT* Subject, unsigned int AnimNr) { }
75 
76  /// Notifies the observer that an animation channel has changed.
77  /// @param Subject The model document with the model in which the channel has changed.
78  /// @param ChannelNr The number of the anim channel that has changed.
79  virtual void Notify_ChannelChanged(SubjectT* Subject, unsigned int ChannelNr) { }
80 
81  /// Notifies the observer that the list of submodels has changed.
82  /// @param Subject The model document with the model in which the list of submodels has changed.
83  virtual void Notify_SubmodelsChanged(SubjectT* Subject) { }
84 
85  /// Notifies the observer that the animation state has changed.
86  /// @param Subject The model document whose AnimStateT has changed.
87  virtual void Notify_AnimStateChanged(SubjectT* Subject) { }
88 
89  /// This method is called whenever a subject is about the be destroyed (and become unavailable).
90  /// \param dyingSubject The subject that is being destroyed.
91  virtual void Notify_SubjectDies(SubjectT* dyingSubject)=0;
92 
93  /// The virtual destructor.
94  virtual ~ObserverT();
95 
96 
97  protected:
98 
99  /// The constructor. It is protected so that only derived classes can create instances of this class.
100  ObserverT();
101  };
102 
103 
104  class SubjectT
105  {
106  public:
107 
108  /// Registers the observer Obs for notification on updates of this class.
109  /// \param Obs The observer that is to be registered.
110  void RegisterObserver(ObserverT* Obs);
111 
112  /// Unregisters the observer Obs from further notification on updates of this class.
113  /// \param Obs The observer that is to be unregistered.
114  void UnregisterObserver(ObserverT* Obs);
115 
116  void UpdateAllObservers_SelectionChanged(ModelElementTypeT Type, const ArrayT<unsigned int>& OldSel, const ArrayT<unsigned int>& NewSel);
117  void UpdateAllObservers_Created(ModelElementTypeT Type, const ArrayT<unsigned int>& Indices);
118  void UpdateAllObservers_Deleted(ModelElementTypeT Type, const ArrayT<unsigned int>& Indices);
119  void UpdateAllObservers_JointChanged(unsigned int JointNr);
120  void UpdateAllObservers_MeshChanged(unsigned int MeshNr);
121  void UpdateAllObservers_SkinChanged(unsigned int SkinNr);
122  void UpdateAllObservers_GuiFixtureChanged(unsigned int GuiFixtureNr);
123  void UpdateAllObservers_AnimChanged(unsigned int AnimNr);
124  void UpdateAllObservers_ChannelChanged(unsigned int ChannelNr);
125  void UpdateAllObservers_SubmodelsChanged();
126  void UpdateAllObservers_AnimStateChanged();
127 
128  /// The virtual destructor.
129  virtual ~SubjectT();
130 
131 
132  protected:
133 
134  /// The constructor. It is protected so that only derived classes can create instances of this class.
135  SubjectT();
136 
137 
138  private:
139 
140  /// The list of the observers that have registered for notification on updates of this class.
141  ArrayT<ObserverT*> m_Observers;
142  };
143 }
144 
145 #endif
void UnregisterObserver(ObserverT *Obs)
Unregisters the observer Obs from further notification on updates of this class.
Definition: ObserverPattern.cpp:39
virtual void Notify_Created(SubjectT *Subject, ModelElementTypeT Type, const ArrayT< unsigned int > &Indices)
This method is called when new elements have been created and were added to the model.
Definition: ObserverPattern.hpp:43
virtual void Notify_MeshChanged(SubjectT *Subject, unsigned int MeshNr)
Notifies the observer that a mesh has changed.
Definition: ObserverPattern.hpp:59
ObserverT()
The constructor. It is protected so that only derived classes can create instances of this class...
Definition: ObserverPattern.cpp:13
virtual void Notify_Deleted(SubjectT *Subject, ModelElementTypeT Type, const ArrayT< unsigned int > &Indices)
This method is called when new elements were deleted from the model.
Definition: ObserverPattern.hpp:49
virtual void Notify_AnimStateChanged(SubjectT *Subject)
Notifies the observer that the animation state has changed.
Definition: ObserverPattern.hpp:87
virtual ~ObserverT()
The virtual destructor.
Definition: ObserverPattern.cpp:18
void RegisterObserver(ObserverT *Obs)
Registers the observer Obs for notification on updates of this class.
Definition: ObserverPattern.cpp:28
virtual void Notify_AnimChanged(SubjectT *Subject, unsigned int AnimNr)
Notifies the observer that an animation sequence has changed.
Definition: ObserverPattern.hpp:74
Definition: ObserverPattern.hpp:104
virtual void Notify_GuiFixtureChanged(SubjectT *Subject, unsigned int GuiFixtureNr)
Notifies the observer that a GUI fixture has changed.
Definition: ObserverPattern.hpp:69
virtual void Notify_SubjectDies(SubjectT *dyingSubject)=0
This method is called whenever a subject is about the be destroyed (and become unavailable).
Definition: ObserverPattern.hpp:158
SubjectT()
The constructor. It is protected so that only derived classes can create instances of this class...
Definition: ObserverPattern.cpp:23
virtual ~SubjectT()
The virtual destructor.
Definition: ObserverPattern.cpp:124
virtual void Notify_JointChanged(SubjectT *Subject, unsigned int JointNr)
Notifies the observer that a joint has changed.
Definition: ObserverPattern.hpp:54
virtual void Notify_ChannelChanged(SubjectT *Subject, unsigned int ChannelNr)
Notifies the observer that an animation channel has changed.
Definition: ObserverPattern.hpp:79
virtual void Notify_SubmodelsChanged(SubjectT *Subject)
Notifies the observer that the list of submodels has changed.
Definition: ObserverPattern.hpp:83
virtual void Notify_SkinChanged(SubjectT *Subject, unsigned int SkinNr)
Notifies the observer that a skin has changed.
Definition: ObserverPattern.hpp:64
Definition: ObserverPattern.hpp:28
virtual void Notify_SelectionChanged(SubjectT *Subject, ModelElementTypeT Type, const ArrayT< unsigned int > &OldSel, const ArrayT< unsigned int > &NewSel)
This method is called whenever the selection of a model changed.
Definition: ObserverPattern.hpp:37