arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

AlgObj.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     6-20-03
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2003 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 
00018 ///AlgObj.cpp
00019 /// simian core
00020 
00021 #include "AlgObj.h"
00022 #include "DataObj.h"
00023 #include "SourceObj.h"
00024 
00025 ///////////////////////////////////////////////////////////////////////////
00026 /// Construction
00027 ///////////////////////////////////////////////////////////////////////////
00028 AlgObj::AlgObj()
00029 :AlgObjIF(),
00030  _inputs(0),
00031  _nInputs(0)
00032 {
00033 
00034 }
00035 
00036 AlgObj::AlgObj(unsigned int numInputs)
00037 :AlgObjIF(),
00038  _inputs(0),
00039  _nInputs(0)
00040 {
00041    setNumInputs(numInputs);
00042 }
00043 
00044 
00045 AlgObj::~AlgObj()
00046 {
00047    if(_inputs)
00048       delete[] _inputs;
00049 }
00050 
00051 ///////////////////////////////////////////////////////////////////////////
00052 ///////////////////////////////////////////////////////////////////////////
00053 /// Execute Interface
00054 ///////////////////////////////////////////////////////////////////////////
00055 ///////////////////////////////////////////////////////////////////////////
00056 
00057 ///////////////////////////////////////////////////////////////////////////
00058 /// Execute if anything changed
00059 ///////////////////////////////////////////////////////////////////////////
00060 void AlgObj::execute()
00061 {
00062    //derr("execute()");
00063 
00064    /// if update was called, this is a duplicate,
00065    /// Let's just assume we don't have a millon inputs
00066    if(!checkInputs())
00067    {
00068       derr("execute(), inputs not ready!!");
00069    }
00070 
00071    //derr("execute(), after check inputs");
00072 
00073    //if(_updating)/// TODO: debug loop check
00074    //{
00075    //   derr("execute(), loop detected");
00076    //}
00077    
00078    if(inputsModified()) _modified = true;
00079 
00080    if(_modified)
00081    {
00082       //derr("execDef->");
00083       execDef();
00084       _modified = false;
00085    }
00086 }
00087 
00088 ///////////////////////////////////////////////////////////////////////////
00089 /// Execute no matter what!
00090 ///////////////////////////////////////////////////////////////////////////
00091 void AlgObj::force()
00092 {
00093    if(!checkInputs())
00094    {
00095       derr("force(), inputs not ready, deffering execution");
00096       _modified = true;
00097       return;
00098    }
00099    execDef();
00100    _modified = false;
00101    _forward = false;
00102 }
00103 
00104 ///////////////////////////////////////////////////////////////////////////
00105 ///////////////////////////////////////////////////////////////////////////
00106 /// Update Interface
00107 ///////////////////////////////////////////////////////////////////////////
00108 ///////////////////////////////////////////////////////////////////////////
00109 
00110 ///////////////////////////////////////////////////////////////////////////
00111 /// Update Data object
00112 ///////////////////////////////////////////////////////////////////////////
00113 void AlgObj::update_(DataObjSP d)
00114 {
00115    /// must mark forward!!!
00116    if(!_forward) return;
00117    _modified = true;
00118    if(!checkInputs())
00119    {
00120       //derr("update_(DataObjSP), inputs not ready, differing exectution");
00121       return;
00122    }
00123    execute();
00124    /// not forward no more
00125    _forward = false;
00126 }
00127 
00128 ///////////////////////////////////////////////////////////////////////////
00129 ///////////////////////////////////////////////////////////////////////////
00130 /// Protected Member Functions
00131 ///////////////////////////////////////////////////////////////////////////
00132 ///////////////////////////////////////////////////////////////////////////
00133 
00134 ///////////////////////////////////////////////////////////////////////////
00135 /// check if we have modified inputs
00136 ///////////////////////////////////////////////////////////////////////////
00137 bool AlgObj::inputsModified()
00138 {
00139    for(int i=0; i<_nInputs; ++i)
00140    {
00141       if(!_inputs[i].isNull())
00142       {  
00143          if(_inputs[i]->isModified())
00144          {
00145             derr("inputs modified()");
00146             return true;
00147          }
00148       }
00149    }
00150    return false;
00151 }
00152 
00153 ///////////////////////////////////////////////////////////////////////////
00154 /// Check inputs
00155 ///////////////////////////////////////////////////////////////////////////
00156 bool AlgObj::checkInputs()
00157 {
00158   // check each input
00159    for(int i=0; i<_nInputs; ++i) 
00160    {
00161       // null pointers are bad!
00162       if(!_inputs[i].isNull()) 
00163       {
00164          // see if the request was successfull
00165          if(!_inputs[i]->request_()) 
00166          {
00167             return false;  // request failed
00168          }
00169       }
00170       else // null pointer, report it
00171       {
00172          derr("request(), null DataObj encountered");
00173       }
00174    }
00175    /// everything checked out so we must be ok
00176    return true;
00177 }
00178 
00179 ///////////////////////////////////////////////////////////////////////////
00180 /// Add Input
00181 ///////////////////////////////////////////////////////////////////////////
00182 void AlgObj::addInput(DataObjSP d)
00183 {
00184    if(d.isNull())
00185    {
00186       derr("addInput(), attempting to add NULL input");
00187       return;
00188    }
00189    setInputN(_nInputs, d);
00190 }
00191 
00192 ///////////////////////////////////////////////////////////////////////////
00193 /// Del Input
00194 ///////////////////////////////////////////////////////////////////////////
00195 void AlgObj::delInput(DataObjSP d)
00196 {
00197    if(!isInput(d))
00198    {
00199       derr("delInput(), DataObject is not input");
00200       return;
00201    }
00202    
00203    DataObjSP *objNew = new DataObjSP[_nInputs - 1];
00204    int idx = 0;
00205    for(int i=0; i<_nInputs; ++i)
00206    {
00207       if(d != _inputs[i])
00208       {
00209          objNew[idx] = _inputs[i];
00210          ++idx;
00211       }
00212    }
00213 
00214    d->delConsumer(SimModObjSP(this));
00215 
00216    delete[] _inputs;
00217    _inputs = objNew;
00218    --_nInputs;
00219 }
00220 
00221 ///////////////////////////////////////////////////////////////////////////
00222 /// isInput
00223 ///////////////////////////////////////////////////////////////////////////
00224 bool AlgObj::isInput(const DataObjSP d) const
00225 {
00226    if(d.isNull()) return false;
00227    for(int i=0; i<_nInputs; ++i)
00228    {
00229       if(d == _inputs[i]) return true;
00230    }
00231    return false;
00232 }
00233 
00234 ///////////////////////////////////////////////////////////////////////////
00235 /// SetNumInputs
00236 ///////////////////////////////////////////////////////////////////////////
00237 void AlgObj::setNumInputs(int nInputs)
00238 {
00239    if(nInputs == _nInputs) /// nothing to do
00240    {
00241       return;
00242    }
00243    /// create new input array
00244    DataObjSP *inputsNew = new DataObjSP[nInputs];
00245    
00246    /// copy old inputs
00247    for(int i=0; i<nInputs && i<_nInputs; ++i)
00248    {
00249       inputsNew[i] = _inputs[i];
00250    }
00251 
00252    /// nuke old input array
00253    delete[] _inputs;
00254 
00255    /// set new array
00256    _inputs = inputsNew;
00257    _nInputs = nInputs;
00258 
00259 }
00260 
00261 ///////////////////////////////////////////////////////////////////////////
00262 /// Set Input "N"
00263 ///////////////////////////////////////////////////////////////////////////
00264 void AlgObj::setInputN(int inNum, DataObjSP d)
00265 {
00266    if(d.isNull())
00267    {
00268       derr("setInputN(), attempting to set a null input!!");
00269       return;
00270    }
00271    if(inNum >= _nInputs)
00272    {
00273       setNumInputs(_nInputs + 1);
00274    }
00275 
00276    _inputs[inNum] = d;
00277    d->addConsumer(SimModObjSP(this));
00278 }
00279 
00280 ///////////////////////////////////////////////////////////////////////////
00281 /// Get Input "N"
00282 ///////////////////////////////////////////////////////////////////////////
00283 DataObjIF *AlgObj::getInputN(int inNum)
00284 {
00285    if(inNum >= _nInputs)
00286    {
00287       derr("getInputN(), index number greater than number of inputs");
00288       return 0;
00289    }
00290    return _inputs[inNum].getPtr();
00291 }
00292 
00293 

Send questions, comments, and bug reports to:
jmk