Mercurial > hg > octave-nkf
annotate liboctave/lo-traits.h @ 9685:e793865ede63
implement builtin_type
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 02 Oct 2009 08:27:44 +0200 |
parents | 0f6683a8150a |
children | 192d94cff6c1 |
rev | line source |
---|---|
8725 | 1 /* |
2 | |
3 Copyright (C) 2009 John W. Eaton | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #if !defined (octave_liboctave_traits_h) | |
24 #define octave_liboctave_traits_h 1 | |
25 | |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
26 // Ideas for these classes taken from C++ Templates, The Complete |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
27 // Guide by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
28 // (2003). |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
29 |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
30 // Select a type based on the value of a constant expression. |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
31 |
8725 | 32 template <bool cond, typename T1, typename T2> |
33 class if_then_else; | |
34 | |
35 template<typename T1, typename T2> | |
36 class if_then_else<true, T1, T2> | |
37 { | |
38 public: | |
39 | |
40 typedef T1 result; | |
41 }; | |
42 | |
43 template<typename T1, typename T2> | |
44 class if_then_else<false, T1, T2> | |
45 { | |
46 public: | |
47 | |
48 typedef T2 result; | |
49 }; | |
50 | |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
51 // Determine whether a template paramter is a class type. |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
52 |
8725 | 53 template<typename T1> |
54 class is_class_type | |
55 { | |
56 private: | |
57 | |
58 typedef char one; | |
59 typedef struct { char c[2]; } two; | |
60 | |
61 // Classes can have pointers to members. | |
62 template<typename T2> static one is_class_type_test (int T2::*); | |
63 | |
64 // Catch everything else. | |
65 template<typename T2> static two is_class_type_test (...); | |
66 | |
67 public: | |
68 | |
69 enum { yes = sizeof (is_class_type_test<T1> (0)) == 1 }; | |
70 enum { no = ! yes }; | |
71 }; | |
72 | |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
73 // Define typename ref_param<T>::type as T const& if T is a class |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
74 // type. Otherwise, define it to be T. |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
75 |
8725 | 76 template<typename T> |
77 class ref_param | |
78 { | |
79 public: | |
80 | |
81 typedef typename if_then_else<is_class_type<T>::no, T, T const&>::result type; | |
82 }; | |
83 | |
9685 | 84 // Will turn TemplatedClass<T> to T, leave T otherwise. |
85 // Useful for stripping wrapper classes, like octave_int. | |
86 | |
87 template<template<typename> class TemplatedClass, typename T> | |
88 class strip_template_param | |
89 { | |
90 public: | |
91 typedef T type; | |
92 }; | |
93 | |
94 template<template<typename> class TemplatedClass, typename T> | |
95 class strip_template_param<TemplatedClass, TemplatedClass<T> > | |
96 { | |
97 public: | |
98 typedef T type; | |
99 }; | |
100 | |
8725 | 101 #endif |
102 | |
103 /* | |
104 ;;; Local Variables: *** | |
105 ;;; mode: C *** | |
106 ;;; page-delimiter: "^/\\*" *** | |
107 ;;; End: *** | |
108 */ |