⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string_view.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 4 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        stlsoft/string/string_view.hpp (originally stlsoft/string_view.hpp)
 *
 * Purpose:     basic_string_view class.
 *
 * Created:     16th October 2004
 * Updated:     10th June 2006
 *
 * Thanks to:   Bjorn Karlsson and Scott Patterson for discussions on various
 *              naming and design issues. Thanks also to Pablo Aguilar for
 *              his eagle eyed reviews of beta code. :-)
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2004-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 stlsoft/string/string_view.hpp
 *
 * \brief [C++ only] Definition of the stlsoft::basic_string_view class
 *  template.
 *  (\ref group__library__string "String" Library.)
 */

#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
#define STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_VIEW_MAJOR       3
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_VIEW_MINOR       0
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_VIEW_REVISION    1
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_VIEW_EDIT        69
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Compatibility
 */

/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MWERKS: __MWERKS__<0x3000
STLSOFT_COMPILER_IS_WATCOM:
[Incompatibilies-end]
 */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#if defined(STLSOFT_COMPILER_IS_MWERKS) && \
    ((__MWERKS__ & 0xff00) < 0x3000)
# error stlsoft/string/string_view.hpp not compatible with Metrowerks 7.x (v2.4)
#endif /* compiler */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
# include <stlsoft/string/char_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_HPP_ITERATOR
# include <stlsoft/iterator.hpp>        // for stlsoft iterator bases
#endif /* !STLSOFT_INCL_STLSOFT_HPP_ITERATOR */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS
# include <stlsoft/collections/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
# include <stlsoft/memory/allocator_selector.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# include <stdexcept>                   // for std::out_of_range
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

// class basic_string_view
/** \brief A string class that holds no internal storage, and merely represents a window into other string storage
 *
 * \ingroup group__library__string
 *
 * \param C The character type
 * \param T The traits type. On translators that support default template arguments this is defaulted
 *           to char_traits<C>
 * \param A The allocator type. On translators that support default template arguments this is defaulted
 *           to allocator_selector<C>::allocator_type. This is only used by the proxy generated by c_str()
 * \ingroup concepts_view
 */
template<   ss_typename_param_k C
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
        ,   ss_typename_param_k T = char_traits<C>
        ,   ss_typename_param_k A = ss_typename_type_def_k allocator_selector<C>::allocator_type
#else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
        ,   ss_typename_param_k T /* = char_traits<C> */
        ,   ss_typename_param_k A /* = allocator_selector<C>::allocator_type */
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
        >
class basic_string_view
#if defined(STLSOFT_COMPILER_IS_DMC)
    : protected A
#else /* ? compiler */
    : private A
#endif /* compiler */
    , public stl_collection_tag
{
public:
    /// The value type
    typedef C                               value_type;
    /// The traits type
    typedef T                               traits_type;
    /// The allocator type
    typedef A                               allocator_type;
    /// The current parameterisation of the type
    typedef basic_string_view<C, T, A>      class_type;
    /// The character type
    typedef value_type                      char_type;
#if 0
    /// The pointer type
    ///
    /// \note This type is not used by any functions, but is here to facilitate
    /// type detection
    typedef value_type const                *pointer;
#endif /* 0 */
    /// The non-mutable (const) pointer type
    typedef value_type const                *const_pointer;
#if 0
    /// The reference type
    typedef value_type                      &reference;
#endif /* 0 */
    /// The non-mutable (const) reference type
    typedef value_type const                &const_reference;
    /// The size type
    typedef ss_size_t                       size_type;
    /// The difference type
    typedef ss_ptrdiff_t                    difference_type;

#if 0
    /// The iterator type
    typedef
#if !defined(STLSOFT_COMPILER_IS_BORLAND)
           ss_typename_type_k
#endif /* compiler */
                       pointer_iterator <   value_type
                                        ,   pointer
                                        ,   reference
                                        >::type             iterator;
#endif /* 0 */
    /// 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;

#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
#if 0
    /// The reverse iterator type
    typedef reverse_iterator_base       <   iterator
                                        ,   value_type
                                        ,   reference
                                        ,   pointer
                                        ,   difference_type
                                        >                   reverse_iterator;
#endif /* 0 */

    /// The non-mutating (const) reverse iterator type
    typedef const_reverse_iterator_base <   const_iterator
                                        ,   value_type const
                                        ,   const_reference
                                        ,   const_pointer
                                        ,   difference_type
                                        >                   const_reverse_iterator;
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */

/// \name Construction
/// @{
public:
    /// Default constructor
    basic_string_view();
    /// Copy constructor
    basic_string_view(class_type const &rhs);
    /// Construct from the given string at the specified position
    basic_string_view(class_type const &s, size_type pos);
    /// Construct with \c cch characters from the given string at the specified position
    basic_string_view(class_type const &s, size_type pos, size_type cch);
    /// Construct from a null-terminated character string
    basic_string_view(char_type const *s); // No, not explicit! Sigh
    /// Construct with \c cch characters from the given character string
    basic_string_view(char_type const *s, size_type cch);
    /// Construct from the range [first:last)
    basic_string_view(char_type const *first, char_type const *last);
    /// Destructor
    ~basic_string_view() stlsoft_throw_0();

    /// Copy assignment operator
    class_type &operator =(class_type const &rhs);

/// @}

/// \name Operations
/// @{
public:
    /// Swaps the contents between \c this and \c other
    void swap(class_type &other) stlsoft_throw_0();

    /// Empties the string
    void clear() stlsoft_throw_0();

    /// \brief Clear c_str() representation
    ///
    /// Causes the c_str() representation, if currently allocated (see c_str() for details),
    /// to be destroyed, in order that the next call to c_str() will result in a fresh
    /// nul-terminated copy of the 'current' contents of the view.
    void refresh() stlsoft_throw_0();
/// @}

/// \name Attributes
/// @{
public:
    /// The number of elements in the string
    size_type size() const stlsoft_throw_0();
    /// The maximum number of elements that can be stored in the string
    static size_type max_size() stlsoft_throw_0();
    /// The number of elements in the string
    size_type length() const stlsoft_throw_0();
    /// The storage currently allocated by the string
    size_type capacity() const stlsoft_throw_0();
    /// Indicates whether the string is empty
    ss_bool_t empty() const stlsoft_throw_0();

    /// Returns an instance of the allocator type
    allocator_type get_allocator() const;
/// @}

/// \name Comparison
/// @{
public:
    /// Evaluates whether two strings are equal
    ss_bool_t equal(class_type const &rhs) const stlsoft_throw_0();
    /// Evaluates whether two strings are equal
    ss_bool_t equal(value_type const *rhs, size_type cchRhs) const stlsoft_throw_0();

    /// Compares \c this with the given string
    ss_sint_t compare(size_type pos, size_type cch, value_type const *s, size_type cchRhs) const stlsoft_throw_0();
    /// Compares \c this with the given string
    ss_sint_t compare(size_type pos, size_type cch, value_type const *s) const stlsoft_throw_0();
    /// Compares \c this with the given string
    ss_sint_t compare(value_type const *s) const stlsoft_throw_0();
    /// Compares \c this with the given string
    ss_sint_t compare(size_type pos, size_type cch, class_type const &rhs, size_type posRhs, size_type cchRhs) const stlsoft_throw_0();
    /// Compares \c this with the given string
    ss_sint_t compare(size_type pos, size_type cch, class_type const &rhs) const stlsoft_throw_0();
    /// Compares \c this with the given string
    ss_sint_t compare(class_type const &rhs) const stlsoft_throw_0();
/// @}

/// \name Accessors
/// @{
public:
#if 0
    /// Returns mutable reference at the given index
    reference               operator [](size_type index);
#endif /* 0 */
    /// Returns non-mutable (const) reference at the given index
    ///
    /// \note Follows the convention of std::basic_string in returning a reference to
    /// char_type() if index == size(). This reference will <b>not</b> be part of the
    /// block constituting the string's viewed area.
    const_reference         operator [](size_type index) const;

#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    /// Returns non-mutable (const) reference at the given index
    ///
    /// \note Throws std::out_of_range if index >= size()
    const_reference         at(size_type index) const;
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */

    /// Returns null-terminated non-mutable (const) pointer to string data
    ///
    /// \note This returns a nul-terminated copy of the string. If the view's underlying
    /// string changes after this point, this will not be reflected in the value returned
    /// by c_str(), until such time as refresh() is called.
    ///
    /// \note If the view's parameterisation is with a no-throw allocator, behaviour
    /// is undefined in con
    value_type const        *c_str() const;
#if 0
    /// Facility for calling refresh() followed by c_str()
    ///
    /// \param bRefresh call refresh() before c_str()
    ///
    /// \note If bRefresh is \c false has identical semantics to c_str()
    value_type const        *c_str(ss_bool_t bRefresh) const;
#endif /* 0 */
    /// Returns non-mutable (const) pointer to string data
    value_type const        *data() const stlsoft_throw_0();
    /// Returns value of base pointer
    value_type const        *base() const stlsoft_throw_0();

#if 0
    /// Returns the first character in the string
    ///
    /// \note It is up to the user to ensure that the string is not empty
    reference               front();
    /// Returns the last character in the string
    ///
    /// \note It is up to the user to ensure that the string is not empty
    reference               back();
#endif /* 0 */
    /// Returns the first character in the string
    ///
    /// \note It is up to the user to ensure that the string is not empty
    const_reference         front() const;
    /// Returns the last character in the string
    ///
    /// \note It is up to the user to ensure that the string is not empty
    const_reference         back() const;

    /// Copies elements into the given destination
    size_type copy(value_type *dest, size_type cch, size_type pos = 0) const;
/// @}

/// \name Iteration
/// @{
public:
    /// Begins the iteration
    ///
    /// \return A non-mutable (const) iterator representing the start of the sequence
    const_iterator          begin() const;
    /// Ends the iteration
    ///
    /// \return A non-mutable (const) iterator representing the end of the sequence
    const_iterator          end() const;
#if 0
    /// Begins the iteration
    ///
    /// \return An iterator representing the start of the sequence
    iterator                begin();
    /// Ends the iteration
    ///
    /// \return An iterator representing the end of the sequence
    iterator                end();
#endif /* 0 */

#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
    /// Begins the reverse iteration
    ///
    /// \return A non-mutable (const) iterator representing the start of the reverse sequence
    const_reverse_iterator  rbegin() const;
    /// Ends the reverse iteration
    ///
    /// \return A non-mutable (const) iterator representing the end of the reverse sequence
    const_reverse_iterator  rend() const;
#if 0
    /// Begins the reverse iteration
    ///
    /// \return An iterator representing the start of the reverse sequence
    reverse_iterator        rbegin();
    /// Ends the reverse iteration
    ///
    /// \return An iterator representing the end of the reverse sequence
    reverse_iterator        rend();
#endif /* 0 */
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
/// @}

/// \name Invariant
#ifdef STLSOFT_UNITTEST
public:
#else
private:
#endif /* STLSOFT_UNITTEST */
    ss_bool_t is_valid() const;

/// \name Implementation
/// @{
private:
    // Empty string
    static char_type const *empty_string_();

    // Comparison
    static ss_sint_t compare_(char_type const *lhs, size_type lhs_len, char_type const *rhs, size_type rhs_len);

    // Closes the m_cstr member
    void close_() stlsoft_throw_0();

    // Closes the m_cstr member and sets to NULL
    void close_set_null_() stlsoft_throw_0();
/// @}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -