00001 #include <phPowerData.h> 00002 #include <phbase.h> 00003 00004 /* ------------------------------------------------------------------------ */ 00005 phPowerData::phPowerData() 00006 { 00007 phFUNCTION("phPowerData::phPowerData") 00008 int locked = 0; 00009 int rwlocked = 0; 00010 00011 phTHIS_LOOSE_LOCK(locked); 00012 phTHIS_WRITELOCK_LOOSE(rwlocked); 00013 00014 this->setName("phPowerData"); 00015 00016 this->m_charge = 0; 00017 00018 phTHIS_RWUNLOCK_LOOSE(rwlocked); 00019 phTHIS_LOOSE_UNLOCK(locked); 00020 } 00021 00022 /* ------------------------------------------------------------------------ */ 00023 int phPowerData::copy( phObject *copyto_object ) 00024 { 00025 phFUNCTION("phPowerData::copy") 00026 00027 if (this->isNamed(((phObject *)copyto_object)->getName())) 00028 { 00029 phPowerData *copyto_data = (phPowerData *)copyto_object; 00030 return copyto_data->copyData(*this); 00031 } 00032 /* Otherwise I'll assume they are incompatible */ 00033 else 00034 { 00035 phPRINT_RC(-1,NULL, "Invalid object pairing in update." 00036 "[ this:%s != target:%s ]", 00037 this->getName(), ((phObject *)copyto_object)->getName()); 00038 return phFAIL; 00039 } 00040 } 00041 00042 /* ------------------------------------------------------------------------ */ 00043 int phPowerData::swap( phObject *object ) 00044 { 00045 phFUNCTION("phPowerData::swap") 00046 00047 if (this->isNamed(((phObject *)object)->getName())) 00048 { 00049 phPowerData *data = (phPowerData *)object; 00050 00051 return data->swapData(*this); 00052 } 00053 /* Otherwise I'll assume they are incompatible */ 00054 else 00055 { 00056 phPRINT_RC(-1,NULL, "Invalid object pairing in update." 00057 "[ this:%s != target:%s ]", 00058 this->getName(), ((phObject *)object)->getName()); 00059 return phFAIL; 00060 } 00061 } 00062 00063 /* ------------------------------------------------------------------------ */ 00064 int phPowerData::copyData( phPowerData ©from ) 00065 { 00066 phFUNCTION("phPowerData::copyData") 00067 int rwlocked = 0; 00068 int other_rwlocked = 0; 00069 00070 phTHIS_WRITELOCK(rwlocked); 00071 phREADLOCK(copyfrom,other_rwlocked); 00072 00073 this->m_charge = copyfrom.m_charge; 00074 00075 rc = this->notify(); 00076 phPRINT_RC(rc,NULL,"this->notify()"); 00077 00078 phRWUNLOCK(copyfrom,other_rwlocked); 00079 phTHIS_RWUNLOCK(rwlocked); 00080 00081 return phSUCCESS; 00082 error: 00083 phRWUNLOCK_ERROR(copyfrom,other_rwlocked); 00084 phTHIS_RWUNLOCK_ERROR(rwlocked); 00085 00086 return phFAIL; 00087 } 00088 00089 /* ------------------------------------------------------------------------ */ 00090 int phPowerData::swapData( phPowerData &data ) 00091 { 00092 phFUNCTION("phPowerData::swapData") 00093 int rwlocked = 0; 00094 int other_rwlocked = 0; 00095 double t_charge = 0; 00096 00097 phTHIS_WRITELOCK(rwlocked); 00098 00099 if (this->isSwapEnabled()) 00100 { 00101 phWRITELOCK(data,other_rwlocked); 00102 00103 t_charge = data.m_charge; 00104 00105 data.m_charge = this->m_charge; 00106 00107 this->m_charge = t_charge; 00108 00109 rc = this->notify(); 00110 phPRINT_RC(rc,NULL,"this->notify()"); 00111 00112 rc = data.notify(); 00113 phPRINT_RC(rc,NULL,"data.notify()"); 00114 00115 phRWUNLOCK(data,other_rwlocked); 00116 } 00117 else 00118 { 00119 rc = this->copyData(data); 00120 phCHECK_RC(rc,NULL,"this->copyData(data)"); 00121 } 00122 phTHIS_RWUNLOCK(rwlocked); 00123 00124 return phSUCCESS; 00125 error: 00126 phRWUNLOCK_ERROR(data,other_rwlocked); 00127 phTHIS_RWUNLOCK_ERROR(rwlocked); 00128 00129 return phFAIL; 00130 } 00131 00132 /* ------------------------------------------------------------------------ */ 00133 int phPowerData::reset() 00134 { 00135 phFUNCTION("phPowerData::reset") 00136 int rwlocked = 0; 00137 00138 phTHIS_WRITELOCK(rwlocked); 00139 00140 this->m_charge = 0; 00141 00142 phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL); 00143 } 00144 00145 00146 /* ------------------------------------------------------------------------ */ 00147 int phPowerData::set(double charge) 00148 { 00149 phFUNCTION("phPowerData::set") 00150 int rwlocked = 0; 00151 00152 phTHIS_WRITELOCK(rwlocked); 00153 00154 this->m_charge = charge; 00155 00156 rc = this->notify(); 00157 phPRINT_RC(rc,NULL,"this->notify()"); 00158 00159 phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL); 00160 } 00161 00162 /* ------------------------------------------------------------------------ */ 00163 int phPowerData::get(double *charge) 00164 { 00165 phFUNCTION("phPowerData::get") 00166 int rwlocked = 0; 00167 00168 phTHIS_READLOCK(rwlocked); 00169 00170 if (charge != NULL) 00171 *charge = this->m_charge; 00172 00173 phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL); 00174 } 00175 00176 /* ------------------------------------------------------------------------ */ 00177 double phPowerData::getCharge( ) 00178 { 00179 phFUNCTION("phPowerData::getCharge") 00180 int rwlocked = 0; 00181 double retval = 0; 00182 00183 phTHIS_READLOCK(rwlocked); 00184 00185 retval = this->m_charge; 00186 00187 phTHIS_RWUNLOCK_RET(rwlocked,retval,0); 00188 } 00189 00190 /* ------------------------------------------------------------------------ */ 00191 void phPowerData::print_data( ) 00192 { 00193 phFUNCTION("phPowerData::print_data") 00194 int i = 0; 00195 int j = 0; 00196 int rwlocked = 0; 00197 00198 phTHIS_READLOCK_LOOSE(rwlocked); 00199 00200 fprintf(stderr,"----------------------------------------------------\n"); 00201 fprintf(stderr,"%s\n",this->getName()); 00202 fprintf(stderr,"----------------------------------------------------\n"); 00203 fprintf(stderr,"Charge: %0.8f\n",this->m_charge); 00204 fprintf(stderr,"----------------------------------------------------\n"); 00205 00206 phTHIS_RWUNLOCK_LOOSE(rwlocked); 00207 }
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |