Main Page | Class List | File List | Class Members | File Members

LambdaVector.cpp

Go to the documentation of this file.
00001 
00005 //
00006 // Date:      13 September 2005
00007 //
00008 // Copyright (c) Sergey Satskiy 2005
00009 //               <sergesatsky@yahoo.com>
00010 //
00011 // Permission to copy, use, modify, sell and distribute this software 
00012 // is granted provided this copyright notice appears in all copies. 
00013 // This software is provided "as is" without express or implied
00014 // warranty, and with no claim as to its suitability for any purpose.
00015 //
00016 
00017 #include <iostream>
00018 #include <vector>
00019 using namespace std;
00020 
00021 #include "boost/lambda/lambda.hpp"
00022 #include "boost/function.hpp"
00023 using namespace boost;
00024 using namespace boost::lambda;
00025 
00026 
00030 void f( const double &  Value )
00031 {
00032     cout << "f called. Value: " << Value << endl;
00033 }
00034 
00038 void g( const double &  Value )
00039 {
00040     cout << "g called. Value: " << Value << endl;
00041 }
00042 
00043 
00044 
00045 
00049 struct CallbackStorage
00050 {
00052     CallbackStorage() {}
00053 
00055     vector< function< void ( const double & ) > >       v;
00056 
00057 
00061     void insert( function< void ( const double & ) >  func )
00062     {
00063         v.push_back( func );
00064     }
00065 
00066     
00070     void operator() ( const double &  val )
00071     {
00072         vector< function< void ( const double & ) > >::iterator k;
00073         for ( k = v.begin(); k != v.end(); ++k )
00074         {
00075             (*k)( val );
00076         }
00077     }
00078 
00079     
00085     void insert( double &  Ref )
00086     {
00087             // Generates a function in place!
00088         function< void ( const double & ) >  g = ( var( Ref ) = _1 );
00089         v.push_back( g );
00090     }
00091 
00092 };
00093 
00094 
00095 
00096 
00097 
00098 int  main( void )
00099 {
00100     CallbackStorage     Storage;
00101     double              Value1( 1.1 );
00102     double              Value2( 2.2 );
00103     double              Value3( 3.3 );
00104 
00105 
00106         // Register some callbacks
00107     Storage.insert( f );            // Regular function
00108     Storage.insert( Value1 );       // Register variable - assignment function is generated
00109     Storage.insert( Value2 );       // Register variable - assignment function is generated
00110     Storage.insert( Value3 );       // Register variable - assignment function is generated
00111     Storage.insert( g );            // Regular function
00112 
00113     cout << "Before a call" << endl
00114          << "    Value1 = "     << Value1 << endl
00115          << "    Value2 = "     << Value2 << endl
00116          << "    Value3 = "     << Value3 << endl;
00117 
00118     Storage( 777.88 );
00119 
00120     cout << "After a call"  << endl
00121          << "    Value1 = "     << Value1 << endl
00122          << "    Value2 = "     << Value2 << endl
00123          << "    Value3 = "     << Value3 << endl;
00124     
00125     return 0;
00126 }
00127 

Generated on Mon Sep 26 00:09:23 2005 for Games around boost::lambda by  doxygen 1.4.4