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  

stlsoft_array_proxy.h

Go to the documentation of this file.
00001 /* 
00002  * File:        stlsoft_array_proxy.h
00003  *
00004  * Purpose:     Definition of the array_proxy template, which provides managed
00005  *              access to arrays, and can be used to avoid polymorphic array
00006  *              problems.
00007  *
00008  * Created:     11th November 2002
00009  * Updated:     11th September 2004
00010  *
00011  * Home:        http://stlsoft.org/
00012  *
00013  * Copyright (c) 2002-2004, Matthew Wilson and Synesis Software
00014  * All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *
00019  * - Redistributions of source code must retain the above copyright notice, this
00020  *   list of conditions and the following disclaimer.
00021  * - Redistributions in binary form must reproduce the above copyright notice,
00022  *   this list of conditions and the following disclaimer in the documentation
00023  *   and/or other materials provided with the distribution.
00024  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00025  *   any contributors may be used to endorse or promote products derived from
00026  *   this software without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038  * POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  * 
00041 
00042 
00046 
00047 #ifndef STLSOFT_INCL_H_STLSOFT_ARRAY_PROXY
00048 #define STLSOFT_INCL_H_STLSOFT_ARRAY_PROXY
00049 
00050 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00051 # define STLSOFT_VER_H_STLSOFT_ARRAY_PROXY_MAJOR        2
00052 # define STLSOFT_VER_H_STLSOFT_ARRAY_PROXY_MINOR        0
00053 # define STLSOFT_VER_H_STLSOFT_ARRAY_PROXY_REVISION     1
00054 # define STLSOFT_VER_H_STLSOFT_ARRAY_PROXY_EDIT         26
00055 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00056 
00057 /* 
00058  * Includes
00059  */
00060 
00061 #ifndef STLSOFT_INCL_H_STLSOFT
00062 # include "stlsoft.h"               // Include the STLSoft root header
00063 #endif /* !STLSOFT_INCL_H_STLSOFT */
00064 #ifndef STLSOFT_INCL_H_STLSOFT_CONSTRAINTS
00065 # include "stlsoft_constraints.h"   // must_be_same_size, must_have_base
00066 #endif /* !STLSOFT_INCL_H_STLSOFT_CONSTRAINTS */
00067 
00068 /* 
00069  * Namespace
00070  */
00071 
00072 #ifndef _STLSOFT_NO_NAMESPACE
00073 namespace stlsoft
00074 {
00075 #endif /* _STLSOFT_NO_NAMESPACE */
00076 
00077 /* 
00078  * Classes
00079  */
00080 
00083 template <ss_typename_param_k T>
00084 class array_proxy
00085 {
00086 public:
00087     typedef T                   value_type;
00088     typedef array_proxy<T>      class_type;
00089     typedef value_type          *pointer;
00090     typedef value_type          *const_pointer;     // This is deliberately non-const
00091     typedef value_type          *iterator;
00092     typedef value_type          *const_iterator;    // This is deliberately non-const
00093     typedef value_type          &reference;
00094     typedef value_type          &const_reference;   // This is deliberately non-const
00095     typedef ss_size_t           size_type;
00096 
00099 public:
00100 #ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00105     template <ss_typename_param_k D>
00106     array_proxy(array_proxy<D> const &d)
00107         : m_begin(d.begin())
00108         , m_end(d.end())
00109     {
00110         // Ensures that D is a derived type of T. (Actually that is
00111         // handled in the initialiser list, but putting it here
00112         // makes the intent quite explicit.)
00113         stlsoft_constraint_must_have_base(D, T);
00114 
00115         // Ensures that D and T are the same size.
00116         stlsoft_constraint_must_be_same_size(D, T);
00117     }
00118 
00119 # ifdef __STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
00123     template <ss_typename_param_k D, ss_size_t N>
00124     ss_explicit_k array_proxy(D (&d)[N])
00125         : m_begin(&d[0])
00126         , m_end(&d[N])
00127     {
00128         // Ensures that D is a derived type of T. (Actually that is
00129         // handled in the initialiser list, but putting it here
00130         // makes the intent quite explicit.)
00131         stlsoft_constraint_must_have_base(D, T);
00132 
00133         // Ensures that D and T are the same size.
00134         stlsoft_constraint_must_be_same_size(D, T);
00135     }
00136 
00140 #  if defined(STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR)
00141     template <ss_size_t N>
00142     ss_explicit_k array_proxy(T (&t)[N])
00143         : m_begin(&t[0])
00144         , m_end(&t[N])
00145     {}
00146 #  endif /* STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR */
00147 # endif /* __STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
00148 #endif // __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00149 
00154 #if !defined(__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
00155     defined(STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR)
00156     array_proxy(pointer begin, pointer end)
00157         : m_begin(begin)
00158         , m_end(end)
00159     {}
00160 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR */
00161 
00162 #ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00167     template <ss_typename_param_k D>
00168     array_proxy(D *begin, D *end)
00169         : m_begin(begin)
00170         , m_end(end)
00171     {
00172         // Ensures that D is a derived type of T. (Actually that is
00173         // handled in the initialiser list, but putting it here
00174         // makes the intent quite explicit.)
00175         stlsoft_constraint_must_have_base(D, T);
00176 
00177         // Ensures that D and T are the same size.
00178         stlsoft_constraint_must_be_same_size(D, T);
00179    }
00180 #endif // __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00181 
00182 #if !defined(__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT) || \
00183     defined(STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR)
00188     array_proxy(pointer p, ss_size_t n)
00189         : m_begin(p)
00190         , m_end(p + n)
00191     {}
00192 #endif /* !__STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT || STLSOFT_CF_NON_TEMPLATE_CTOR_REQUIRED_WITH_TEMPLATE_CTOR */
00193 
00194 #ifdef __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00199     template <ss_typename_param_k D>
00200     array_proxy(D *p, ss_size_t n)
00201         : m_begin(p)
00202         , m_end(p + n)
00203     {
00204         // Ensures that D is a derived type of T. (Actually that is
00205         // handled in the initialiser list, but putting it here
00206         // makes the intent quite explicit.)
00207         stlsoft_constraint_must_have_base(D, T);
00208 
00209         // Ensures that D and T are the same size.
00210         stlsoft_constraint_must_be_same_size(D, T);
00211     }
00212 #endif // __STLSOFT_CF_MEMBER_TEMPLATE_CTOR_SUPPORT
00213 
00214 
00217 public:
00220     pointer base()
00221     {
00222         return m_begin;
00223     }
00226     pointer base() const
00227     {
00228         return m_begin;
00229     }
00231     size_type size() const
00232     {
00233         return m_end - m_begin;
00234     }
00236     ss_bool_t empty() const
00237     {
00238         return m_begin == m_end;
00239     }
00241     static size_type max_size()
00242     {
00243         return static_cast<size_type>(-1) / sizeof(value_type);
00244     }
00246 
00249 public:
00254     reference operator [](ss_size_t index)
00255     {
00256         stlsoft_message_assert("index out of bounds, in array_proxy", !(size() < index));
00257 
00258         return m_begin[index];
00259     }
00264     const_reference operator [](ss_size_t index) const
00265     {
00266         stlsoft_message_assert("index out of bounds, in array_proxy", !(size() < index));
00267 
00268         return const_cast<pointer>(m_begin)[index];
00269     }
00271 
00274 public:
00278     iterator begin()
00279     {
00280         return m_begin;
00281     }
00285     iterator end()
00286     {
00287         return m_end;
00288     }
00292     const_iterator begin() const
00293     {
00294         return m_begin;
00295     }
00299     const_iterator end() const
00300     {
00301         return m_end;
00302     }
00304 
00305 // Members
00306 private:
00307     pointer const m_begin;
00308     pointer const m_end;
00309 
00310 // Not to be implemented
00311 private:
00312     array_proxy &operator =(array_proxy const &);
00313 };
00314 
00315 #ifdef __STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
00316 template <ss_typename_param_k T, ss_size_t N>
00317 inline array_proxy<T> make_array_proxy(T (&t)[N])
00318 {
00319     return array_proxy<T>(&t[0], &t[N]);
00320 //    return array_proxy<T>(t); // This one not used, because CodeWarrior gets confused
00321 }
00322 #endif /* __STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
00323 
00324 template <ss_typename_param_k T>
00325 inline array_proxy<T> make_array_proxy(T *begin, T *end)
00326 {
00327     return array_proxy<T>(begin, end);
00328 }
00329 
00330 template <ss_typename_param_k T>
00331 inline array_proxy<T> make_array_proxy(T *p, ss_size_t n)
00332 {
00333     return array_proxy<T>(p, n);
00334 }
00335 
00337 // Unit-testing
00338 
00339 #ifdef STLSOFT_UNITTEST
00340 
00341 namespace unittest
00342 {
00343     ss_bool_t test_stlsoft_array_proxy(unittest_reporter *r)
00344     {
00345         ss_bool_t               bSuccess    =   true;
00346 
00347         unittest_initialiser    init(r, "STLSoft", "array_proxy", __FILE__);
00348 
00349 #if 0
00350         if(<<TODO>>)
00351         {
00352             r->report("<<TODO>> failed ", __LINE__);
00353             bSuccess = false;
00354         }
00355 #endif /* 0 */
00356 
00357         return bSuccess;
00358     }
00359 
00360     unittest_registrar    unittest_stlsoft_array_proxy(test_stlsoft_array_proxy);
00361 
00362 } // namespace unittest
00363 
00364 #endif /* STLSOFT_UNITTEST */
00365 
00366 /* 
00367 
00368 #ifndef _STLSOFT_NO_NAMESPACE
00369 } // namespace stlsoft
00370 #endif /* _STLSOFT_NO_NAMESPACE */
00371 
00372 /* 
00373 
00374 #endif /* !STLSOFT_INCL_H_STLSOFT_ARRAY_PROXY */
00375 
00376 /* 

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