mainForCountedExample.cpp

00001 
00002 
00003 #include "CountedExample.h"
00004 #include <ipc/core.h>
00005 #include <unistd.h>
00006 #include <iostream>
00007 
00008 void sl(const unsigned int s) {
00009     std::cout << "Sleeping for " << s << " seconds" << std::endl;
00010     sleep(s);
00011 };
00012 
00013 void createACountedExampleAndDoStuff() {
00014   std::cout << "Making a CountedExample ..." << std::endl;
00015   CountedExample & countedExampleSrv = *(new CountedExample); // not supposed to do "LargeCarrot largeCarrot;" as this can lead to largeCarrot being deleted twice -- once when largeCarrot goes out of scope, and once if/when someone remotely requests it be deleted.".  For this reason, we are equally only supposed to delete it with _destroy();
00016   std::cout << "... finishing making a CountedExample" << std::endl;
00017   sl(1);
00018   {
00019     // get a reference to the countedExample object
00020     std::cout << "Getting a reference to the countedExample object" << std::endl;
00021     Stew::CountedExample_var countedExampleRef = countedExampleSrv._this();
00022     sl(1);
00023   };
00024 
00025   {
00026     // get a different reference to the countedExample object
00027     std::cout << "Getting a different reference to the countedExample object" << std::endl;
00028     Stew::CountedExample_var countedExampleRef = countedExampleSrv._this();
00029     sl(1);
00030 
00031     std::cout << "Decrementing the count on the second reference ... should lead to the death of the servant" << std::endl;
00032     countedExampleRef->removeRef(); // we implemented this method so that it would end the life of the servant.
00033     // it is vital we call shutdown for this temporary object otherwise we will leak memory.
00034     sl(1);
00035 
00036   };
00037   
00038 };
00039 
00040 int main(int argc, char * argv[]) {
00041   std::cout << "This program demonstrates how to use an object that will live only for a short time (eg a return type) that will not be published in the IPC::Partition index, and where the lifetime of the servant managing this object is to be controlled by the user manually via calls to addCount() and removeCount().  This makes it a little bit like an \"unsafe\" boost::shared_ptr." << std::endl;
00042   std::cout << "Initialising IPC ..." << std::endl;
00043   IPCCore::init(argc, argv);
00044 
00045   // create the server for a CountedExample object
00046   createACountedExampleAndDoStuff();
00047 
00048   std::cout << "Bye" << std::endl;
00049 };

Generated on Mon Feb 6 14:01:22 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6