Changeset 280 in pacpusframework for trunk


Ignore:
Timestamp:
Mar 20, 2014, 11:59:05 AM (11 years ago)
Author:
Marek Kurdej
Message:

Major: DbiteException uses boost::exception.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Pacpus/kernel/DbiteException.h

    r156 r280  
    1818#include <Pacpus/kernel/PacpusException.h>
    1919
     20#include <ios>
    2021#include <string>
    2122
     
    2930    /// Ctor.
    3031    /// @param what Information about the exception.
    31     DbiteException(const std::string & what);
     32    DbiteException(std::string const& what = "");
    3233
    3334    /// Dtor.
     
    3536};
    3637
     38typedef boost::error_info<struct tag_errinfo_ios_base_open_mode, std::ios_base::openmode> errinfo_ios_base_open_mode;
     39
    3740} // namespace pacpus
    3841
  • trunk/include/Pacpus/kernel/PacpusException.h

    r156 r280  
    1717#include <Pacpus/kernel/PacpusLibConfig.h>
    1818
    19 //#include <boost/exception/exception.hpp>
     19#include <boost/exception/exception.hpp>
     20#include <boost/exception/error_info.hpp>
    2021#include <exception>
    2122#include <string>
     
    2324#ifdef _MSC_VER
    2425#   pragma warning(push)
    25 #   pragma warning(disable: 4251)
     26#   pragma warning(disable: 4251 4275)
    2627#endif // _MSC_VER
    2728
    28 namespace pacpus {
     29namespace pacpus
     30{
    2931
    3032/// Base class for all exceptions in the framework
    3133class PACPUSLIB_API PacpusException
    32     : virtual public std::exception
    33     //, virtual public boost::exception
     34    : /*virtual*/ public std::runtime_error
     35    , virtual public boost::exception
    3436{
    3537public:
    3638    /// Ctor.
    3739    /// @param what Information about the exception.
    38     PacpusException(const std::string & what);
     40    PacpusException(std::string const& what = "");
    3941
    4042    /// Dtor.
    41     virtual ~PacpusException() throw();
    42 
    43     /// Gets more information about the error.
    44     ///
    45     /// @returns Message containing information about the error.
    46     virtual const char * what() const throw();
    47 
    48 protected:
    49     std::string mWhat;
     43    virtual ~PacpusException();
    5044};
    5145
    5246} // namespace pacpus
     47
     48typedef boost::error_info<struct tag_errinfo_signal, int> errinfo_signal;
    5349
    5450#ifdef _MSC_VER
  • trunk/src/FileLib/src/DbiteException.cpp

    r156 r280  
    1010using namespace pacpus;
    1111
    12 DbiteException::DbiteException(const std::string& what)
     12DbiteException::DbiteException(std::string const& what)
    1313    : PacpusException(what)
    1414{
  • trunk/src/FileLib/src/DbiteFile.cpp

    r141 r280  
    1010#include <Pacpus/kernel/Log.h>
    1111
     12#include <boost/exception/detail/exception_ptr.hpp>
     13#include <boost/exception/errinfo_api_function.hpp>
     14#include <boost/exception/errinfo_file_name.hpp>
     15#include <boost/exception/errinfo_nested_exception.hpp>
    1216#include <cassert>
    1317#include <cmath>
     
    1620#include <sstream>
    1721
     22using boost::copy_exception;
     23using boost::errinfo_api_function;
     24using boost::errinfo_file_name;
     25using boost::errinfo_nested_exception;
     26using boost::exception_ptr;
    1827using namespace pacpus;
    19 using namespace std;
     28using std::fstream;
     29using std::ios_base;
     30using std::min;
     31using std::max;
     32using std::numeric_limits;
     33using std::streamsize;
     34using std::string;
     35using std::stringstream;
    2036
    2137// FIXME: record size is only 8-bit long!
     
    3248}
    3349
    34 bool doesFileExist(const string & path);
     50bool doesFileExist(string const& path);
    3551
    3652DbiteFile::DbiteFile()
     
    5773    stringstream os;
    5874
    59     os << "Signature    : " << getSignature() << endl;
    60     os << "Version      : " << getVersion() << endl;
    61     os << "Header size  : " << getDataOffset() << " [bytes]" << endl;
    62     os << "Record size  : " << getRecordSize() << " [bytes]" << endl;
    63     os << "Data type    : " << getType() << endl;
    64     os << "Time min.    : " << getTimeMin() << endl;
    65     os << "Time max.    : " << getTimeMax() << endl;
    66     os << "Duration     : " << (getTimeMax() - getTimeMin()) / kMicrosInSecond << " [seconds]" << endl;
    67     os << "File size    : " << getFileSize() << " [bytes]" << endl;
    68     os << "Record count : " << getRecordCount() << endl;
     75    os << "Signature    : " << getSignature() << "\n";
     76    os << "Version      : " << getVersion() << "\n";
     77    os << "Header size  : " << getDataOffset() << " [bytes]" << "\n";
     78    os << "Record size  : " << getRecordSize() << " [bytes]" << "\n";
     79    os << "Data type    : " << getType() << "\n";
     80    os << "Time min.    : " << getTimeMin() << "\n";
     81    os << "Time max.    : " << getTimeMax() << "\n";
     82    os << "Duration     : " << (getTimeMax() - getTimeMin()) / kMicrosInSecond << " [seconds]" << "\n";
     83    os << "File size    : " << getFileSize() << " [bytes]" << "\n";
     84    os << "Record count : " << getRecordCount() << "\n";
    6985
    7086    return os.str();
     
    91107    setPath(path);
    92108
    93     mFile.open(getPath().c_str(), ios_base::binary | ios_base::in);
     109    ios_base::openmode openmode = ios_base::binary | ios_base::in;
     110    mFile.open(getPath().c_str(), openmode);
    94111    try {
    95112        checkFileOpen();
     
    98115        LOG_ERROR("Dbite exception: " << e.what());
    99116        close();
    100         throw DbiteException("cannot open file for reading");
     117        BOOST_THROW_EXCEPTION(DbiteException("cannot open file for reading")
     118            << errinfo_file_name(mPath)
     119            << errinfo_ios_base_open_mode(openmode)
     120        );
    101121    }
    102122
    103123    try {
    104124        readHeader();
    105     } catch (DbiteException & e) {
     125    } catch (DbiteException& e) {
    106126        (void) e; // unused
    107127        LOG_ERROR("Dbite exception: " << e.what());
    108128        // TODO: readAndDiagnoseHeader();
    109129        close();
    110         throw DbiteException("cannot read header");
     130        BOOST_THROW_EXCEPTION(DbiteException("cannot read header")
     131            << errinfo_nested_exception(copy_exception(e))
     132        );
    111133    }
    112134    verifyHeader();
     
    156178    if (!isOpen()) {
    157179        LOG_ERROR("cannot open file for writing");
    158         throw DbiteException("cannot open file for writing");
     180        BOOST_THROW_EXCEPTION(DbiteException("cannot open file for writing")
     181            << errinfo_ios_base_open_mode(openmode)
     182        );
    159183    }
    160184
     
    170194            // TODO: readAndDiagnoseHeader();
    171195            close();
    172             throw DbiteException("cannot read header");
     196            BOOST_THROW_EXCEPTION(DbiteException("cannot read header"));
    173197        }
    174198    } else {
     
    336360    if (getVersion() != VERSION_NUMBER) {
    337361        LOG_ERROR("bad version number");
    338         throw DbiteException("bad version number");
     362        BOOST_THROW_EXCEPTION(DbiteException("bad version number"));
    339363    }
    340364
    341365    if (!isSignatureCorrect()) {
    342366        LOG_ERROR("bad signature");
    343         throw DbiteException("bad signature");
     367        BOOST_THROW_EXCEPTION(DbiteException("bad signature"));
    344368    }
    345369
     
    511535    if (isVariableDataSize()) {
    512536        LOG_ERROR("cannot read nth record in a variable");
    513         throw DbiteException("cannot read nth record in a variable");
     537        BOOST_THROW_EXCEPTION(DbiteException("cannot read nth record in a variable"));
    514538    }
    515539
     
    518542    if (getFileSize() < nthRecordPosition) {
    519543        LOG_WARN("wanted to read past the file end");
    520         //throw DbiteException("wanted to read past the file end");
     544        //BOOST_THROW_EXCEPTION(DbiteException("wanted to read past the file end"));
    521545    }
    522546    mFile.clear();
     
    530554    if (!buffer) {
    531555        LOG_WARN("data buffer must be a non-NULL pointer");
    532         throw DbiteException("data buffer must be a non-NULL pointer");
     556        BOOST_THROW_EXCEPTION(DbiteException("data buffer must be a non-NULL pointer"));
    533557    }
    534558
     
    582606        }
    583607        LOG_ERROR("cannot read acquisition time");
    584         throw DbiteException("cannot read acquisition time");
     608        BOOST_THROW_EXCEPTION(DbiteException("cannot read acquisition time")
     609            << errinfo_file_name(mPath)
     610            << errinfo_api_function("read")
     611        );
    585612    }
    586613
     
    613640            LOG_INFO("buffer size = " << bufferSize);
    614641            LOG_INFO("data   size = " << dataSize);
    615             throw DbiteException("buffer is smaller than the data size");
     642            BOOST_THROW_EXCEPTION(DbiteException("buffer is smaller than the data size"));
    616643            // TODO: don't throw exception, but read only as much data as possible
    617644        }
     
    682709        // read forward
    683710        readResult = readRecordForward(time, timeRange, buffer, bufferSize);
    684     } catch (DbiteException & e) {
     711    } catch (DbiteException& /*e*/) {
    685712        setReadPosition(initialPosition);
    686         throw e;
     713        throw;
    687714    }
    688715
     
    703730        if (dataSize > static_cast<size_t>(numeric_limits<VariableDataSizeT>::max())) {
    704731            LOG_ERROR("variable data size too big");
    705             throw DbiteException("variable data size too big");
     732            BOOST_THROW_EXCEPTION(DbiteException("variable data size too big")
     733                << errinfo_file_name(mPath)
     734            );
    706735        }
    707736    }
     
    734763            writtenBytes += sizeof(VariableDataSizeT);
    735764        }
    736     } catch (DbiteException & e) {
     765    } catch (DbiteException& e) {
    737766        LOG_ERROR("Dbite exception: " << e.what());
    738767        LOG_INFO("restoring initial write position");
     
    740769        // on error, go back to record start
    741770        setWritePosition(- (int64_t) writtenBytes, ios_base::cur);
    742         throw e;
     771        throw;
    743772    }
    744773 
     
    766795    if (!mFile) {
    767796        LOG_ERROR("cannot read data");
    768         throw DbiteException("cannot read data");
     797        BOOST_THROW_EXCEPTION(DbiteException("cannot read data")
     798            << errinfo_file_name(mPath)
     799            << errinfo_api_function("read")
     800        );
    769801    }
    770802}
     
    775807    if (!mFile) {
    776808        LOG_ERROR("cannot write data");
    777         throw DbiteException("cannot write data");
     809        BOOST_THROW_EXCEPTION(DbiteException("cannot write data")
     810            << errinfo_file_name(mPath)
     811            << errinfo_api_function("write")
     812        );
    778813    }
    779814}
     
    789824        string errorMessage = "file is not open";
    790825        LOG_WARN(errorMessage);
    791         throw DbiteException(errorMessage.c_str());
     826        BOOST_THROW_EXCEPTION(DbiteException(errorMessage.c_str())
     827            << errinfo_file_name(mPath)
     828        );
    792829    }
    793830}
  • trunk/src/PacpusLib/ComponentBase.cpp

    r279 r280  
    509509    } catch (po::error& e) {
    510510        LOG_WARN(e.what());
    511         throw PacpusException(e.what());
     511        BOOST_THROW_EXCEPTION(PacpusException(e.what()));
    512512    }
    513513
  • trunk/src/PacpusLib/PacpusApplication.cpp

    r222 r280  
    8989    try {
    9090        return QApplication::notify(receiver, ev);
    91     } catch (PacpusException & e) {
     91    } catch (PacpusException& e) {
    9292        (void)e; // unused
    93         LOG_ERROR("PacpusException thrown:" << e.what());
    94     } catch (boost::exception & e) {
     93        LOG_ERROR("PacpusException thrown:" << e.what() << "\n" << boost::diagnostic_information(e));
     94    } catch (boost::exception& e) {
    9595        (void)e; // unused
    9696        LOG_ERROR("boost::exception thrown:" << boost::diagnostic_information(e));
    97     } catch (std::exception & e) {
     97    } catch (std::exception& e) {
    9898        (void)e; // unused
    9999        LOG_ERROR("std::exception thrown:" << e.what());
     
    125125    std::stringstream errorMessage;
    126126    errorMessage << "received signal number " << signal;
    127     throw PacpusException(errorMessage.str());
     127    BOOST_THROW_EXCEPTION(PacpusException(errorMessage.str())
     128        << errinfo_signal(signal)
     129    );
    128130}
  • trunk/src/PacpusLib/PacpusException.cpp

    r156 r280  
    1010using namespace pacpus;
    1111
    12 PacpusException::PacpusException(const std::string & what)
    13     : mWhat(what)
     12PacpusException::PacpusException(std::string const& what)
     13    : std::runtime_error(what)
    1414{
    1515}
    1616
    17 PacpusException::~PacpusException() throw()
     17PacpusException::~PacpusException()
    1818{
    1919}
    20 
    21 const char * PacpusException::what() const throw()
    22 {
    23     return mWhat.c_str();
    24 }
  • trunk/src/PacpusLib/XmlConfigFile.cpp

    r231 r280  
    88
    99#include <Pacpus/kernel/Log.h>
     10#include <Pacpus/kernel/PacpusException.h>
    1011
    1112#include <cassert>
     
    6768    } else {
    6869        LOG_ERROR("cannot open XML document " << kXmlConfigFilename);
    69         throw "cannot open XML document file";
     70        BOOST_THROW_EXCEPTION(PacpusException("cannot open XML document file"));
    7071    }
    7172}
Note: See TracChangeset for help on using the changeset viewer.