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.

282 lines
7.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: pantheios/inserters/adaptor.hpp
  3. *
  4. * Purpose: Inserter adaptors for the Pantheios application layer.
  5. *
  6. * Created: 29th July 2006
  7. * Updated: 18th June 2012
  8. *
  9. * Home: http://www.pantheios.org/
  10. *
  11. * Copyright (c) 2006-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/adaptor.hpp
  41. *
  42. * [C++ only] Inserter adaptors for the Pantheios application layer.
  43. */
  44. #ifndef PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INSERTER
  45. #define PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INSERTER
  46. /* /////////////////////////////////////////////////////////////////////////
  47. * Version information
  48. */
  49. #ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
  50. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INSERTER_MAJOR 1
  51. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INSERTER_MINOR 2
  52. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INSERTER_REVISION 4
  53. # define PANTHEIOS_VER_PANTHEIOS_INSERTERS_HPP_INSERTER_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. /* /////////////////////////////////////////////////////////////////////////
  62. * Namespace
  63. */
  64. #if !defined(PANTHEIOS_NO_NAMESPACE)
  65. namespace pantheios
  66. {
  67. #endif /* !PANTHEIOS_NO_NAMESPACE */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Classes
  70. */
  71. /**
  72. *
  73. * \ingroup group__application_layer_interface__inserters
  74. *
  75. */
  76. template< ss_typename_param_k T
  77. , ss_typename_param_k S
  78. >
  79. class inserter
  80. {
  81. public:
  82. typedef T value_type;
  83. typedef S string_type;
  84. typedef inserter<T, S> class_type;
  85. public:
  86. inserter(T value, S (*pfn)(T))
  87. : m_sourceValue(value)
  88. , m_convertedValue()
  89. , m_pfn(pfn)
  90. , m_bInitialised(false)
  91. {}
  92. public:
  93. pan_char_t const* data() const
  94. {
  95. if(!m_bInitialised)
  96. {
  97. convert_();
  98. }
  99. return m_convertedValue.data();
  100. }
  101. size_t length() const
  102. {
  103. if(!m_bInitialised)
  104. {
  105. convert_();
  106. }
  107. return m_convertedValue.length();
  108. }
  109. private:
  110. void convert_() const
  111. {
  112. const_cast<class_type*>(this)->convert_();
  113. }
  114. void convert_()
  115. {
  116. m_convertedValue = m_pfn(m_sourceValue);
  117. m_bInitialised = true;
  118. }
  119. private:
  120. T m_sourceValue;
  121. S m_convertedValue;
  122. S (*m_pfn)(T);
  123. bool m_bInitialised;
  124. private:
  125. class_type& operator =(class_type const&);
  126. };
  127. /* /////////////////////////////////////////////////////////////////////////
  128. * Inserter functions
  129. */
  130. template< ss_typename_param_k T
  131. , ss_typename_param_k S
  132. >
  133. inline inserter<T, S> insert(T &value, S (*pfn)(T))
  134. {
  135. return inserter<T, S>(value, pfn);
  136. }
  137. #if 0
  138. template< ss_typename_param_k T
  139. , ss_typename_param_k S
  140. >
  141. inline inserter<T const&, S> insert(T const& value, S (*pfn)(T const&))
  142. {
  143. return inserter<T const&, S>(value, pfn);
  144. }
  145. template< ss_typename_param_k T
  146. , ss_typename_param_k S
  147. >
  148. inline inserter<T const*, S> insert(T const* value, S (*pfn)(T const*))
  149. {
  150. return inserter<T const*, S>(value, pfn);
  151. }
  152. template< ss_typename_param_k T
  153. , ss_typename_param_k S
  154. >
  155. inline inserter<T*, S> insert(T *value, S (*pfn)(T*))
  156. {
  157. return inserter<T*, S>(value, pfn);
  158. }
  159. #endif /* 0 */
  160. /* /////////////////////////////////////////////////////////////////////////
  161. * Shim functions
  162. */
  163. # if !defined(PANTHEIOS_NO_NAMESPACE)
  164. namespace shims
  165. {
  166. # endif /* !PANTHEIOS_NO_NAMESPACE */
  167. template< ss_typename_param_k T
  168. , ss_typename_param_k S
  169. >
  170. inline
  171. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  172. wchar_t const*
  173. c_str_data_w(inserter<T, S> const& i)
  174. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  175. char const*
  176. c_str_data_a(inserter<T, S> const& i)
  177. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  178. {
  179. return i.data();
  180. }
  181. template< ss_typename_param_k T
  182. , ss_typename_param_k S
  183. >
  184. inline
  185. size_t
  186. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  187. c_str_len_w(inserter<T, S> const& i)
  188. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  189. c_str_len_a(inserter<T, S> const& i)
  190. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  191. {
  192. return i.length();
  193. }
  194. # if !defined(PANTHEIOS_NO_NAMESPACE)
  195. } /* namespace shims */
  196. # if defined(STLSOFT_COMPILER_IS_GCC)
  197. /* GCC does not seem to correctly handle the phases of
  198. * processing of C++ templates, so we need to 'use' the
  199. * shims into the same namespace as the inserter class
  200. * in order that ADL can suffice instead.
  201. */
  202. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  203. using ::pantheios::shims::c_str_data_w;
  204. using ::pantheios::shims::c_str_len_w;
  205. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  206. using ::pantheios::shims::c_str_data_a;
  207. using ::pantheios::shims::c_str_len_a;
  208. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  209. # endif /* compiler */
  210. # endif /* !PANTHEIOS_NO_NAMESPACE */
  211. /* /////////////////////////////////////////////////////////////////////////
  212. * Namespace
  213. */
  214. #if !defined(PANTHEIOS_NO_NAMESPACE)
  215. } /* namespace pantheios */
  216. namespace stlsoft
  217. {
  218. // 'Export' the string access shims into the STLSoft namespace
  219. //
  220. // c_str_ptr(_a) is not necessary for version 1.0 of Pantheios, but it's
  221. // defined and exported in order to allow for the case where someone
  222. // may find a legitimate use for the conversion classes additional to
  223. // the type-tunneling of the Pantheios API.
  224. # ifdef PANTHEIOS_USE_WIDE_STRINGS
  225. using ::pantheios::shims::c_str_data_w;
  226. using ::pantheios::shims::c_str_len_w;
  227. # else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  228. using ::pantheios::shims::c_str_data_a;
  229. using ::pantheios::shims::c_str_len_a;
  230. # endif /* PANTHEIOS_USE_WIDE_STRINGS */
  231. }
  232. #endif /* !PANTHEIOS_NO_NAMESPACE */
  233. /* /////////////////////////////////////////////////////////////////////////
  234. * Inclusion
  235. */
  236. #ifdef STLSOFT_PPF_pragma_once_SUPPORT
  237. # pragma once
  238. #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
  239. /* ////////////////////////////////////////////////////////////////////// */
  240. #endif /* !PANTHEIOS_INCL_PANTHEIOS_INSERTERS_HPP_INSERTER */
  241. /* ///////////////////////////// end of file //////////////////////////// */