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.

250 lines
8.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: rangelib/cstring_range.hpp
  3. *
  4. * Purpose: Range adaptor for C-strings.
  5. *
  6. * Created: 17th May 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-2009, 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 met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file rangelib/cstring_range.hpp Range adaptor for C-strings */
  40. #ifndef RANGELIB_INCL_RANGELIB_HPP_CSTRING_RANGE
  41. #define RANGELIB_INCL_RANGELIB_HPP_CSTRING_RANGE
  42. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  43. # define RANGELIB_VER_RANGELIB_HPP_CSTRING_RANGE_MAJOR 2
  44. # define RANGELIB_VER_RANGELIB_HPP_CSTRING_RANGE_MINOR 3
  45. # define RANGELIB_VER_RANGELIB_HPP_CSTRING_RANGE_REVISION 3
  46. # define RANGELIB_VER_RANGELIB_HPP_CSTRING_RANGE_EDIT 37
  47. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  48. /* /////////////////////////////////////////////////////////////////////////
  49. * Auto-generation and compatibility
  50. */
  51. /*
  52. [Incompatibilies-start]
  53. STLSOFT_COMPILER_IS_MSVC: _MSC_VER < 1200
  54. STLSOFT_COMPILER_IS_MWERKS: (__MWERKS__ & 0xFF00) < 0x3000
  55. [Incompatibilies-end]
  56. */
  57. /* /////////////////////////////////////////////////////////////////////////
  58. * Includes
  59. */
  60. #ifndef RANGELIB_INCL_RANGELIB_HPP_RANGELIB
  61. # include <rangelib/rangelib.hpp>
  62. #endif /* !RANGELIB_INCL_RANGELIB_HPP_RANGELIB */
  63. #ifndef RANGELIB_INCL_RANGELIB_HPP_RANGE_CATEGORIES
  64. # include <rangelib/range_categories.hpp>
  65. #endif /* !RANGELIB_INCL_RANGELIB_HPP_RANGE_CATEGORIES */
  66. #ifndef RANGELIB_INCL_RANGELIB_HPP_OPERATOR_ADAPTORS
  67. # include <rangelib/operator_adaptors.hpp>
  68. #endif /* !RANGELIB_INCL_RANGELIB_HPP_OPERATOR_ADAPTORS */
  69. #ifndef STLSOFT_INCL_STLSOFT_META_HPP_CAPABILITIES
  70. # include <stlsoft/meta/capabilities.hpp>
  71. #endif /* !STLSOFT_INCL_STLSOFT_META_HPP_CAPABILITIES */
  72. #ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_BOOL_TYPE
  73. # include <stlsoft/meta/is_bool_type.hpp>
  74. #endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_BOOL_TYPE */
  75. #ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE
  76. # include <stlsoft/meta/is_integral_type.hpp>
  77. #endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE */
  78. #ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_NUMERIC_TYPE
  79. # include <stlsoft/meta/is_numeric_type.hpp>
  80. #endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_NUMERIC_TYPE */
  81. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
  82. # include <stlsoft/util/constraints.hpp>
  83. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS */
  84. /* /////////////////////////////////////////////////////////////////////////
  85. * Namespace
  86. */
  87. #ifndef RANGELIB_NO_NAMESPACE
  88. # if defined(_STLSOFT_NO_NAMESPACE) || \
  89. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  90. /* There is no stlsoft namespace, so must define ::rangelib */
  91. namespace rangelib
  92. {
  93. # else
  94. /* Define stlsoft::rangelib_project */
  95. namespace stlsoft
  96. {
  97. namespace rangelib_project
  98. {
  99. # endif /* _STLSOFT_NO_NAMESPACE */
  100. #endif /* !RANGELIB_NO_NAMESPACE */
  101. /* /////////////////////////////////////////////////////////////////////////
  102. * Classes
  103. */
  104. /** \brief Traits type for cstring_range
  105. *
  106. * \ingroup group__library__rangelib
  107. */
  108. template <ss_typename_param_k C>
  109. struct cstring_range_traits
  110. {
  111. /// \name Types
  112. /// @{
  113. public:
  114. /// The value type
  115. typedef C value_type;
  116. /// The non-mutable (const) reference type
  117. typedef C const& const_reference;
  118. /// @}
  119. };
  120. /** \brief This class represents a C-style string as a range
  121. *
  122. * \ingroup group__library__rangelib
  123. *
  124. * It is categoried as a Notional Range
  125. *
  126. * It could be used as follows
  127. \code
  128. // Create a range based on a C-string
  129. stlsoft::cstring_range r("This is a literal string");
  130. // Count the number of i's in the string
  131. size_t num_Is = stlsoft::r_count(r, 'i');
  132. \endcode
  133. */
  134. template <ss_typename_param_k C>
  135. class cstring_range
  136. : public non_mutating_operator_adaptor< cstring_range<C>
  137. , cstring_range_traits<C>
  138. > // This provides the operator forms of the methods
  139. , public notional_range_tag
  140. {
  141. /// \name Types
  142. /// @{
  143. public:
  144. /// The value type
  145. typedef C value_type;
  146. /// The range tag type
  147. typedef notional_range_tag range_tag_type;
  148. /// The current parameterisation of the type
  149. typedef cstring_range<C> class_type;
  150. /// The non-mutable (const) reference type
  151. typedef value_type const& const_reference;
  152. /// @}
  153. /// \name Construction
  154. /// @{
  155. public:
  156. /// Constructor
  157. ///
  158. /// \param s The C-string for which this instance will act as a range
  159. cstring_range(value_type const* s)
  160. : m_s(s)
  161. {
  162. STLSOFT_MESSAGE_ASSERT("NULL string passed to cstring_range constructor", NULL != s);
  163. }
  164. /// Destructor
  165. ~cstring_range() stlsoft_throw_0()
  166. {
  167. // This is a constraint to ensure that this template is not used
  168. // for any non-character types.
  169. STLSOFT_STATIC_ASSERT(0 != is_integral_type<value_type>::value);
  170. STLSOFT_STATIC_ASSERT(0 == is_numeric_type<value_type>::value);
  171. STLSOFT_STATIC_ASSERT(0 == is_bool_type<value_type>::value);
  172. }
  173. /// @}
  174. /// \name Range methods
  175. /// @{
  176. public:
  177. /// Indicates whether the range is open
  178. ss_bool_t is_open() const
  179. {
  180. STLSOFT_ASSERT(NULL != m_s);
  181. return '\0' != *m_s;
  182. }
  183. /// Returns the current value in the range
  184. const_reference current() const
  185. {
  186. STLSOFT_MESSAGE_ASSERT("Attempting to access the value of a closed range", is_open());
  187. return *m_s;
  188. }
  189. /// Advances the current position in the range
  190. class_type& advance()
  191. {
  192. STLSOFT_MESSAGE_ASSERT("Attempting to advance a closed range", is_open());
  193. ++m_s;
  194. return *this;
  195. }
  196. /// @}
  197. // Members
  198. private:
  199. value_type const *m_s;
  200. };
  201. ////////////////////////////////////////////////////////////////////////////
  202. // Unit-testing
  203. #ifdef STLSOFT_UNITTEST
  204. # include "./unittest/cstring_range_unittest_.h"
  205. #endif /* STLSOFT_UNITTEST */
  206. /* ////////////////////////////////////////////////////////////////////// */
  207. #ifndef RANGELIB_NO_NAMESPACE
  208. # if defined(_STLSOFT_NO_NAMESPACE) || \
  209. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  210. } // namespace rangelib
  211. # else
  212. } // namespace rangelib_project
  213. } // namespace stlsoft
  214. # endif /* _STLSOFT_NO_NAMESPACE */
  215. #endif /* !RANGELIB_NO_NAMESPACE */
  216. /* ////////////////////////////////////////////////////////////////////// */
  217. #endif /* !RANGELIB_INCL_RANGELIB_HPP_CSTRING_RANGE */
  218. /* ///////////////////////////// end of file //////////////////////////// */