[Libphidget-cvs-commits] CVS: libphidget/src/phidget++ CPhidget.cc,1.5,1.6 CPhidget.h,1.4,1.5 CPhidg
Status: Alpha
Brought to you by:
jstrohm
Update of /cvsroot/libphidget/libphidget/src/phidget++ In directory usw-pr-cvs1:/tmp/cvs-serv31521 Modified Files: CPhidget.cc CPhidget.h CPhidgetManager.cc CServo.cc CServoController.cc CUID.cc CUniqueDevice.cc helper.h Log Message: Major changes in how the event model works, it seems to work correctly now. Also added GUARD defines around all functions and methods. This will output a nice stack dump when exceptions occur. Index: CPhidget.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CPhidget.cc,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CPhidget.cc 14 Sep 2002 01:51:31 -0000 1.5 --- CPhidget.cc 16 Sep 2002 06:43:33 -0000 1.6 *************** *** 18,21 **** --- 18,22 ---- _device(CPhidgetManager::getInstance()->getDeviceHandle(uid)) { + METHOD_GUARD; // Ask the phidget manager if we really exist as a device, if not throw an exception *************** *** 23,84 **** throw runtime_error("Phidget doesn't exist."); ! _last_wasAttached = error(phidgetAttached(_device)) == 1; } CPhidget::~CPhidget () { error(phidgetClose(_device)); } int CPhidget::classType() { ! //return(_device->type->deviceClass); return (error(phidgetTypeDeviceClass(error(phidgetType(_device))))); } const char *CPhidget::name() { ! //return(_device->type->name); return (error(phidgetTypeName(error(phidgetType(_device))))); } int CPhidget::vendorID() { ! //return(_device->type->vendorID); return (error(phidgetTypeVendorID(error(phidgetType(_device))))); } int CPhidget::productID() { ! //return(_device->type->productID); return (error(phidgetTypeProductID(error(phidgetType(_device))))); } bool CPhidget::isAttached() { ! //if (_device->attached==1) if (error(phidgetAttached(_device)) == 1) return (true); return (false); } void CPhidget::processEvents() { ! bool temp = isAttached(); ! ! if (!_last_wasAttached && temp) ! onAttach(); ! else if (_last_wasAttached && !temp) ! onDetach(); ! _last_wasAttached = temp; } void CPhidget::onAttach() { } void CPhidget::onDetach() { } --- 24,96 ---- throw runtime_error("Phidget doesn't exist."); ! //_last_wasAttached = error(phidgetAttached(_device)) == 1; ! METHOD_UNGUARD; } CPhidget::~CPhidget () { + METHOD_GUARD; error(phidgetClose(_device)); + METHOD_UNGUARD; } int CPhidget::classType() { ! METHOD_GUARD; return (error(phidgetTypeDeviceClass(error(phidgetType(_device))))); + METHOD_UNGUARD; } const char *CPhidget::name() { ! METHOD_GUARD; return (error(phidgetTypeName(error(phidgetType(_device))))); + METHOD_UNGUARD; } int CPhidget::vendorID() { ! METHOD_GUARD; return (error(phidgetTypeVendorID(error(phidgetType(_device))))); + METHOD_UNGUARD; } int CPhidget::productID() { ! METHOD_GUARD; return (error(phidgetTypeProductID(error(phidgetType(_device))))); + METHOD_UNGUARD; } bool CPhidget::isAttached() { ! METHOD_GUARD; if (error(phidgetAttached(_device)) == 1) return (true); return (false); + METHOD_UNGUARD; } void CPhidget::processEvents() { ! METHOD_GUARD; ! bool is=error(phidgetAttached(_device))==1; ! bool was=error(phidgetWasAttached(_device))==1; ! if (is && !was) onAttach(); ! if (!is && was) onDetach(); ! METHOD_UNGUARD; } void CPhidget::onAttach() { + METHOD_GUARD; + METHOD_UNGUARD; } void CPhidget::onDetach() { + METHOD_GUARD; + METHOD_UNGUARD; } Index: CPhidget.h =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CPhidget.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CPhidget.h 12 Sep 2002 00:24:29 -0000 1.4 --- CPhidget.h 16 Sep 2002 06:43:33 -0000 1.5 *************** *** 23,27 **** { private: ! bool _last_wasAttached; //bool _userConstructed; --- 23,27 ---- { private: ! //bool _last_wasAttached; //bool _userConstructed; Index: CPhidgetManager.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CPhidgetManager.cc,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CPhidgetManager.cc 14 Sep 2002 01:51:31 -0000 1.5 --- CPhidgetManager.cc 16 Sep 2002 06:43:33 -0000 1.6 *************** *** 20,28 **** --- 20,31 ---- void CPhidgetManager::registerDevice(CUniqueDevice *dev) { + METHOD_GUARD; _uniqueDevices.push_back(dev); + METHOD_UNGUARD; } void CPhidgetManager::unregisterDevice(CUniqueDevice *dev) { + METHOD_GUARD; vector <CUniqueDevice *>::iterator i; *************** *** 34,37 **** --- 37,41 ---- throw runtime_error("Can't unregister a device that wasn't registered."); + METHOD_UNGUARD; } *************** *** 70,73 **** --- 74,78 ---- CPhidgetManager::CPhidgetManager () { + METHOD_GUARD; error(phidgetInit(1)); *************** *** 86,93 **** --- 91,100 ---- for (int t=0;t<_phidgets.size();t++) _phidgets[t]->processEvents(); */ + METHOD_UNGUARD; } CPhidgetManager::~CPhidgetManager () { + METHOD_GUARD; /* for (int t=0;t<_phidgets.size();t++) *************** *** 101,108 **** --- 108,117 ---- error(phidgetDeinit()); + METHOD_UNGUARD; } vector <CUID >CPhidgetManager::query(const EDeviceClass tp) { + METHOD_GUARD; set < CUID >devices; *************** *** 139,146 **** --- 148,157 ---- return (temp); + METHOD_UNGUARD; } struct phidget *CPhidgetManager::getDeviceHandle(const CUID &uid) { + METHOD_GUARD; if (uid.id() != -1) *************** *** 158,161 **** --- 169,173 ---- return (NULL); + METHOD_UNGUARD; } *************** *** 174,193 **** bool CPhidgetManager::checkForEvents() { bool found = (error(phidgetEvents()) == 1); // Get the list of phidgets if (found) { - /* - int phidget_count; - struct phidget **phidgets=phidget_get_phidgets(&phidget_count); - - for (int t=0;t<phidget_count;t++) - { - // We open this device - addPossibleNewDevice(phidgets[t]); - } - for (int t=0;t<_phidgets.size();t++) _phidgets[t]->processEvents(); - */ - for (int t = 0; t < _uniqueDevices.size(); t++) _uniqueDevices[t]->processEvents(); --- 186,194 ---- bool CPhidgetManager::checkForEvents() { + METHOD_GUARD; bool found = (error(phidgetEvents()) == 1); // Get the list of phidgets if (found) { for (int t = 0; t < _uniqueDevices.size(); t++) _uniqueDevices[t]->processEvents(); *************** *** 195,202 **** --- 196,205 ---- return (found); + METHOD_UNGUARD; } CUniqueDevice *CPhidgetManager::find(const CUID &uid, bool create) { + METHOD_GUARD; CUniqueDevice *temp; bool isPhidget = false; *************** *** 246,253 **** --- 249,258 ---- // The base phidget should exists and can now create it for us. return (temp->find(uid, create)); + METHOD_UNGUARD; } CPhidget *CPhidgetManager::createPhidget(const CUID &uid) { + METHOD_GUARD; // This device better not exists, we assume that has already been checked. if (uid.id() != -1) *************** *** 260,263 **** --- 265,269 ---- return (sc); + METHOD_UNGUARD; } Index: CServo.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CServo.cc,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CServo.cc 12 Sep 2002 03:02:07 -0000 1.5 --- CServo.cc 16 Sep 2002 06:43:33 -0000 1.6 *************** *** 11,14 **** --- 11,15 ---- #include "CServoController.h" #include "CPhidgetManager.h" + #include "helper.h" /* *************** *** 31,34 **** --- 32,37 ---- _systemManaged(true) { + METHOD_GUARD; + METHOD_UNGUARD; } *************** *** 36,39 **** --- 39,43 ---- _systemManaged(false) { + METHOD_GUARD; // We have to create it this way, because the phidget manager will // be in charge of this servo controller, or return us a servo controller that has already been created. *************** *** 42,56 **** --- 46,65 ---- // Let the controller know about this servo _controller->registerServo(this, uid.id()); + METHOD_UNGUARD; } CServo::~CServo () { + METHOD_GUARD; if (!_systemManaged) _controller->unregisterServo(UID()); + METHOD_UNGUARD; } void CServo::position(float p) { + METHOD_GUARD; _position = p; _controller->update(); + METHOD_UNGUARD; } Index: CServoController.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CServoController.cc,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CServoController.cc 14 Sep 2002 01:51:31 -0000 1.5 --- CServoController.cc 16 Sep 2002 06:43:33 -0000 1.6 *************** *** 15,21 **** #include "helper.h" ! CServoController::CServoController (const CUID &uid):CPhidget (uid), _servoCount(-1), ! _servos(NULL) { // Make sure it was constructed properly if (classType() != LP_SERVO_CONTROLLER) --- 15,24 ---- #include "helper.h" ! CServoController::CServoController (const CUID &uid) : ! CPhidget (uid), ! _servoCount(-1), ! _servos(NULL) { + METHOD_GUARD; // Make sure it was constructed properly if (classType() != LP_SERVO_CONTROLLER) *************** *** 46,53 **** --- 49,58 ---- } } + METHOD_UNGUARD; } CServoController::~CServoController () { + METHOD_GUARD; for (int t = 0; t < _servoCount; t++) if (_servos[t].first) *************** *** 55,70 **** --- 60,79 ---- delete[]_servos; + METHOD_UNGUARD; } CUniqueDevice *CServoController::find(const CUID &uid, bool create) { + METHOD_GUARD; if (uid.serial() == UID().serial() && uid.id() >= 0 && uid.id() < _servoCount) return (servo(uid.id(), create)); return (CPhidget::find(uid)); + METHOD_UNGUARD; } CServo *CServoController::servo(const int id, bool create) { + METHOD_GUARD; if (id < 0 || id >= _servoCount) throw runtime_error("Invalid servo id"); *************** *** 76,83 **** --- 85,94 ---- return (_servos[id].second); + METHOD_UNGUARD; } void CServoController::registerServo(CServo *servo, int id) { + METHOD_GUARD; if (id < 0 || id >= _servoCount) throw runtime_error("Invalid servo id"); *************** *** 88,95 **** --- 99,108 ---- _servos[id].first = false; _servos[id].second = servo; + METHOD_UNGUARD; } void CServoController::unregisterServo(const CUID &uid) { + METHOD_GUARD; if ((uid.id() < 0 && uid.id() >= _servoCount) || uid.serial() != UID().serial()) *************** *** 103,110 **** --- 116,125 ---- _servos[uid.id()].first = true; _servos[uid.id()].second = NULL; + METHOD_UNGUARD; } void CServoController::setServoCount(int num) { + METHOD_GUARD; _servoCount = num; *************** *** 115,118 **** --- 130,134 ---- _servos[t].second = NULL; } + METHOD_UNGUARD; } *************** *** 191,194 **** --- 207,211 ---- void CServoController::update() { + METHOD_GUARD; if (_servoCount) { float p1 = 0, p2 = 0, p3 = 0, p4 = 0; *************** *** 205,208 **** --- 222,226 ---- error(phidgetQuadServo(device(), p1, p2, p3, p4)); } + METHOD_UNGUARD; } Index: CUID.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CUID.cc,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CUID.cc 12 Sep 2002 00:24:29 -0000 1.4 --- CUID.cc 16 Sep 2002 06:43:33 -0000 1.5 *************** *** 10,50 **** #include "CUID.h" #include "CUniqueDevice.h" ! CUID::CUID (const int serial):_serial(serial), ! _id(-1) { } ! CUID::CUID (const int serial, const int id):_serial(serial), ! _id(id) { } ! CUID::CUID ():_serial(-1), ! _id(-1) { } ! CUID::CUID (const CUniqueDevice *device, const int id):_serial(device->UID().serial()), ! _id(id) { } CUID::CUID (const CUID &id) { (*this) = id; } ! const CUID &CUID::operator = (const CUID &id) { _serial = id._serial; _id = id._id; return (*this); } ! bool CUID::operator == (const CUID &id) ! const { if (_serial != id._serial) return (false); --- 10,69 ---- #include "CUID.h" #include "CUniqueDevice.h" + #include "helper.h" ! CUID::CUID (const int serial) : ! _serial(serial), ! _id(-1) { + METHOD_GUARD; + METHOD_UNGUARD; } ! CUID::CUID (const int serial, const int id) : ! _serial(serial), ! _id(id) { + METHOD_GUARD; + METHOD_UNGUARD; } ! CUID::CUID () : ! _serial(-1), ! _id(-1) { + METHOD_GUARD; + METHOD_UNGUARD; } ! CUID::CUID (const CUniqueDevice *device, const int id) : ! _serial(device->UID().serial()), ! _id(id) { + METHOD_GUARD; + METHOD_UNGUARD; } CUID::CUID (const CUID &id) { + METHOD_GUARD; (*this) = id; + METHOD_UNGUARD; } ! const CUID &CUID::operator = (const CUID &id) ! { ! METHOD_GUARD; _serial = id._serial; _id = id._id; return (*this); + METHOD_UNGUARD; } ! bool CUID::operator == (const CUID &id) const ! { ! METHOD_GUARD; if (_serial != id._serial) return (false); *************** *** 52,100 **** return (false); return (true); ! } ! CUID::~CUID () ! { ! } ! int CUID::serial() ! const ! { ! return (_serial); ! } ! int CUID::id() const ! { ! return (_id); ! } - bool CUID::operator < (const CUID &id) - const - { - if (_serial < id._serial) - return (true); - if (_serial > id._serial) - return (false); - return (_id < id._id); - } - bool CUID::operator > (const CUID &id) const - { - if (_serial < id._serial) - return (false); - if (_serial > id._serial) - return (true); - return (_id > id._id); - } ! string CUID::asString() ! const ! { ! char temp[16]; ! if (_id == -1) ! sprintf(temp, "%d", _serial); ! else ! sprintf(temp, "%d:%d", _serial, _id); ! return (string(temp)); ! } --- 71,131 ---- return (false); return (true); ! METHOD_UNGUARD; ! } ! CUID::~CUID () ! { ! METHOD_GUARD; ! METHOD_UNGUARD; ! } ! int CUID::serial() const ! { ! METHOD_GUARD; ! return (_serial); ! METHOD_UNGUARD; ! } + int CUID::id() const + { + METHOD_GUARD; + return (_id); + METHOD_UNGUARD; + } ! bool CUID::operator < (const CUID &id) const ! { ! METHOD_GUARD; ! if (_serial < id._serial) ! return (true); ! if (_serial > id._serial) ! return (false); ! return (_id < id._id); ! METHOD_UNGUARD; ! } ! bool CUID::operator > (const CUID &id) const ! { ! METHOD_GUARD; ! if (_serial < id._serial) ! return (false); ! if (_serial > id._serial) ! return (true); ! return (_id > id._id); ! METHOD_UNGUARD; ! } ! string CUID::asString() const ! { ! METHOD_GUARD; ! char temp[16]; ! ! if (_id == -1) ! sprintf(temp, "%d", _serial); ! else ! sprintf(temp, "%d:%d", _serial, _id); ! ! return (string(temp)); ! METHOD_UNGUARD; ! } Index: CUniqueDevice.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/CUniqueDevice.cc,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CUniqueDevice.cc 12 Sep 2002 00:24:29 -0000 1.4 --- CUniqueDevice.cc 16 Sep 2002 06:43:33 -0000 1.5 *************** *** 1,12 **** --- 1,17 ---- #include "CUniqueDevice.h" #include "CPhidgetManager.h" + #include "helper.h" void CUniqueDevice::init() { + METHOD_GUARD; CPhidgetManager::getInstance()->registerDevice(this); + METHOD_UNGUARD; } CUniqueDevice::~CUniqueDevice () { + METHOD_GUARD; CPhidgetManager::getInstance()->unregisterDevice(this); + METHOD_UNGUARD; } Index: helper.h =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/phidget++/helper.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** helper.h 14 Sep 2002 01:51:31 -0000 1.1 --- helper.h 16 Sep 2002 06:43:33 -0000 1.2 *************** *** 4,11 **** --- 4,105 ---- #include <stdexcept> #include <string> + #include <iostream> + + + // These guard functions produce a nice stack dump on an exception + //#define MESSAGE_ON_FUNCTION_ENTER + //#define MESSAGE_ON_METHOD_ENTER + + #define LOG std::cout + + // define object check routine, see if this is NULL + #define CHECK_OBJECT if (this==NULL) throw(runtime_error("this was NULL")); + + #ifdef _WIN32 + #define LOCATION __FILE__ << "#" << __LINE__ << " " + #else + #define LOCATION __FILE__ << "#" << __LINE__ << " " << __PRETTY_FUNCTION__ + #endif + + + #ifdef MESSAGE_ON_FUNCTION_ENTER + #define FUNCTION_ENTER_MESSAGE LOG << "Function: " << LOCATION << std::endl + #else // MESSAGE_ON_FUNCTION_ENTER + #define FUNCTION_ENTER_MESSAGE + #endif // MESSAGE_ON_FUNCTION_ENTER + + #ifdef MESSAGE_ON_METHOD_ENTER + #define METHOD_ENTER_MESSAGE LOG << "Method: " << LOCATION << std::endl + #else // MESSAGE_ON_METHOD_ENTER + #define METHOD_ENTER_MESSAGE + #endif // MESSAGE_ON_METHOD_ENTER + + + + + + #define METHOD_GUARD \ + CHECK_OBJECT; \ + METHOD_ENTER_MESSAGE; \ + try \ + { \ + + #define METHOD_UNGUARD } \ + catch(const string &msg) \ + { \ + LOG << LOCATION << " "; \ + LOG << "String:" << msg << std::endl; \ + throw; \ + } \ + catch(const std::exception &e) \ + { \ + LOG << LOCATION << " "; \ + LOG << "Exception:" << e.what() << std::endl; \ + throw; \ + } \ + catch(...) \ + { \ + LOG << LOCATION << " "; \ + LOG << "Unknown exception caught." << std::endl; \ + throw; \ + } + + + + + // function guards + #define FUNC_GUARD \ + FUNCTION_ENTER_MESSAGE; \ + try \ + { \ + + #define FUNC_UNGUARD } \ + catch(const string &msg) \ + { \ + LOG << LOCATION << " "; \ + LOG << "String:" << msg << endl; \ + throw; \ + } \ + catch(const std::exception &e) \ + { \ + LOG << LOCATION << " "; \ + LOG << "Exception:" << e.what() << endl; \ + throw; \ + } \ + catch(...) \ + { \ + LOG << LOCATION << " "; \ + LOG << "Unknown exception caught." << endl; \ + throw; \ + } + + + + template <class X> X &error(X &value) { + FUNC_GUARD; enum ELPError err; *************** *** 16,19 **** --- 110,114 ---- return(value); + FUNC_UNGUARD; } *************** *** 21,24 **** --- 116,120 ---- const X &error(const X &value) { + FUNC_GUARD; enum ELPError err; *************** *** 29,33 **** --- 125,131 ---- return(value); + FUNC_UNGUARD; } + #endif |