home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / bloodshed / devcpp-4.9.9.2_setup.exe / type_traits.h < prev    next >
C/C++ Source or Header  |  2005-01-29  |  12KB  |  406 lines

  1. // Type traits implementation -*- C++ -*-
  2.  
  3. // Copyright (C) 2001, 2004 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library.  This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 2, or (at your option)
  9. // any later version.
  10.  
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15.  
  16. // You should have received a copy of the GNU General Public License along
  17. // with this library; see the file COPYING.  If not, write to the Free
  18. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. // USA.
  20.  
  21. // As a special exception, you may use this file as part of a free software
  22. // library without restriction.  Specifically, if other files instantiate
  23. // templates or use macros or inline functions from this file, or you compile
  24. // this file and link it with other files to produce an executable, this
  25. // file does not by itself cause the resulting executable to be covered by
  26. // the GNU General Public License.  This exception does not however
  27. // invalidate any other reasons why the executable file might be covered by
  28. // the GNU General Public License.
  29.  
  30. /*
  31.  *
  32.  * Copyright (c) 1997
  33.  * Silicon Graphics Computer Systems, Inc.
  34.  *
  35.  * Permission to use, copy, modify, distribute and sell this software
  36.  * and its documentation for any purpose is hereby granted without fee,
  37.  * provided that the above copyright notice appear in all copies and
  38.  * that both that copyright notice and this permission notice appear
  39.  * in supporting documentation.  Silicon Graphics makes no
  40.  * representations about the suitability of this software for any
  41.  * purpose.  It is provided "as is" without express or implied warranty.
  42.  */
  43.  
  44. /** @file type_traits.h
  45.  *  This is an internal header file, included by other library headers.
  46.  *  You should not attempt to use it directly.
  47.  */
  48.  
  49. #ifndef _TYPE_TRAITS_H
  50. #define _TYPE_TRAITS_H 1
  51.  
  52. #pragma GCC system_header
  53.  
  54. #include <bits/c++config.h>
  55.  
  56. /*
  57. This header file provides a framework for allowing compile time dispatch
  58. based on type attributes. This is useful when writing template code.
  59. For example, when making a copy of an array of an unknown type, it helps
  60. to know if the type has a trivial copy constructor or not, to help decide
  61. if a memcpy can be used.
  62.  
  63. The class template __type_traits provides a series of typedefs each of
  64. which is either __true_type or __false_type. The argument to
  65. __type_traits can be any type. The typedefs within this template will
  66. attain their correct values by one of these means:
  67.     1. The general instantiation contain conservative values which work
  68.        for all types.
  69.     2. Specializations may be declared to make distinctions between types.
  70.     3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
  71.        will automatically provide the appropriate specializations for all
  72.        types.
  73.  
  74. EXAMPLE:
  75.  
  76. //Copy an array of elements which have non-trivial copy constructors
  77. template <class _Tp> void
  78.   copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
  79. //Copy an array of elements which have trivial copy constructors. Use memcpy.
  80. template <class _Tp> void
  81.   copy(_Tp* __source,_Tp* __destination,int __n,__true_type);
  82.  
  83. //Copy an array of any type by using the most efficient copy mechanism
  84. template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
  85.    copy(__source,__destination,__n,
  86.         typename __type_traits<_Tp>::has_trivial_copy_constructor());
  87. }
  88. */
  89.  
  90. struct __true_type {};
  91. struct __false_type {};
  92.  
  93. template <class _Tp>
  94.   struct __type_traits
  95.   {
  96.     typedef __true_type     this_dummy_member_must_be_first;
  97.     /* Do not remove this member. It informs a compiler which
  98.        automatically specializes __type_traits that this
  99.        __type_traits template is special. It just makes sure that
  100.        things work if an implementation is using a template
  101.        called __type_traits for something unrelated. */
  102.  
  103.    /* The following restrictions should be observed for the sake of
  104.       compilers which automatically produce type specific specializations
  105.       of this class:
  106.           - You may reorder the members below if you wish
  107.           - You may remove any of the members below if you wish
  108.           - You must not rename members without making the corresponding
  109.             name change in the compiler
  110.           - Members you add will be treated like regular members unless
  111.             you add the appropriate support in the compiler. */
  112.  
  113.  
  114.     typedef __false_type    has_trivial_default_constructor;
  115.     typedef __false_type    has_trivial_copy_constructor;
  116.     typedef __false_type    has_trivial_assignment_operator;
  117.     typedef __false_type    has_trivial_destructor;
  118.     typedef __false_type    is_POD_type;
  119.   };
  120.  
  121.  
  122. // Provide some specializations.
  123.  
  124. template<>
  125.   struct __type_traits<bool>
  126.   {
  127.     typedef __true_type    has_trivial_default_constructor;
  128.     typedef __true_type    has_trivial_copy_constructor;
  129.     typedef __true_type    has_trivial_assignment_operator;
  130.     typedef __true_type    has_trivial_destructor;
  131.     typedef __true_type    is_POD_type;
  132.   };
  133.  
  134. template<>
  135.   struct __type_traits<char>
  136.   {
  137.     typedef __true_type    has_trivial_default_constructor;
  138.     typedef __true_type    has_trivial_copy_constructor;
  139.     typedef __true_type    has_trivial_assignment_operator;
  140.     typedef __true_type    has_trivial_destructor;
  141.     typedef __true_type    is_POD_type;
  142.   };
  143.  
  144. template<>
  145.   struct __type_traits<signed char>
  146.   {
  147.     typedef __true_type    has_trivial_default_constructor;
  148.     typedef __true_type    has_trivial_copy_constructor;
  149.     typedef __true_type    has_trivial_assignment_operator;
  150.     typedef __true_type    has_trivial_destructor;
  151.     typedef __true_type    is_POD_type;
  152.   };
  153.  
  154. template<>
  155.   struct __type_traits<unsigned char>
  156.   {
  157.     typedef __true_type    has_trivial_default_constructor;
  158.     typedef __true_type    has_trivial_copy_constructor;
  159.     typedef __true_type    has_trivial_assignment_operator;
  160.     typedef __true_type    has_trivial_destructor;
  161.     typedef __true_type    is_POD_type;
  162.   };
  163.  
  164. template<>
  165.   struct __type_traits<wchar_t>
  166.   {
  167.     typedef __true_type    has_trivial_default_constructor;
  168.     typedef __true_type    has_trivial_copy_constructor;
  169.     typedef __true_type    has_trivial_assignment_operator;
  170.     typedef __true_type    has_trivial_destructor;
  171.     typedef __true_type    is_POD_type;
  172.   };
  173.  
  174. template<>
  175.   struct __type_traits<short>
  176.   {
  177.     typedef __true_type    has_trivial_default_constructor;
  178.     typedef __true_type    has_trivial_copy_constructor;
  179.     typedef __true_type    has_trivial_assignment_operator;
  180.     typedef __true_type    has_trivial_destructor;
  181.     typedef __true_type    is_POD_type;
  182.   };
  183.  
  184. template<>
  185.   struct __type_traits<unsigned short>
  186.   {
  187.     typedef __true_type    has_trivial_default_constructor;
  188.     typedef __true_type    has_trivial_copy_constructor;
  189.     typedef __true_type    has_trivial_assignment_operator;
  190.     typedef __true_type    has_trivial_destructor;
  191.     typedef __true_type    is_POD_type;
  192.   };
  193.  
  194. template<>
  195.   struct __type_traits<int>
  196.   {
  197.     typedef __true_type    has_trivial_default_constructor;
  198.     typedef __true_type    has_trivial_copy_constructor;
  199.     typedef __true_type    has_trivial_assignment_operator;
  200.     typedef __true_type    has_trivial_destructor;
  201.     typedef __true_type    is_POD_type;
  202.   };
  203.  
  204. template<>
  205.   struct __type_traits<unsigned int>
  206.   {
  207.     typedef __true_type    has_trivial_default_constructor;
  208.     typedef __true_type    has_trivial_copy_constructor;
  209.     typedef __true_type    has_trivial_assignment_operator;
  210.     typedef __true_type    has_trivial_destructor;
  211.     typedef __true_type    is_POD_type;
  212.   };
  213.  
  214. template<>
  215.   struct __type_traits<long>
  216.   {
  217.     typedef __true_type    has_trivial_default_constructor;
  218.     typedef __true_type    has_trivial_copy_constructor;
  219.     typedef __true_type    has_trivial_assignment_operator;
  220.     typedef __true_type    has_trivial_destructor;
  221.     typedef __true_type    is_POD_type;
  222.   };
  223.  
  224. template<>
  225.   struct __type_traits<unsigned long>
  226.   {
  227.     typedef __true_type    has_trivial_default_constructor;
  228.     typedef __true_type    has_trivial_copy_constructor;
  229.     typedef __true_type    has_trivial_assignment_operator;
  230.     typedef __true_type    has_trivial_destructor;
  231.     typedef __true_type    is_POD_type;
  232.   };
  233.  
  234. template<>
  235.   struct __type_traits<long long>
  236.   {
  237.     typedef __true_type    has_trivial_default_constructor;
  238.     typedef __true_type    has_trivial_copy_constructor;
  239.     typedef __true_type    has_trivial_assignment_operator;
  240.     typedef __true_type    has_trivial_destructor;
  241.     typedef __true_type    is_POD_type;
  242.   };
  243.  
  244. template<>
  245.   struct __type_traits<unsigned long long>
  246.   {
  247.     typedef __true_type    has_trivial_default_constructor;
  248.     typedef __true_type    has_trivial_copy_constructor;
  249.     typedef __true_type    has_trivial_assignment_operator;
  250.     typedef __true_type    has_trivial_destructor;
  251.     typedef __true_type    is_POD_type;
  252.   };
  253.  
  254. template<>
  255.   struct __type_traits<float>
  256.   {
  257.     typedef __true_type    has_trivial_default_constructor;
  258.     typedef __true_type    has_trivial_copy_constructor;
  259.     typedef __true_type    has_trivial_assignment_operator;
  260.     typedef __true_type    has_trivial_destructor;
  261.     typedef __true_type    is_POD_type;
  262.   };
  263.  
  264. template<>
  265.   struct __type_traits<double>
  266.   {
  267.     typedef __true_type    has_trivial_default_constructor;
  268.     typedef __true_type    has_trivial_copy_constructor;
  269.     typedef __true_type    has_trivial_assignment_operator;
  270.     typedef __true_type    has_trivial_destructor;
  271.     typedef __true_type    is_POD_type;
  272.   };
  273.  
  274. template<>
  275.   struct __type_traits<long double>
  276.   {
  277.     typedef __true_type    has_trivial_default_constructor;
  278.     typedef __true_type    has_trivial_copy_constructor;
  279.     typedef __true_type    has_trivial_assignment_operator;
  280.     typedef __true_type    has_trivial_destructor;
  281.     typedef __true_type    is_POD_type;
  282.   };
  283.  
  284. template <class _Tp>
  285.   struct __type_traits<_Tp*>
  286.   {
  287.     typedef __true_type    has_trivial_default_constructor;
  288.     typedef __true_type    has_trivial_copy_constructor;
  289.     typedef __true_type    has_trivial_assignment_operator;
  290.     typedef __true_type    has_trivial_destructor;
  291.     typedef __true_type    is_POD_type;
  292.   };
  293.  
  294. // The following could be written in terms of numeric_limits.
  295. // We're doing it separately to reduce the number of dependencies.
  296.  
  297. template <class _Tp>
  298.   struct _Is_integer
  299.   {
  300.     typedef __false_type _Integral;
  301.   };
  302.  
  303. template<>
  304.   struct _Is_integer<bool>
  305.   {
  306.     typedef __true_type _Integral;
  307.   };
  308.  
  309. template<>
  310.   struct _Is_integer<char>
  311.   {
  312.     typedef __true_type _Integral;
  313.   };
  314.  
  315. template<>
  316.   struct _Is_integer<signed char>
  317.   {
  318.     typedef __true_type _Integral;
  319.   };
  320.  
  321. template<>
  322.   struct _Is_integer<unsigned char>
  323.   {
  324.     typedef __true_type _Integral;
  325.   };
  326.  
  327. template<>
  328.   struct _Is_integer<wchar_t>
  329.   {
  330.     typedef __true_type _Integral;
  331.   };
  332.  
  333. template<>
  334.   struct _Is_integer<short>
  335.   {
  336.     typedef __true_type _Integral;
  337.   };
  338.  
  339. template<>
  340.   struct _Is_integer<unsigned short>
  341.   {
  342.     typedef __true_type _Integral;
  343.   };
  344.  
  345. template<>
  346.   struct _Is_integer<int>
  347.   {
  348.     typedef __true_type _Integral;
  349.   };
  350.  
  351. template<>
  352.   struct _Is_integer<unsigned int>
  353.   {
  354.     typedef __true_type _Integral;
  355.   };
  356.  
  357. template<>
  358.   struct _Is_integer<long>
  359.   {
  360.     typedef __true_type _Integral;
  361.   };
  362.  
  363. template<>
  364.   struct _Is_integer<unsigned long>
  365.   {
  366.     typedef __true_type _Integral;
  367.   };
  368.  
  369. template<>
  370.   struct _Is_integer<long long>
  371.   {
  372.     typedef __true_type _Integral;
  373.   };
  374.  
  375. template<>
  376.   struct _Is_integer<unsigned long long>
  377.   {
  378.     typedef __true_type _Integral;
  379.   };
  380.  
  381. template<typename _Tp>
  382.   struct _Is_normal_iterator
  383.   {
  384.     typedef __false_type _Normal;
  385.   };
  386.  
  387. // Forward declaration hack, should really include this from somewhere.
  388. namespace __gnu_cxx
  389. {
  390.   template<typename _Iterator, typename _Container>
  391.     class __normal_iterator;
  392. }
  393.  
  394. template<typename _Iterator, typename _Container>
  395.   struct _Is_normal_iterator< __gnu_cxx::__normal_iterator<_Iterator,
  396.                                _Container> >
  397.   {
  398.     typedef __true_type _Normal;
  399.   };
  400.  
  401. #endif /* _TYPE_TRAITS_H */
  402.  
  403. // Local Variables:
  404. // mode:C++
  405. // End:
  406.