lo-traits.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_liboctave_traits_h)
00024 #define octave_liboctave_traits_h 1
00025 
00026 // Ideas for these classes taken from C++ Templates, The Complete
00027 // Guide by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley
00028 // (2003).
00029 
00030 // Select a type based on the value of a constant expression.
00031 
00032 template <bool cond, typename T1, typename T2>
00033 class if_then_else;
00034 
00035 template<typename T1, typename T2>
00036 class if_then_else<true, T1, T2>
00037 {
00038 public:
00039 
00040   typedef T1 result;
00041 };
00042 
00043 template<typename T1, typename T2>
00044 class if_then_else<false, T1, T2>
00045 {
00046 public:
00047 
00048   typedef T2 result;
00049 };
00050 
00051 // Determine whether two types are equal.
00052 template <class T1, class T2>
00053 class equal_types
00054 {
00055 public:
00056 
00057   static const bool value = false;
00058 };
00059 
00060 template <class T>
00061 class equal_types <T, T>
00062 {
00063 public:
00064 
00065   static const bool value = true;
00066 };
00067 
00068 // Determine whether a type is an instance of a template.
00069 
00070 template <template <class> class Template, class T>
00071 class is_instance
00072 {
00073 public:
00074 
00075   static const bool value = false;
00076 };
00077 
00078 template <template <class> class Template, class T>
00079 class is_instance <Template, Template<T> >
00080 {
00081 public:
00082 
00083   static const bool value = true;
00084 };
00085 
00086 // Determine whether a template paramter is a class type.
00087 
00088 template<typename T1>
00089 class is_class_type
00090 {
00091 private:
00092 
00093   typedef char one;
00094   typedef struct { char c[2]; } two;
00095 
00096   // Classes can have pointers to members.
00097   template<typename T2> static one is_class_type_test (int T2::*);
00098 
00099   // Catch everything else.
00100   template<typename T2> static two is_class_type_test (...);
00101 
00102 public:
00103 
00104   enum { yes = sizeof (is_class_type_test<T1> (0)) == 1 };
00105   enum { no = ! yes };
00106 };
00107 
00108 // Define typename ref_param<T>::type as T const& if T is a class
00109 // type.  Otherwise, define it to be T.
00110 
00111 template<typename T>
00112 class ref_param
00113 {
00114 public:
00115 
00116   typedef typename if_then_else<is_class_type<T>::no, T, T const&>::result type;
00117 };
00118 
00119 // Will turn TemplatedClass<T> to T, leave T otherwise.
00120 // Useful for stripping wrapper classes, like octave_int.
00121 
00122 template<template<typename> class TemplatedClass, typename T>
00123 class strip_template_param
00124 {
00125 public:
00126   typedef T type;
00127 };
00128 
00129 template<template<typename> class TemplatedClass, typename T>
00130 class strip_template_param<TemplatedClass, TemplatedClass<T> >
00131 {
00132 public:
00133   typedef T type;
00134 };
00135 
00136 // Will turn TemplatedClass<T> to TemplatedClass<S>, T to S otherwise.
00137 // Useful for generic promotions.
00138 
00139 template<template<typename> class TemplatedClass, typename T, typename S>
00140 class subst_template_param
00141 {
00142 public:
00143   typedef S type;
00144 };
00145 
00146 template<template<typename> class TemplatedClass, typename T, typename S>
00147 class subst_template_param<TemplatedClass, TemplatedClass<T>, S>
00148 {
00149 public:
00150   typedef TemplatedClass<S> type;
00151 };
00152 
00153 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines