<aspect xmlns:src="http://www.sdml.info/srcML/src" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cpp="http://www.sdml.info/srcML/cpp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://control.ee.ethz.ch/XWeaver/AspectX" xsi:schemaLocation="http://control.ee.ethz.ch/XWeaver/AspectX ../../../../src/xsd/aspectX.xsd" name="Observer">
        
<description>
            Sample aspect program that implements the 
            
<a href="http://www.cs.clemson.edu/~malloy/courses/patterns/observer.html">observer design pattern</a>. This 
            aspect demonstrate the 
<a href="http://home.earthlink.net/~huston2/dp/ObserverDemosCpp">simplified</a>
            observer design pattern implementation where the subject has a hard-wired number of objects.
            The subject "pushes" updates to the observers.
            
<p />
            There are two observes in this example -- classes 
<code>DivObs</code> and <code>ModObs</code>
            Both classes implements 
<code>update()</code> method that is to be called by subject class
            
<code>Subject</code> whenever its internal state changes. The internal state of the 
            
<code>Subject</code> class can only be changed by calling its <code>setVal()</code>
            setter method.
            
<p />
        This aspect is implemented through five advices. The first advice inserts the code that initialize 
        registered observers. The second advice adds the observer declarations to the subject.
        The third advise inserts 
<code>update()</code> calls to the setter method of the subject. The
        fourth and fifth advices add the necessary header files to the respective files.
        
<author>A. Pasetti, O. Rohlik</author>
        
</description>
        
         
<pointcut name="targetMethodImplementation" type="src:function" constraint="src:name='Subject::setVal'">
                
<description>
      Points to the implementations of the method 
<code>Subject::setVal</code>
                        This is the only method in the base code that changes the state of the 
                        observed object.
    
</description>
        
</pointcut>
        
        
<pointcut name="targetImplementationUnit" type="src:unit">
                
<description>Points to all units that contain implementations of methods that must be
                        modified. This pointcut identifies all the source files that contain an
                        implementation of the target method.                
                
</description>
                
<restriction type="contain">
                        
<pointcutRef type="src:function" ref="targetMethodImplementation" />
                
</restriction>
        
</pointcut>

        
<pointcut name="targetClass" type="src:class" constraint="src:name='Subject'">
                
<description>
                        Points to the target class to which declarations of observers should be added.
                        The class is identified by its name: 
<code>Subject</code>.
                
</description>
        
</pointcut>
        
         
<pointcut name="targetClassDeclaration" type="src:unit">
                
<description>
            Points to the units that contain the declaration of the target class.
        
</description>
                
<restriction type="contain">
                        
<pointcutRef type="src:class" ref="targetClass" />
                
</restriction>
        
</pointcut>

    
<pointcut name="constructorImplementation" type="src:constructor" constraint="src:name='Subject::Subject'">
                
<description>
            Points to the target class constructors. 
        
</description>
    
</pointcut>

        
<advice type="add" name="addInitializers">
                
<description>
                    Add the initializers that sets up observers. In this example 
                    two observers are considered: 
<code>_div</code> of type 
                    
<code>DivObs</code> that takes the observed value and 
                    divides it by 4 and
                    
<code>_mod</code> of type 
                    
<code>ModObs</code> that takes the observed value and 
                    divides it by 3. Both observers output the reminder computed 
                    using modulo operation. 
                
</description>
                
<pointcutRef ref="constructorImplementation" type="src:constructor" />
                
<codeModifier type="initializer">
                    
<text>_div(4)</text>
                
</codeModifier>
                
<codeModifier type="initializer">
                    
<text>_mod(3)</text>
                    
<text />
                
</codeModifier>
        
</advice> 
                
        
<advice name="addObserversDeclaraion" type="add">
        
<description>
            Add observers to the class declaration. This is normally done using 
<code>attach()</code>
            method of subject, however using aspect it is possible to insert the full list of observers
            directly to the subject (if the observers do not change during the program execution).
        
</description>
            
<pointcutRef ref="targetClass" type="src:class" />
            
<codeModifier type="declaration">
                
<accessModifier type="private" />
                
<text>DivObs  _div; // added by aspect </text>
            
<text>ModObs  _mod; // added by aspect </text>
            
</codeModifier>
        
</advice>
        
        
<advice name="addObserverNotification" type="end">
        
<description>
            Add observer notification instructions to the method that changes the state of the 
            subject.
            This is normally done using 
<code>notify()</code>
            method of subject, however using aspect it is possible to insert the full list 
            
<code>update()</code> calls directly to the base code
            (if the observers do not change during the program execution).
        
</description>
        
<pointcutRef ref="targetMethodImplementation" type="src:function" />
            
<codeModifier type="codeFragment">
                
<text>_div.update( _val ); // added by aspect </text>
            
<text>_mod.update( _val ); // added by aspect </text>
            
</codeModifier>
        
</advice>
        
        
<advice type="add" name="addIncludesToDefinition">
            
<description>
                Add the 
<code>#include</code> preprocessor instructions.
            Both classes defined in included header files are needed as they are used 
            in the class declaration to declare member variables 
            (observers) 
<code>_div</code> and <code>_mod</code>.
            
</description>
            
<pointcutRef ref="targetClassDeclaration" type="src:unit" />
                
<codeModifier type="include">
                    
<text>#include "DivObs.h" // added by aspect </text>
            
<text>#include "ModObs.h" // added by aspect </text>
                
</codeModifier>
        
</advice>

        
<advice type="add" name="addIncludesToDeclaration">
            
<description>
                Add the 
<code>#include</code> preprocessor instructions.
            Both classes defined in included header files are needed as they are used 
            in the 
<code>setVal()</code> method to call <code>update()</code> method 
            of observers 
<code>_div</code> and <code>_mod</code>.
        
</description>
                
<pointcutRef ref="targetImplementationUnit" type="src:unit" />
                
<codeModifier type="include">
      
<text>#include "DivObs.h" // added by aspect </text>
      
<text>#include "ModObs.h" // added by aspect </text>
                
</codeModifier>
        
</advice>
        
</aspect>






































v