This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] tr1/type_traits: add is_base_of


Hi,

the below essentially completes the implementation of tr1/type_traits:
from now on, testing and bug fixes only.

Regtested x86-linux.

Paolo.

////////////////
2005-03-04  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/type_traits: Add is_base_of.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_base_of/is_base_of.cc: New.
	* testsuite/tr1/4_metaprogramming/relationships_between_types/
	is_base_of/typedefs.cc: Likewise.
diff -urN libstdc++-v3-orig/include/tr1/type_traits libstdc++-v3/include/tr1/type_traits
--- libstdc++-v3-orig/include/tr1/type_traits	2005-03-03 13:10:54.000000000 +0100
+++ libstdc++-v3/include/tr1/type_traits	2005-03-05 00:58:26.000000000 +0100
@@ -42,7 +42,7 @@
 {
 namespace tr1
 {
-  // For use in is_enum, is_abstract and elsewhere.
+  // For use in __conv_helper, is_abstract and elsewhere.
   struct __sfinae_types
   {
     typedef char __one;
@@ -456,6 +456,43 @@
     struct is_same<_Tp, _Tp>
     : public true_type { };
 
+  // See Daveed Vandevoorde explanation in http://tinyurl.com/502f.
+  // Also see Rani Sharoni in http://tinyurl.com/6jvyq.
+  template<typename _Base, typename _Derived,
+	   bool = (!__is_union_or_class<_Base>::value
+		   || !__is_union_or_class<_Derived>::value
+		   || is_same<_Base, _Derived>::value)>
+    struct __is_base_of_helper
+    : public __sfinae_types
+    {
+    private:
+      typedef typename remove_cv<_Base>::type     _NoCv_Base;      
+      typedef typename remove_cv<_Derived>::type  _NoCv_Derived;
+      
+      template<typename _Up>
+        static __one __test(_NoCv_Derived&, _Up);
+      static __two __test(_NoCv_Base&, int);
+   
+      struct _Conv
+      {
+	operator _NoCv_Derived&();
+	operator _NoCv_Base&() const;
+      };
+   
+    public:
+      static const bool __value = sizeof(__test(_Conv(), 0)) == 1;
+    };
+
+  template<typename _Base, typename _Derived>
+    struct __is_base_of_helper<_Base, _Derived, true>
+    { static const bool __value = is_same<_Base, _Derived>::value; };
+
+  template<typename _Base, typename _Derived>
+    struct is_base_of
+    : public integral_constant<bool,
+			       __is_base_of_helper<_Base, _Derived>::__value>
+    { };
+
   template<typename _Tp>
     struct __is_int_or_cref
     {
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/is_base_of.cc	2005-03-04 23:30:10.000000000 +0100
@@ -0,0 +1,94 @@
+// 2005-03-04  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 4.6 Relationships between types
+
+#include <tr1/type_traits>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+class HiddenCons
+{
+  HiddenCons();
+  HiddenCons(const HiddenCons&);
+};
+
+class DerivedHiddenCons
+: private HiddenCons
+{
+  DerivedHiddenCons();
+  DerivedHiddenCons(const DerivedHiddenCons&);
+};
+
+class MultiDerivedHiddenCons
+: private HiddenCons, private __gnu_test::ClassType 
+{
+  MultiDerivedHiddenCons();
+  MultiDerivedHiddenCons(const MultiDerivedHiddenCons&);
+};
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using std::tr1::is_base_of;
+  using namespace __gnu_test;
+
+  // Positive tests.
+  VERIFY( (test_relationship<is_base_of, int, int>(true)) );
+  VERIFY( (test_relationship<is_base_of, EnumType, EnumType>(true)) );
+  VERIFY( (test_relationship<is_base_of, UnionType, UnionType>(true)) );
+  VERIFY( (test_relationship<is_base_of, AbstractClass,
+	   AbstractClass>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, DerivedType>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, const DerivedType>(true)) );
+  VERIFY( (test_relationship<is_base_of, volatile ClassType,
+	   volatile DerivedType>(true)) );  
+  VERIFY( (test_relationship<is_base_of, PolymorphicClass,
+	   DerivedPolymorphic>(true)) );
+  VERIFY( (test_relationship<is_base_of, HiddenCons,
+	   DerivedHiddenCons>(true)) );
+  VERIFY( (test_relationship<is_base_of, HiddenCons,
+	   MultiDerivedHiddenCons>(true)) );
+  VERIFY( (test_relationship<is_base_of, ClassType,
+	   MultiDerivedHiddenCons>(true)) );
+
+  // Negative tests.
+  VERIFY( (test_relationship<is_base_of, int, const int>(false)) );
+  VERIFY( (test_relationship<is_base_of, volatile UnionType,
+	   UnionType>(false)) );
+  VERIFY( (test_relationship<is_base_of, int&, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, AbstractClass, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, ClassType, AbstractClass>(false)) );  
+  VERIFY( (test_relationship<is_base_of, DerivedType, ClassType>(false)) );
+  VERIFY( (test_relationship<is_base_of, DerivedPolymorphic,
+	   PolymorphicClass>(false)) );
+  VERIFY( (test_relationship<is_base_of, DerivedHiddenCons,
+	   HiddenCons>(false)) );
+  VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
+	   HiddenCons>(false)) );
+  VERIFY( (test_relationship<is_base_of, MultiDerivedHiddenCons,
+	   ClassType>(false)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc
--- libstdc++-v3-orig/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/tr1/4_metaprogramming/relationships_between_types/is_base_of/typedefs.cc	2005-03-04 20:12:31.000000000 +0100
@@ -0,0 +1,36 @@
+// 2005-03-04  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2005 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 
+// NB: This file is for testing tr1/type_traits with NO OTHER INCLUDES.
+
+#include <tr1/type_traits>
+
+// { dg-do compile }
+
+void test01()
+{
+  // Check for required typedefs
+  typedef std::tr1::is_base_of<int, int>      test_type;
+  typedef test_type::value_type               value_type;
+  typedef test_type::type                     type;
+  typedef test_type::type::value_type         type_value_type;
+  typedef test_type::type::type               type_type;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]