Program Listing for File data_exception.hpp

Return to documentation for file (/tmp/ws/src/ecl_core/ecl_exceptions/include/ecl/exceptions/data_exception.hpp)

/*****************************************************************************
** Ifdefs
*****************************************************************************/

#ifndef ECL_EXCEPTIONS_DATA_EXCEPTION_HPP_
#define ECL_EXCEPTIONS_DATA_EXCEPTION_HPP_

/*****************************************************************************
** Disable check
*****************************************************************************/

#include <ecl/config/ecl.hpp>
#ifndef ECL_DISABLE_EXCEPTIONS

/*****************************************************************************
** Includes
*****************************************************************************/

#include <string>
#include <sstream>
#include <ecl/errors/handlers.hpp>
#include "exception.hpp"
#include <ecl/errors/macros.hpp>

/*****************************************************************************
** Namespaces
*****************************************************************************/

namespace ecl {

/*****************************************************************************
** Interface [DataException<Data>]
*****************************************************************************/
template <typename Data>
class DataException : public Exception
{
    public:
        DataException(const char* loc, ErrorFlag error, Data &d );
        DataException(const char* loc, ErrorFlag error, const std::string &msg, const Data &d );
        DataException(const char* loc, const DataException<Data> &e );

        virtual ~DataException() throw() {}

        const char* what() const throw();

        const ErrorFlag& flag() const { return error_type; }
        const Data& data() const { return error_data; }
    private:
        void create_combined_message();
        ErrorFlag error_type;
        Data error_data;
        std::string message;
        std::string combined_message;
};

/*****************************************************************************
 * Implementation
 ****************************************************************************/
template <typename Data>
DataException<Data>::DataException(const char* loc, ErrorFlag error, Data &d ) :
    Exception(loc),
    error_type(error),
    error_data(d)
{
    create_combined_message();
}
template <typename Data>
DataException<Data>::DataException(const char* loc, ErrorFlag error, const std::string &msg, const Data &d ) :
    Exception(loc),
    error_type(error),
    error_data(d),
    message(msg)
{
    create_combined_message();
}
template <typename Data>
DataException<Data>::DataException(const char* loc, const DataException<Data> &e ) :
    Exception(loc),
    error_type(e.flag()),
    error_data(e.data()),
    message(e.message)
{
    location = std::string(loc) + "\n         : " + e.location;
    create_combined_message();
}

template <typename Data>
void DataException<Data>::create_combined_message() {

    std::ostringstream stream;
    stream << "\n" << "Location : " << this->location << "\n";
    stream << "Flag     : " << Error(error_type).what() << "\n";
    if ( message.size() > 0 ) {
        stream << "Detail   : " << message << "\n";
    }
    stream << "Data     : " << error_data << "\n";
    this->combined_message = stream.str();
}

template <typename Data>
const char* DataException<Data>::what() const throw() {
    return combined_message.c_str();
}

} // namespace ecl

#endif /* ECL_DISABLE_EXCEPTIONS */
#endif /* ECL_EXCEPTIONS_DATA_EXCEPTION_HPP_*/