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.

325 lines
9.7 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/boolean.hpp
  3. *
  4. * Purpose: String inserter for booleans.
  5. *
  6. * Created: 3rd August 2008
  7. * Updated: 18th June 2012
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2008-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 names of Matthew Wilson and Synesis Software nor the names
  24. * of any contributors may be used to endorse or promote products derived
  25. * 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/boolean.hpp
  41. *
  42. * [C++ only] String inserter for booleans.
  43. */
  44. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_BOOLEAN
  45. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_BOOLEAN
  46. /* /////////////////////////////////////////////////////////////////////////
  47. * Version information
  48. */
  49. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  50. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_BOOLEAN_MAJOR 1
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_BOOLEAN_MINOR 3
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_BOOLEAN_REVISION 3
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_BOOLEAN_EDIT 17
  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. /* /////////////////////////////////////////////////////////////////////////
  65. * Namespace
  66. */
  67. #if !defined(PANTHEIOS_NO_NAMESPACE)
  68. namespace pantheios
  69. {
  70. #else /* ? !PANTHEIOS_NO_NAMESPACE */
  71. # if defined(__RPCNDR_H__)
  72. # error pantheios::boolean may not be used with Windows when namespace is suspended. If you are including pantheios/inserters.hpp, try including only those specific inserter files that you need
  73. # endif /* rpcndr.h */
  74. #endif /* !PANTHEIOS_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * Classes
  77. */
  78. /** Class for inserting Boolean types into Pantheios diagnostic logging
  79. * statements.
  80. *
  81. * \ingroup group__application_layer_interface__inserters
  82. *
  83. * Consider the following statement:
  84. *
  85. * \code
  86. char s[] = "abc";
  87. std::string str("def");
  88. bool b = false;
  89. pantheios::log(pantheios::notice, "s=", s, ", b=", pantheios::boolean(b), ", str=", str);
  90. * \endcode
  91. *
  92. * This will produce the output:
  93. *
  94. * &nbsp;&nbsp;&nbsp;&nbsp;<b>s=abc, b=false, str=def</b>
  95. *
  96. * \note The class provides the static method set_value_strings() for
  97. * assigning user-defined strings to represent true and false values.
  98. */
  99. class boolean
  100. {
  101. /// \name Member Types
  102. /// @{
  103. public:
  104. typedef boolean class_type;
  105. /// @}
  106. /// \name Construction
  107. /// @{
  108. public:
  109. /// Construct from a boolean value
  110. ///
  111. /// \param value The boolean whose value will be represented as a string
  112. explicit boolean(bool value)
  113. : m_value(value)
  114. {}
  115. private:
  116. class_type& operator =(class_type const&);
  117. /// @}
  118. /// \name Control
  119. /// @{
  120. public:
  121. static void set_value_strings(pan_char_t const* falseName, pan_char_t const* trueName) /* throw(std::bad_alloc) */;
  122. /// @}
  123. /// \name Accessors
  124. /// @{
  125. public:
  126. /// A possibly non-nul-terminated non-null pointer to the c-style string representation of the integer
  127. pan_char_t const* data() const
  128. {
  129. return get_slice_(m_value)->ptr;
  130. }
  131. /// A nul-terminated non-null pointer to the c-style string representation of the integer
  132. pan_char_t const* c_str() const
  133. {
  134. return get_slice_(m_value)->ptr;
  135. }
  136. /// The length of the c-style string representation of the integer
  137. size_t length() const
  138. {
  139. return get_slice_(m_value)->len;
  140. }
  141. /// @}
  142. /// \name Implementation
  143. /// @{
  144. private:
  145. virtual
  146. pan_slice_t const* get_slice_(bool value) const
  147. {
  148. return get_slice_(0, value);
  149. }
  150. protected:
  151. static pan_slice_t const* get_slice_(
  152. int index
  153. , bool value
  154. );
  155. /// @}
  156. /// \name Member Variables
  157. /// @{
  158. protected:
  159. const bool m_value;
  160. /// @}
  161. };
  162. /* /////////////////////////////////////////////////////////////////////////
  163. * Implementation
  164. */
  165. /* /////////////////////////////////////////////////////////////////////////
  166. * String Access Shims
  167. */
  168. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  169. # if !defined(PANTHEIOS_NO_NAMESPACE)
  170. namespace shims
  171. {
  172. # endif /* !PANTHEIOS_NO_NAMESPACE */
  173. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  174. /** \overload c_str_data_w(boolean const&) */
  175. inline wchar_t const* c_str_data_w(boolean const& b)
  176. {
  177. return b.data();
  178. }
  179. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  180. /** \overload c_str_data_a(boolean const&) */
  181. inline char const* c_str_data_a(boolean const& b)
  182. {
  183. return b.data();
  184. }
  185. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  186. /** \overload c_str_data(boolean const&) */
  187. inline pan_char_t const* c_str_data(boolean const& b)
  188. {
  189. return b.data();
  190. }
  191. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  192. /** \overload c_str_len_w(boolean const&) */
  193. inline size_t c_str_len_w(boolean const& b)
  194. {
  195. return b.length();
  196. }
  197. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  198. /** \overload c_str_len_a(boolean const&) */
  199. inline size_t c_str_len_a(boolean const& b)
  200. {
  201. return b.length();
  202. }
  203. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  204. /** \overload c_str_len(boolean const&) */
  205. inline size_t c_str_len(boolean const& b)
  206. {
  207. return b.length();
  208. }
  209. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  210. /** \overload c_str_ptr_w(boolean const&) */
  211. inline wchar_t const* c_str_ptr_w(boolean const& b)
  212. {
  213. return b.c_str();
  214. }
  215. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  216. /** \overload c_str_ptr_a(boolean const&) */
  217. inline char const* c_str_ptr_a(boolean const& b)
  218. {
  219. return b.c_str();
  220. }
  221. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  222. /** \overload c_str_ptr(boolean const&) */
  223. inline pan_char_t const* c_str_ptr(boolean const& b)
  224. {
  225. return b.c_str();
  226. }
  227. # if !defined(PANTHEIOS_NO_NAMESPACE)
  228. } /* namespace shims */
  229. # if defined(STLSOFT_COMPILER_IS_GCC)
  230. /* GCC does not seem to correctly handle the phases of
  231. * processing of C++ templates, so we need to 'use' the
  232. * shims into the same namespace as the inserter class
  233. * in order that ADL can suffice instead.
  234. */
  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. # endif /* compiler */
  248. # endif /* !PANTHEIOS_NO_NAMESPACE */
  249. #endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */
  250. /* /////////////////////////////////////////////////////////////////////////
  251. * Namespace
  252. */
  253. #if !defined(PANTHEIOS_NO_NAMESPACE)
  254. } /* namespace pantheios */
  255. namespace stlsoft
  256. {
  257. // 'Export' the string access shims into the STLSoft namespace
  258. //
  259. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  260. // defined and exported in order to allow for the case where someone
  261. // may find a legitimate use for the conversion classes additional to
  262. // the type-tunneling of the Pantheios API.
  263. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  264. using ::pantheios::shims::c_str_data_w;
  265. using ::pantheios::shims::c_str_len_w;
  266. using ::pantheios::shims::c_str_ptr_w;
  267. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  268. using ::pantheios::shims::c_str_data_a;
  269. using ::pantheios::shims::c_str_len_a;
  270. using ::pantheios::shims::c_str_ptr_a;
  271. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  272. using ::pantheios::shims::c_str_data;
  273. using ::pantheios::shims::c_str_len;
  274. using ::pantheios::shims::c_str_ptr;
  275. }
  276. #endif /* !PANTHEIOS_NO_NAMESPACE */
  277. /* /////////////////////////////////////////////////////////////////////////
  278. * Inclusion
  279. */
  280. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  281. # pragma once
  282. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  283. /* ////////////////////////////////////////////////////////////////////// */
  284. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_BOOLEAN */
  285. /* ///////////////////////////// end of file //////////////////////////// */