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.

346 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/character.hpp
  3. *
  4. * Purpose: String inserters for fundamental types
  5. *
  6. * Created: 21st June 2005
  7. * Updated: 31st July 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/character.hpp
  42. *
  43. * [C++ only] Definition of the pantheios::character string inserter
  44. * for built-in character type(s).
  45. *
  46. * This file is not included by default.
  47. *
  48. <pre>
  49. \#include &lt;pantheios/inserters/character.hpp>
  50. \#include &lt;pantheios/pantheios.hpp>
  51. . . .
  52. char c = '~';
  53. pantheios::log_DEBUG("c=", pantheios::character(c));
  54. </pre>
  55. *
  56. * The short alias \c ch is available, via inclusion of
  57. * pantheios/inserters/ch.hpp
  58. *
  59. <pre>
  60. \#include &lt;pantheios/inserters/ch.hpp>
  61. . . .
  62. char c = '~';
  63. pantheios::log_DEBUG("c=", pantheios::ch(c));
  64. </pre>
  65. */
  66. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_CHARACTER
  67. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_CHARACTER
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Version information
  70. */
  71. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  72. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_CHARACTER_MAJOR 1
  73. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_CHARACTER_MINOR 5
  74. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_CHARACTER_REVISION 2
  75. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_CHARACTER_EDIT 24
  76. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  77. /* /////////////////////////////////////////////////////////////////////////
  78. * Includes
  79. */
  80. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  81. # include <pantheios/pantheios.h>
  82. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  83. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT
  84. # include <pantheios/inserters/fmt.hpp>
  85. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_FMT */
  86. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
  87. # include <stlsoft/shims/access/string/fwd.h>
  88. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD */
  89. /* /////////////////////////////////////////////////////////////////////////
  90. * Namespace
  91. */
  92. #if !defined(PANTHEIOS_NO_NAMESPACE)
  93. namespace pantheios
  94. {
  95. #endif /* !PANTHEIOS_NO_NAMESPACE */
  96. /* /////////////////////////////////////////////////////////////////////////
  97. * Inserter classes
  98. */
  99. /** Class for inserting characters into Pantheios diagnostic logging
  100. * statements.
  101. *
  102. * \ingroup group__application_layer_interface__inserters
  103. *
  104. * This class converts a character variable into a string, thereby enabling
  105. * it to be inserted into a logging statement. Consider the following
  106. * statement:
  107. *
  108. * \code
  109. char c = '#';
  110. char s[] = "abc";
  111. std::string str("def");
  112. pantheios::log(pantheios::notice, "s=", s, ", c=", pantheios::character(c), ", str=", str);
  113. * \endcode
  114. *
  115. * This will produce the output:
  116. *
  117. * &nbsp;&nbsp;&nbsp;&nbsp;<b>s=abc, c=#, str=def</b>
  118. *
  119. * \note Currently, Pantheios does not support the insertion of character types
  120. * in diagnostic logging statements, due to the various ambiguities inherent
  121. * in the C++ language. (See chapters 14, 15, 19, 24 of
  122. * <a href = "http://imperfectcplusplus.com" target="_blank">Imperfect C++</a>
  123. * for discussions of these issues.) It is possible that a future version of
  124. * the library will be able to incorporate them directly, so long as that does
  125. * not sacrifice Pantheios's central claim of not paying for what you don't use.
  126. */
  127. class character
  128. {
  129. public:
  130. /// This type
  131. typedef character class_type;
  132. public:
  133. /// Construct from a <code>char</code> value
  134. explicit character(pan_char_t value);
  135. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  136. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  137. explicit character(char value);
  138. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  139. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  140. private:
  141. explicit character(int); // Prevents implicit conversion from an integer
  142. public:
  143. /// A possibly non-nul-terminated non-null pointer to the c-style string representation of the character
  144. pan_char_t const* data() const;
  145. /// A nul-terminated non-null pointer to the c-style string representation of the character
  146. pan_char_t const* c_str() const;
  147. /// The length of the c-style string representation of the character
  148. size_t length() const;
  149. private:
  150. pan_char_t m_value[2];
  151. private:
  152. #if !defined(STLSOFT_COMPILER_IS_GCC)
  153. character(class_type const&);
  154. #endif /* compiler */
  155. class_type& operator =(class_type const&);
  156. };
  157. /* /////////////////////////////////////////////////////////////////////////
  158. * String Access Shims
  159. */
  160. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  161. # if !defined(PANTHEIOS_NO_NAMESPACE)
  162. namespace shims
  163. {
  164. # endif /* !PANTHEIOS_NO_NAMESPACE */
  165. /** \overload c_str_data_a(character const&) */
  166. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  167. inline wchar_t const* c_str_data_w(character const& r)
  168. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  169. inline char const* c_str_data_a(character const& r)
  170. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  171. {
  172. return r.data();
  173. }
  174. /** \overload c_str_data(character const&) */
  175. inline pan_char_t const* c_str_data(character const& r)
  176. {
  177. return r.data();
  178. }
  179. /** \overload c_str_len_a(character const&) */
  180. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  181. inline size_t c_str_len_w(character const& r)
  182. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  183. inline size_t c_str_len_a(character const& r)
  184. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  185. {
  186. return r.length();
  187. }
  188. /** \overload c_str_len(character const&) */
  189. inline size_t c_str_len(character const& r)
  190. {
  191. return r.length();
  192. }
  193. /** \overload c_str_ptr_a(character const&) */
  194. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  195. inline wchar_t const* c_str_ptr_a(character const& r)
  196. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  197. inline char const* c_str_ptr_a(character const& r)
  198. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  199. {
  200. return r.c_str();
  201. }
  202. /** \overload c_str_ptr(character const&) */
  203. inline pan_char_t const* c_str_ptr(character const& r)
  204. {
  205. return r.c_str();
  206. }
  207. # if !defined(PANTHEIOS_NO_NAMESPACE)
  208. } /* namespace shims */
  209. # if defined(STLSOFT_COMPILER_IS_GCC)
  210. /* GCC does not seem to correctly handle the phases of
  211. * processing of C++ templates, so we need to 'use' the
  212. * shims into the same namespace as the inserter class
  213. * in order that ADL can suffice instead.
  214. */
  215. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  216. using ::pantheios::shims::c_str_data_w;
  217. using ::pantheios::shims::c_str_len_w;
  218. using ::pantheios::shims::c_str_ptr_w;
  219. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  220. using ::pantheios::shims::c_str_data_a;
  221. using ::pantheios::shims::c_str_len_a;
  222. using ::pantheios::shims::c_str_ptr_a;
  223. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  224. using ::pantheios::shims::c_str_data;
  225. using ::pantheios::shims::c_str_len;
  226. using ::pantheios::shims::c_str_ptr;
  227. # endif /* compiler */
  228. # endif /* !PANTHEIOS_NO_NAMESPACE */
  229. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  230. /* /////////////////////////////////////////////////////////////////////////
  231. * Implementation
  232. */
  233. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  234. inline /* explicit */ character::character(pan_char_t value)
  235. {
  236. m_value[0] = value;
  237. m_value[1] = '\0';
  238. }
  239. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  240. inline /* explicit */ character::character(char value)
  241. {
  242. m_value[0] = value;
  243. m_value[1] = '\0';
  244. }
  245. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  246. inline pan_char_t const* character::data() const
  247. {
  248. return m_value;
  249. }
  250. inline pan_char_t const* character::c_str() const
  251. {
  252. return m_value;
  253. }
  254. inline size_t character::length() const
  255. {
  256. return 1;
  257. }
  258. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  259. /* /////////////////////////////////////////////////////////////////////////
  260. * Namespace
  261. */
  262. #if !defined(PANTHEIOS_NO_NAMESPACE)
  263. } /* namespace pantheios */
  264. namespace stlsoft
  265. {
  266. // 'Export' the string access shims into the STLSoft namespace
  267. //
  268. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  269. // defined and exported in order to allow for the case where someone
  270. // may find a legitimate use for the conversion classes additional to
  271. // the type-tunneling of the Pantheios API.
  272. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  273. using ::pantheios::shims::c_str_data_w;
  274. using ::pantheios::shims::c_str_len_w;
  275. using ::pantheios::shims::c_str_ptr_w;
  276. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  277. using ::pantheios::shims::c_str_data_a;
  278. using ::pantheios::shims::c_str_len_a;
  279. using ::pantheios::shims::c_str_ptr_a;
  280. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  281. using ::pantheios::shims::c_str_data;
  282. using ::pantheios::shims::c_str_len;
  283. using ::pantheios::shims::c_str_ptr;
  284. }
  285. #endif /* !PANTHEIOS_NO_NAMESPACE */
  286. /* /////////////////////////////////////////////////////////////////////////
  287. * Inclusion
  288. */
  289. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  290. # pragma once
  291. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  292. /* ////////////////////////////////////////////////////////////////////// */
  293. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_CHARACTER */
  294. /* ///////////////////////////// end of file //////////////////////////// */