Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phObjectList.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission : 
00003         Realtime Vision Processing System
00004     
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008     
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #include <phObject.h>
00033 #include <phMutex.h>
00034 #include <phList.h>
00035 
00036 #include <phObjectList.h>
00037 
00038 #include <phError.h>
00039 #include <phMemory.h>
00040 #include <phPrint.h>
00041 
00042 /* ---------------------------------------------------------------------- */
00043 /* ---------------------------------------------------------------------- */
00044 phObjectNode::phObjectNode()
00045 {
00046     phFUNCTION("phObjectNode::phObjectNode")
00047     int locked = 0;
00048 
00049     phTHIS_LOOSE_LOCK(locked);
00050 
00051     setName("phObjectNode");
00052     this->m_obj = NULL;
00053     this->m_autoDelete = 0;
00054     
00055     phTHIS_LOOSE_UNLOCK(locked);
00056 }
00057 
00058 /* ---------------------------------------------------------------------- */
00059 phObjectNode::phObjectNode(phObject *pobj, int autoDelete_on)
00060 {
00061     phFUNCTION("phObjectNode::phObjectNode(phObject *pobj,int autoDelete_on)")
00062     int locked = 0;
00063 
00064     phTHIS_LOOSE_LOCK(locked);
00065     
00066     setName("phObjectNode");
00067     this->m_obj = pobj;
00068     this->m_autoDelete = autoDelete_on;
00069 
00070     phTHIS_LOOSE_UNLOCK(locked);
00071 }
00072 
00073 /* ---------------------------------------------------------------------- */
00074 phObjectNode::~phObjectNode()
00075 {
00076     phFUNCTION("phObjectNode::~phObjectNode")
00077     int locked = 0;
00078 
00079     phTHIS_LOOSE_LOCK(locked);
00080 
00081     if (this->m_autoDelete)
00082     {
00083         phDelete( this->m_obj );
00084         this->m_obj = NULL;
00085     }
00086 
00087     phTHIS_LOOSE_UNLOCK(locked);
00088 }
00089 
00090 /* ---------------------------------------------------------------------- */
00091 int phObjectNode::setObject( phObject *obj )
00092 {
00093     phFUNCTION("phObjectNode::setObject")
00094     int locked = 0;
00095 
00096     phTHIS_LOOSE_LOCK(locked);
00097 
00098     this->m_obj = obj;
00099 
00100     phTHIS_LOOSE_UNLOCK(locked);
00101      
00102     return phSUCCESS;
00103 }
00104 
00105 /* ---------------------------------------------------------------------- */
00106 phObject *phObjectNode::getObject()
00107 {
00108     phFUNCTION("phObjectNode::getObject")
00109     int locked = 0;
00110 
00111     phObject *obj = NULL;
00112 
00113     phTHIS_LOOSE_LOCK(locked);
00114 
00115     obj = this->m_obj;
00116 
00117     phTHIS_LOOSE_UNLOCK(locked);
00118      
00119     return obj;
00120 }
00121 
00122 /* ---------------------------------------------------------------------- */
00123 phObjectNode *phObjectNode::getNextObject() 
00124 { 
00125     return (phObjectNode *)phListNode::getNext(); 
00126 }
00127 
00128 /* ---------------------------------------------------------------------- */
00129 phObjectNode *phObjectNode::getPreviousObject() 
00130 { 
00131     return (phObjectNode *)phListNode::getPrevious(); 
00132 }
00133 
00134 /* ---------------------------------------------------------------------- */
00135 int phObjectNode::enableAutoDelete()
00136 {
00137     phFUNCTION("phObjectNode::enableAutoDelete")
00138     int locked = 0;
00139 
00140     phTHIS_LOOSE_LOCK(locked);
00141 
00142     this->m_autoDelete = 1;
00143 
00144     phTHIS_LOOSE_UNLOCK(locked);
00145 
00146     return phSUCCESS;
00147 }
00148 
00149 /* ---------------------------------------------------------------------- */
00150 int phObjectNode::disableAutoDelete()
00151 {
00152     phFUNCTION("phObjectNode::disableAutoDelete")
00153     int locked = 0;
00154 
00155     phTHIS_LOOSE_LOCK(locked);
00156 
00157     this->m_autoDelete = 0;
00158 
00159     phTHIS_LOOSE_UNLOCK(locked);
00160 
00161     return phSUCCESS;
00162 }
00163 
00164 
00165 
00166 /* ---------------------------------------------------------------------- */
00167 /* ---------------------------------------------------------------------- */
00168 phObjectList::phObjectList()
00169 {
00170     phFUNCTION("phObjectList::phObjectList")
00171     int locked = 0;
00172     
00173     phTHIS_LOOSE_LOCK(locked);
00174     
00175     setName("phObjectList");
00176     
00177     phTHIS_LOOSE_UNLOCK(locked);
00178 }
00179 
00180 /* ---------------------------------------------------------------------- */
00181 phObjectList::~phObjectList()
00182 {
00183     phFUNCTION("phObjectList::~phObjectList")
00184     
00185 }
00186 /* ---------------------------------------------------------------------- */
00187 phObjectNode *phObjectList::findObject( phObject *pobj )
00188 {
00189     phFUNCTION("phObjectList::findObject")
00190     int locked = 0;
00191 
00192     phObjectNode *node = NULL;
00193 
00194     phTHIS_LOOSE_LOCK(locked);
00195 
00196     node = (phObjectNode *)this->m_head;
00197 
00198     while (node != NULL)
00199     {
00200         if (node->getObject() == pobj)
00201         {
00202             /* found the match */
00203             goto success;
00204         }
00205         node = node->getNextObject();
00206     }
00207 
00208     /* didn't find the match */
00209     node = NULL;
00210 
00211     /* jump to here: saves code, unlocks and returns the node match */
00212 success:
00213     phTHIS_LOOSE_UNLOCK(locked);
00214 
00215     return node;
00216 }
00217 
00218 /* ---------------------------------------------------------------------- */
00219 phObjectNode *phObjectList::insertObject( phObject *pobj, uint32_t index  )
00220 {
00221     phFUNCTION("phList::insertObject")
00222     int locked = 0;
00223     phObjectNode *new_node = NULL;
00224 
00225     if (pobj == NULL) return NULL;
00226 
00227     phTHIS_LOCK(locked);
00228 
00229     new_node = new phObjectNode( pobj );
00230     phCHECK_NULLPTR(new_node,"new","new phDataNode");
00231 
00232     rc = this->phList::insert( new_node, index );
00233     phCHECK_RC(rc,NULL,"this->insert(new_node)");
00234     
00235     phTHIS_UNLOCK(locked);
00236 
00237     return new_node;
00238 error:
00239     phTHIS_ERROR_UNLOCK(locked);
00240 
00241     return NULL;
00242 }
00243 /* ---------------------------------------------------------------------- */
00244 phObject * phObjectList::removeObject( phObjectNode   *node )
00245 {
00246     phFUNCTION("phObjectList::removeObject(phObject *node)")
00247     int locked = 0;
00248     phObject *pobj = NULL;
00249 
00250     if (node == NULL) return NULL;
00251     
00252     phTHIS_LOOSE_LOCK(locked);
00253 
00254     rc = node->lock();
00255     phPRINT_RC(rc,NULL,"node->lock()");
00256     
00257     pobj = node->getObject();
00258     
00259     /* int phList::remove( phListNode *pobj ); */
00260     rc = this->phList::remove( node );
00261     /* Don't fail; we have the pointer; no need to return NULL */
00262     phPRINT_RC(rc, NULL, "this->phList::remove( node );");
00263 
00264     rc = node->setObject(NULL);
00265     phPRINT_RC(rc,NULL,"rc = node->setObject(NULL);");
00266    
00267     phDelete(node);
00268     
00269     phTHIS_LOOSE_UNLOCK(locked);
00270 
00271     return pobj;
00272 }
00273  
00274 /* ---------------------------------------------------------------------- */
00275 phObject * phObjectList::removeObject( phObject   *obj )
00276 {
00277     phFUNCTION("phObjectList::removeObject( phObject *obj )")
00278     int locked = 0;
00279 
00280     phObjectNode *node = NULL;
00281     phObject     *robj = NULL;
00282 
00283     phTHIS_LOCK(locked);
00284 
00285     node = this->findObject( obj );
00286     /* no need to complain heree */
00287     if (node == NULL) goto error;
00288 
00289     robj = this->removeObject( node );
00290     /* complain, cause this would be an error */
00291     phCHECK_NULLPTR(robj, NULL, "this->removeObject( node );" );
00292             
00293     phTHIS_UNLOCK(locked);
00294 
00295     return robj;
00296 error:
00297     phTHIS_ERROR_UNLOCK(locked);
00298 
00299     return NULL;
00300 }   
00301 
00302 /* ---------------------------------------------------------------------- */
00303 phObjectNode * phObjectList::enqueueObject( phObject *pobj )
00304 {
00305     phFUNCTION("phObjectList::enqueueObject")
00306     int locked = 0;
00307     phObjectNode *rptr = NULL;
00308 
00309     if (pobj == NULL) return NULL;
00310 
00311     phTHIS_LOCK(locked);
00312 
00313     rptr = this->phObjectList::insertObject( pobj );
00314     phCHECK_NULLPTR(rptr,NULL,"this->phObjectList::insertObject(pobj)");
00315     
00316     phTHIS_UNLOCK(locked);
00317 
00318     return rptr;
00319 error:
00320     phTHIS_ERROR_UNLOCK(locked);
00321 
00322     return NULL;
00323 }
00324 
00325 /* ---------------------------------------------------------------------- */
00326 phObject * phObjectList::dequeueObject( )
00327 {
00328     phFUNCTION("phObjectList::dequeueObject")
00329     int locked = 0;
00330 
00331     phObjectNode *node = NULL;
00332     phObject *pobj = NULL;
00333 
00334     phTHIS_LOCK(locked);
00335 
00336     node = (phObjectNode *)this->phList::dequeue();
00337     phCHECK_NULLPTR(node, NULL, "this->phList::dequeue()");
00338 
00339     rc = node->lock();
00340     phPRINT_RC(rc,NULL,"node->lock()");
00341     
00342     pobj = node->getObject();
00343     
00344     phDelete(node);   
00345     
00346     phTHIS_UNLOCK(locked);
00347 
00348     return pobj;
00349 error:
00350     phTHIS_ERROR_UNLOCK(locked);
00351 
00352     return NULL;
00353 }
00354     
00355 /* ---------------------------------------------------------------------- */
00356 phObjectNode * phObjectList::pushObject( phObject *pobj )
00357 {
00358     phFUNCTION("phObjectList::pushObject")
00359     int locked = 0;
00360     phObjectNode *rptr = NULL;
00361 
00362     if (pobj == NULL) return NULL;
00363 
00364     phTHIS_LOCK(locked);
00365 
00366     rptr = this->phObjectList::insertObject( pobj, this->length() );
00367     phCHECK_NULLPTR(rptr,NULL,"this->phObjectList::insertObject(new_node,this->length())");
00368     
00369     phTHIS_UNLOCK(locked);
00370 
00371     return rptr;
00372 error:
00373     phTHIS_ERROR_UNLOCK(locked);
00374 
00375     return NULL;
00376 }
00377 
00378 /* ---------------------------------------------------------------------- */
00379 phObject * phObjectList::popObject( )
00380 {
00381     return this->dequeueObject( );
00382 }
00383     
00384 /* ---------------------------------------------------------------------- */
00385 phObjectNode * phObjectList::unshiftObject( phObject *pobj )
00386 {
00387     return this->enqueueObject( pobj );
00388 }
00389 
00390 /* ---------------------------------------------------------------------- */
00391 phObject * phObjectList::shiftObject( )
00392 {
00393     phFUNCTION("phObjectList::shiftObject")
00394     int locked = 0;
00395 
00396     phObjectNode *node = NULL;
00397     phObject *pobj = NULL;
00398 
00399     phTHIS_LOCK(locked);
00400 
00401     node = (phObjectNode *)this->phList::shift();
00402     phCHECK_NULLPTR(node, NULL, "this->phList::shift()");
00403 
00404     rc = node->lock();
00405     phPRINT_RC(rc,NULL,"node->lock()");
00406     
00407     pobj = node->getObject();
00408     
00409     phDelete(node); 
00410     
00411     phTHIS_UNLOCK(locked);
00412 
00413     return pobj;
00414 error:
00415     phTHIS_ERROR_UNLOCK(locked);
00416 
00417     return NULL;
00418 }
00419 
00420 /* ---------------------------------------------------------------------- */
00421 phObjectNode *phObjectList::getHeadObject()
00422 {
00423     return (phObjectNode *)phList::getHead(); 
00424 }
00425 
00426 /* ---------------------------------------------------------------------- */
00427 phObjectNode *phObjectList::getTailObject()
00428 {
00429     return (phObjectNode *)phList::getTail(); 
00430 }




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:02 2007 for phission by  doxygen 1.4.4