ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

236 lines
5.5 KiB

  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. #include <string>
  11. #include <booster/backtrace.h>
  12. #include <functional>
  13. #include <booster/config.h>
  14. namespace booster {
  15. ///
  16. /// \brief this namespace includes partial implementation of std::tr1's/boost's system_error, error_code
  17. /// classes
  18. ///
  19. namespace system {
  20. ///
  21. /// \brief this class represents a category of errors.
  22. ///
  23. /// Technically it allows to convert a numerical error representation to
  24. /// human readable error message.
  25. ///
  26. /// This class is just an interface that should be implemented for each
  27. /// specific category
  28. ///
  29. class error_category {
  30. public:
  31. virtual ~error_category()
  32. {
  33. }
  34. ///
  35. /// The name of the category
  36. ///
  37. virtual char const *name() const = 0;
  38. ///
  39. /// Convert the error code representation to the human readable text
  40. ///
  41. virtual std::string message(int ev) const = 0;
  42. bool operator==(error_category const &other) const
  43. {
  44. return this==&other;
  45. }
  46. bool operator!=(error_category const &other) const
  47. {
  48. return this!=&other;
  49. }
  50. bool operator<(error_category const &other) const
  51. {
  52. return std::less<error_category const *>()(this,&other);
  53. }
  54. };
  55. BOOSTER_API error_category const &get_system_category();
  56. BOOSTER_UNUSED static const error_category &system_category = get_system_category();
  57. #ifdef BOOSTER_WIN32
  58. BOOSTER_API error_category const &get_windows_category();
  59. BOOSTER_UNUSED static const error_category &windows_category = get_system_category();
  60. #endif
  61. #ifdef BOOSTER_POSIX
  62. BOOSTER_API error_category const &get_posix_category();
  63. BOOSTER_UNUSED static const error_category &posix_category = get_system_category();
  64. #endif
  65. ///
  66. /// \brief The lightweight object that carries a error code information and its
  67. /// category.
  68. ///
  69. /// It is a pair: an integer code and a reference to the error_category object.
  70. ///
  71. class error_code {
  72. public:
  73. ///
  74. /// Create an empty error object - no error
  75. ///
  76. error_code() :
  77. value_(0),
  78. category_(&system_category)
  79. {
  80. }
  81. ///
  82. /// Create a error object withing specific category \a cat with code \a val
  83. ///
  84. error_code(int val,error_category const &cat) :
  85. value_(val),
  86. category_(&cat)
  87. {
  88. }
  89. ///
  90. /// Get the numeric code of the error
  91. ///
  92. int value() const
  93. {
  94. return value_;
  95. }
  96. ///
  97. /// Get the reference to the specific category
  98. ///
  99. const error_category &category() const
  100. {
  101. return *category_;
  102. }
  103. ///
  104. /// Convert the error code to the human readable string
  105. ///
  106. std::string message() const
  107. {
  108. return std::string(category_->name()) + ": " + category_->message(value_);
  109. }
  110. ///
  111. /// Convert to bool - returns true of it is a error and false if it is not (value()==0)
  112. ///
  113. operator bool () const
  114. {
  115. return value() != 0;
  116. }
  117. private:
  118. int value_;
  119. error_category const *category_;
  120. };
  121. ///
  122. /// Compare two error code for equality
  123. ///
  124. inline bool operator==(error_code const &left,error_code const &right)
  125. {
  126. return left.value() == right.value() && left.category() == right.category();
  127. }
  128. ///
  129. /// Compare two error code for inequality
  130. ///
  131. inline bool operator!=(error_code const &left,error_code const &right)
  132. {
  133. return !(left==right);
  134. }
  135. ///
  136. /// \brief This is the object that should be thrown in case of the error.
  137. ///
  138. /// It consists
  139. /// of two parts: the error_code object and the optional error message that would
  140. /// be added to the error code itself.
  141. ///
  142. class system_error : public booster::runtime_error {
  143. public:
  144. ///
  145. /// Create a system error from error_code \a e
  146. ///
  147. system_error(error_code const &e) :
  148. booster::runtime_error(e.message()),
  149. error_(e)
  150. {
  151. }
  152. ///
  153. /// Create a system error from error_code \a e and additional message
  154. ///
  155. system_error(error_code const &e,std::string const &message) :
  156. booster::runtime_error(e.message()+": " + message),
  157. error_(e)
  158. {
  159. }
  160. ///
  161. /// Create a system error from error_code \a e and an additional message \a message
  162. ///
  163. system_error(error_code const &e,char const *message) :
  164. booster::runtime_error(e.message()+": " + message),
  165. error_(e)
  166. {
  167. }
  168. ///
  169. /// Create a system error from error_code defined by integer code \a ev, a error category
  170. /// \a category and an additional message \a message
  171. ///
  172. system_error(int ev,error_category const &category,char const *message) :
  173. booster::runtime_error(
  174. std::string(category.name())
  175. + ": " + category.message(ev)
  176. + ": " + message
  177. ),
  178. error_(ev,category)
  179. {
  180. }
  181. ///
  182. /// Create a system error from error_code defined by integer code \a ev, a error category
  183. /// \a category and an additional message \a message
  184. ///
  185. system_error(int ev,error_category const &category,std::string const &message) :
  186. booster::runtime_error(
  187. std::string(category.name())
  188. + ": " + category.message(ev)
  189. + ": " + message
  190. ),
  191. error_(ev,category)
  192. {
  193. }
  194. ///
  195. /// Create a system error from error_code defined by integer code \a ev, a error category
  196. /// \a category
  197. ///
  198. system_error(int ev,error_category const &category) :
  199. booster::runtime_error(
  200. std::string(category.name())
  201. + ": " + category.message(ev)
  202. ),
  203. error_(ev,category)
  204. {
  205. }
  206. ///
  207. /// Get the error code for the thrown error
  208. ///
  209. error_code const &code() const
  210. {
  211. return error_;
  212. }
  213. private:
  214. error_code error_;
  215. };
  216. } // system
  217. } // booster
  218. #endif