Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ShapeRoot.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef _SHAPEROOT_H_
00003 #define _SHAPEROOT_H_
00004 
00005 #include <vector>
00006 
00007 #include "ShapeSpace.h"
00008 #include "ShapeTypes.h"
00009 #include "BaseData.h"
00010 
00011 namespace DualCoding {
00012 
00013 //! Parent class for all Shape<xxxData> objects
00014 
00015 /*! Shape<T> points to data objects of type T within a ShapeSpace, e.g.,
00016     @a Shape<LineData> points to a @a LineData object.
00017 
00018     Creating a Shape registers the data object in the ShapeSpace and sets the
00019     refcount to 1.  Copying a shape increments the refcount, and deleting
00020     a shape decrements it.  If refcount goes to zero we delete the
00021     data object.  If the user calls deleteShape on a Shape, we remove the
00022     shape from the ShapeSpace but don't actually delete it until the
00023     reference count goes to zero.
00024 */
00025 
00026 class ShapeRoot {
00027 protected:
00028   int        id;
00029   BaseData   *data;
00030   
00031   friend class ShapeSpace;
00032   friend std::ostream& operator<<(std::ostream &os, const ShapeRoot &r);
00033 
00034 public:
00035   //! Construct a dummy ShapeRoot, e.g., so we can assign into it, or return it
00036   //! as an indicator of an invalid or failure result.
00037   ShapeRoot(void): id(-1), data(NULL) {};  
00038   
00039   //! The usual constructor.
00040   ShapeRoot(BaseData *p);
00041   
00042   //! Copy constructor: shallow copy
00043   ShapeRoot(const ShapeRoot &other);
00044 
00045   virtual ~ShapeRoot(void);
00046 
00047   void deleteShape(void);
00048 
00049   void sanity_check(void) const;
00050   
00051   inline bool isValid() const { 
00052     return data != NULL && id > 0 && id == data->id;
00053   }
00054 
00055   void setInvalid() {
00056     if ( id > 0 )
00057       id = -id;
00058   }
00059 
00060   // Overload the -> operator, and other things...
00061   virtual BaseData* operator-> (void);
00062   virtual BaseData* operator-> (void) const;
00063 
00064   int getId() const { return id; }
00065   virtual BaseData& getData() const;
00066   ShapeSpace& getSpace() const;
00067 
00068   //! Shape comparison.  Invalid shapes are considered equal.
00069   //@{
00070   virtual bool operator==(const ShapeRoot& other) const;
00071 
00072   virtual bool operator!=(const ShapeRoot& other) const {
00073     return ! operator==(other);
00074   }
00075   //@}
00076 
00077   ShapeRoot& operator=(const ShapeRoot&);
00078 
00079 protected:
00080   ShapeRoot& addShape(BaseData *p) { return p->space->addShape(p); }
00081 
00082 };
00083 
00084 std::ostream& operator<<(std::ostream &os, const ShapeRoot &r);
00085 
00086 //****************
00087 
00088 
00089 #define ShapeRootTypeConst(_arg,_type) (*reinterpret_cast<const Shape<_type>*>(&_arg))
00090 #define ShapeRootType(_arg,_type) (*reinterpret_cast<Shape<_type>*>(&_arg))
00091 
00092 #define SHAPESTUFF_H(T) \
00093   Shape<T>() : ShapeRoot() {} \
00094   Shape<T>(T* newdata) : ShapeRoot(addShape(newdata)) {}  \
00095   virtual T* operator->(); \
00096   virtual T* operator->() const; \
00097   virtual T& getData() const;
00098 
00099 #define SHAPESTUFF_CC(T) \
00100   T* Shape<T>::operator->() { sanity_check(); return static_cast<T*>(data); };        \
00101   T* Shape<T>::operator->() const { sanity_check(); return static_cast<T*>(data); };  \
00102   T& Shape<T>::getData() const { return  *static_cast<T*>(data); };
00103 
00104 } // namespace
00105 
00106 #endif

DualCoding 4.0
Generated Thu Nov 22 00:52:36 2007 by Doxygen 1.5.4