| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /**
 | 
|---|
| 9 |  * \file observers_observables.dox
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  * Created on: Oct 12, 2011
 | 
|---|
| 12 |  *    Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | /** \page observers Observers and Observables
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * The Observer/Observerable mechanism is crucial to let different and
 | 
|---|
| 18 |  * independent components know about changes among one another. E.g. when an
 | 
|---|
| 19 |  * the element of an atom changes, the GUI (\ref graphical) needs to know
 | 
|---|
| 20 |  * about this change to plot the atom with a different color or shape.
 | 
|---|
| 21 |  *
 | 
|---|
| 22 |  * The Observer/Observerable mechanism is basically just a call-back: A
 | 
|---|
| 23 |  * class announces itself as Observable with having a specific interface
 | 
|---|
| 24 |  * of functions. Observer may signOn() to this class and have a specific
 | 
|---|
| 25 |  * function (update()) be called whenever a change occurs in the Observable
 | 
|---|
| 26 |  * class.
 | 
|---|
| 27 |  *
 | 
|---|
| 28 |  * Note that additionally an Observable may have various \a channels and
 | 
|---|
| 29 |  * Observers may signOn() to just such a channel to get a very specific
 | 
|---|
| 30 |  * update. E.g. a LinkedCell class needs to know when the position of an
 | 
|---|
| 31 |  * atom changes but it does not need to know about changes of element or
 | 
|---|
| 32 |  * velocity, ... These updates are called \a Notifications.
 | 
|---|
| 33 |  *
 | 
|---|
| 34 |  * As an example:
 | 
|---|
| 35 |  * \code
 | 
|---|
| 36 |  * World::getInstance().signOn(this, World::getInstance().getChannel(World::AtomInserted));
 | 
|---|
| 37 |  * \endcode
 | 
|---|
| 38 |  * Here, in the constructor of GLWorldView the class signs on to atoms
 | 
|---|
| 39 |  * being inserted into the World such that it can add these onto the display
 | 
|---|
| 40 |  * as well. Notifications are received via recieveNotifications(), e.g.
 | 
|---|
| 41 |  * which you have to implement for an Observer class
 | 
|---|
| 42 |  * \code
 | 
|---|
| 43 |  *   switch (notification->getChannelNo()) {
 | 
|---|
| 44 |  *   case World::AtomInserted:
 | 
|---|
| 45 |  *   {
 | 
|---|
| 46 |  *     const atom *_atom = World::getInstance().lastChanged<atom>();
 | 
|---|
| 47 |  *     emit atomInserted(_atom);
 | 
|---|
| 48 |  *     break;
 | 
|---|
| 49 |  *   }
 | 
|---|
| 50 |  * \endcode
 | 
|---|
| 51 |  * Here, we just switch over all events that we process and care about (note
 | 
|---|
| 52 |  * that we only get called for those for which we have subscribed) and proceed.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  * \note There is a complete logging mechanism available for this. However,
 | 
|---|
| 55 |  * it has to be compiled via a specific switch (\ref install). So, when
 | 
|---|
| 56 |  * you need to know why your function isn't notified, that's the way to
 | 
|---|
| 57 |  * find out.
 | 
|---|
| 58 |  *
 | 
|---|
| 59 |  * Be wary of where the update occurs: while the World tells you about new
 | 
|---|
| 60 |  * or destroyed atoms, only the atom itself tells you when its position has
 | 
|---|
| 61 |  * changed!
 | 
|---|
| 62 |  *
 | 
|---|
| 63 |  * \section observers-world Observers and the World
 | 
|---|
| 64 |  *
 | 
|---|
| 65 |  * The World, containing the arrays of all atoms and molecules, is a bit
 | 
|---|
| 66 |  * special with these observers. E.g. when you request a non-const array
 | 
|---|
| 67 |  * of atoms you receive an ObservedIterator. That is one where automatically
 | 
|---|
| 68 |  * it is assumed that you changed something and hence update() is called
 | 
|---|
| 69 |  * after you stepped over one of its elements.
 | 
|---|
| 70 |  *
 | 
|---|
| 71 |  * Thus, use const-iterators and arrays whenever possible, first to avoid
 | 
|---|
| 72 |  * this overhead, but second to avoid making pain-in-the-ass, hard-to-find
 | 
|---|
| 73 |  * mistakes.
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  * Also, the world stores specific information about what changed in the
 | 
|---|
| 76 |  * template function World::lastChanged() where it might contain reference
 | 
|---|
| 77 |  * to an atom that is about to be destroyed or just added.
 | 
|---|
| 78 |  *
 | 
|---|
| 79 |  *
 | 
|---|
| 80 |  * \date 2011-10-31
 | 
|---|
| 81 |  *
 | 
|---|
| 82 |  */
 | 
|---|