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.

277 lines
8.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/exception.hpp
  3. *
  4. * Purpose: String inserter for std::exception-derived types.
  5. *
  6. * Created: 22nd March 2010
  7. * Updated: 26th November 2010
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2010, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are
  16. * met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  24. * names of any contributors may be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. /** \file pantheios/inserters/exception.hpp
  41. *
  42. * [C++ only] String inserter for std::exception-derived types.
  43. */
  44. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_EXCEPTION
  45. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_EXCEPTION
  46. /* /////////////////////////////////////////////////////////////////////////
  47. * Version information
  48. */
  49. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  50. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_EXCEPTION_MAJOR 1
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_EXCEPTION_MINOR 0
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_EXCEPTION_REVISION 1
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_EXCEPTION_EDIT 2
  54. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  55. /* /////////////////////////////////////////////////////////////////////////
  56. * Includes
  57. */
  58. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  59. # include <pantheios/pantheios.h>
  60. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  61. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
  62. # include <stlsoft/shims/access/string/fwd.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
  64. #include <exception>
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Namespace
  67. */
  68. #if !defined(PANTHEIOS_NO_NAMESPACE)
  69. namespace pantheios
  70. {
  71. #endif /* !PANTHEIOS_NO_NAMESPACE */
  72. /* /////////////////////////////////////////////////////////////////////////
  73. * Typedefs
  74. */
  75. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  76. # if !defined(PANTHEIOS_NO_NAMESPACE)
  77. namespace inserters
  78. {
  79. # endif /* !PANTHEIOS_NO_NAMESPACE */
  80. class exception_inserter
  81. {
  82. public:
  83. typedef exception_inserter class_type;
  84. public:
  85. explicit exception_inserter(std::exception const& x);
  86. private:
  87. #if !defined(STLSOFT_COMPILER_IS_GCC)
  88. // exception_inserter(class_type const&);
  89. #endif /* compiler */
  90. class_type& operator =(class_type const&);
  91. public:
  92. pan_char_t const* c_str() const;
  93. pan_char_t const* data() const;
  94. size_t length() const;
  95. private:
  96. void construct_() const;
  97. void construct_();
  98. private:
  99. pan_char_t* m_value;
  100. size_t m_len;
  101. std::exception const& m_x;
  102. };
  103. # if !defined(PANTHEIOS_NO_NAMESPACE)
  104. } /* namespace inserters */
  105. # endif /* !PANTHEIOS_NO_NAMESPACE */
  106. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  107. /* /////////////////////////////////////////////////////////////////////////
  108. * Typedefs
  109. */
  110. /** Converts a reference to an exception to an instance of a type that
  111. * allows the exception to be inserted into Pantheios diagnostic logging
  112. * statements.
  113. */
  114. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  115. inline
  116. # if !defined(PANTHEIOS_NO_NAMESPACE)
  117. inserters::
  118. # endif /* !PANTHEIOS_NO_NAMESPACE */
  119. exception_inserter exception(
  120. std::exception const& x
  121. )
  122. {
  123. return
  124. # if !defined(PANTHEIOS_NO_NAMESPACE)
  125. inserters::
  126. # endif /* !PANTHEIOS_NO_NAMESPACE */
  127. exception_inserter(x);
  128. }
  129. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  130. inline std::exception const& exception(
  131. std::exception const& x
  132. )
  133. {
  134. return x;
  135. }
  136. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  137. /* /////////////////////////////////////////////////////////////////////////
  138. * String Access Shims
  139. */
  140. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  141. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  142. # if !defined(PANTHEIOS_NO_NAMESPACE)
  143. namespace shims
  144. {
  145. using inserters::exception_inserter;
  146. # endif /* !PANTHEIOS_NO_NAMESPACE */
  147. /** \overload c_str_data_a(exception_inserter const&) */
  148. inline wchar_t const* c_str_data_w(exception_inserter const& i)
  149. {
  150. return i.data();
  151. }
  152. /** \overload c_str_data(exception_inserter const&) */
  153. inline pan_char_t const* c_str_data(exception_inserter const& i)
  154. {
  155. return i.data();
  156. }
  157. /** \overload c_str_len_a(exception_inserter const&) */
  158. inline size_t c_str_len_w(exception_inserter const& i)
  159. {
  160. return i.length();
  161. }
  162. /** \overload c_str_len(exception_inserter const&) */
  163. inline size_t c_str_len(exception_inserter const& i)
  164. {
  165. return i.length();
  166. }
  167. /** \overload c_str_ptr_a(exception_inserter const&) */
  168. inline wchar_t const* c_str_ptr_w(exception_inserter const& i)
  169. {
  170. return i.c_str();
  171. }
  172. /** \overload c_str_ptr(exception_inserter const&) */
  173. inline pan_char_t const* c_str_ptr(exception_inserter const& i)
  174. {
  175. return i.c_str();
  176. }
  177. # if !defined(PANTHEIOS_NO_NAMESPACE)
  178. } /* namespace shims */
  179. # if defined(STLSOFT_COMPILER_IS_GCC)
  180. /* GCC does not seem to correctly handle the phases of
  181. * processing of C++ templates, so we need to 'use' the
  182. * shims into the same namespace as the inserter class
  183. * in order that ADL can suffice instead.
  184. */
  185. using ::pantheios::shims::c_str_data_w;
  186. using ::pantheios::shims::c_str_len_w;
  187. using ::pantheios::shims::c_str_ptr_w;
  188. using ::pantheios::shims::c_str_data;
  189. using ::pantheios::shims::c_str_len;
  190. using ::pantheios::shims::c_str_ptr;
  191. # endif /* compiler */
  192. # endif /* !PANTHEIOS_NO_NAMESPACE */
  193. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  194. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  195. /* /////////////////////////////////////////////////////////////////////////
  196. * Namespace
  197. */
  198. #if !defined(PANTHEIOS_NO_NAMESPACE)
  199. } /* namespace pantheios */
  200. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  201. namespace stlsoft
  202. {
  203. // 'Export' the string access shims into the STLSoft namespace
  204. //
  205. // c_str_ptr(_w) is not necessary for version 1.0 of Pantheios, but it's
  206. // defined and exported in order to allow for the case where someone
  207. // may find a legitimate use for the conversion classes additional to
  208. // the type-tunneling of the Pantheios API.
  209. using ::pantheios::shims::c_str_data_w;
  210. using ::pantheios::shims::c_str_len_w;
  211. using ::pantheios::shims::c_str_ptr_w;
  212. using ::pantheios::shims::c_str_data;
  213. using ::pantheios::shims::c_str_len;
  214. using ::pantheios::shims::c_str_ptr;
  215. }
  216. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  217. #endif /* !PANTHEIOS_NO_NAMESPACE */
  218. /* /////////////////////////////////////////////////////////////////////////
  219. * Inclusion
  220. */
  221. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  222. # pragma once
  223. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  224. /* ////////////////////////////////////////////////////////////////////// */
  225. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_EXCEPTION */
  226. /* ///////////////////////////// end of file //////////////////////////// */