Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Type.cpp

Go to the documentation of this file.
00001 #include "Type.h"
00002 #include "X3DNodeProxy.h"
00003 #include "Component.h"
00004 
00005 #include <iostream>
00006 
00007 using namespace X3DTK;
00008 using namespace std;
00009 
00010 int Type::count = 0;
00011 MFType Type::_typeList = MFType();  
00012 MFNodeProxy Type::_proxyList = MFNodeProxy();
00013 bool Type::_functionDefinition = false;
00014 
00015 void Type::setParent(const Type *parent)
00016 {
00017   _parent = (Type *)parent;
00018 }
00019 
00020 void Type::addOneReference()
00021 {
00022   ++_refCount;
00023 }
00024 
00025 void Type::removeOneReference(Type *type)
00026 {
00027   --type->_refCount;
00028   
00029   //removes the instance of the parents.
00030   if (type->_parent != 0)
00031     removeOneReference(type->_parent);
00032     
00033   if (type->_refCount == 0)  
00034   {
00035     //remove in the child list of the parent.
00036     if (type->_parent != 0)    
00037       type->_parent->_childList.erase(type->_name);
00038       
00039     //remove in the list.
00040     _typeList.erase(type->_name);
00041 
00042     //cout << "erase" << endl;
00043     delete type;
00044   }
00045 }
00046 
00047 Type *Type::getTypeOfName(const SFString &name)
00048 {
00049   MFType::const_iterator it = _typeList.find(name); 
00050   if (it != _typeList.end())
00051     return (*it).second;
00052     
00053   //the type doesn't exist.
00054   return 0;  
00055 }
00056 
00057 Type *Type::getTypeOfId(int id)
00058 {
00059   // iterate the Types
00060   for (MFType::const_iterator it = _typeList.begin(); it != _typeList.end(); ++it)
00061     if ((*it).second->getId() == id)
00062       return (*it).second;
00063       
00064   return 0;      
00065 }
00066 void Type::beginFunctionDefinition()
00067 {
00068   _functionDefinition = true;
00069 }
00070 
00071 void Type::endFunctionDefinition()
00072 {
00073   _functionDefinition = false;
00074 }
00075 
00076 void Type::recomputeIds()
00077 {
00078 }
00079 
00080 void Type::printDerivationTree()
00081 {
00082   cout << "Derivation tree:" << endl;
00083   SFString tab("");
00084   //finding the nodes which have no parent.
00085   for (MFType::const_iterator it = _typeList.begin(); it != _typeList.end(); ++it)
00086   {
00087     if ((*it).second->_parent == 0)
00088     {
00089       (*it).second->printDerivationTree(tab);
00090     } 
00091   }    
00092   cout << "Derivation tree finished" << endl;
00093 }
00094 
00095 Type::Type(const SFString &name)
00096 : _name(name)
00097 {
00098   //cout << "new Type " << name << endl;
00099 }
00100 
00101 Type::~Type()
00102 {
00103   //cout << "destr Type = " << _name << endl;
00104   Component::removeOneReference(_component);
00105   _childList.clear();
00106 }
00107 
00108 void Type::defineTagName(Type **type, const SFString &name, const SFString &component)
00109 {
00110   Type *t = getTypeOfName(name);
00111   
00112   if (t == 0)
00113   {
00114     t = new Type(name);
00115     t->_id = count;
00116     t->_refCount = 0;
00117     t->_parent = 0;
00118       
00119     //type is the parent of t;    
00120     if (*type != 0)    
00121       (*type)->_childList[name] = t;
00122 
00123     t->_parent = *type;
00124 
00125     if (!_functionDefinition)
00126     {
00127       ++count;
00128       _typeList[name] = t;
00129 
00130       //advertising proxies
00131       for (MFNodeProxy::iterator itProxy = _proxyList.begin(); itProxy != _proxyList.end(); ++itProxy) 
00132         (*itProxy)->addType(t);
00133     }
00134     
00135     if (component == "")
00136       t->_component = Component::getComponent(t->_parent->_component->getName());
00137     else
00138     {
00139       t->_component = Component::getComponent(component);
00140       if (t->_component == 0)
00141         t->_component = new Component(component);
00142     }    
00143     //cout << "def tag Type = " << name << endl;
00144     //component referenced by a new Type.
00145     t->_component->addOneReference();  
00146   }
00147   
00148   //the type t is referenced by a new Node.
00149   t->addOneReference();
00150   *type = t;
00151 }
00152 
00153 void Type::addNodeProxy(X3DNodeProxy *proxy)
00154 {
00155   //updating proxy list
00156   _proxyList.push_back(proxy);
00157   
00158   //adding all types
00159   for (MFType::const_iterator it = _typeList.begin(); it != _typeList.end(); ++it)
00160     proxy->addType((*it).second);
00161 }
00162 
00163 void Type::removeNodeProxy(X3DNodeProxy *proxy)
00164 {
00165   _proxyList.remove(proxy);
00166 }
00167 
00168 void Type::printDerivationTree(SFString tab) const
00169 {
00170   cout << tab << _name << ": " << _refCount << ", " << _component->getName() << ": " << _component->_refCount << endl;
00171   tab += "  ";  
00172 
00173   for (MFType::const_iterator it = _childList.begin(); it != _childList.end(); ++it)
00174     (*it).second->printDerivationTree(tab);
00175 }

Generated on Wed May 14 10:03:11 2003 for X3DToolKit by doxygen1.3