# 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  

abstract_interface.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_ABSTRACT_H
00027 #define NL_ABSTRACT_H
00028 
00029 #ifdef NL_OS_WINDOWS
00030 
00031 #pragma warning (disable: 4666)
00032 #pragma warning (disable: 4786) 
00033 #pragma warning (disable: 4620) 
00034 #pragma warning (disable: 4275)
00035 #pragma warning (disable: 4251)
00036 
00037 #endif
00038 
00039 #include <map>
00040 #include <iostream>
00041 #include <string>
00042 #include "nel/misc/file.h"
00043 #include "nel/ai/e/ai_exception.h"
00044 
00045 namespace NLAIC //Gen pour general.
00046 {
00047 
00049         char *stringBuild(const char *str, ...);        
00050 
00052         std::string stringGetBuild(const char *str, ...);       
00053 
00062         class IPointerGestion : public NLMISC::IStreamable
00063         {
00064         private:
00066                 sint32 _Ref;
00067         public:
00068 
00070                 IPointerGestion():_Ref(1)
00071                 {
00072                 }
00073 
00075                 IPointerGestion(const IPointerGestion &):_Ref(1)
00076                 {
00077                 }
00078                 
00080                 void incRef()
00081                 {
00082                         _Ref ++;
00083                 }
00084                 
00086                 virtual void release()
00087                 {
00088                         if(decRef() == 0) 
00089                                         delete this;
00090                 }
00091                 
00093                 sint32 decRef()
00094                 {
00095                         return --_Ref;
00096                 }
00097 
00099                 const sint32 &getRef() const
00100                 {
00101                         return _Ref;
00102                 }
00103 
00104                 virtual void getDebugString(std::string &t) const
00105                 {
00106                         t += "None";
00107                 }
00108 
00109                 virtual ~IPointerGestion()
00110                 {                                               
00111                 }
00112 
00113                 
00115 
00116                 virtual std::string     getClassName()
00117                 {
00118                         return std::string("<unnamed>");
00119                 }
00120                 virtual void serial(NLMISC::IStream     &) throw(NLMISC::EStream)
00121                 {
00122                 }
00124 
00125 
00126         };
00127         
00128         class CIdentTypeAlloc;
00129         class IClassFactory;
00130         class IBasicInterface;
00131         class CTypeOfObject;
00132         class CTypeOfOperator;
00133 
00142         class CIdentType: public IPointerGestion
00143         {       
00144         friend class CIdentTypeAlloc;
00145         private:
00147                 char *_Ident;           
00149                 CTypeOfObject *_ObjType;
00151                 CTypeOfOperator *_OpSupport;
00153                 sint32 _Index;
00154 
00155         public:
00157                 static CIdentType VoidType;
00158 
00159         private:
00160 
00162                 CIdentType(const char *ident,const CTypeOfObject &objType,const CTypeOfOperator &opSupport,sint32 index);
00163 
00164         public:
00166                 CIdentType(NLMISC::IStream &);
00167 
00170                 CIdentType(const char *ident);
00171 
00175                 CIdentType(const char *ident,const IClassFactory &classCFactory,
00176                                                                         const CTypeOfObject &objType,
00177                                                                         const CTypeOfOperator &opSupport);
00178 
00179                 CIdentType(const CIdentType &i);
00180                 ~CIdentType();
00181 
00183                 operator const char * () const
00184                 {
00185 #ifdef NL_DEBUG
00186                         if(this == NULL) return "<Inknown>";
00187 #endif
00188                         return _Ident;
00189                 }
00190                                                 
00191         
00193                 void operator = (const CIdentType &a);
00194 
00196                 bool operator == (const CIdentType &a) const
00197                 {                       
00198                         return _Index == a._Index;                      
00199                 }
00201                 bool operator < (const CIdentType &a) const
00202                 {                       
00203                         return strcmp(_Ident,a._Ident) < 0;
00204                 }                                               
00205 
00206                 bool operator <(const char *a) const
00207                 {
00208                         return strcmp(_Ident,a) < 0;
00209                 }
00210 
00211                 bool operator >(const CIdentType &a) const
00212                 {
00213                         return strcmp(_Ident,a._Ident) > 0;
00214                 }                                               
00215 
00216                 sint32 cmp (const CIdentType & a) const
00217                 {
00218                         return strcmp(_Ident,a._Ident);
00219                 }
00220 
00221                 operator const CTypeOfObject &() const
00222                 {
00223                         return *_ObjType;
00224                 }
00225                 
00226                 operator const CTypeOfOperator &() const
00227                 {
00228                         return *_OpSupport;
00229                 }
00230 
00231                 sint32 getIndex() const
00232                 {
00233                         return _Index;
00234                 }               
00235 
00237                 const IBasicInterface *allocClass() const;
00239                 const IClassFactory *getFactory() const;
00240                                 
00242 
00243                 virtual std::string     getClassName()
00244                 {
00245                         return std::string(_Ident);
00246                 }
00247                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream);     
00249 
00250 
00251                 void addObjectType(sint32);
00252         };
00253 
00254 
00262         class IBasicType: public IPointerGestion
00263         {
00264         public:
00265                 IBasicType()
00266                 {
00267                 }
00268 
00269                 IBasicType(const IBasicType &a):IPointerGestion(a)
00270                 {
00271                 }
00275                 virtual const CIdentType &getType() const = 0;
00276 
00280                 virtual const IBasicType *clone() const = 0;
00281 
00285                 virtual const IBasicType *newInstance() const = 0;              
00286 
00290                 virtual void getDebugString(std::string &) const = 0;
00291 
00292                 virtual const std::string getInfo()
00293                 {
00294                         std::string s;
00295                         getDebugString(s);
00296                         return s;
00297                 }
00298 
00299                 virtual ~IBasicType()
00300                 {
00301                 }
00302         };
00303 
00312         class IBasicInterface: public IBasicType
00313         {
00314         
00315         public:
00316                 IBasicInterface()
00317                 {
00318                 }
00319 
00320                 ~IBasicInterface()
00321                 {
00322                 }
00323 
00324                 IBasicInterface(const IBasicInterface &a): IBasicType(a)
00325                 {
00326                 }
00327 
00328                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream)
00329                 {
00330                         if ( f.isReading() )
00331                                 load( f );
00332                         else
00333                                 save( f );
00334                 }
00335 
00339                 virtual void save(NLMISC::IStream &) = 0;
00343                 virtual void load(NLMISC::IStream &) = 0;                       
00344         };
00345                 
00346         inline void IBasicInterface::save(NLMISC::IStream &)
00347         {
00348         } 
00349 
00350         inline void IBasicInterface::load(NLMISC::IStream &)
00351         {
00352         }
00353                 
00354 
00362         class IClassFactory: public IBasicType
00363         {
00364 
00365         public:
00369                 virtual const IBasicInterface *createInstance() const = 0;
00370 
00374                 virtual const IBasicInterface *getClass() const = 0;
00375 
00377                 virtual void setClass(const IBasicInterface &) = 0;
00378 
00379         };
00380 
00387         class CSelfClassFactory : public IClassFactory
00388         {
00389         private:
00391                 IBasicInterface *_Inst;
00392         public:
00394                 CSelfClassFactory(const IBasicInterface &a): _Inst((IBasicInterface *)a.newInstance())
00395                 {
00396                         
00397                 }
00398 
00400                 const IBasicInterface *createInstance() const 
00401                 {
00402                         return (const IBasicInterface *)_Inst->newInstance();
00403                 }
00404                 
00406                 const IBasicType *clone() const
00407                 {
00408                          IBasicType *x = new CSelfClassFactory(*_Inst);
00409                          return x;
00410                 }
00411                 
00413                 const IBasicType *newInstance() const
00414                 {
00415                         return clone();
00416                 }               
00417                 
00419                 void getDebugString(std::string &text) const
00420                 {                       
00421                         //sprintf(text,"CSelfClassFactory sur l'interface %s",(const char *)_Inst->getType());
00422                         text = stringGetBuild("CSelfClassFactory sur l'interface %s",(const char *)_Inst->getType());
00423                 }               
00424 
00426                 const CIdentType &getType() const
00427                 {
00428                         return _Inst->getType();
00429                 }               
00430 
00432                 virtual const IBasicInterface *getClass() const
00433                 {
00434                         return _Inst;
00435                 }                       
00436 
00437                 virtual void setClass(const IBasicInterface &inst)
00438                 {
00439                         _Inst->release();
00440                         _Inst = (IBasicInterface *)inst.clone();                        
00441                 }
00442 
00443                         
00444                 ~CSelfClassFactory()
00445                 {
00446                         _Inst->release();
00447                 }
00448         };
00449         
00450 
00457         class IIO: public IBasicInterface
00458         {
00459 
00460         public:
00461                 IIO()
00462                 {
00463                 }
00464                 virtual void Echo(char *CStringType, ...) const = 0;
00465                 virtual const std::string InPut() const = 0;
00466                 virtual ~IIO()
00467                 {
00468                 }
00469         };      
00470 
00471         void Out(const char *, ...);
00472         void setDefaultIIO(const IIO *);
00473         const IIO *getDefaultIIO();
00474 
00475         
00482         class CIdentTypeAlloc
00483         {       
00484         private:                
00485                 CIdentType *_Id;
00486 
00487         public:
00489                 CIdentTypeAlloc(NLMISC::IStream &is):_Id(NULL){load(is);}
00490                 CIdentTypeAlloc():_Id(NULL){}
00491 
00493                 void load(NLMISC::IStream &is);
00494 
00496                 const IBasicInterface *allocClass()
00497                 {
00498                         if(_Id != NULL) return _Id->allocClass();
00499                         else return NULL;
00500                 }               
00501 
00502                 virtual ~CIdentTypeAlloc()
00503                 {
00504                         if(_Id != NULL) delete _Id;
00505                 }
00506 
00508                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream);             
00509         };
00510         
00511 
00513 
00514 
00515         inline NLMISC::IStream &operator >> (NLMISC::IStream &is, CIdentTypeAlloc &o)
00516         {
00517                 o.load(is);
00518                 return is;
00519         }
00520 
00522         NLMISC::IStream &operator << (NLMISC::IStream &os, CIdentType &o);      
00523 
00525         inline NLMISC::IStream &operator << (NLMISC::IStream &os, IBasicInterface &o)
00526         {
00527                 o.save(os);
00528                 return os;
00529         }
00530 
00532         inline NLMISC::IStream &operator >> (NLMISC::IStream &is, IBasicInterface &o)
00533         {
00534                 o.load(is);
00535                 return is;
00536         }       
00538 }
00539 #endif