Went the subclassed exception route (at the expense of lots of extra

code and possibly extra maintenance). It will match the Python
interface and it's I guess what Stroustrup recommends.
This commit is contained in:
rocky
2006-01-17 02:09:32 +00:00
parent 004152ec29
commit 1736ea9c4e
3 changed files with 78 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/* /*
$Id: eject.cpp,v 1.3 2006/01/15 10:39:15 rocky Exp $ $Id: eject.cpp,v 1.4 2006/01/17 02:09:32 rocky Exp $
Copyright (C) 2005 Rocky Bernstein <rocky@panix.com> Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
@@ -61,6 +61,10 @@ main(int argc, const char *argv[])
device.ejectMedia(psz_drive); device.ejectMedia(psz_drive);
printf("CD in CD-ROM drive %s ejected.\n", psz_drive); printf("CD in CD-ROM drive %s ejected.\n", psz_drive);
} }
catch ( CdioDevice::DriverOpUninit e ) {
printf("Can't Ejecting CD from CD-ROM drive: driver is not initialized.\n",
psz_drive);
}
catch ( CdioDevice::DriverOpException e ) { catch ( CdioDevice::DriverOpException e ) {
printf("Ejecting CD from CD-ROM drive %s operation error:\n\t%s.\n", printf("Ejecting CD from CD-ROM drive %s operation error:\n\t%s.\n",
psz_drive, e.get_msg()); psz_drive, e.get_msg());

View File

@@ -1,5 +1,5 @@
/* -*- C++ -*- /* -*- C++ -*-
$Id: cdio.hpp,v 1.4 2006/01/15 10:39:15 rocky Exp $ $Id: cdio.hpp,v 1.5 2006/01/17 02:09:32 rocky Exp $
Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com> Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
@@ -115,6 +115,7 @@ public:
{ {
public: public:
driver_return_code_t driver_return_code; driver_return_code_t driver_return_code;
DriverOpException( void ) { };
DriverOpException( driver_return_code_t drc ) { DriverOpException( driver_return_code_t drc ) {
driver_return_code = drc; driver_return_code = drc;
}; };
@@ -126,6 +127,48 @@ public:
}; };
}; };
class DriverOpError: public DriverOpException
{
public:
DriverOpError(void) { driver_return_code = DRIVER_OP_ERROR; }
};
class DriverOpUnsupported: public DriverOpException
{
public:
DriverOpUnsupported(void) { driver_return_code = DRIVER_OP_UNSUPPORTED; }
};
class DriverOpUninit: public DriverOpException
{
public:
DriverOpUninit(void) { driver_return_code = DRIVER_OP_UNINIT; }
};
class DriverOpNotPermitted: public DriverOpException
{
public:
DriverOpNotPermitted(void) {driver_return_code = DRIVER_OP_NOT_PERMITTED;}
};
class DriverOpBadParameter: public DriverOpException
{
public:
DriverOpBadParameter(void) {driver_return_code = DRIVER_OP_BAD_PARAMETER;}
};
class DriverOpBadPointer: public DriverOpException
{
public:
DriverOpBadPointer(void) {driver_return_code = DRIVER_OP_BAD_POINTER;}
};
class DriverOpNoDriver: public DriverOpException
{
public:
DriverOpNoDriver(void) {driver_return_code = DRIVER_OP_NO_DRIVER;}
};
// Other member functions // Other member functions
#include "device.hpp" #include "device.hpp"
#include "disc.hpp" #include "disc.hpp"
@@ -134,10 +177,28 @@ public:
private: private:
CdIo_t *p_cdio; CdIo_t *p_cdio;
void throw_device_exception(driver_return_code_t drc) void possible_throw_device_exception(driver_return_code_t drc)
{ {
if (DRIVER_OP_SUCCESS == drc) return; switch (drc) {
throw DriverOpException(drc); case DRIVER_OP_SUCCESS:
return;
case DRIVER_OP_ERROR:
throw DriverOpError();
case DRIVER_OP_UNSUPPORTED:
throw DriverOpUnsupported();
case DRIVER_OP_UNINIT:
throw DriverOpUninit();
case DRIVER_OP_NOT_PERMITTED:
throw DriverOpNotPermitted();
case DRIVER_OP_BAD_PARAMETER:
throw DriverOpBadParameter();
case DRIVER_OP_BAD_POINTER:
throw DriverOpBadPointer();
case DRIVER_OP_NO_DRIVER:
throw DriverOpNoDriver();
default:
throw DriverOpException(drc);
}
} }
}; };

View File

@@ -1,5 +1,5 @@
/* -*- C++ -*- /* -*- C++ -*-
$Id: device.hpp,v 1.2 2006/01/15 10:39:15 rocky Exp $ $Id: device.hpp,v 1.3 2006/01/17 02:09:32 rocky Exp $
Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com> Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
@@ -47,7 +47,7 @@ close()
void closeTray (const char *psz_drive, /*in/out*/ driver_id_t &driver_id) void closeTray (const char *psz_drive, /*in/out*/ driver_id_t &driver_id)
{ {
driver_return_code_t drc = cdio_close_tray (psz_drive, &driver_id); driver_return_code_t drc = cdio_close_tray (psz_drive, &driver_id);
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -71,7 +71,7 @@ void
ejectMedia () ejectMedia ()
{ {
driver_return_code_t drc = cdio_eject_media(&p_cdio); driver_return_code_t drc = cdio_eject_media(&p_cdio);
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -83,7 +83,7 @@ void
ejectMedia (const char *psz_drive) ejectMedia (const char *psz_drive)
{ {
driver_return_code_t drc = cdio_eject_media_drive(psz_drive); driver_return_code_t drc = cdio_eject_media_drive(psz_drive);
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -321,7 +321,7 @@ void
getLastSession (/*out*/ lsn_t &i_last_session) getLastSession (/*out*/ lsn_t &i_last_session)
{ {
driver_return_code_t drc = cdio_get_last_session(p_cdio, &i_last_session); driver_return_code_t drc = cdio_get_last_session(p_cdio, &i_last_session);
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -472,7 +472,7 @@ void
setBlocksize ( int i_blocksize ) setBlocksize ( int i_blocksize )
{ {
driver_return_code_t drc = cdio_set_blocksize ( p_cdio, i_blocksize ); driver_return_code_t drc = cdio_set_blocksize ( p_cdio, i_blocksize );
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -482,7 +482,7 @@ void
setSpeed ( int i_speed ) setSpeed ( int i_speed )
{ {
driver_return_code_t drc = cdio_set_speed ( p_cdio, i_speed ); driver_return_code_t drc = cdio_set_speed ( p_cdio, i_speed );
throw_device_exception(drc); possible_throw_device_exception(drc);
} }
/*! /*!
@@ -495,6 +495,6 @@ void
setArg (const char key[], const char value[]) setArg (const char key[], const char value[])
{ {
driver_return_code_t drc = cdio_set_arg (p_cdio, key, value); driver_return_code_t drc = cdio_set_arg (p_cdio, key, value);
throw_device_exception(drc); possible_throw_device_exception(drc);
} }