# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

ident_type.cpp

Go to the documentation of this file.
00001 
00006 /* Copyright, 2000 Nevrax Ltd.
00007  *
00008  * This file is part of NEVRAX NEL.
00009  * NEVRAX NEL is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2, or (at your option)
00012  * any later version.
00013 
00014  * NEVRAX NEL is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * General Public License for more details.
00018 
00019  * You should have received a copy of the GNU General Public License
00020  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00021  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00022  * MA 02111-1307, USA.
00023  */
00024 
00025 #include <stdio.h>
00026 #include <iostream.h>
00027 #include <stdio.h>
00028 #include <stdarg.h>
00029 
00030 
00031 #include "nel/ai/c/abstract_interface.h" 
00032 #include "nel/ai/e/ai_exception.h" 
00033 #include "nel/ai/c/registry_class.h"
00034 #include "nel/ai/c/registry_type.h"
00035 
00036 namespace NLAIC
00037 {
00038 
00039         //freopen( "file.txt", "w", stdout ); 
00040         char *stringBuild(const char *str, ...)
00041         {               
00042                 char    temp[64*1024];
00043                 va_list argument;
00044                 va_start (argument, str);
00045                 
00046                 vsprintf(temp, str, argument);
00047                 char *s = new char [strlen(temp)+1];
00048                 strcpy(s,temp);
00049                 return s;
00050         }
00051 
00052         const IIO *DefaultIIO = NULL;
00053         void setDefaultIIO(const IIO *io)
00054         {
00055                 DefaultIIO = io;
00056         }
00057 
00058         const IIO *getDefaultIIO()
00059         {
00060                 return DefaultIIO;
00061         }
00062 
00063         void Out(const char *str,...)
00064         {               
00065                 char    temp[64*1024];
00066                 va_list argument;
00067                 va_start (argument, str);
00068                 
00069                 vsprintf(temp, str, argument);
00070                 if(DefaultIIO != NULL) 
00071                         DefaultIIO->Echo(temp);
00072                 else
00073                 {
00074                         fprintf(stderr,temp);
00075                         fflush(stderr);
00076                 }
00077         }
00078 
00079         std::string stringGetBuild(const char *str, ...)
00080         {
00081                 char    temp[64*1024];
00082                 va_list argument;
00083                 va_start (argument, str);
00084                 
00085                 vsprintf(temp, str, argument);                  
00086 
00087                 return std::string(temp);
00088 
00089         }
00090 
00091         CIdentType CIdentType::VoidType = CIdentType("VOID",CTypeOfObject::tObject,CTypeOfOperator(0),-1);
00092 
00093         CIdentType::~CIdentType()
00094         {
00095                 if(_Ident != NULL) delete (char *)_Ident;
00096                 if(_ObjType != NULL) _ObjType->release();
00097                 if(_OpSupport != NULL) _OpSupport->release();
00098         }
00099 
00100         CIdentType::CIdentType(NLMISC::IStream &f)
00101         {
00102                 _ObjType = NULL;
00103                 _OpSupport = NULL;
00104                 _Ident = NULL;
00105                 serial(f);
00106         }
00107 
00108         CIdentType::CIdentType(const char *ident,const CTypeOfObject &objType,const CTypeOfOperator &opSupport,sint32 index): _ObjType((CTypeOfObject *)objType.clone()),
00109                                                                                                                               _OpSupport((CTypeOfOperator *)opSupport.clone()),
00110                                                                                                                               _Index(-1)
00111         {
00112                 _Ident = new char [strlen(ident) + 1];
00113                 strcpy((char *)_Ident,ident);
00114                 _Index = index;
00115         }
00116         
00117         CIdentType::CIdentType(const char *ident,const IClassFactory &classCFactory,const CTypeOfObject &objType,const CTypeOfOperator &opSupport): _ObjType((CTypeOfObject *)objType.clone()),
00118                                                                                                                                                     _OpSupport((CTypeOfOperator *)opSupport.clone()),
00119                                                                                                                                                     _Index(-1)
00120         {       
00121                 _Ident = new char [strlen(ident) + 1];
00122                 strcpy((char *)_Ident,ident);
00123                                 
00124                 try
00125                 {                               
00126                         CRegistry *r = getRegistry();
00127                         _Index = r->registerClass(*this,classCFactory);
00128                 }
00129                 catch (NLAIE::IException &err)
00130                 {
00131                         delete _Ident;
00132                         _Ident = NULL;
00133                         _ObjType->release();
00134                         _OpSupport->release();
00135                         throw NLAIE::CExceptionContainer(err.what());
00136                 }               
00137         }
00138 
00139         
00140         CIdentType::CIdentType(const char *ident): _Index(-1)
00141         {
00142                 _Ident = new char [strlen(ident) + 1];
00143                 strcpy((char *)_Ident,ident);
00144 
00145                 try
00146                 {                               
00147                         CRegistry *r = getRegistry();                   
00148                         _Index = r->getNumIdent(*this);
00149                         const CIdentType &id= r->getIdent(_Index);
00150                         _ObjType = (CTypeOfObject *)&((const CTypeOfObject &)id);
00151                         _ObjType->incRef();
00152                         _OpSupport = (CTypeOfOperator *)&((const CTypeOfOperator &)id);
00153                         _OpSupport->incRef();
00154                 }
00155                 catch (NLAIE::IException &err)
00156                 {
00157                         delete _Ident;
00158                         _Ident = NULL;
00159                         _ObjType = NULL;
00160                         _OpSupport = NULL;
00161                         NLAIE::CExceptionContainer e(err.what());
00162                         throw e;
00163                 }               
00164         }
00165 
00166         CIdentType::CIdentType(const CIdentType &i):_ObjType((CTypeOfObject *)i._ObjType->clone()),_OpSupport((CTypeOfOperator *)i._OpSupport->clone()),_Index(-1)
00167         {
00168                 _Index = i._Index;
00169                 _Ident = new char [strlen(i._Ident) + 1];
00170                 strcpy((char *)_Ident,i._Ident);
00171         }
00172 
00173         const IBasicInterface *CIdentType::allocClass() const
00174         {
00175                 return getRegistry()->createInstance(_Index);
00176         }
00177 
00178         const IClassFactory *CIdentType::getFactory() const
00179         {
00180                 return getRegistry()->getFactory(_Index);
00181         }
00182 
00183         void CIdentType::addObjectType(sint32 t)
00184         {
00185                 if ( _ObjType != NULL )
00186                         _ObjType->addType(t);
00187         }
00188 
00189         
00190         void CIdentType::operator = (const CIdentType &i)
00191         {
00192                 delete _Ident;
00193                 _Ident = new char [strlen(i._Ident) + 1];
00194                 strcpy((char *)_Ident,i._Ident);                
00195                 _Index = i._Index;
00196         }       
00197 
00198         void CIdentType::serial(NLMISC::IStream &f) throw(NLMISC::EStream)
00199         {
00200                 if(!f.isReading())
00201                 {                       
00202                         f.serial(_Index);
00203                         std::string x(_Ident);
00204                         f.serial(x);
00205                 }
00206                 else
00207                 {
00208                         std::string s;
00209                         sint32 n;                       
00210                         f.serial(n);
00211                         f.serial(s);
00212 
00213                         if(_Ident) delete _Ident;
00214                         
00215                         _Ident = new char [strlen(s.data()) + 1];
00216                         strcpy((char *)_Ident,s.data());
00217                         try
00218                         {                               
00219                                 _Index = getRegistry()->getNumIdent(*this);
00220                                 const CIdentType &id= getRegistry()->getIdent(_Index);
00221                                 if(_ObjType) _ObjType->release();
00222                                 if(_OpSupport) _OpSupport->release();
00223 
00224                                 _ObjType = (CTypeOfObject *)&((const CTypeOfObject &)id);
00225                                 _ObjType->incRef();
00226                                 _OpSupport = (CTypeOfOperator *)&((const CTypeOfOperator &)id);
00227                                 _OpSupport->incRef();
00228                         }
00229                         catch (NLAIE::IException &)
00230                         {
00231                                 throw NLMISC::EStream();
00232                         }
00233                         
00234                 }
00235         }
00236 
00237         void CIdentTypeAlloc::load(NLMISC::IStream &is) 
00238         {
00239                 if(_Id != NULL) 
00240                         delete _Id;
00241 
00242                 /*sint32 i;
00243                 is.serial(i);           
00244                 std::string cn;
00245                 is.serial( cn );
00246                 char *className = new char [strlen(cn.data()) + 1];
00247                 strcpy( className, cn.data() );         
00248 
00249                 CTypeOfOperator op(is);
00250                 CTypeOfObject ty(is);*/
00251                 
00252                 _Id = new CIdentType(is);
00253                 //delete [] className;
00254         }       
00255 
00256 
00257         void CIdentTypeAlloc::serial(NLMISC::IStream &is)  throw(NLMISC::EStream)
00258         {
00259                 if(is.isReading()) load(is);
00260                 else
00261                 {
00262                         if(_Id != NULL) 
00263                         {
00264                                 _Id->serial(is);
00265                         }
00266                         else
00267                         {
00268                                 throw NLMISC::EStream();
00269                         }
00270                 }
00271                 
00272         }       
00273 
00274         NLMISC::IStream &operator << (NLMISC::IStream &os, CIdentType &o)
00275         {               
00276                 o.serial(os);
00277                 return os;
00278         }
00279         
00280 }