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.

379 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/slice.hpp
  3. *
  4. * Purpose: String inserter for slices of strings.
  5. *
  6. * Created: 13th February 2010
  7. * Updated: 17th March 2012
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2010-2012, 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/slice.hpp
  41. *
  42. * [c++ only] definition of the pantheios::slice string inserter for
  43. * for string slices.
  44. */
  45. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD
  46. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD
  47. /* /////////////////////////////////////////////////////////////////////////
  48. * Version information
  49. */
  50. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_MAJOR 1
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_MINOR 0
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_REVISION 3
  54. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_PAD_EDIT 6
  55. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Includes
  58. */
  59. #ifndef PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS
  60. # include <pantheios/pantheios.h>
  61. #endif /* !PANTHEIOS_INCL_PANTHEIOS_H_PANTHEIOS */
  62. #include <stlsoft/memory/auto_buffer.hpp>
  63. #include <stlsoft/shims/access/string.hpp>
  64. /* /////////////////////////////////////////////////////////////////////////
  65. * Namespace
  66. */
  67. #if !defined(PANTHEIOS_NO_NAMESPACE)
  68. namespace pantheios
  69. {
  70. #endif /* !PANTHEIOS_NO_NAMESPACE */
  71. /* /////////////////////////////////////////////////////////////////////////
  72. * Classes
  73. */
  74. #ifdef PANTHEIOS_CF_NAMESPACE_SUPPORT
  75. //namespace inserters
  76. //{
  77. #endif /* PANTHEIOS_CF_NAMESPACE_SUPPORT */
  78. /** Class for inserting string slices into Pantheios diagnostic logging
  79. * statements.
  80. */
  81. class slice_inserter
  82. {
  83. /// \name Member Types
  84. /// @{
  85. public:
  86. typedef slice_inserter class_type;
  87. private:
  88. typedef stlsoft::auto_buffer<pan_char_t, 256> buffer_type_;
  89. /// @}
  90. /// \name Construction
  91. /// @{
  92. public:
  93. slice_inserter(
  94. pan_char_t const* str
  95. , size_t len
  96. , pan_char_t const* strName
  97. , pan_char_t const* lenName
  98. , pan_char_t const* equals
  99. , pan_char_t const* separator
  100. );
  101. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  102. slice_inserter(class_type const& rhs);
  103. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  104. /// @}
  105. /// \name Accessors
  106. /// @{
  107. public:
  108. /// A possibly non-nul-terminated non-null pointer to the c-style string representation of the slice
  109. pan_char_t const* data() const;
  110. /// A nul-terminated non-null pointer to the c-style string representation of the slice
  111. pan_char_t const* c_str() const;
  112. /// The length of the c-style string representation of the slice
  113. size_t length() const;
  114. /// @}
  115. /// \name Implementation
  116. /// @{
  117. private:
  118. void construct_() const;
  119. void construct_();
  120. /// @}
  121. /// \name Member Variables
  122. /// @{
  123. private:
  124. pan_char_t const* m_str;
  125. size_t m_len;
  126. pan_char_t const* m_strName;
  127. pan_char_t const* m_lenName;
  128. pan_char_t const* m_equals;
  129. pan_char_t const* m_separator;
  130. buffer_type_ m_buffer;
  131. /// @}
  132. /// \name Not to be implemented
  133. /// @{
  134. private:
  135. class_type& operator =(class_type const&);
  136. /// @}
  137. };
  138. #ifdef PANTHEIOS_CF_NAMESPACE_SUPPORT
  139. //namespace inserters
  140. //{
  141. #endif /* PANTHEIOS_CF_NAMESPACE_SUPPORT */
  142. /* /////////////////////////////////////////////////////////////////////////
  143. * Inserter functions
  144. */
  145. /** Defines slice of a string
  146. *
  147. * \param str Pointer to the first character in the string to be sliced
  148. * \param len Number of characters to be sliced
  149. *
  150. */
  151. inline pan_slice_t slice(
  152. pan_char_t const* str
  153. , size_t len
  154. )
  155. {
  156. return pan_slice_t(str, len);
  157. }
  158. /** Defines a slice of string
  159. *
  160. * \param str Pointer to the first character in the string to be sliced
  161. * \param len Number of characters to be sliced
  162. * \param strName Name of the string parameter
  163. */
  164. inline slice_inserter slice(
  165. pan_char_t const* str
  166. , size_t len
  167. , pan_char_t const* strName
  168. )
  169. {
  170. return slice_inserter(str, len, strName, NULL, NULL, NULL);
  171. }
  172. /** Defines a slice of string
  173. *
  174. * \param str Pointer to the first character in the string to be sliced
  175. * \param len Number of characters to be sliced
  176. * \param strName Name of the string parameter
  177. * \param lenName Name of the length parameter
  178. */
  179. inline slice_inserter slice(
  180. pan_char_t const* str
  181. , size_t len
  182. , pan_char_t const* strName
  183. , pan_char_t const* lenName
  184. )
  185. {
  186. return slice_inserter(str, len, strName, lenName, NULL, NULL);
  187. }
  188. /** Defines a slice of string
  189. *
  190. * \param str Pointer to the first character in the string to be sliced
  191. * \param len Number of characters to be sliced
  192. * \param strName Name of the string parameter
  193. * \param lenName Name of the length parameter
  194. * \param equals The string that separates the name and the value
  195. */
  196. inline slice_inserter slice(
  197. pan_char_t const* str
  198. , size_t len
  199. , pan_char_t const* strName
  200. , pan_char_t const* lenName
  201. , pan_char_t const* equals
  202. )
  203. {
  204. return slice_inserter(str, len, strName, lenName, equals, NULL);
  205. }
  206. /** Defines a slice of string
  207. *
  208. * \param str Pointer to the first character in the string to be sliced
  209. * \param len Number of characters to be sliced
  210. * \param strName Name of the string parameter
  211. * \param lenName Name of the length parameter
  212. * \param equals The string that separates the name and the value
  213. * \param separator The string to be used between the string and length parameters
  214. */
  215. inline slice_inserter slice(
  216. pan_char_t const* str
  217. , size_t len
  218. , pan_char_t const* strName
  219. , pan_char_t const* lenName
  220. , pan_char_t const* equals
  221. , pan_char_t const* separator
  222. )
  223. {
  224. return slice_inserter(str, len, strName, lenName, equals, separator);
  225. }
  226. /* /////////////////////////////////////////////////////////////////////////
  227. * String Access Shims
  228. */
  229. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  230. # if !defined(PANTHEIOS_NO_NAMESPACE)
  231. namespace shims
  232. {
  233. # endif /* !PANTHEIOS_NO_NAMESPACE */
  234. /** \overload c_str_data_a(slice_inserter const&) */
  235. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  236. inline wchar_t const* c_str_data_w(slice_inserter const& i)
  237. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  238. inline char const* c_str_data_a(slice_inserter const& i)
  239. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  240. {
  241. return i.data();
  242. }
  243. /** \overload c_str_data(slice_inserter const&) */
  244. inline pan_char_t const* c_str_data(slice_inserter const& i)
  245. {
  246. return i.data();
  247. }
  248. /** \overload c_str_len_a(slice_inserter const&) */
  249. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  250. inline size_t c_str_len_w(slice_inserter const& i)
  251. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  252. inline size_t c_str_len_a(slice_inserter const& i)
  253. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  254. {
  255. return i.length();
  256. }
  257. /** \overload c_str_len(slice_inserter const&) */
  258. inline size_t c_str_len(slice_inserter const& i)
  259. {
  260. return i.length();
  261. }
  262. /** \overload c_str_ptr_a(slice_inserter const&) */
  263. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  264. inline wchar_t const* c_str_ptr_w(slice_inserter const& i)
  265. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  266. inline char const* c_str_ptr_a(slice_inserter const& i)
  267. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  268. {
  269. return i.c_str();
  270. }
  271. /** \overload c_str_ptr(slice_inserter const&) */
  272. inline pan_char_t const* c_str_ptr(slice_inserter const& i)
  273. {
  274. return i.c_str();
  275. }
  276. # if !defined(PANTHEIOS_NO_NAMESPACE)
  277. } /* namespace shims */
  278. # if defined(STLSOFT_COMPILER_IS_GCC)
  279. /* GCC does not seem to correctly handle the phases of
  280. * processing of C++ templates, so we need to 'use' the
  281. * shims into the same namespace as the inserter class
  282. * in order that ADL can suffice instead.
  283. */
  284. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  285. using ::pantheios::shims::c_str_data_w;
  286. using ::pantheios::shims::c_str_len_w;
  287. using ::pantheios::shims::c_str_ptr_w;
  288. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  289. using ::pantheios::shims::c_str_data_a;
  290. using ::pantheios::shims::c_str_len_a;
  291. using ::pantheios::shims::c_str_ptr_a;
  292. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  293. using ::pantheios::shims::c_str_data;
  294. using ::pantheios::shims::c_str_len;
  295. using ::pantheios::shims::c_str_ptr;
  296. # endif /* compiler */
  297. # endif /* !PANTHEIOS_NO_NAMESPACE */
  298. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  299. /* /////////////////////////////////////////////////////////////////////////
  300. * Namespace
  301. */
  302. #if !defined(PANTHEIOS_NO_NAMESPACE)
  303. } /* namespace pantheios */
  304. namespace stlsoft
  305. {
  306. // 'Export' the string access shims into the STLSoft namespace
  307. //
  308. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  309. // defined and exported in order to allow for the case where someone
  310. // may find a legitimate use for the conversion classes additional to
  311. // the type-tunneling of the Pantheios API.
  312. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  313. using ::pantheios::shims::c_str_data_w;
  314. using ::pantheios::shims::c_str_len_w;
  315. using ::pantheios::shims::c_str_ptr_w;
  316. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  317. using ::pantheios::shims::c_str_data_a;
  318. using ::pantheios::shims::c_str_len_a;
  319. using ::pantheios::shims::c_str_ptr_a;
  320. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  321. using ::pantheios::shims::c_str_data;
  322. using ::pantheios::shims::c_str_len;
  323. using ::pantheios::shims::c_str_ptr;
  324. }
  325. #endif /* !PANTHEIOS_NO_NAMESPACE */
  326. /* /////////////////////////////////////////////////////////////////////////
  327. * Inclusion
  328. */
  329. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  330. # pragma once
  331. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  332. /* ////////////////////////////////////////////////////////////////////// */
  333. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_PAD */
  334. /* ///////////////////////////// end of file //////////////////////////// */