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_GUIEDITOR_OBSERVER_PATTERN_HPP_INCLUDED
8 #define CAFU_GUIEDITOR_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 "Templates/Array.hpp"
20 #include "Templates/Pointer.hpp"
21 
22 
23 namespace cf { namespace GuiSys { class WindowT; } }
24 namespace cf { namespace TypeSys { class VarBaseT; } }
25 
26 
27 namespace GuiEditor
28 {
29  class SubjectT;
30 
31  enum WindowModDetailE
32  {
33  WMD_GENERIC, ///< Generic change of windows (useful if the subject doesn't know what exactly has been changed).
34  WMD_HIERARCHY ///< The position of a window in the window hierarchy has been changed.
35  };
36 
37 
38  class ObserverT
39  {
40  public:
41 
42  /// This method is called whenever the window selection of a GUI subject changed.
43  /// @param Subject The GUI document in which the selection has been changed.
44  /// @param OldSelection Array of the previously selected windows.
45  /// @param NewSelection Array of the new selected windows.
46  virtual void NotifySubjectChanged_Selection(SubjectT* Subject, const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& OldSelection, const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& NewSelection) { }
47 
48  /// Notifies the observer that one or more windows have been created.
49  /// @param Subject The GUI document in which the windows have been created.
50  /// @param Windows List of created windows.
52 
53  /// Notifies the observer that one or more windows have been deleted.
54  /// @param Subject The GUI document in which the windows have been deleted.
55  /// @param Windows List of deleted windows.
57 
58  /// Notifies the observer that a general GUI property has been modified.
59  /// @param Subject The GUI document whose GUI property has been modified.
61 
62  /// @name Modification notification method and overloads.
63  /// These methods notify the observer that one or more GUI windows have been modified.
64  /// The first 3 parameters are common to all of these methods since they are the basic information needed to communicate
65  /// an object modification.
66  //@{
67 
68  /// @param Subject The GUI document in which the elements have been modified.
69  /// @param Windows List of modified windows.
70  /// @param Detail Information about what has been modified:
71  /// Can be WMD_GENERIC or WMD_HIERARCHY.
72  virtual void NotifySubjectChanged_Modified(SubjectT* Subject, const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& Windows, WindowModDetailE Detail) { }
73  //@}
74 
75  /// Notifies the observer that a variable has changed.
76  /// @param Subject The GUI document in which a variable has changed.
77  /// @param Var The variable whose value has changed.
78  virtual void Notify_Changed(SubjectT* Subject, const cf::TypeSys::VarBaseT& Var) { }
79 
80  /// This method is called whenever a subject is about the be destroyed (and become unavailable).
81  /// @param dyingSubject The subject that is being destroyed.
82  virtual void NotifySubjectDies(SubjectT* dyingSubject)=0;
83 
84  /// The virtual destructor.
85  virtual ~ObserverT();
86 
87 
88  protected:
89 
90  /// The constructor. It is protected so that only derived classes can create instances of this class.
91  ObserverT();
92  };
93 
94 
95  class SubjectT
96  {
97  public:
98 
99  /// Registers the observer Obs for notification on updates of this class.
100  /// \param Obs The observer that is to be registered.
101  void RegisterObserver(ObserverT* Obs);
102 
103  /// Unregisters the observer Obs from further notification on updates of this class.
104  /// \param Obs The observer that is to be unregistered.
105  void UnregisterObserver(ObserverT* Obs);
106 
107  void UpdateAllObservers_SelectionChanged(const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& OldSelection, const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& NewSelection);
108  void UpdateAllObservers_Created(const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& Windows);
109  void UpdateAllObservers_Created(IntrusivePtrT<cf::GuiSys::WindowT> Window);
110  void UpdateAllObservers_Deleted(const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& Windows);
111  void UpdateAllObservers_Deleted(IntrusivePtrT<cf::GuiSys::WindowT> Window);
112  void UpdateAllObservers_GuiPropertyModified();
113  void UpdateAllObservers_Modified(const ArrayT< IntrusivePtrT<cf::GuiSys::WindowT> >& Windows, WindowModDetailE Detail);
114  void UpdateAllObservers_Modified(IntrusivePtrT<cf::GuiSys::WindowT> Window, WindowModDetailE Detail);
115  void UpdateAllObservers_Modified(const cf::TypeSys::VarBaseT& Var);
116 
117  /// The virtual destructor.
118  virtual ~SubjectT();
119 
120 
121  protected:
122 
123  /// The constructor. It is protected so that only derived classes can create instances of this class.
124  SubjectT();
125 
126 
127  private:
128 
129  /// The list of the observers that have registered for notification on updates of this class.
130  ArrayT<ObserverT*> m_Observers;
131  };
132 }
133 
134 #endif
SubjectT()
The constructor. It is protected so that only derived classes can create instances of this class...
Definition: ObserverPattern.cpp:24
virtual ~SubjectT()
The virtual destructor.
Definition: ObserverPattern.cpp:117
ObserverT()
The constructor. It is protected so that only derived classes can create instances of this class...
Definition: ObserverPattern.cpp:14
virtual void NotifySubjectChanged_Selection(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GuiSys::WindowT > > &OldSelection, const ArrayT< IntrusivePtrT< cf::GuiSys::WindowT > > &NewSelection)
This method is called whenever the window selection of a GUI subject changed.
Definition: ObserverPattern.hpp:46
Definition: ObserverPattern.hpp:38
void UnregisterObserver(ObserverT *Obs)
Unregisters the observer Obs from further notification on updates of this class.
Definition: ObserverPattern.cpp:40
virtual void NotifySubjectChanged_Modified(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GuiSys::WindowT > > &Windows, WindowModDetailE Detail)
Definition: ObserverPattern.hpp:72
virtual void NotifySubjectChanged_Created(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GuiSys::WindowT > > &Windows)
Notifies the observer that one or more windows have been created.
Definition: ObserverPattern.hpp:51
virtual void NotifySubjectChanged_Deleted(SubjectT *Subject, const ArrayT< IntrusivePtrT< cf::GuiSys::WindowT > > &Windows)
Notifies the observer that one or more windows have been deleted.
Definition: ObserverPattern.hpp:56
virtual void NotifySubjectDies(SubjectT *dyingSubject)=0
This method is called whenever a subject is about the be destroyed (and become unavailable).
virtual void Notify_Changed(SubjectT *Subject, const cf::TypeSys::VarBaseT &Var)
Notifies the observer that a variable has changed.
Definition: ObserverPattern.hpp:78
Definition: ObserverPattern.hpp:158
This is the common base class for the VarT classes.
Definition: Variables.hpp:113
virtual ~ObserverT()
The virtual destructor.
Definition: ObserverPattern.cpp:19
void RegisterObserver(ObserverT *Obs)
Registers the observer Obs for notification on updates of this class.
Definition: ObserverPattern.cpp:29
virtual void NotifySubjectChanged_GuiPropertyModified(SubjectT *Subject)
Notifies the observer that a general GUI property has been modified.
Definition: ObserverPattern.hpp:60
Definition: ObserverPattern.hpp:95
Definition: Renderer.hpp:16