📄 constraints.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/util/constraints.hpp (originally MTAlgo.h, ::SynesisStl)
*
* Purpose: Compile-time template constraints templates.
*
* Created: 19th November 1998
* Updated: 13th March 2007
*
* 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-2007, 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/util/constraints.hpp
*
* \brief [C++ only] Definition of compile-time template constraints
* templates
* (\ref group__library__utility__constraints "Constraints" Library).
*/
#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
#define STLSOFT_INCL_STLSOFT_UTIL_HPP_CONSTRAINTS
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_UTIL_HPP_CONSTRAINTS_MAJOR 5
# define STLSOFT_VER_STLSOFT_UTIL_HPP_CONSTRAINTS_MINOR 0
# define STLSOFT_VER_STLSOFT_UTIL_HPP_CONSTRAINTS_REVISION 1
# define STLSOFT_VER_STLSOFT_UTIL_HPP_CONSTRAINTS_EDIT 94
#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
*/
/** \brief Constraint to ensure that the one type is convertible to another via inheritance
*
* \ingroup group__library__utility__constraints
*
* \param D The derived type
* \param B The base type
*
* It may be used as follows:
\code
class Parent {};
class Child : public Parent {};
class Orphan {};
stlsoft::must_have_base<Parent, Parent>(); // Ok
stlsoft::must_have_base<Child, Parent>(); // Ok
stlsoft::must_have_base<Child, Child>(); // Ok
stlsoft::must_have_base<Child, Parent>(); // Compile error!
stlsoft::must_have_base<Orphan, Parent>(); // Compile error!
stlsoft::must_have_base<Orphan, Child>(); // Compile error!
stlsoft::must_have_base<Parent, Orphan>(); // Compile error!
stlsoft::must_have_base<Child, Orphan>(); // Compile error!
\endcode
*
* \note This is borrowed from Bjarne Stroustrup's idea as posted to comp.lang.c++.moderated
* 17th February 2001.
*
* \see stlsoft::must_be_derived
*/
// [[synesis:class:constraint:must_have_base<T<D>, T<B>>]]
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);
}
};
/** \brief Constraint to ensure that the one type is convertible to another via inheritance,
* but is not the same type
*
* \ingroup group__library__utility__constraints
*
* \param D The derived type
* \param B The base type
*
* It may be used as follows:
\code
class Parent {};
class Child : public Parent {};
class Orphan {};
stlsoft::must_be_derived<Parent, Parent>(); // Compile error!
stlsoft::must_be_derived<Child, Parent>(); // Ok
stlsoft::must_be_derived<Child, Child>(); // Compile error!
stlsoft::must_have_base<Child, Parent>(); // Compile error!
stlsoft::must_be_derived<Orphan, Parent>(); // Compile error!
stlsoft::must_be_derived<Orphan, Child>(); // Compile error!
stlsoft::must_be_derived<Parent, Orphan>(); // Compile error!
stlsoft::must_be_derived<Child, Orphan>(); // Compile error!
\endcode
*
* \note This extension to the must_have_base constraint was proposed by
* Peter Bannister after reading Chapter 1 of \ref section__publishing__books__imperfectcplusplus.
*
* \see stlsoft::must_have_base
*/
// [[synesis:class:constraint:must_be_derived<T<D>, T<B>>]]
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);
}
};
/** \brief Constrains two types to be of the same size
*
* \ingroup group__library__utility__constraints
*
* \param T1 The first type
* \param T2 The second type
*
* It may be used as follows:
\code
stlsoft::must_be_same_size<long, long>(); // Ok
stlsoft::must_be_same_size<char, long>(); // Compile error!
stlsoft::must_be_same_size<long, char>(); // Compile error!
\endcode
*
*/
// [[synesis:class:constraint:must_be_same_size<T<T1>, T<T2>>]]
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);
}
};
/** \brief 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>)
*
* \ingroup group__library__utility__constraints
*
* \param T The type to be constrained
*
* It may be used as follows:
\code
typedef std::vector<int> vec_t;
stlsoft::must_be_subscriptable<int*>(); // Ok
stlsoft::must_be_subscriptable<vec_t>(); // Ok
stlsoft::must_be_subscriptable<double>(); // Compile error!
\endcode
*
* \see stlsoft::must_subscript_as_decayable_pointer
*/
// [[synesis:class:constraint:must_be_subscriptable<T<T>>]]
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]);
}
};
/** \brief Constraint to enforce that a given type is an actual array or pointer, rather than
* a user-defined type with a subscript operator.
*
* \ingroup group__library__utility__constraints
*
* \param T The type to be constrained
*
* It may be used as follows:
\code
typedef std::vector<int> vec_t;
stlsoft::must_subscript_as_decayable_pointer<int*>(); // Ok
stlsoft::must_subscript_as_decayable_pointer<vec_t>(); // Compile error!
stlsoft::must_subscript_as_decayable_pointer<double>(); // Compile error!
\endcode
*
* \see stlsoft::must_be_subscriptable
*/
// [[synesis:class:constraint:must_subscript_as_decayable_pointer<T<T>>]]
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]);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -