Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ShapeRoot.cc

Go to the documentation of this file.
00001 #include "SketchSpace.h"
00002 #include "ShapeSpace.h"
00003 #include "ShapeTypes.h"
00004 #include "ShapeRoot.h"
00005 #include "BaseData.h"
00006 
00007 using namespace std;
00008 
00009 namespace DualCoding {
00010 
00011 ShapeRoot::ShapeRoot(BaseData *p) : id(p->getId()), data(p) {
00012   ++data->refcount;
00013 }
00014 
00015 ShapeRoot::ShapeRoot(const ShapeRoot &other) 
00016   : id(other.id), data(other.data) {
00017   if ( data != NULL )
00018     ++data->refcount;
00019 };
00020 
00021 ShapeRoot& ShapeRoot::operator=(const ShapeRoot& src) {
00022   if ( data != NULL && --data->refcount == 0 && data != src.data )
00023     delete data;
00024   id = src.id;
00025   data = src.data;
00026   if ( data != NULL )
00027     ++data->refcount;
00028   return *this;
00029 }
00030 
00031 ShapeRoot::~ShapeRoot(void) { 
00032   if ( data != NULL && --data->refcount == 0 )
00033    delete data;
00034 }
00035 
00036 void ShapeRoot::deleteShape(void) { 
00037   if ( isValid() )
00038    data->space->deleteShape(*this); 
00039 }
00040 
00041 void ShapeRoot::sanity_check(void) const {
00042   if ( !isValid() ) {
00043     cerr << "ERROR: Reference to invalid shape at " << (const void*)this
00044       << "  id=" << id << "  data=" << (const void*)data;
00045    if ( data != NULL )
00046     cout << "  data->id=" << data->id;
00047    cout << endl;
00048   }
00049 }
00050 
00051 ShapeSpace& ShapeRoot::getSpace() const {
00052   sanity_check(); 
00053   return *(data->space);
00054 }
00055 
00056 bool ShapeRoot::operator==(const ShapeRoot& other) const {
00057   if ( isValid() )
00058     if ( other.isValid() )
00059       return id == other.id;
00060     else
00061       return false;
00062   else
00063     return !other.isValid();
00064 }      
00065 
00066 std::ostream& operator<<(std::ostream &os, const ShapeRoot &r) {
00067   if ( r.isValid() ) {
00068     cout << r.data->getSpace().name << ":" << "Shape<" << r->getTypeName() << ">("
00069    << r->getName() << ",id="
00070    << r.id  /* << ", data=" << r.data */
00071    << ")";
00072   }
00073   else {
00074    cout << "Shape(id=" << r.getId() << ",data=" << reinterpret_cast<size_t>(r.data) << ")[invalid]";
00075   }
00076   return os;
00077 }
00078 
00079 } // namespace

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