From 8982e52a5ff85a717aa5ca5d2d6a420c122852fe Mon Sep 17 00:00:00 2001 From: John Maddock Date: Wed, 2 Aug 2000 10:58:59 +0000 Subject: [PATCH] type traits update [added is_convertible and alignment_of] [SVN r7675] --- include/boost/detail/ob_type_traits.hpp | 88 +++++++++++++++++++++++ include/boost/detail/type_traits.hpp | 96 ++++++++++++++++++++++++- 2 files changed, 181 insertions(+), 3 deletions(-) diff --git a/include/boost/detail/ob_type_traits.hpp b/include/boost/detail/ob_type_traits.hpp index c31901c..e00aeff 100644 --- a/include/boost/detail/ob_type_traits.hpp +++ b/include/boost/detail/ob_type_traits.hpp @@ -10,10 +10,28 @@ // support partial specialisation. (C) John Maddock 2000 /* Release notes: + 31st July 2000: + Added is_convertable, alignment_of. 23rd July 2000: Fixed is_void specialization. (JM) */ +// +// partial copyright for is_convertible: +// +// Copyright (C) 2000 Jeremy Siek (jsiek@lsc.nd.edu) +// Copyright (C) 1999, 2000 Jaakko J„rvi (jaakko.jarvi@cs.utu.fi) +// +// Permission to copy and use this software is granted, +// provided this copyright notice appears in all copies. +// Permission to modify the code and to distribute modified code is granted, +// provided this copyright notice appears in all copies, and a notice +// that the code was modified is included with the copyright notice. +// +// This software is provided "as is" without express or implied warranty, +// and with no claim as to its suitability for any purpose. + + #ifndef BOOST_OB_TYPE_TRAITS_HPP #define BOOST_OB_TYPE_TRAITS_HPP @@ -273,6 +291,76 @@ template struct is_POD { enum{ value = is_scalar::value //JM 7Jan2000 || BOOST_IS_POD(T) }; }; +namespace detail{ + + // This workaround is necessary to handle when From is void + // which is normally taken care of by the partial specialization + // of the is_convertible class. +#ifdef BOOST_MSVC6_MEMBER_TEMPLATES + struct from_not_void_conversion { + template + struct bind { + typedef char (&no)[1]; + typedef char (&yes)[2]; + static no check(...); + static yes check(To); + public: + void foo(); // avoid warning about all members being private + static From from; + enum { exists = sizeof( check(from) ) == sizeof(yes) }; + }; + }; + struct from_is_void_conversion { + template + struct bind { + enum { exists = is_void::value }; + }; + }; + + template + struct conversion_helper { + typedef from_not_void_conversion type; + }; + template <> + struct conversion_helper { + typedef from_is_void_conversion type; + }; +#endif +} // namespace detail + +template +class is_convertible +{ +#ifdef BOOST_MSVC6_MEMBER_TEMPLATES + typedef typename detail::conversion_helper::type Selector; + typedef Selector::template bind Conversion; +public: + enum { value = Conversion::exists }; +#else + typedef char (&no)[1]; + typedef char (&yes)[2]; + static no check(...); + static yes check(To); + public: + void foo(); // avoid warning about all members being private + static From from; + enum { value = sizeof( check(from) ) == sizeof(yes) }; +#endif +}; + +template +class alignment_of +{ + struct padded + { + char c; + T t; + padded(); + }; +public: + enum{ value = sizeof(padded) - sizeof(T) }; +}; + //*? is type T an empty composite type (allows cv-qual) template struct is_empty { enum{ value = BOOST_IS_EMPTY(T) }; }; diff --git a/include/boost/detail/type_traits.hpp b/include/boost/detail/type_traits.hpp index 4e69c05..6d109d0 100644 --- a/include/boost/detail/type_traits.hpp +++ b/include/boost/detail/type_traits.hpp @@ -5,12 +5,33 @@ // warranty, and with no claim as to its suitability for any purpose. // See http://www.boost.org for most recent version including documentation. +// +// misc traits classes that operate on or describe a type. +// see libs/utility/type_traits.htm /* Release notes: + 31st July 2000: + Added is_convertable, alignment_of, modified is_empty. 23rd July 2000: Added Borland specific fixes for reference types (Steve Cleary). */ +// +// partial copyright for is_convertible: +// +// Copyright (C) 2000 Jeremy Siek (jsiek@lsc.nd.edu) +// Copyright (C) 1999, 2000 Jaakko J„rvi (jaakko.jarvi@cs.utu.fi) +// +// Permission to copy and use this software is granted, +// provided this copyright notice appears in all copies. +// Permission to modify the code and to distribute modified code is granted, +// provided this copyright notice appears in all copies, and a notice +// that the code was modified is included with the copyright notice. +// +// This software is provided "as is" without express or implied warranty, +// and with no claim as to its suitability for any purpose. + + #ifndef BOOST_DETAIL_TYPE_TRAITS_HPP #define BOOST_DETAIL_TYPE_TRAITS_HPP @@ -419,11 +440,80 @@ public: template struct is_POD { static const bool value = is_POD::value; }; +// +// is one type convertable to another? +template +class is_convertible +{ + typedef char (&no)[1]; + typedef char (&yes)[2]; +# if defined(__BORLANDC__) || defined(__GNUC__) + // This workaround for Borland breaks the EDG C++ frontend, + // so we only use it for Borland. + template + struct checker + { + static no check(...); + static yes check(T); + }; + static From from; +public: + static const bool value = sizeof( checker::check(from) ) == sizeof(yes); + +# else // not __BORLANDC__ + static no check(...); + static yes check(To); + static From from; +public: + static const bool value = sizeof( check(from) ) == sizeof(yes); +# endif + void foo(); // avoid warning about all members being private +}; + +template +struct is_convertible +{ + static const bool value = false; +}; +template +struct is_convertible +{ + static const bool value = false; +}; +template <> +struct is_convertible +{ + static const bool value = true; +}; +// +// get the alignment of some arbitrary type: +template +class alignment_of +{ + struct padded + { + char c; + T t; + padded(); + }; +public: + static const unsigned value = sizeof(padded) - sizeof(T); +}; +// +// references have to be treated specially, assume +// that a reference is just a special pointer: +template +class alignment_of +{ +public: + static const unsigned value = alignment_of::value; +}; + // // JM 7Jan2000 // namespace detail{ -template +template struct empty_helper{ static const bool value = false; }; template @@ -437,7 +527,7 @@ struct empty_helper_t1 : public T struct empty_helper_t2 { int i[256]; }; template -struct empty_helper +struct empty_helper { static const bool value = (sizeof(empty_helper_t1) == sizeof(empty_helper_t2)); }; @@ -449,7 +539,7 @@ template struct is_empty private: typedef typename remove_cv::type cvt; public: - static const bool value = ::boost::detail::empty_helper::value>::value + static const bool value = ::boost::detail::empty_helper::value , is_convertible::value>::value || BOOST_IS_EMPTY(cvt); };