CppCMS
system_error.h
1 //
2 // Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOSTER_SYSTEM_ERROR_H
9 #define BOOSTER_SYSTEM_ERROR_H
10 
11 #include <string>
12 #include <booster/backtrace.h>
13 #include <functional>
14 
15 #include <booster/config.h>
16 
17 namespace booster {
18 
23 namespace system {
24 
35  public:
36  virtual ~error_category()
37  {
38  }
42  virtual char const *name() const = 0;
46  virtual std::string message(int ev) const = 0;
47 
48  bool operator==(error_category const &other) const
49  {
50  return this==&other;
51  }
52  bool operator!=(error_category const &other) const
53  {
54  return this!=&other;
55  }
56  bool operator<(error_category const &other) const
57  {
58  return std::less<error_category const *>()(this,&other);
59  }
60 
61  };
62 
63  BOOSTER_API error_category const &get_system_category();
64  BOOSTER_UNUSED static const error_category &system_category = get_system_category();
65 
66  #ifdef BOOSTER_WIN32
67  BOOSTER_API error_category const &get_windows_category();
68  BOOSTER_UNUSED static const error_category &windows_category = get_system_category();
69  #endif
70 
71  #ifdef BOOSTER_POSIX
72  BOOSTER_API error_category const &get_posix_category();
73  BOOSTER_UNUSED static const error_category &posix_category = get_system_category();
74  #endif
75 
76 
83  class error_code {
84  public:
89  value_(0),
90  category_(&system_category)
91  {
92  }
96  error_code(int val,error_category const &cat) :
97  value_(val),
98  category_(&cat)
99  {
100  }
104  int value() const
105  {
106  return value_;
107  }
111  const error_category &category() const
112  {
113  return *category_;
114  }
118  std::string message() const
119  {
120  return std::string(category_->name()) + ": " + category_->message(value_);
121  }
125  operator bool () const
126  {
127  return value() != 0;
128  }
129  private:
130  int value_;
131  error_category const *category_;
132  };
133 
137  inline bool operator==(error_code const &left,error_code const &right)
138  {
139  return left.value() == right.value() && left.category() == right.category();
140  }
144  inline bool operator!=(error_code const &left,error_code const &right)
145  {
146  return !(left==right);
147  }
148 
157  public:
163  error_(e)
164  {
165  }
169  system_error(error_code const &e,std::string const &message) :
170  booster::runtime_error(e.message()+": " + message),
171  error_(e)
172  {
173  }
177  system_error(error_code const &e,char const *message) :
178  booster::runtime_error(e.message()+": " + message),
179  error_(e)
180  {
181  }
186  system_error(int ev,error_category const &category,char const *message) :
188  std::string(category.name())
189  + ": " + category.message(ev)
190  + ": " + message
191  ),
192  error_(ev,category)
193  {
194  }
199  system_error(int ev,error_category const &category,std::string const &message) :
201  std::string(category.name())
202  + ": " + category.message(ev)
203  + ": " + message
204  ),
205  error_(ev,category)
206  {
207  }
212  system_error(int ev,error_category const &category) :
214  std::string(category.name())
215  + ": " + category.message(ev)
216  ),
217  error_(ev,category)
218  {
219  }
223  error_code const &code() const
224  {
225  return error_;
226  }
227  private:
228  error_code error_;
229  };
230 
231 } // system
232 } // booster
233 
234 
235 #endif
system_error(int ev, error_category const &category)
Definition: system_error.h:212
std::string message() const
Definition: system_error.h:118
const error_category & category() const
Definition: system_error.h:111
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
system_error(error_code const &e)
Definition: system_error.h:161
error_code(int val, error_category const &cat)
Definition: system_error.h:96
system_error(error_code const &e, char const *message)
Definition: system_error.h:177
The lightweight object that carries a error code information and its category.
Definition: system_error.h:83
system_error(int ev, error_category const &category, char const *message)
Definition: system_error.h:186
this class represents a category of errors.
Definition: system_error.h:34
error_code()
Definition: system_error.h:88
int value() const
Definition: system_error.h:104
system_error(error_code const &e, std::string const &message)
Definition: system_error.h:169
virtual std::string message(int ev) const =0
system_error(int ev, error_category const &category, std::string const &message)
Definition: system_error.h:199
virtual char const * name() const =0
error_code const & code() const
Definition: system_error.h:223
Booster library namespace. The library that implements Boost Like API in ABI backward compatible way...
Definition: application.h:23
This is the object that should be thrown in case of the error.
Definition: system_error.h:156