📄 carray_adaptors.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: mfcstl/carray_adaptors.hpp (derived from mfcstl_array_adaptor.h)
*
* Purpose: Contains the definition of the CArray_cadaptor and CArray_iadaptor
* class templates.
*
* Created: 1st December 2002
* Updated: 10th June 2006
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2006, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/// \file mfcstl/carray_adaptors.hpp
///
/// Contains the definition of the CArray_cadaptor and CArray_iadaptor class templates.
#ifndef MFCSTL_INCL_MFCSTL_HPP_CARRAY_ADAPTORS
#define MFCSTL_INCL_MFCSTL_HPP_CARRAY_ADAPTORS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define MFCSTL_VER_MFCSTL_HPP_CARRAY_ADAPTORS_MAJOR 3
# define MFCSTL_VER_MFCSTL_HPP_CARRAY_ADAPTORS_MINOR 8
# define MFCSTL_VER_MFCSTL_HPP_CARRAY_ADAPTORS_REVISION 1
# define MFCSTL_VER_MFCSTL_HPP_CARRAY_ADAPTORS_EDIT 64
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MSVC: _MSC_VER==1300
[Incompatibilies-end]
*/
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef MFCSTL_INCL_MFCSTL_HPP_MFCSTL
# include <mfcstl/mfcstl.hpp>
#endif /* !MFCSTL_INCL_MFCSTL_HPP_MFCSTL */
#ifndef MFCSTL_INCL_MFCSTL_MEMORY_HPP_AFX_ALLOCATOR
# include <mfcstl/memory/afx_allocator.hpp>
#endif /* !MFCSTL_INCL_MFCSTL_MEMORY_HPP_AFX_ALLOCATOR */
#ifndef MFCSTL_INCL_MFCSTL_CONTAINERS_HPP_CARRAY_SWAP
# include <mfcstl/containers/carray_swap.hpp>
#endif /* !MFCSTL_INCL_MFCSTL_CONTAINERS_HPP_CARRAY_SWAP */
#ifndef MFCSTL_INCL_MFCSTL_CONTAINERS_HPP_CARRAY_TRAITS
# include <mfcstl/containers/carray_traits.hpp>
#endif /* !MFCSTL_INCL_MFCSTL_CONTAINERS_HPP_CARRAY_TRAITS */
#ifndef MFCSTL_INCL_MFCSTL_UTIL_HPP_MEMORY_EXCEPTION_TRANSLATION_POLICIES
# include <mfcstl/util/memory_exception_translation_policies.hpp>
#endif /* !MFCSTL_INCL_MFCSTL_UTIL_HPP_MEMORY_EXCEPTION_TRANSLATION_POLICIES */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_GENERATORS
# include <stlsoft/util/std/iterator_generators.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_GENERATORS */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_CAPABILITIES
# include <stlsoft/meta/capabilities.hpp>
#endif /* STLSOFT_INCL_STLSOFT_META_HPP_CAPABILITIES */
#ifndef STLSOFT_INCL_STLSOFT_HPP_META
# include <stlsoft/meta.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_HPP_META */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS
# include <stlsoft/collections/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS */
#if defined(STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT) && \
!defined(MFCSTL_NO_INCLUDE_AFXTEMPL_BY_DEFAULT)
# include <afxtempl.h>
#endif /* STLSOFT_CF_TEMPLATE_PARTIAL_SPECIALISATION_SUPPORT && !MFCSTL_NO_INCLUDE_AFXTEMPL_BY_DEFAULT */
#ifdef STLSOFT_UNITTEST
# include <afxtempl.h>
# include <stlsoft/string/simple_string.hpp>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200
# define MFCSTL_ARRAY_ADAPTORS_NO_TEMPLATE_RANGE_OPS
#endif /* compiler */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _MFCSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::mfcstl */
namespace mfcstl
{
# else
/* Define stlsoft::mfcstl_project */
namespace stlsoft
{
namespace mfcstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_MFCSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
// Forward declarations
template< ss_typename_param_k A
, ss_typename_param_k I
, ss_typename_param_k T
>
class CArray_adaptor_base;
template< ss_typename_param_k A
, ss_typename_param_k T
>
class CArray_cadaptor;
template< ss_typename_param_k A
, ss_typename_param_k T
>
class CArray_iadaptor;
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/// \brief Adaptor class, providing implementation for CArray_cadaptor and
/// CArray_iadaptor classes
///
/// \param A The array class, e.g. CObArray, CArray<long>, etc.
/// \param I The interface specialisation, e.g. CArray_cadaptor<CObArray>, CArray_iadaptor<CArray<long> >, etc.
/// \param T The traits class, e.g. CArray_traits<CObArray>
///
/// \note The elements in an adapted array are moved, during insertion / erasure, rather than copied. This
/// means that if the elements in the container maintain pointers to their elements, or their peers, then
/// they are not suitable for use.
template< ss_typename_param_k A
, ss_typename_param_k I
, ss_typename_param_k T
>
class CArray_adaptor_base
: public stlsoft_ns_qual(stl_collection_tag)
{
/// \name Member Types
/// @{
public:
/// The type of the underlying MFC array
typedef A array_type;
private:
typedef I interface_class_type;
typedef T array_traits_type;
#if defined(MFCSTL_CARRAY_ADAPTORS_USE_BAD_ALLOC_POLICY)
typedef bad_alloc_throwing_policy exception_translation_policy_type;
#else /* ? MFCSTL_CARRAY_ADAPTORS_USE_BAD_ALLOC_POLICY */
typedef CMemoryException_throwing_policy exception_translation_policy_type;
#endif /* MFCSTL_CARRAY_ADAPTORS_USE_BAD_ALLOC_POLICY */
public:
/// The value type
///
/// \note If the compiler report "use of undefined type" when you're using the adaptor class(es)
/// with CArray<>, ensure that you've included <b>afxtempl</b> <i>before</i> you include this file.
typedef ss_typename_type_k array_traits_type::value_type value_type;
/// The allocator type
typedef afx_allocator<value_type> allocator_type;
/// The mutating (non-const) reference type
typedef ss_typename_type_k allocator_type::reference reference;
/// The non-mutating (const) reference type
typedef ss_typename_type_k allocator_type::const_reference const_reference;
/// The mutating (non-const) pointer type
typedef ss_typename_type_k allocator_type::pointer pointer;
/// The non-mutating (const) pointer type
typedef ss_typename_type_k allocator_type::const_pointer const_pointer;
/// The mutating (non-const) iterator type
typedef
# if !defined(STLSOFT_COMPILER_IS_BORLAND)
ss_typename_type_k
# endif /* compiler */
pointer_iterator < value_type
, pointer
, reference
>::type iterator;
/// The non-mutating (const) iterator type
typedef
# if !defined(STLSOFT_COMPILER_IS_BORLAND)
ss_typename_type_k
# endif /* compiler */
pointer_iterator < value_type const
, const_pointer
, const_reference
>::type const_iterator;
/// The size type
typedef ms_size_t size_type;
/// The difference type
typedef ms_ptrdiff_t difference_type;
/// The instantiation of the current type
typedef CArray_adaptor_base<A, I, T> class_type;
#ifdef STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT
/// The mutating (non-const) reverse iterator type
typedef ss_typename_type_k reverse_iterator_generator < iterator
, value_type
, reference
, pointer
, difference_type
>::type reverse_iterator;
/// The non-mutating (const) reverse iterator type
typedef ss_typename_type_k const_reverse_iterator_generator < const_iterator
, value_type
, const_reference
, const_pointer
, difference_type
>::type const_reverse_iterator;
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
/// @}
/// \name Constants
/// @{
protected:
enum { growthGranularity = 16 };
static size_type calc_increment_(size_type n)
{
size_type numBlocks = n / growthGranularity;
return (1 + numBlocks) * growthGranularity;
}
/// @}
/// \name Underlying Container Access
/// @{
public:
/// \brief Returns a mutating (non-const) reference to the underlying array
array_type &get_CArray()
{
return static_cast<interface_class_type*>(this)->get_actual_array();
}
/// \brief Returns a non-mutating (const) reference to the underlying array
array_type const &get_CArray() const
{
return static_cast<interface_class_type const*>(this)->get_actual_array();
}
/// @}
/// \name Construction
/// @{
protected:
/// \brief Default constructor.
///
/// This is protected, because CArray_adaptor_base serves as an abstract base
/// for CArray_cadaptor and CArray_iadaptor
CArray_adaptor_base()
{}
/// \brief Destructor
~CArray_adaptor_base() stlsoft_throw_0()
{}
public:
/// Returns a copy of the allocator used by the container
allocator_type get_allocator() const
{
return allocator_type();
}
#if defined(_DEBUG) || \
defined(MFCSTL_ARRAY_ADAPTORS_ALLOW_CAPACITY_PEEK)
public:
#else /* ? _DEBUG */
protected:
#endif /* _DEBUG */
size_type grow_increment() const
{
class GrowByGetter
: public array_type
{
public:
size_type grow_increment() const
{
return m_nGrowBy;
}
};
return static_cast<GrowByGetter const&>(get_CArray()).grow_increment();
}
size_type capacity() const
{
class CapacityGetter
: public array_type
{
public:
size_type capacity() const
{
return m_nMaxSize;
}
};
return static_cast<CapacityGetter const&>(get_CArray()).capacity();
}
/// @}
/// \name Assignment
/// @{
public:
/// \brief Assigns a number of copies of the given value to the array, erasing all prior content
///
/// \param n The number of values to assign
/// \param value The value of which n copies are to be assigned
///
/// \note Exception-safety is <b>strong</b> if MFCSTL_CARRAY_SWAP_MEMBERS_SUPPORT is defined, otherwise <b>weak</b>.
///
/// \note The elements are default constructed, and then copy-assigned
void assign(size_type n, value_type const &value)
{
#ifdef MFCSTL_CARRAY_SWAP_MEMBERS_SUPPORT
try
{
array_type ar;
ar.SetSize(0, calc_increment_(n));
if(n > 0) // Can't pass 0 to InsertAt()
{
ar.InsertAt(0, value, static_cast<int>(n));
}
CArray_swap(this->get_CArray(), ar);
}
catch(CMemoryException *px)
{
exception_translation_policy_type::handle(px);
}
catch(mfcstl_ns_qual_std(bad_alloc) &x)
{
exception_translation_policy_type::handle(x);
}
#else /* ? MFCSTL_CARRAY_SWAP_MEMBERS_SUPPORT */
// if( empty() &&
// 0 != n)
// {
// resize(1);
// }
resize(n);
mfcstl_ns_qual_std(fill_n)(begin(), n, value);
#endif /* MFCSTL_CARRAY_SWAP_MEMBERS_SUPPORT */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -