"Fossies" - the Fresh Open Source Software Archive

Member "erltools/stl/type_tra.h" (25 Jul 1999, 8568 Bytes) of package /linux/misc/old/erltools-4.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "type_tra.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *
    3  * Copyright (c) 1997
    4  * Silicon Graphics Computer Systems, Inc.
    5  *
    6  * Permission to use, copy, modify, distribute and sell this software
    7  * and its documentation for any purpose is hereby granted without fee,
    8  * provided that the above copyright notice appear in all copies and
    9  * that both that copyright notice and this permission notice appear
   10  * in supporting documentation.  Silicon Graphics makes no
   11  * representations about the suitability of this software for any
   12  * purpose.  It is provided "as is" without express or implied warranty.
   13  */
   14 
   15 #ifndef __TYPE_TRAITS_H
   16 #define __TYPE_TRAITS_H
   17 
   18 #include <stl_config.h>
   19 
   20 /*
   21 This header file provides a framework for allowing compile time dispatch
   22 based on type attributes. This is useful when writing template code.
   23 For example, when making a copy of an array of an unknown type, it helps
   24 to know if the type has a trivial copy constructor or not, to help decide
   25 if a memcpy can be used.
   26 
   27 The class template __type_traits provides a series of typedefs each of
   28 which is either __true_type or __false_type. The argument to
   29 __type_traits can be any type. The typedefs within this template will
   30 attain their correct values by one of these means:
   31     1. The general instantiation contain conservative values which work
   32        for all types.
   33     2. Specializations may be declared to make distinctions between types.
   34     3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
   35        will automatically provide the appropriate specializations for all
   36        types.
   37 
   38 EXAMPLE:
   39 
   40 //Copy an array of elements which have non-trivial copy constructors
   41 template <class T> void copy(T* source,T* destination,int n,__false_type);
   42 //Copy an array of elements which have trivial copy constructors. Use memcpy.
   43 template <class T> void copy(T* source,T* destination,int n,__true_type);
   44 
   45 //Copy an array of any type by using the most efficient copy mechanism
   46 template <class T> inline void copy(T* source,T* destination,int n) {
   47    copy(source,destination,n,__type_traits<T>::has_trivial_copy_constructor());
   48 }
   49 */
   50 
   51 
   52 struct __true_type {
   53 };
   54 
   55 struct __false_type {
   56 };
   57 
   58 template <class type>
   59 struct __type_traits { 
   60    typedef __true_type     this_dummy_member_must_be_first;
   61                    /* Do not remove this member. It informs a compiler which
   62                       automatically specializes __type_traits that this
   63                       __type_traits template is special. It just makes sure that
   64                       things work if an implementation is using a template
   65                       called __type_traits for something unrelated. */
   66 
   67    /* The following restrictions should be observed for the sake of
   68       compilers which automatically produce type specific specializations 
   69       of this class:
   70           - You may reorder the members below if you wish
   71           - You may remove any of the members below if you wish
   72           - You must not rename members without making the corresponding
   73             name change in the compiler
   74           - Members you add will be treated like regular members unless
   75             you add the appropriate support in the compiler. */
   76  
   77 
   78    typedef __false_type    has_trivial_default_constructor;
   79    typedef __false_type    has_trivial_copy_constructor;
   80    typedef __false_type    has_trivial_assignment_operator;
   81    typedef __false_type    has_trivial_destructor;
   82    typedef __false_type    is_POD_type;
   83 };
   84 
   85 
   86 
   87 // Provide some specializations.  This is harmless for compilers that
   88 //  have built-in __types_traits support, and essential for compilers
   89 //  that don't.
   90 
   91 struct __type_traits<char> {
   92    typedef __true_type    has_trivial_default_constructor;
   93    typedef __true_type    has_trivial_copy_constructor;
   94    typedef __true_type    has_trivial_assignment_operator;
   95    typedef __true_type    has_trivial_destructor;
   96    typedef __true_type    is_POD_type;
   97 };
   98 
   99 struct __type_traits<signed char> {
  100    typedef __true_type    has_trivial_default_constructor;
  101    typedef __true_type    has_trivial_copy_constructor;
  102    typedef __true_type    has_trivial_assignment_operator;
  103    typedef __true_type    has_trivial_destructor;
  104    typedef __true_type    is_POD_type;
  105 };
  106 
  107 struct __type_traits<unsigned char> {
  108    typedef __true_type    has_trivial_default_constructor;
  109    typedef __true_type    has_trivial_copy_constructor;
  110    typedef __true_type    has_trivial_assignment_operator;
  111    typedef __true_type    has_trivial_destructor;
  112    typedef __true_type    is_POD_type;
  113 };
  114 
  115 struct __type_traits<short> {
  116    typedef __true_type    has_trivial_default_constructor;
  117    typedef __true_type    has_trivial_copy_constructor;
  118    typedef __true_type    has_trivial_assignment_operator;
  119    typedef __true_type    has_trivial_destructor;
  120    typedef __true_type    is_POD_type;
  121 };
  122 
  123 struct __type_traits<unsigned short> {
  124    typedef __true_type    has_trivial_default_constructor;
  125    typedef __true_type    has_trivial_copy_constructor;
  126    typedef __true_type    has_trivial_assignment_operator;
  127    typedef __true_type    has_trivial_destructor;
  128    typedef __true_type    is_POD_type;
  129 };
  130 
  131 struct __type_traits<int> {
  132    typedef __true_type    has_trivial_default_constructor;
  133    typedef __true_type    has_trivial_copy_constructor;
  134    typedef __true_type    has_trivial_assignment_operator;
  135    typedef __true_type    has_trivial_destructor;
  136    typedef __true_type    is_POD_type;
  137 };
  138 
  139 struct __type_traits<unsigned int> {
  140    typedef __true_type    has_trivial_default_constructor;
  141    typedef __true_type    has_trivial_copy_constructor;
  142    typedef __true_type    has_trivial_assignment_operator;
  143    typedef __true_type    has_trivial_destructor;
  144    typedef __true_type    is_POD_type;
  145 };
  146 
  147 struct __type_traits<long> {
  148    typedef __true_type    has_trivial_default_constructor;
  149    typedef __true_type    has_trivial_copy_constructor;
  150    typedef __true_type    has_trivial_assignment_operator;
  151    typedef __true_type    has_trivial_destructor;
  152    typedef __true_type    is_POD_type;
  153 };
  154 
  155 struct __type_traits<unsigned long> {
  156    typedef __true_type    has_trivial_default_constructor;
  157    typedef __true_type    has_trivial_copy_constructor;
  158    typedef __true_type    has_trivial_assignment_operator;
  159    typedef __true_type    has_trivial_destructor;
  160    typedef __true_type    is_POD_type;
  161 };
  162 
  163 struct __type_traits<float> {
  164    typedef __true_type    has_trivial_default_constructor;
  165    typedef __true_type    has_trivial_copy_constructor;
  166    typedef __true_type    has_trivial_assignment_operator;
  167    typedef __true_type    has_trivial_destructor;
  168    typedef __true_type    is_POD_type;
  169 };
  170 
  171 struct __type_traits<double> {
  172    typedef __true_type    has_trivial_default_constructor;
  173    typedef __true_type    has_trivial_copy_constructor;
  174    typedef __true_type    has_trivial_assignment_operator;
  175    typedef __true_type    has_trivial_destructor;
  176    typedef __true_type    is_POD_type;
  177 };
  178 
  179 struct __type_traits<long double> {
  180    typedef __true_type    has_trivial_default_constructor;
  181    typedef __true_type    has_trivial_copy_constructor;
  182    typedef __true_type    has_trivial_assignment_operator;
  183    typedef __true_type    has_trivial_destructor;
  184    typedef __true_type    is_POD_type;
  185 };
  186 
  187 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  188 
  189 template <class T>
  190 struct __type_traits<T*> {
  191    typedef __true_type    has_trivial_default_constructor;
  192    typedef __true_type    has_trivial_copy_constructor;
  193    typedef __true_type    has_trivial_assignment_operator;
  194    typedef __true_type    has_trivial_destructor;
  195    typedef __true_type    is_POD_type;
  196 };
  197 
  198 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  199 
  200 struct __type_traits<char*> {
  201    typedef __true_type    has_trivial_default_constructor;
  202    typedef __true_type    has_trivial_copy_constructor;
  203    typedef __true_type    has_trivial_assignment_operator;
  204    typedef __true_type    has_trivial_destructor;
  205    typedef __true_type    is_POD_type;
  206 };
  207 
  208 struct __type_traits<signed char*> {
  209    typedef __true_type    has_trivial_default_constructor;
  210    typedef __true_type    has_trivial_copy_constructor;
  211    typedef __true_type    has_trivial_assignment_operator;
  212    typedef __true_type    has_trivial_destructor;
  213    typedef __true_type    is_POD_type;
  214 };
  215 
  216 struct __type_traits<unsigned char*> {
  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 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  225 
  226 
  227 #endif /* __TYPE_TRAITS_H */