00001 /*
00002 File: Action.h
00003
00004 Function: Defines an 'Action' which can be applied to all members of
00005 a container.
00006
00007 Author(s): Andrew Willmott
00008
00009 Copyright: (c) 1995-2000, Andrew Willmott
00010 */
00011
00012 #ifndef __Action__
00013 #define __Action__
00014
00015 #include "cl/Basics.h"
00016
00017 #define TAction Action<T>
00018
00019
00020 // --- Abstract Action Class ---------------------------------------------------
00021
00022
00023 template <class T> class Action
00024 {
00025 public:
00026
00027 virtual Void Start() {};
00028 virtual Void Process(T &t) = 0; // Abstract action method.
00029 virtual Void Stop() {};
00030 };
00031
00032
00033 // --- A demonstration Action --------------------------------------------
00034
00035
00036 template <class T> class StatsFinder : public Action<T>
00037 {
00038 public:
00039
00040 Void Start();
00041 Void Process(T &t);
00042 Void Stop();
00043
00044 T mean;
00045 T variance;
00046
00047 protected:
00048
00049 Int n;
00050 T sumX;
00051 T sumXSqr;
00052 };
00053
00054
00055 template <class T> Void StatsFinder<T>::Start()
00056 {
00057 sumX = sumXSqr = 0;
00058 n = 0;
00059 }
00060
00061 template <class T> Void StatsFinder<T>::Process(T &t)
00062 {
00063 sumX += t;
00064 sumXSqr += sqr(t);
00065 n++;
00066 }
00067
00068 template <class T> Void StatsFinder<T>::Stop()
00069 {
00070 mean = sumX / n;
00071 variance = sumXSqr / n - sqr(mean);
00072 }
00073
00074 #endif