# 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  

var.cpp

Go to the documentation of this file.
00001 /* Copyright, 2000 Nevrax Ltd.
00002  *
00003  * This file is part of NEVRAX <MODULE_NAME>.
00004  * NEVRAX <MODULE_NAME> is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2, or (at your option)
00007  * any later version.
00008 
00009  * NEVRAX <MODULE_NAME> is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with NEVRAX <MODULE_NAME>; see the file COPYING. If not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00017  * MA 02111-1307, USA.
00018  */
00019 
00020 #include "nel/ai/logic/var.h"
00021 #include "nel/ai/logic/boolval.h"
00022 
00023 #include "nel/ai/script/compilateur.h"
00024 #include "nel/ai/script/constraint.h"
00025 #include "nel/ai/script/type_def.h"
00026 
00027 
00028 namespace NLAILOGIC
00029 {
00030         using namespace NLAIAGENT;
00031         
00032 
00033         IBaseVar::IBaseVar(const IVarName &n)
00034         {
00035                 _Name = (IVarName *) n.clone();
00036         }
00037 
00038         IBaseVar::IBaseVar(const char *n)       
00039         {
00040                 _Name = new CStringVarName(n);
00041         }
00042 
00043         IBaseVar::IBaseVar(const IBaseVar &cp)
00044         {
00045                 _Name = (IVarName *) cp._Name->clone();
00046         }
00047 
00048 
00049         tQueue IBaseVar::isMember(const IVarName *className, const IVarName *name, const IObjectIA &param) const
00050         {
00051                 tQueue result = IObjetOp::isMember( className, name, param);
00052 
00053                 if ( result.size() )
00054                         return result;
00055 
00056                 if ( *name == CStringVarName("set") )
00057                 {
00058                         IObjectIA *op_type = (IObjectIA *) new NLAISCRIPT::COperandVoid();
00059                         result.push( CIdMethod(IObjetOp::getMethodIndexSize() + 1,0.0,NULL, op_type ) );
00060                 }
00061                 return result;
00062         }
00063 
00064         sint32 IBaseVar::getMethodIndexSize() const
00065         {
00066                 return IObjetOp::getMethodIndexSize() + 1;
00067         }
00068 
00069         // Executes a method from its index id and with its parameters
00070         IObjectIA::CProcessResult IBaseVar::runMethodeMember(sint32 id, IObjectIA *param)
00071         {
00072 
00073                 if ( id <= IObjetOp::getMethodIndexSize() )
00074                         return IObjetOp::runMethodeMember(id, param);
00075 
00076                 IObjetOp *x = (IObjetOp *) ((IBaseGroupType *) param)->getFront();
00077                 ((IBaseGroupType *)param)->popFront();
00078 
00079 #ifdef NL_DEBUG
00080                 std::string buf;
00081                 x->getDebugString(buf);
00082                 getDebugString(buf);
00083 #endif
00084 
00085                 if ( id == ( IObjetOp::getMethodIndexSize() + 1 ) )
00086                 {
00087                         setValue( x );
00088                         IObjectIA::CProcessResult r;
00089                         r.ResultState =  processIdle;
00090                         r.Result = NULL;
00091                         return r;
00092                 }
00093 
00094                 // TODO: throw exception....
00095                 return IObjectIA::CProcessResult();
00096         }
00097 
00098         void IBaseVar::setName(IVarName &name)
00099         {
00100                 if ( _Name )
00101                         _Name->release();
00102 
00103                 _Name = (IVarName *) name.clone();
00104         }
00105 
00106         IVarName &IBaseVar::getName() const
00107         {
00108                 return *_Name;
00109         }
00110 
00111         IBaseVar::~IBaseVar()
00112         {
00113                 if ( _Name )
00114                         _Name->release();
00115         }
00116 
00117         CVar::CVar(const IVarName &n, IObjetOp *value) : IBaseVar( n )
00118         {
00119                 _Value = value;
00120 
00121                 if ( _Value )
00122                         _Value->incRef();
00123         }
00124 
00125         CVar::CVar(const char *n, IObjetOp *value) : IBaseVar( n )
00126         {
00127                 _Value = value;
00128 
00129                 if ( _Value )
00130                         _Value->incRef();
00131         }
00132 
00133 
00134         CVar::CVar(const CVar &cp) : IBaseVar( cp )
00135         {
00136                 _Value = cp._Value;
00137                 if ( _Value ) 
00138                         _Value->incRef();
00139         } 
00140 
00141         CVar::~CVar()
00142         {
00143                 if ( _Value )
00144                         _Value->release();
00145         }
00146 
00147         void CVar::setValue(IObjetOp *obj)
00148         {
00149                 if ( _Value )
00150                         _Value->release();
00151 
00152                 _Value = obj;
00153                 _Value->incRef();
00154         }
00155 
00156         IObjetOp *CVar::getValue() const
00157         {
00158                 return _Value;
00159         }
00160                 
00161         const NLAIC::IBasicType *CVar::clone() const
00162         {
00163                 NLAIC::IBasicInterface *m = new CVar( *this );
00164                 return m;
00165         }
00166 
00167         const NLAIC::IBasicType *CVar::newInstance() const
00168         {
00169                 return clone();
00170         }
00171 
00172         void CVar::save(NLMISC::IStream &os)
00173         {                                       
00174                 IObjetOp::save( os );
00175                 _Name->save( os );
00176                 if ( _Value ) 
00177                 {
00178                         bool val = true;
00179                         os.serial( (bool &) val );
00180                         os.serial( (NLAIC::CIdentType &) _Value->getType() );
00181                         _Value->save(os);
00182                 }
00183                 else
00184                 {
00185                         bool val = false;
00186                         os.serial( (bool &) val );
00187                 }
00188         }
00189 
00190         void CVar::load(NLMISC::IStream &is)
00191         {
00192                 if ( _Value )
00193                         _Value->release();
00194                 IObjetOp::load(is);
00195                 _Name->load(is);        
00196                 NLAIC::CIdentTypeAlloc id;
00197                 is.serial( id );
00198                 _Value = (CVar *)id.allocClass();
00199                 _Value->load(is);       
00200         }
00201 
00202         void CVar::getDebugString(std::string &text) const
00203         {
00204                 std::string buf;
00205                 if ( _Value )
00206                         _Value->getDebugString( buf );
00207                 else
00208                         buf = "<undefined>";
00209                 text += NLAIC::stringGetBuild("CVar<%s> = %s <%8x>\n",_Name->getString(), buf.c_str() , this);
00210         }
00211 
00212         bool CVar::isEqual(const CVar &a) const
00213         {
00214                 return ( (*(IObjectIA *)a._Value) == (*_Value) );
00215         }
00216 
00217         const IObjectIA::CProcessResult &CVar::run()
00218         {                       
00219                 return IObjectIA::ProcessRun;
00220         }
00221 
00222         bool CVar::isEqual(const IBasicObjectIA &a) const
00223         {
00224                 CVar &tmp = (CVar &) a;
00225 
00226                 if( !_Value || !tmp._Value )
00227                         return false;
00228 
00229                 return ( (*(IObjectIA *)tmp._Value) == (*_Value) );
00230         }
00231 
00232         const NLAIC::CIdentType &CVar::getType() const
00233         {
00234                 return IdVar;
00235         }
00236 
00237         bool CVar::operator==(IBaseVar *var)
00238         {
00239                 return ( (CVar *) var )->_Value == _Value ;
00240         }
00241 
00242         bool CVar::unify(IBaseVar *y, bool assign)
00243         {       
00244                 IObjetOp *x_val = _Value;
00245                 IObjetOp *y_val = y->getValue();
00246                 
00247                 if ( !x_val && !y_val )
00248                         return true;
00249 
00250                 if ( x_val && y_val )
00251                         if ( (*x_val) == (*y_val) )
00252                                 return true;
00253                         else
00254                                 return false;
00255                 else
00256                 {
00257                         if ( y_val && assign )
00258                         {
00259                                 _Value = y_val;
00260                                 _Value->incRef();
00261                         }
00262                         return true;
00263                 }
00264         }
00265 
00266         bool CVar::unify(IObjetOp *y_val, bool assign) {
00267                 IObjetOp *x_val = _Value;
00268                 
00269                 if ( !x_val && !y_val )
00270                         return true;
00271 
00272                 if ( x_val && y_val )
00273                         if ( (*x_val) == (*y_val) )
00274                                 return true;
00275                         else
00276                                 return false;
00277                 else
00278                 {
00279                         if ( y_val && assign )
00280                         {
00281                                 _Value = y_val;
00282                                 _Value->incRef();
00283                         }
00284                         return true;
00285                 }
00286         }
00287 
00288         bool CVar::isTrue() const
00289         {
00290                 return false;
00291         }
00292 
00293         IObjetOp *CVar::operator == (IObjetOp &a) const
00294         {
00295                 return new CBoolType( ((CVar &)a)._Value == _Value  );
00296         }
00297 }