# 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 "nel/misc/file.h"
00042 #include "nel/ai/e/ai_exception.h"
00043 
00044 namespace NLAIC //Gen pour general.
00045 {
00047         char *stringBuild(const char *str, ...);        
00048 
00057         class IPointerGestion : public NLMISC::IStreamable
00058         {
00059         private:
00061                 sint32 _Ref;
00062         public:
00063 
00065                 IPointerGestion():_Ref(1)
00066                 {
00067                 }
00068 
00070                 IPointerGestion(const IPointerGestion &):_Ref(1)
00071                 {
00072                 }
00073                 
00075                 void incRef()
00076                 {
00077                         _Ref ++;
00078                 }
00079                 
00081                 virtual void release()
00082                 {
00083                         if(decRef() == 0) 
00084                                         delete this;
00085                 }
00086                 
00088                 sint32 decRef()
00089                 {
00090                         return --_Ref ;
00091                 }
00092 
00094                 const sint32 &getRef() const
00095                 {
00096                         return _Ref;
00097                 }
00098 
00099                 virtual ~IPointerGestion()
00100                 {                                               
00101                 }
00102 
00103                 
00105 
00106                 virtual std::string     getClassName()
00107                 {
00108                         return std::string("<unnamed>");
00109                 }
00110                 virtual void serial(NLMISC::IStream     &) throw(NLMISC::EStream)
00111                 {
00112                 }
00114 
00115 
00116         };
00117         
00118         class CIdentTypeAlloc;
00119         class IClassCFactory;
00120         class IBasicInterface;
00121         class CTypeOfObject;
00122         class CTypeOfOperator;
00123 
00132         class CIdentType: public IPointerGestion
00133         {       
00134         friend class CIdentTypeAlloc;
00135         private:
00137                 char *_Ident;           
00139                 CTypeOfObject *_ObjType;
00141                 CTypeOfOperator *_OpSupport;
00143                 sint32 _Index;
00144 
00145         public:
00147                 static CIdentType VoidType;
00148 
00149         private:
00150 
00152                 CIdentType(const char *ident,const CTypeOfObject &objType,const CTypeOfOperator &opSupport,sint32 index);
00153 
00154         public:
00156                 CIdentType(NLMISC::IStream &);
00157 
00160                 CIdentType(const char *ident);
00161 
00165                 CIdentType(const char *ident,const IClassCFactory &classCFactory,
00166                                                                         const CTypeOfObject &objType,
00167                                                                         const CTypeOfOperator &opSupport);
00168 
00169                 CIdentType(const CIdentType &i);
00170                 ~CIdentType();
00171 
00173                 operator const char * () const
00174                 {
00175                         return _Ident;
00176                 }
00177                                                 
00178         
00180                 void operator = (const CIdentType &a);
00181 
00183                 bool operator == (const CIdentType &a) const
00184                 {                       
00185                         return _Index == a._Index;                      
00186                 }
00188                 bool operator < (const CIdentType &a) const
00189                 {                       
00190                         return strcmp(_Ident,a._Ident) < 0;
00191                 }                                               
00192 
00193                 bool operator <(const char *a) const
00194                 {
00195                         return strcmp(_Ident,a) < 0;
00196                 }
00197 
00198                 bool operator >(const CIdentType &a) const
00199                 {
00200                         return strcmp(_Ident,a._Ident) > 0;
00201                 }                                               
00202 
00203                 sint32 cmp (const CIdentType & a) const
00204                 {
00205                         return strcmp(_Ident,a._Ident);
00206                 }
00207 
00208                 operator const CTypeOfObject &() const
00209                 {
00210                         return *_ObjType;
00211                 }
00212                 
00213                 operator const CTypeOfOperator &() const
00214                 {
00215                         return *_OpSupport;
00216                 }
00217 
00218                 sint32 getIndex() const
00219                 {
00220                         return _Index;
00221                 }               
00222 
00224                 const IBasicInterface *allocClass() const;
00226                 const IClassCFactory *getFactory() const;
00227                                 
00229 
00230                 virtual std::string     getClassName()
00231                 {
00232                         return std::string(_Ident);
00233                 }
00234                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream);     
00236         };
00237 
00238 
00246         class IBasicType: public IPointerGestion
00247         {
00248         public:
00249                 IBasicType()
00250                 {
00251                 }
00252 
00253                 IBasicType(const IBasicType &a):IPointerGestion(a)
00254                 {
00255                 }
00259                 virtual const CIdentType &getType() const = 0;
00260 
00264                 virtual const IBasicType *clone() const = 0;
00265 
00269                 virtual const IBasicType *newInstance() const = 0;              
00270 
00274                 virtual void getDebugString(char *) const = 0;
00275 
00276                 virtual ~IBasicType()
00277                 {
00278                 }
00279         };
00280 
00289         class IBasicInterface: public IBasicType
00290         {
00291         
00292         public:
00293                 IBasicInterface()
00294                 {
00295                 }
00296 
00297                 ~IBasicInterface()
00298                 {
00299                 }
00300 
00301                 IBasicInterface(const IBasicInterface &a): IBasicType(a)
00302                 {
00303                 }
00304 
00305                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream)
00306                 {
00307                         if ( f.isReading() )
00308                                 load( f );
00309                         else
00310                                 save( f );
00311                 }
00312 
00316                 virtual void save(NLMISC::IStream &) = 0;
00320                 virtual void load(NLMISC::IStream &) = 0;                       
00321         };
00322                 
00323         inline void IBasicInterface::save(NLMISC::IStream &)
00324         {
00325         } 
00326 
00327         inline void IBasicInterface::load(NLMISC::IStream &)
00328         {
00329         }
00330                 
00331 
00339         class IClassCFactory: public IBasicType
00340         {
00341 
00342         public:
00346                 virtual const IBasicInterface *createInstance() const = 0;
00347 
00351                 virtual const IBasicInterface *getClass() const = 0;
00352 
00353         };
00354 
00361         class CSelfClassCFactory : public IClassCFactory
00362         {
00363         private:
00365                 IBasicInterface *_Inst;
00366         public:
00368                 CSelfClassCFactory(const IBasicInterface &a): _Inst((IBasicInterface *)a.newInstance())
00369                 {
00370                         
00371                 }
00372 
00374                 const IBasicInterface *createInstance() const 
00375                 {
00376                         return (const IBasicInterface *)_Inst->newInstance();
00377                 }
00378                 
00380                 const IBasicType *clone() const
00381                 {
00382                          IBasicType *x = new CSelfClassCFactory(*_Inst);
00383                          return x;
00384                 }
00385                 
00387                 const IBasicType *newInstance() const
00388                 {
00389                         return clone();
00390                 }               
00391                 
00393                 void getDebugString(char *text) const
00394                 {
00395                         sprintf(text,"CSelfClassCFactory sur l'interface %s",(const char *)_Inst->getType());
00396                 }               
00397 
00399                 const CIdentType &getType() const
00400                 {
00401                         return _Inst->getType();
00402                 }
00403 
00405                 void setInst(const IBasicInterface &a)
00406                 {
00408                         _Inst->release();
00410                         _Inst = (IBasicInterface *)a.newInstance();
00411                 }
00412 
00414                 virtual const IBasicInterface *getClass() const
00415                 {
00416                         return _Inst;
00417                 }
00418                 
00420                 CSelfClassCFactory &operator = (const IBasicInterface &inst)
00421                 {
00422                         _Inst->release();
00423                         _Inst = (IBasicInterface *)inst.clone();
00424                         return *this;
00425                 }
00426 
00427                         
00428                 ~CSelfClassCFactory()
00429                 {
00430                         _Inst->release();
00431                 }
00432         };
00433         
00434 
00441         class IIO: public IBasicInterface
00442         {
00443         public:
00444                 IIO()
00445                 {
00446                 }
00447                 virtual void Echo(char *CStringType, ...) = 0;
00448                 virtual char *InPut() = 0;
00449                 virtual ~IIO()
00450                 {
00451                 }
00452         };      
00453 
00454         
00461         class CIdentTypeAlloc
00462         {       
00463         private:                
00464                 CIdentType *_Id;
00465 
00466         public:
00468                 CIdentTypeAlloc(NLMISC::IStream &is):_Id(NULL){load(is);}
00469                 CIdentTypeAlloc():_Id(NULL){}
00470 
00472                 void load(NLMISC::IStream &is);
00473 
00475                 const IBasicInterface *allocClass()
00476                 {
00477                         if(_Id != NULL) return _Id->allocClass();
00478                         else return NULL;
00479                 }               
00480 
00481                 ~CIdentTypeAlloc()
00482                 {
00483                         if(_Id != NULL) delete _Id;
00484                 }
00485 
00487                 virtual void serial(NLMISC::IStream     &f) throw(NLMISC::EStream);             
00488         };
00489         
00490 
00492 
00493 
00494         inline NLMISC::IStream &operator >> (NLMISC::IStream &is, CIdentTypeAlloc &o)
00495         {
00496                 o.load(is);
00497                 return is;
00498         }
00499 
00501         NLMISC::IStream &operator << (NLMISC::IStream &os, CIdentType &o);      
00502 
00504         inline NLMISC::IStream &operator << (NLMISC::IStream &os, IBasicInterface &o)
00505         {
00506                 o.save(os);
00507                 return os;
00508         }
00509 
00511         inline NLMISC::IStream &operator >> (NLMISC::IStream &is, IBasicInterface &o)
00512         {
00513                 o.load(is);
00514                 return is;
00515         }       
00517 }
00518 #endif