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   friend std::istream& operator>>(std::istream &is, ShapeRoot &r) { r.id = 0; r.data = NULL; return is; }; // dummy to make DataEvent's load/save happy
00034 
00035 public:
00036   //! Construct a dummy ShapeRoot, e.g., so we can assign into it, or return it
00037   //! as an indicator of an invalid or failure result.
00038   ShapeRoot(void): id(-1), data(NULL) {};  
00039   
00040   //! The usual constructor.
00041   ShapeRoot(BaseData *p);
00042   
00043   //! Copy constructor: shallow copy
00044   ShapeRoot(const ShapeRoot &other);
00045 
00046   virtual ~ShapeRoot(void);
00047 
00048   void deleteShape(void);
00049 
00050   void sanity_check(void) const;
00051   
00052   inline bool isValid() const { 
00053     return data != NULL && id > 0 && id == data->id;
00054   }
00055 
00056   void setInvalid() {
00057     if ( id > 0 )
00058       id = -id;
00059   }
00060 
00061   inline operator bool() const { return isValid(); }
00062 
00063   // Overload the -> operator, and other things...
00064   virtual BaseData* operator-> (void) { sanity_check(); return data; }
00065   virtual const BaseData* operator-> (void) const { sanity_check(); return data; }
00066   virtual BaseData& operator* (void) { sanity_check(); return *data; }
00067   virtual const BaseData& operator* (void) const { sanity_check(); return *data; }
00068   virtual BaseData& getData() { return *data; }
00069   virtual const BaseData& getData() const { return *data; }
00070 
00071   int getId() const { return id; }
00072   ShapeSpace& getSpace() const;
00073 
00074   //! Shape comparison.  Invalid shapes are considered equal.
00075   //@{
00076   virtual bool operator==(const ShapeRoot& other) const;
00077 
00078   virtual bool operator!=(const ShapeRoot& other) const {
00079     return ! operator==(other);
00080   }
00081   //@}
00082 
00083   ShapeRoot& operator=(const ShapeRoot&);
00084 
00085 protected:
00086   ShapeRoot& addShape(BaseData *p) { return p->space->addShape(p); }
00087 
00088 };
00089 
00090 std::ostream& operator<<(std::ostream &os, const ShapeRoot &r);
00091 
00092 // ****************
00093 
00094 
00095 #define ShapeRootTypeConst(_arg,_type) (*reinterpret_cast<const Shape<_type>*>(&_arg))
00096 #define ShapeRootType(_arg,_type) (*reinterpret_cast<Shape<_type>*>(&_arg))
00097 
00098 #define SHAPESTUFF_H(T) \
00099   Shape<T>() : ShapeRoot() {} \
00100   Shape<T>(T* newdata) : ShapeRoot(addShape(newdata)) {}  \
00101   T* operator->() { sanity_check(); return static_cast<T*>(data); } \
00102   const T* operator->() const { sanity_check(); return static_cast<T*>(data); } \
00103   T& operator*() { sanity_check(); return *static_cast<T*>(data); } \
00104   const T& operator*() const { sanity_check(); return *static_cast<T*>(data); } \
00105   T& getData() { return *static_cast<T*>(data); } \
00106   const T& getData() const { return *static_cast<T*>(data); }
00107   
00108 #define SHAPESTUFF_CC(T) \
00109 
00110 
00111 } // namespace
00112 
00113 #endif

DualCoding 5.1CVS
Generated Mon May 9 04:56:27 2016 by Doxygen 1.6.3