STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

cstring_range.hpp

Go to the documentation of this file.
00001 /* 
00002  * File:        rangelib/cstring_range.hpp
00003  *
00004  * Purpose:     Range adaptor for C-strings.
00005  *
00006  * Created:     17th May 2004
00007  * Updated:     12th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00043 #ifndef STLSOFT_INCL_RANGELIB_HPP_CSTRING_RANGE
00044 #define STLSOFT_INCL_RANGELIB_HPP_CSTRING_RANGE
00045 
00046 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00047 # define STLSOFT_VER_RANGELIB_HPP_CSTRING_RANGE_MAJOR     1
00048 # define STLSOFT_VER_RANGELIB_HPP_CSTRING_RANGE_MINOR     2
00049 # define STLSOFT_VER_RANGELIB_HPP_CSTRING_RANGE_REVISION  1
00050 # define STLSOFT_VER_RANGELIB_HPP_CSTRING_RANGE_EDIT      7
00051 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00052 
00053 /* 
00054  * Includes
00055  */
00056 
00057 #ifndef STLSOFT_INCL_H_STLSOFT
00058 # include <stlsoft.h>                           // Include the STLSoft root header
00059 #endif /* !STLSOFT_INCL_H_STLSOFT */
00060 #ifndef STLSOFT_INCL_RANGELIB_HPP_RANGE_CATEGORIES
00061 # include <rangelib/range_categories.hpp>
00062 #endif /* !STLSOFT_INCL_RANGELIB_HPP_RANGE_CATEGORIES */
00063 #ifndef STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL
00064 # include <stlsoft_operator_bool.h> //
00065 #endif /* !STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL */
00066 #ifndef STLSOFT_INCL_H_STLSOFT_ITERATOR
00067 # include <stlsoft_iterator.h> //
00068 #endif /* !STLSOFT_INCL_H_STLSOFT_ITERATOR */
00069 #ifndef STLSOFT_INCL_H_STLSOFT_CONSTRAINTS
00070 # include <stlsoft_constraints.h>       // not_implicitly_comparable
00071 #endif /* !STLSOFT_INCL_H_STLSOFT_CONSTRAINTS */
00072 
00073 /* 
00074  * Namespace
00075  */
00076 
00077 #ifndef _STLSOFT_NO_NAMESPACE
00078 namespace stlsoft
00079 {
00080 #endif /* _STLSOFT_NO_NAMESPACE */
00081 
00082 /* 
00083 
00088 
00089 /* 
00090  * Classes
00091  */
00092 
00109 template <ss_typename_param_k C>
00110 class cstring_range
00111     : public notional_range_tag
00112 {
00113 public:
00115     typedef C                                               value_type;
00117     typedef cstring_range<C>                                class_type;
00119     typedef value_type const                                *const_pointer;
00121     typedef value_type const                                &const_reference;
00122 
00125 public:
00129     cstring_range(value_type const *s)
00130         : m_s(s)
00131     {
00132         stlsoft_message_assert("NULL string passed to cstring_range constructor", NULL != s);
00133     }
00135     ~cstring_range()
00136     {
00137         // This is a constraint to ensure that this template is not used
00138         // for any non-character types.
00139         stlsoft_static_assert(0 != is_integral_type<value_type>::value);
00140         stlsoft_static_assert(0 == is_numeric_type<value_type>::value);
00141         stlsoft_static_assert(0 == is_bool_type<value_type>::value);
00142     }
00144 
00147 private:
00148     STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, boolean_generator_type, boolean_type);
00149 public:
00151     ss_bool_t is_open() const
00152     {
00153         stlsoft_assert(NULL != m_s);
00154 
00155         return '\0' != *m_s;
00156     }
00158     const_reference current() const
00159     {
00160         stlsoft_message_assert("Attempting to access the value of a closed range", is_open());
00161 
00162         return *m_s;
00163     }
00165     class_type &advance()
00166     {
00167         stlsoft_message_assert("Attempting to advance a closed range", is_open());
00168 
00169         ++m_s;
00170 
00171         return *this;
00172     }
00173 
00175     operator boolean_type() const
00176     {
00177         return boolean_generator_type::translate(is_open());
00178     }
00180     const_reference operator *() const
00181     {
00182         return current();
00183     }
00185     class_type &operator ++()
00186     {
00187         return advance();
00188     }
00191     class_type operator ++(int)
00192     {
00193         class_type  ret(*this);
00194 
00195         operator ++();
00196 
00197         return ret;
00198     }
00200 
00201 // Members
00202 private:
00203     value_type const    *m_s;
00204 };
00205 
00207 // Unit-testing
00208 
00209 #ifdef STLSOFT_UNITTEST
00210 
00211 namespace unittest
00212 {
00213     namespace
00214     {
00215     } // anonymous namespace
00216 
00217     ss_bool_t test_stlsoft_rangelib_cstring_range(unittest_reporter *r)
00218     {
00219         using stlsoft::unittest::unittest_initialiser;
00220 
00221         ss_bool_t               bSuccess    =   true;
00222 
00223         unittest_initialiser    init(r, "RangeLib", "cstring_range", __FILE__);
00224 
00225         typedef cstring_range<char> cstring_range_t;
00226 
00227         cstring_range_t r1("Hello, Natty!");
00228         size_t          len;
00229 
00230         for(len = 0; r1; ++r1, ++len)
00231         {}
00232 
00233         if(13 != len)
00234         {
00235             r->report("manual enumeration failed", __LINE__);
00236             bSuccess = false;
00237         }
00238 
00239         if(r1.is_open())
00240         {
00241             r->report("closed range presents as open (is_open() method)", __LINE__);
00242             bSuccess = false;
00243         }
00244 
00245         if(r1)
00246         {
00247             r->report("closed range presents as open (operator \"bool\"())", __LINE__);
00248             bSuccess = false;
00249         }
00250 
00251         if(cstring_range_t(""))
00252         {
00253             r->report("closed range presents as open", __LINE__);
00254             bSuccess = false;
00255         }
00256 
00257         return bSuccess;
00258     }
00259 
00260     unittest_registrar    unittest_stlsoft_rangelib_cstring_range(test_stlsoft_rangelib_cstring_range);
00261 
00262 } // namespace unittest
00263 
00264 #endif /* STLSOFT_UNITTEST */
00265 
00266 /* 
00267 
00269 
00270 /* 
00271 
00272 #ifndef _STLSOFT_NO_NAMESPACE
00273 } // namespace stlsoft
00274 #endif /* _STLSOFT_NO_NAMESPACE */
00275 
00276 /* 
00277 
00278 #endif /* !STLSOFT_INCL_RANGELIB_HPP_CSTRING_RANGE */
00279 
00280 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004