From 464b277ba2697cc48d75cc5a343f4209338291d5 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 11 Jan 2005 12:29:31 +0000 Subject: [PATCH] type_traits: Implement alignment_of and aligned_storage. 2005-01-11 Paolo Carlini * include/tr1/type_traits: Implement alignment_of and aligned_storage. * testsuite/tr1/4_metaprogramming/other_transformations/ aligned_storage/aligned_storage.cc: New. * testsuite/tr1/4_metaprogramming/other_transformations/ aligned_storage/typedefs.cc: Likewise. * testsuite/tr1/4_metaprogramming/type_properties/ alignment_of/alignment_of.cc: Likewise. * testsuite/tr1/4_metaprogramming/type_properties/ alignment_of/typedefs.cc: Likewise. From-SVN: r93183 --- libstdc++-v3/ChangeLog | 12 +++ libstdc++-v3/include/tr1/type_traits | 85 +++++++++++++++++-- .../aligned_storage/aligned_storage.cc | 63 ++++++++++++++ .../aligned_storage/typedefs.cc | 33 +++++++ .../alignment_of/alignment_of.cc | 45 ++++++++++ .../type_properties/alignment_of/typedefs.cc | 36 ++++++++ 6 files changed, 269 insertions(+), 5 deletions(-) create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc create mode 100644 libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 7652f5ebfcf..bb1599cae1e 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,15 @@ +2005-01-11 Paolo Carlini + + * include/tr1/type_traits: Implement alignment_of and aligned_storage. + * testsuite/tr1/4_metaprogramming/other_transformations/ + aligned_storage/aligned_storage.cc: New. + * testsuite/tr1/4_metaprogramming/other_transformations/ + aligned_storage/typedefs.cc: Likewise. + * testsuite/tr1/4_metaprogramming/type_properties/ + alignment_of/alignment_of.cc: Likewise. + * testsuite/tr1/4_metaprogramming/type_properties/ + alignment_of/typedefs.cc: Likewise. + 2005-01-10 Paolo Carlini * Makefile.in: Regenerate. diff --git a/libstdc++-v3/include/tr1/type_traits b/libstdc++-v3/include/tr1/type_traits index eb1087fd281..1cb847c760e 100644 --- a/libstdc++-v3/include/tr1/type_traits +++ b/libstdc++-v3/include/tr1/type_traits @@ -1,6 +1,6 @@ // TR1 type_traits -*- C++ -*- -// Copyright (C) 2004 Free Software Foundation, Inc. +// Copyright (C) 2004, 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 @@ -144,10 +144,10 @@ namespace tr1 // template // struct is_member_function_pointer // : public false_type { }; - // _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*, - // is_function<_Tp>::value) - - // Ugly, temporary workaround for member functions with up to 15 arguments. + // _DEFINE_SPEC(2, is_member_function_pointer, _Tp _Cp::*, + // is_function<_Tp>::value) + // + // Temporary workaround for member functions with up to 15 arguments: template struct __is_mfp_helper { static const bool __value = false; }; @@ -516,6 +516,10 @@ namespace tr1 template struct has_virtual_destructor : public false_type { }; + + template + struct alignment_of + : public integral_constant { }; template struct rank @@ -652,6 +656,77 @@ namespace tr1 { typedef typename remove_reference<_Tp>::type* type; }; /// @brief other transformations [4.8]. + + // Due to c++/19163 and c++/17743, for the time being we cannot use + // the correct, neat implementation :-( + // + // template + // struct aligned_storage + // { typedef char type[_Len] __attribute__((aligned(_Align))); } + // + // Temporary workaround, useful for Align up to 32: + template + struct aligned_storage { }; + + template + struct aligned_storage<_Len, 1> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(1))); + }; + }; + + template + struct aligned_storage<_Len, 2> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(2))); + }; + }; + + template + struct aligned_storage<_Len, 4> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(4))); + }; + }; + + template + struct aligned_storage<_Len, 8> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(8))); + }; + }; + + template + struct aligned_storage<_Len, 16> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(16))); + }; + }; + + template + struct aligned_storage<_Len, 32> + { + union type + { + unsigned char __data[_Len]; + char __align __attribute__((aligned(32))); + }; + }; #undef _DEFINE_SPEC_0_HELPER #undef _DEFINE_SPEC_1_HELPER diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc new file mode 100644 index 00000000000..de04f979c57 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/aligned_storage.cc @@ -0,0 +1,63 @@ +// 2005-01-11 Paolo Carlini +// +// 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.8 Other transformations + +#include +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + using std::tr1::aligned_storage; + using std::tr1::alignment_of; + using namespace __gnu_test; + + const std::size_t align_c = alignment_of::value; + VERIFY( (sizeof(aligned_storage<4, align_c>::type) >= 4) ); + VERIFY( (__alignof__(aligned_storage<4, align_c>::type) == align_c) ); + + const std::size_t align_s = alignment_of::value; + VERIFY( (sizeof(aligned_storage<1, align_s>::type) >= 1) ); + VERIFY( (__alignof__(aligned_storage<1, align_s>::type) == align_s) ); + + const std::size_t align_i = alignment_of::value; + VERIFY( (sizeof(aligned_storage<7, align_i>::type) >= 7) ); + VERIFY( (__alignof__(aligned_storage<7, align_i>::type) == align_i) ); + + const std::size_t align_d = alignment_of::value; + VERIFY( (sizeof(aligned_storage<2, align_d>::type) >= 2) ); + VERIFY( (__alignof__(aligned_storage<2, align_d>::type) == align_d) ); + + const std::size_t align_ai = alignment_of::value; + VERIFY( (sizeof(aligned_storage<20, align_ai>::type) >= 20) ); + VERIFY( (__alignof__(aligned_storage<20, align_ai>::type) == align_ai) ); + + const std::size_t align_ct = alignment_of::value; + VERIFY( (sizeof(aligned_storage<11, align_ct>::type) >= 11) ); + VERIFY( (__alignof__(aligned_storage<11, align_ct>::type) == align_ct) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc new file mode 100644 index 00000000000..d6b42fb2082 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/other_transformations/aligned_storage/typedefs.cc @@ -0,0 +1,33 @@ +// 2005-01-11 Paolo Carlini +// +// 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 + +// { dg-do compile } + +void test01() +{ + // Check for required typedefs + typedef std::tr1::aligned_storage<1, 1> test_type; + typedef test_type::type type; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc new file mode 100644 index 00000000000..b10fd3350c3 --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/alignment_of.cc @@ -0,0 +1,45 @@ +// 2005-01-11 Paolo Carlini +// +// 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.5.3 Type properties + +#include +#include +#include + +void test01() +{ + bool test __attribute__((unused)) = true; + using std::tr1::alignment_of; + using namespace __gnu_test; + + VERIFY( (test_property(__alignof__(char))) ); + VERIFY( (test_property(__alignof__(short))) ); + VERIFY( (test_property(__alignof__(int))) ); + VERIFY( (test_property(__alignof__(double))) ); + VERIFY( (test_property(__alignof__(int[4]))) ); + VERIFY( (test_property(__alignof__(ClassType))) ); +} + +int main() +{ + test01(); + return 0; +} diff --git a/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc new file mode 100644 index 00000000000..01c193a994a --- /dev/null +++ b/libstdc++-v3/testsuite/tr1/4_metaprogramming/type_properties/alignment_of/typedefs.cc @@ -0,0 +1,36 @@ +// 2005-01-11 Paolo Carlini +// +// 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 + +// { dg-do compile } + +void test01() +{ + // Check for required typedefs + typedef std::tr1::alignment_of 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; +} -- 2.30.2