system_error.h
//
// Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOSTER_SYSTEM_ERROR_H
#define BOOSTER_SYSTEM_ERROR_H
#include <string>
#include <booster/backtrace.h>
#include <functional>
#include <booster/config.h>
namespace booster {
namespace system {
class error_category {
public:
virtual ~error_category()
{
}
virtual char const *name() const = 0;
virtual std::string message(int ev) const = 0;
bool operator==(error_category const &other) const
{
return this==&other;
}
bool operator!=(error_category const &other) const
{
return this!=&other;
}
bool operator<(error_category const &other) const
{
return std::less<error_category const *>()(this,&other);
}
};
BOOSTER_API error_category const &get_system_category();
static const error_category &system_category = get_system_category();
#ifdef BOOSTER_WIN32
BOOSTER_API error_category const &get_windows_category();
static const error_category &windows_category = get_system_category();
#endif
#ifdef BOOSTER_POSIX
BOOSTER_API error_category const &get_posix_category();
static const error_category &posix_category = get_system_category();
#endif
class error_code {
public:
error_code() :
value_(0),
category_(&system_category)
{
}
error_code(int val,error_category const &cat) :
value_(val),
category_(&cat)
{
}
int value() const
{
return value_;
}
const error_category &category() const
{
return *category_;
}
std::string message() const
{
return std::string(category_->name()) + ": " + category_->message(value_);
}
operator bool () const
{
return value() != 0;
}
private:
int value_;
error_category const *category_;
};
inline bool operator==(error_code const &left,error_code const &right)
{
return left.value() == right.value() && left.category() == right.category();
}
inline bool operator!=(error_code const &left,error_code const &right)
{
return !(left==right);
}
class system_error : public booster::runtime_error {
public:
system_error(error_code const &e) :
booster::runtime_error(e.message()),
error_(e)
{
}
system_error(error_code const &e,std::string const &message) :
booster::runtime_error(e.message()+": " + message),
error_(e)
{
}
system_error(error_code const &e,char const *message) :
booster::runtime_error(e.message()+": " + message),
error_(e)
{
}
system_error(int ev,error_category const &category,char const *message) :
booster::runtime_error(
std::string(category.name())
+ ": " + category.message(ev)
+ ": " + message
),
error_(ev,category)
{
}
system_error(int ev,error_category const &category,std::string const &message) :
booster::runtime_error(
std::string(category.name())
+ ": " + category.message(ev)
+ ": " + message
),
error_(ev,category)
{
}
system_error(int ev,error_category const &category) :
booster::runtime_error(
std::string(category.name())
+ ": " + category.message(ev)
),
error_(ev,category)
{
}
error_code const &code() const
{
return error_;
}
private:
error_code error_;
};
} // system
} // booster
#endif
booster::system::system_error::system_error
system_error(int ev, error_category const &category)
Definition: system_error.h:212
booster::system::error_code::message
std::string message() const
Definition: system_error.h:118
booster::system::error_code::category
const error_category & category() const
Definition: system_error.h:111
booster::runtime_error
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
booster::system::system_error::system_error
system_error(error_code const &e)
Definition: system_error.h:161
booster::system::error_code::error_code
error_code(int val, error_category const &cat)
Definition: system_error.h:96
booster::system::system_error::system_error
system_error(error_code const &e, char const *message)
Definition: system_error.h:177
booster::system::error_code
The lightweight object that carries a error code information and its category.
Definition: system_error.h:83
booster::system::system_error::system_error
system_error(int ev, error_category const &category, char const *message)
Definition: system_error.h:186
booster::system::error_category
this class represents a category of errors.
Definition: system_error.h:34
booster::system::error_code::error_code
error_code()
Definition: system_error.h:88
booster::system::error_code::value
int value() const
Definition: system_error.h:104
booster::system::system_error::system_error
system_error(error_code const &e, std::string const &message)
Definition: system_error.h:169
booster::system::operator==
bool operator==(error_code const &left, error_code const &right)
Definition: system_error.h:137
booster::locale::message
basic_message< char > message
Definition: message.h:494
booster::system::error_category::message
virtual std::string message(int ev) const =0
booster::system::system_error::system_error
system_error(int ev, error_category const &category, std::string const &message)
Definition: system_error.h:199
booster::system::error_category::name
virtual char const * name() const =0
booster::system::system_error::code
error_code const & code() const
Definition: system_error.h:223
booster::system::system_error
This is the object that should be thrown in case of the error.
Definition: system_error.h:156
booster::system::operator!=
bool operator!=(error_code const &left, error_code const &right)
Definition: system_error.h:144