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  

filtered_range.hpp

Go to the documentation of this file.
00001 /* 
00002  * File:        rangelib/filtered_range.hpp
00003  *
00004  * Purpose:     Range filter adaptor.
00005  *
00006  * Created:     9th July 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_FILTERED_RANGE
00044 #define STLSOFT_INCL_RANGELIB_HPP_FILTERED_RANGE
00045 
00046 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00047 # define STLSOFT_VER_RANGELIB_HPP_FILTERED_RANGE_MAJOR    1
00048 # define STLSOFT_VER_RANGELIB_HPP_FILTERED_RANGE_MINOR    0
00049 # define STLSOFT_VER_RANGELIB_HPP_FILTERED_RANGE_REVISION 2
00050 # define STLSOFT_VER_RANGELIB_HPP_FILTERED_RANGE_EDIT     3
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>             // stlsoft::operator_bool_generator
00065 #endif /* !STLSOFT_INCL_H_STLSOFT_OPERATOR_BOOL */
00066 #ifndef STLSOFT_INCL_H_STLSOFT_META
00067 # include <stlsoft_meta.h>                      // TMP stuff
00068 #endif /* !STLSOFT_INCL_H_STLSOFT_META */
00069 #ifndef STLSOFT_INCL_H_STLSOFT_FILTER_ITERATOR
00070 # include <stlsoft_filter_iterator.h>           // stlsoft::filtered_iterator
00071 #endif /* !STLSOFT_INCL_H_STLSOFT_FILTER_ITERATOR */
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 
00098 template<   ss_typename_param_k R
00099         ,   ss_typename_param_k P
00100         ,   ss_typename_param_k RT = ss_typename_type_k R::range_tag_type
00101         >
00102 class filtered_range
00103     : public RT
00104 {
00105 public:
00106     typedef R                                                                   filtered_range_type;
00107     typedef P                                                                   filter_predicate_type;
00108     typedef RT                                                                  range_tag_type;
00109     typedef filtered_range<R, P, RT>                                            class_type;
00110 private:
00111     typedef ss_typename_type_k filtered_range_type::iterator                    iterator_base_type;
00112     typedef ss_typename_type_k filtered_range_type::const_iterator              const_iterator_base_type;
00113 public:
00114     typedef filtered_iterator<iterator_base_type, filter_predicate_type>        iterator;
00115     typedef filtered_iterator<const_iterator_base_type, filter_predicate_type>  const_iterator;
00116     typedef ss_typename_type_k filtered_range_type::reference                   reference;
00117     typedef ss_typename_type_k filtered_range_type::const_reference             const_reference;
00118     typedef ss_typename_type_k filtered_range_type::value_type                  value_type;
00119 
00120 public:
00125     filtered_range(filtered_range_type r, filter_predicate_type pr)
00126         : m_range(r)
00127         , m_predicate(pr)
00128     {
00129         for(; m_range; ++m_range)
00130         {
00131             if(m_predicate(*m_range))
00132             {
00133                 break;
00134             }
00135         }
00136     }
00137 
00140 private:
00141     STLSOFT_DEFINE_OPERATOR_BOOL_TYPES_T(class_type, boolean_generator_type, boolean_type);
00142 public:
00144     ss_bool_t is_open() const
00145     {
00146         return m_range.is_open();
00147     }
00149     reference current()
00150     {
00151         stlsoft_assert(is_open());
00152 
00153         return m_range.current();
00154     }
00156     const_reference current() const
00157     {
00158         stlsoft_assert(is_open());
00159 
00160         return m_range.current();
00161     }
00163     class_type &advance()
00164     {
00165         stlsoft_message_assert("Attempting to increment the range past its end point", is_open());
00166 
00167         for(++m_range; m_range; ++m_range)
00168         {
00169             if(m_predicate(*m_range))
00170             {
00171                 break;
00172             }
00173         }
00174 
00175         return *this;
00176     }
00177 
00179     operator boolean_type() const
00180     {
00181         return boolean_generator_type::translate(is_open());
00182     }
00184     reference operator *()
00185     {
00186         return current();
00187     }
00189     const_reference operator *() const
00190     {
00191         return current();
00192     }
00194     class_type &operator ++()
00195     {
00196         return advance();
00197     }
00200     class_type operator ++(int)
00201     {
00202         class_type  ret(*this);
00203 
00204         operator ++();
00205 
00206         return ret;
00207     }
00209 
00212 public:
00214     iterator begin()
00215     {
00216         return iterator(m_range.begin(), m_range.end(), m_predicate);
00217     }
00219     iterator end()
00220     {
00221         return iterator(m_range.end(), m_range.end(), m_predicate);
00222     }
00223 
00225     const_iterator begin() const
00226     {
00227         return const_iterator(m_range.begin(), m_range.end(), m_predicate);
00228     }
00230     const_iterator end() const
00231     {
00232         return const_iterator(m_range.end(), m_range.end(), m_predicate);
00233     }
00235 
00236 // Members
00237 private:
00238     filtered_range_type     m_range;
00239     filter_predicate_type   m_predicate;
00240 };
00241 
00242 template<   ss_typename_param_k R
00243         ,   ss_typename_param_k P
00244         >
00245 inline filtered_range<R, P> filter_range(R r, P p)
00246 {
00247     return filtered_range<R, P>(r, p);
00248 }
00249 
00251 // Unit-testing
00252 
00253 #ifdef STLSOFT_UNITTEST
00254 
00255 namespace unittest
00256 {
00257     ss_bool_t test_rangelib_filtered_range(unittest_reporter *r)
00258     {
00259         using stlsoft::unittest::unittest_initialiser;
00260 
00261         ss_bool_t               bSuccess    =   true;
00262 
00263         unittest_initialiser    init(r, "RangeLib", "filtered_range", __FILE__);
00264 
00265 #if 0
00266         if(NULL != pi1)
00267         {
00268             ator1.construct(pi1, 1968);
00269 
00270             if(1968 != *pi1)
00271             {
00272                 r->report("construct() failed ", __LINE__);
00273                 bSuccess = false;
00274             }
00275         }
00276 #endif /* 0 */
00277 
00278         return bSuccess;
00279     }
00280 
00281     unittest_registrar    unittest_rangelib_filtered_range(test_rangelib_filtered_range);
00282 
00283 } // namespace unittest
00284 
00285 #endif /* STLSOFT_UNITTEST */
00286 
00287 /* 
00288 
00290 
00291 /* 
00292 
00293 #ifndef _STLSOFT_NO_NAMESPACE
00294 } // namespace stlsoft
00295 #endif /* _STLSOFT_NO_NAMESPACE */
00296 
00297 /* 
00298 
00299 #endif /* !STLSOFT_INCL_RANGELIB_HPP_FILTERED_RANGE */
00300 
00301 /* 

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