Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

SharedObject.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SharedObject_h
00003 #define INCLUDED_SharedObject_h
00004 
00005 #include <OPENR/RCRegion.h>
00006 
00007 //! It's nice to have a parent class of SharedObject (which is what you probably want to be reading) so that you can pass around the data structure without worrying about what type is inside the shared memory region.
00008 /*! See MotionManager for an example on how to use this. */
00009 class SharedObjectBase {
00010 public:
00011   void* data() const { return rcr->Base(); } //!< returns a pointer to the data region
00012   RCRegion * getRegion() const { return rcr; } //!< returns the OPEN-R memory region, should you need it
00013 
00014 protected:
00015   SharedObjectBase() : rcr(NULL) {} //!< constructor, protected because you shouldn't need to create this directly, just a common interface to all templates of SharedObject
00016   RCRegion * rcr; //!< the pointer to the shared memory region this is in charge of
00017   
00018 private:
00019   SharedObjectBase(const SharedObjectBase&); //!< this shouldn't be called...
00020   SharedObjectBase& operator=(const SharedObjectBase&); //!< this shouldn't be called...
00021 };  
00022 
00023 //! This templated class allows convenient creation of any type of class wrapped in a shared memory region
00024 /*! @see MotionManager for an example on how to use this.*/
00025 template<class MC>
00026 class SharedObject : public SharedObjectBase {
00027 public:
00028   //!if you really need more than 5 arguments for your class, well, you're one crazy puppy but if you really want to, just make more like shown... (yay templates!)
00029   //!@name templated contructors - allows you to pass constructor arguments on to the object being created
00030 
00031   //! Creates the class with the default constructor
00032   SharedObject() : SharedObjectBase() {
00033     rcr = new RCRegion(calcsize());
00034     new (rcr->Base()) MC;
00035   }
00036   //! Creates the class, passing its constructor t1
00037   template<class T1> SharedObject(T1 t1) : SharedObjectBase() {
00038     rcr = new RCRegion(calcsize());
00039     new (rcr->Base()) MC(t1);
00040   }
00041   //! Creates the class, passing its constructor t1 and t2
00042   template<class T1, class T2> SharedObject(T1 t1, T2 t2) : SharedObjectBase(){
00043     rcr = new RCRegion(calcsize());
00044     new (rcr->Base()) MC(t1,t2);
00045   }
00046   //! Creates the class, passing its constructor t1, t2, and t3
00047   template<class T1, class T2, class T3> SharedObject(T1 t1, T2 t2, T3 t3) : SharedObjectBase(){
00048     rcr = new RCRegion(calcsize());
00049     new (rcr->Base()) MC(t1,t2,t3);
00050   }
00051   //! Creates the class, passing its constructor t1, t2, t3 and t4
00052   template<class T1, class T2, class T3, class T4> SharedObject(T1 t1, T2 t2, T3 t3, T4 t4) : SharedObjectBase(){
00053     rcr = new RCRegion(calcsize());
00054     new (rcr->Base()) MC(t1,t2,t3,t4);
00055   }
00056   //! Creates the class, passing its constructor t1, t2, t3, t4 and t5 - if you need more arguments, just add them
00057   template<class T1, class T2, class T3, class T4, class T5> SharedObject(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) : SharedObjectBase(){
00058     rcr = new RCRegion(calcsize());
00059     new (rcr->Base()) MC(t1,t2,t3,t4,t5);
00060   }
00061   //@}
00062 
00063   MC* operator->() const { return dataCasted(); } //!< smart pointer to the underlying class
00064   MC& operator*() const { return *dataCasted(); } //!< smart pointer to the underlying class
00065   MC& operator[](int i) const { return dataCasted()[i]; } //!< smart pointer to the underlying class
00066 protected:
00067   MC* dataCasted() const { return static_cast<MC*>(data()); } //!< returns a correctly typed pointer to the object's memory
00068 
00069   //!Calculates the size of the memory region to be used, rounding up to the nearest page size
00070   /*! Not sure this is completely necessary, but may be nice.  Of course, this also means even
00071    *  small regions are going to be at least 4K (current page size)  If memory gets tight or we
00072    *  get a lot of little regions floating around, this might be worth checking into */
00073   unsigned int calcsize() {
00074     size_t size = sizeof(MC);
00075     sError error;
00076     size_t page_size;
00077     error = GetPageSize(&page_size);
00078     if (error != sSUCCESS) {
00079       cout << "error: " << error << " getting page size in SharedMem" << endl;
00080       page_size = 4096;
00081     }
00082     
00083     int new_size,num_pages;
00084     num_pages = (size+page_size-1)/page_size;
00085     new_size = num_pages*page_size;
00086     //cout << "req" << size << "new" << new_size << "ps" << page_size << endl;
00087     /*    cout << "data size is " << sizeof(MC) << endl;
00088           cout << "msg size is " << MotionManagerMsg::SIZEOF_MSG << endl;
00089           cout << "SIZE is " << rcr->Size() << endl;
00090           cout << "PAGE is " << page_size << endl;
00091           cout << "BASE is " << (void*)rcr->Base() << endl; */
00092     return new_size;
00093   }
00094 };
00095 
00096 /*! @file
00097  * @brief Defines SharedObject, a wrapper for objects in order to facilitate sending them between processes
00098  * @author ejt (Creator)
00099  *
00100  * $Author: ejt $
00101  * $Name: tekkotsu-1_4_1 $
00102  * $Revision: 1.1 $
00103  * $State: Exp $
00104  * $Date: 2003/03/09 07:06:11 $
00105  */
00106 
00107 #endif //INCLUDED_SharedObject_h

Tekkotsu v1.4
Generated Sat Jul 19 00:06:31 2003 by Doxygen 1.3.2