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

📄 constraints.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        stlsoft/constraints.hpp (formerly stlsoft_constraints.h; originally MTAlgo.h, ::SynesisStl)
 *
 * Purpose:     Compile-time template constraints templates.
 *
 * Created:     19th November 1998
 * Updated:     10th June 2006
 *
 * Thanks:      To Peter Bannister for having the clear thinking to see the
 *              obvious (but only in hindsight) tactic of overloading the
 *              constraints method in must_be_derived.
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 1998-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/constraints.hpp
///
/// Compile-time template constraints templates.

#ifndef STLSOFT_INCL_STLSOFT_HPP_CONSTRAINTS
#define STLSOFT_INCL_STLSOFT_HPP_CONSTRAINTS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_HPP_CONSTRAINTS_MAJOR      4
# define STLSOFT_VER_STLSOFT_HPP_CONSTRAINTS_MINOR      1
# define STLSOFT_VER_STLSOFT_HPP_CONSTRAINTS_REVISION   3
# define STLSOFT_VER_STLSOFT_HPP_CONSTRAINTS_EDIT       87
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

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

#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
#ifndef STLSOFT_INCL_STLSOFT_META_HPP_SIZE_OF
# include <stlsoft/meta/size_of.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_META_HPP_SIZE_OF */

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

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

/* /////////////////////////////////////////////////////////////////////////
 * Macros
 */

#if defined(STLSOFT_COMPILER_IS_BORLAND) || \
    defined(STLSOFT_COMPILER_IS_INTEL) || \
    defined(STLSOFT_COMPILER_IS_MWERKS)
# define stlsoft_constraint_must_be_pod(T)              do { stlsoft_ns_qual(must_be_pod)<T>::func_ptr_type const pfn = stlsoft_ns_qual(must_be_pod)<T>::constraint(); STLSOFT_SUPPRESS_UNUSED(pfn); } while(0)
# define stlsoft_constraint_must_be_pod_or_void(T)      do { stlsoft_ns_qual(must_be_pod_or_void)<T>::func_ptr_type const pfn = stlsoft_ns_qual(must_be_pod_or_void)<T>::constraint(); STLSOFT_SUPPRESS_UNUSED(pfn); } while(0)
#elif defined(STLSOFT_COMPILER_IS_DMC)
# define stlsoft_constraint_must_be_pod(T)          do { int i = sizeof(stlsoft_ns_qual(must_be_pod)<T>::constraint()); } while(0)
# define stlsoft_constraint_must_be_pod_or_void(T)  do { int i = sizeof(stlsoft_ns_qual(must_be_pod_or_void)<T>::constraint()); } while(0)
#else /* ? compiler */
# define stlsoft_constraint_must_be_pod(T)          STLSOFT_STATIC_ASSERT(sizeof(stlsoft_ns_qual(must_be_pod)<T>::constraint()) != 0)
# define stlsoft_constraint_must_be_pod_or_void(T)  STLSOFT_STATIC_ASSERT(sizeof(stlsoft_ns_qual(must_be_pod_or_void)<T>::constraint()) != 0)
#endif /* compiler */

# define stlsoft_constraint_must_be_same_size(T1, T2)   stlsoft_ns_qual(must_be_same_size)<T1, T2>()
# define stlsoft_constraint_must_be_subscriptable(T)    stlsoft_ns_qual(must_be_subscriptable)<T>()
# define stlsoft_constraint_must_have_base(D, B)        stlsoft_ns_qual(must_have_base)<D, B>()
# define stlsoft_constraint_must_be_derived(D, B)       stlsoft_ns_qual(must_be_derived)<D, B>()

/* /////////////////////////////////////////////////////////////////////////
 * Constraints
 */

// must_have_base
//
/// Constraint to ensure that the one type is convertible to another via inheritance
///
/// \param D The derived type
/// \param B The base type
///
/// \note This is borrowed from Bjarne Stroustrup's idea as posted to comp.lang.c++.moderated
/// 17th February 2001.
template<   ss_typename_param_k D
        ,   ss_typename_param_k B
        >
struct must_have_base
{
public:
    ~must_have_base() stlsoft_throw_0()
    {
        void(*p)(D*, B*) = constraints;

        STLSOFT_SUPPRESS_UNUSED(p);
    }

private:
    static void constraints(D* pd, B* pb)
    {
        pb = pd;

        STLSOFT_SUPPRESS_UNUSED(pb);
    }
};


// must_be_derived
//
/// Constraint to ensure that the one type is convertible to another via inheritance,
/// but is not the same type
///
/// \note This extension to the must_have_base class was proposed by Peter Bannister
/// after reading Chapter 1 of Imperfect C++ (http://imperfectcplusplus.com/)
///
/// \param D The derived type
/// \param B The base type
///
/// \note This is borrowed from
template<   ss_typename_param_k D
        ,   ss_typename_param_k B
        >
struct must_be_derived
{
public:
    ~must_be_derived() stlsoft_throw_0()
    {
# if defined(STLSOFT_COMPILER_IS_BORLAND)
        cant_be_overloaded_if_same_type(static_cast<D*>(0)
                                    ,   static_cast<B*>(0));
# else /* ? compiler */
        void(*p)(D*, B*) = cant_be_overloaded_if_same_type;

        STLSOFT_SUPPRESS_UNUSED(p);
# endif /* compiler */
    }

private:
    static void cant_be_overloaded_if_same_type(D* pd, B* pb)
    {
        pb = pd;

        STLSOFT_SUPPRESS_UNUSED(pb);
    }
    static void cant_be_overloaded_if_same_type(B* pb, D* pd)
    {
        pb = pd;

        STLSOFT_SUPPRESS_UNUSED(pb);
    }
};



// must_be_same_size
//
/// \param T1 The parameterising type of the veneer
/// \param T2 The veneer type
template<   ss_typename_param_k T1
        ,   ss_typename_param_k T2
        >
struct must_be_same_size
{
    ~must_be_same_size() stlsoft_throw_0()
    {
        void    (*pfn)(void) = constraints;

        STLSOFT_SUPPRESS_UNUSED(pfn);
    }
private:
    static void constraints()
    {
        // The compiler will bring you here if T1 and T2 are not the same
        // size.
        struct must_be_same_size_
        {
            int T1_must_be_same_size_as_T2 : (static_cast<int>(size_of<T1>::value) == static_cast<int>(size_of<T2>::value));
        };

        const int   T1_must_be_same_size_as_T2    = (static_cast<int>(size_of<T1>::value) == static_cast<int>(size_of<T2>::value));
        int         i[T1_must_be_same_size_as_T2];

        STLSOFT_SUPPRESS_UNUSED(i);
    }
};


/// Constraint to enforce that a given type is an array, or pointer, or user defined type
/// which is amenable to subsripting (i.e. defines <code>operator[]</code> or <code>operator X*()</code>)
template <ss_typename_param_k T>
struct must_be_subscriptable
{
public:
    ~must_be_subscriptable() stlsoft_throw_0()
    {
        int (*pfn)(T const &) = constraints;

        STLSOFT_SUPPRESS_UNUSED(pfn);
    }
private:
    static int constraints(T const &T_is_not_subscriptable)
    {
        // The compiler will bring you here if T is not subscriptable
        return sizeof(T_is_not_subscriptable[0]);
    }
};

/// Constraint to enforce that a given type is an actual array or pointer, rather than
/// a user-defined type with
template <ss_typename_param_k T>
struct must_subscript_as_decayable_pointer
{
public:
    ~must_subscript_as_decayable_pointer() stlsoft_throw_0()
    {
        ss_size_t   (*pfn)(T const &) = constraints;

        STLSOFT_SUPPRESS_UNUSED(pfn);
    }
private:
    static ss_size_t constraints(T const &T_is_not_decay_subscriptable)
    {
        // The compiler will bring you here if T has a user-defined
        // subscript operator.
        return sizeof(0[T_is_not_decay_subscriptable]);
    }
};



// must_be_pod
//
/// Constraint to ensure that a type is a built-in or trivial type.
///
/// \param T The type
// This class can be used to constrain a type to be of either built-in, e.g.
// int, or of a trivial type, i.e. aggregate types or types with publicly
// accessible default contructors and assignment operators
template <ss_typename_param_k T>
union must_be_pod
{
private:
    typedef must_be_pod<T>  class_type;

public:
    T   t;
    int i;

    typedef int (*func_ptr_type)();

    static func_ptr_type constraint()
    {
        return constraints;
    }

⌨️ 快捷键说明

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