Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <iostream> 00003 #include <vector> 00004 using namespace std; 00005 00006 #include <fnobj.h> 00007 #include <fnobjT.h> 00008 #include <fnobjTfn.h> 00009 #include <print.h> 00010 00011 #include <fnobjtest.h> 00012 00013 00014 00015 class hat : public fnobj0<void> 00016 { 00017 public: 00018 void operator()() { cout << "hat" << endl; } 00019 }; 00020 00021 class hat2 : public fnobj0const<void> 00022 { 00023 public: 00024 void operator()() const { cout << "hat2 has const" << endl; } 00025 }; 00026 00027 class hat3 00028 { 00029 public: 00030 00031 void sumfunc() 00032 { cout << "hat3 sumfunc()" << endl; } 00033 00034 void anotherone() const 00035 { cout << "hat3 anotherone() const" << endl; } 00036 }; 00037 00038 00039 00040 void fnobjtest01() 00041 { 00042 hat h1; 00043 00044 cout << "Direct call to h1()" << endl; 00045 h1(); 00046 00047 cout << "call through functional pointer" << endl; 00048 00049 fnobj0T<hat&,void> h2(h1); 00050 fnobj0<void> *ptr; 00051 ptr = & h2; 00052 (*ptr)(); 00053 00054 cout << "member function hat3::sumfunc made into a functional object" << endl; 00055 00056 hat3 h3; 00057 //h3.sumfunc(); 00058 fnobj0Tfn<hat3,void> ph3(h3,&hat3::sumfunc); 00059 00060 ptr = &ph3; 00061 00062 (*ptr)(); 00063 00064 cout << "Testing the const functional ojbect" << endl; 00065 fnobj0const<void> *ptr2 = new hat2(); 00066 (*ptr2)(); 00067 00068 delete ptr2; 00069 00070 cout << "Testing const with a function func. object." << endl; 00071 00072 fnobj0constTfn<hat3,void> qh3(h3,&hat3::anotherone); 00073 ptr2 = &qh3; 00074 (*ptr2)(); 00075 } 00076 00077 00078 class datamodel 00079 { 00080 int age; 00081 public: 00082 00083 int getAge() const 00084 { return age; } 00085 00086 void setAge2(int const age_) 00087 { age=age_; } 00088 00089 }; 00090 00091 class person 00092 { 00093 int age; 00094 public: 00095 void setAge(int const age_) 00096 { age=age_; } 00097 int getthepersonsAge() const 00098 { return age; } 00099 }; 00100 00101 class people 00102 { 00103 public: 00104 00105 fnobj1<void,int const> *setage; 00106 fnobj0const<int> *getage; 00107 00108 people 00109 ( 00110 fnobj1<void,int const> *setage_, 00111 fnobj0const<int> *getage_ 00112 ) 00113 : setage(setage_), getage(getage_) {} 00114 00115 ~people() 00116 { delete setage; delete getage; } 00117 00118 void set(const int k) 00119 { assert(setage!=0); (*setage)(k); } 00120 00121 int get() 00122 { assert(getage!=0); return (*getage)(); } 00123 00124 }; 00125 00126 void fnobjtest02() 00127 { 00128 cout << "Demonstrating functional objects used to build a new type." << endl; 00129 cout << " people is a mixed type. This reminds me of Java interfaces" << endl; 00130 00131 vector< people* > v; 00132 00133 datamodel d1; 00134 d1.setAge2(15); 00135 00136 person p1; 00137 p1.setAge(20); 00138 00139 //new fnobj1Tfn<datamodel,void,int const>(d1,&datamodel::setAge2); 00140 00141 00142 v.push_back 00143 ( 00144 new people( 00145 new fnobj1Tfn<datamodel,void,int const>(d1,&datamodel::setAge2), 00146 new fnobj0constTfn<datamodel,int>(d1,&datamodel::getAge) 00147 ) 00148 ); 00149 00150 00151 v.push_back 00152 ( 00153 new people( 00154 new fnobj1Tfn<person,void,int const>(p1,&person::setAge), 00155 new fnobj0constTfn<person,int>(p1,&person::getthepersonsAge) 00156 ) 00157 ); 00158 00159 00160 00161 for (uint i=0; i<v.size(); ++i) 00162 cout << SHOW( v[i]->get() ) << endl; 00163 00164 for (uint i=0; i<v.size(); ++i) 00165 delete v[i]; 00166 } 00167 00168 00169 00170
1.5.8