/* * Read a bucket of objects * * Uses supported classes in Gbiostream * */ #include <stdlib.h> #include <typeinfo> #include <vector> #include <algorithm> // reverse, transform vector; copy to output #include <functional> // multiplies; negate; contains functors #include "POBroker.h" #include "bucket.h" #include <iostream> using std::cout; using std::endl; int main() { const int MAX = 9; cout << "Creating a database of objects" << endl; POBroker<POBObj> pob("collect.dat"); pob.register_create_function(typeid(Bucket).name(), Bucket::create); POBObj* pobj; Bucket buck; //OR: vector<double>& v = buck.get_items(); for (double num=0; num < MAX; ++num) { buck.push_back(num); //OR: v.push_back(num); //ALSO: buck.get_items().push_back(num); } cout << buck << endl; //OR: vector<double>& V = pnums->get_items(); //OR: copy(V.begin(), V.end(), ostream_iterator<double>(cout," ") ); cout << "Writing a vector of doubles" << endl; unsigned long oi = pob.writeObj(buck); cout << "oi=" << oi << endl; cout << "Searching for inserted object in file" << endl; if (pob.findObj(oi)) { Bucket* pnums = (Bucket*)pob.readObj(oi); cout << "Inserted Object:" << endl; cout << *pnums << endl; cout << "\nReversing this vector of doubles:" << endl; vector<double>& V = pnums->get_items(); reverse(V.begin(), V.end()); cout << *pnums << endl; cout << "\nDoubling this vector of doubles:" << endl; transform(V.begin(),V.end(),V.begin(),V.begin(),multiplies<double>()); //OR: transform(V.begin(),V.end(),V.begin(),negate<double>()); cout << *pnums << endl; pob.modifyObj(pnums); pob.deleteRef(pnums); } else cout << "Not found" << endl; cout << "\nSearching for modified object in file" << endl; if (pob.findObj(oi)) { Bucket* pnums = (Bucket*)pob.readObj(oi); cout << "Modified Object:" << endl; cout << *pnums << endl; pob.deleteRef(pnums); } else cout << "Not found" << endl; cout << "\nExiting from this program" << endl; return 0; } 1