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.

297 lines
10 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/pointer.hpp
  3. *
  4. * Purpose: String inserters for fundamental types
  5. *
  6. * Created: 21st June 2005
  7. * Updated: 14th February 2012
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2005-2012, Matthew Wilson and Synesis Software
  12. * Copyright (c) 1999-2005, Synesis Software and Matthew Wilson
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are
  17. * met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the
  25. * names of any contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * ////////////////////////////////////////////////////////////////////// */
  41. /** \file pantheios/inserters/pointer.hpp
  42. *
  43. * [C++ only] Definition of the pantheios::pointer string inserter for
  44. * for pointer types.
  45. */
  46. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_POINTER
  47. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_POINTER
  48. /* /////////////////////////////////////////////////////////////////////////
  49. * Version information
  50. */
  51. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_POINTER_MAJOR 2
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_POINTER_MINOR 4
  54. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_POINTER_REVISION 4
  55. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_POINTER_EDIT 29
  56. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  57. /* /////////////////////////////////////////////////////////////////////////
  58. * Includes
  59. */
  60. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  61. # include <pantheios/pantheios.h>
  62. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  63. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT
  64. # include <pantheios/inserters/fmt.hpp>
  65. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT */
  66. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
  67. # include <stlsoft/shims/access/string/fwd.h>
  68. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Namespace
  71. */
  72. #if !defined(PANTHEIOS_NO_NAMESPACE)
  73. namespace pantheios
  74. {
  75. #endif /* !PANTHEIOS_NO_NAMESPACE */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Inserter classes
  78. */
  79. /** Class for inserting pointers into Pantheios diagnostic logging
  80. * statements.
  81. *
  82. * \ingroup group__application_layer_interface__inserters
  83. *
  84. * This class converts a pointer variable into a string, thereby enabling it to be
  85. * inserted into a logging statement. Consider the following statement
  86. *
  87. * \code
  88. void* p = reinterpret_cast<void*>(0x01234567);
  89. char s[] = "abc";
  90. std::string str("def");
  91. pantheios::log(pantheios::notice, "s=", s, ", p=", pantheios::pointer(p, 8, pantheios::fmt::fullHex), ", str=", str);
  92. * \endcode
  93. *
  94. * This will produce the output:
  95. *
  96. * &nbsp;&nbsp;&nbsp;&nbsp;<b>s=abc, p=0x01234567, str=def</b>
  97. */
  98. class pointer
  99. {
  100. public:
  101. /// This type
  102. typedef pointer class_type;
  103. public:
  104. /// [DEPRECATED] Construct from a pointer, with width/format specifier
  105. ///
  106. /// \param pv The pointer whose value will be inserted
  107. /// \param widthAndFormat A bitwise combination of width and format. The
  108. /// width must be in the range [0, 20]
  109. ///
  110. /// \deprecated This constructor form is deprecated, and will be removed
  111. /// in a forthcoming release; instead use
  112. /// the \link pantheios::pointer::pointer(void const volatile*, int, int) 3-parameter\endlink
  113. /// overload.
  114. ///
  115. /// \note For backwards-compatibility, specifiying the zeroXPrefix flag
  116. /// also causes zeroPad to be assumed
  117. pointer(void const volatile* pv, int widthAndFormat);
  118. /// Construct from a pointer, specifying width and format
  119. ///
  120. /// \param pv The pointer whose value will be inserted
  121. /// \param minWidth The minimum width. Must be in the range [-20, 20]
  122. /// \param format Combination of \link pantheios::fmt format\endlink flags
  123. ///
  124. /// \pre (minWidth >= -20 && minWidth <= 20)
  125. pointer(void const volatile* pv, int minWidth, int format);
  126. public:
  127. /// A possibly non-nul-terminated non-null pointer to the c-style string representation of the pointer
  128. pan_char_t const* data() const;
  129. /// A nul-terminated non-null pointer to the c-style string representation of the pointer
  130. pan_char_t const* c_str() const;
  131. /// The length of the c-style string representation of the pointer
  132. size_t length() const;
  133. private:
  134. void construct_() const;
  135. void construct_();
  136. static int validate_width_(int minWidth);
  137. private:
  138. void const volatile* m_value;
  139. size_t m_len;
  140. const int m_minWidth;
  141. const int m_format;
  142. pan_char_t m_sz[23];
  143. private:
  144. #if !defined(STLSOFT_COMPILER_IS_GCC)
  145. pointer(class_type const&);
  146. #endif /* compiler */
  147. class_type& operator =(class_type const&);
  148. };
  149. /* /////////////////////////////////////////////////////////////////////////
  150. * String Access Shims
  151. */
  152. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  153. # if !defined(PANTHEIOS_NO_NAMESPACE)
  154. namespace shims
  155. {
  156. # endif /* !PANTHEIOS_NO_NAMESPACE */
  157. /** \overload c_str_data_a(pointer const&) */
  158. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  159. inline wchar_t const* c_str_data_w(pointer const& p)
  160. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  161. inline char const* c_str_data_a(pointer const& p)
  162. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  163. {
  164. return p.data();
  165. }
  166. /** \overload c_str_data(pointer const&) */
  167. inline pan_char_t const* c_str_data(pointer const& p)
  168. {
  169. return p.data();
  170. }
  171. /** \overload c_str_len_a(pointer const&) */
  172. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  173. inline size_t c_str_len_w(pointer const& p)
  174. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  175. inline size_t c_str_len_a(pointer const& p)
  176. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  177. {
  178. return p.length();
  179. }
  180. /** \overload c_str_len(pointer const&) */
  181. inline size_t c_str_len(pointer const& p)
  182. {
  183. return p.length();
  184. }
  185. /** \overload c_str_ptr_a(pointer const&) */
  186. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  187. inline wchar_t const* c_str_ptr_w(pointer const& p)
  188. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  189. inline char const* c_str_ptr_a(pointer const& p)
  190. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  191. {
  192. return p.c_str();
  193. }
  194. /** \overload c_str_ptr(pointer const&) */
  195. inline pan_char_t const* c_str_ptr(pointer const& p)
  196. {
  197. return p.c_str();
  198. }
  199. # if !defined(PANTHEIOS_NO_NAMESPACE)
  200. } /* namespace shims */
  201. # if defined(STLSOFT_COMPILER_IS_GCC)
  202. /* GCC does not seem to correctly handle the phases of
  203. * processing of C++ templates, so we need to 'use' the
  204. * shims into the same namespace as the inserter class
  205. * in order that ADL can suffice instead.
  206. */
  207. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  208. using ::pantheios::shims::c_str_data_w;
  209. using ::pantheios::shims::c_str_len_w;
  210. using ::pantheios::shims::c_str_ptr_w;
  211. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  212. using ::pantheios::shims::c_str_data_a;
  213. using ::pantheios::shims::c_str_len_a;
  214. using ::pantheios::shims::c_str_ptr_a;
  215. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  216. using ::pantheios::shims::c_str_data;
  217. using ::pantheios::shims::c_str_len;
  218. using ::pantheios::shims::c_str_ptr;
  219. # endif /* compiler */
  220. # endif /* !PANTHEIOS_NO_NAMESPACE */
  221. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  222. /* /////////////////////////////////////////////////////////////////////////
  223. * Namespace
  224. */
  225. #if !defined(PANTHEIOS_NO_NAMESPACE)
  226. } /* namespace pantheios */
  227. namespace stlsoft
  228. {
  229. // 'Export' the string access shims into the STLSoft namespace
  230. //
  231. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  232. // defined and exported in order to allow for the case where someone
  233. // may find a legitimate use for the conversion classes additional to
  234. // the type-tunneling of the Pantheios API.
  235. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  236. using ::pantheios::shims::c_str_data_w;
  237. using ::pantheios::shims::c_str_len_w;
  238. using ::pantheios::shims::c_str_ptr_w;
  239. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  240. using ::pantheios::shims::c_str_data_a;
  241. using ::pantheios::shims::c_str_len_a;
  242. using ::pantheios::shims::c_str_ptr_a;
  243. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  244. using ::pantheios::shims::c_str_data;
  245. using ::pantheios::shims::c_str_len;
  246. using ::pantheios::shims::c_str_ptr;
  247. }
  248. #endif /* !PANTHEIOS_NO_NAMESPACE */
  249. /* /////////////////////////////////////////////////////////////////////////
  250. * Inclusion
  251. */
  252. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  253. # pragma once
  254. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  255. /* ////////////////////////////////////////////////////////////////////// */
  256. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_POINTER */
  257. /* ///////////////////////////// end of file //////////////////////////// */