Mercurial > octave
annotate liboctave/util/lo-traits.h @ 33658:b1d6e40ac737 stable tip
NEWS.9.md: Fix typo and minor formatting changes.
* etc/NEWS.9.md: Fix typo. Adjust whitespace. Use markdown syntax for code
snippets.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Wed, 05 Jun 2024 11:27:35 +0200 |
parents | 2e484f9f1f18 |
children |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
32632
2e484f9f1f18
maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
31706
diff
changeset
|
3 // Copyright (C) 2009-2024 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
8725 | 25 |
20791
f7084eae3318
maint: Use Octave coding conventions for #if statements.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
26 #if ! defined (octave_lo_traits_h) |
17822
ebb3ef964372
maint: Use common #define syntax "octave_filename_h" in h_files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
27 #define octave_lo_traits_h 1 |
8725 | 28 |
21244
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
29 #include "octave-config.h" |
1473547f50f5
include octave-config.h in public header files
John W. Eaton <jwe@octave.org>
parents:
21139
diff
changeset
|
30 |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
31 // 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
|
32 // 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
|
33 // (2003). |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
34 |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
35 // 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
|
36 |
8725 | 37 template <bool cond, typename T1, typename T2> |
38 class if_then_else; | |
39 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
40 template <typename T1, typename T2> |
8725 | 41 class if_then_else<true, T1, T2> |
42 { | |
43 public: | |
44 | |
45 typedef T1 result; | |
46 }; | |
47 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
48 template <typename T1, typename T2> |
8725 | 49 class if_then_else<false, T1, T2> |
50 { | |
51 public: | |
52 | |
53 typedef T2 result; | |
54 }; | |
55 | |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
56 // Determine whether two types are equal. |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
57 template <typename T1, typename T2> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
58 class equal_types |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
59 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
60 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
61 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
62 static const bool value = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
63 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
64 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
65 template <typename T> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
66 class equal_types <T, T> |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
67 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
68 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
69 |
10758
f3892d8eea9f
optimize horzcat/vertcat for scalars, cells and structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10158
diff
changeset
|
70 static const bool value = true; |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
71 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
72 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
73 // Determine whether a type is an instance of a template. |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
74 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
75 template <template <typename> class Template, typename T> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
76 class is_instance |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
77 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
78 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
79 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
80 static const bool value = false; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
81 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
82 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
83 template <template <typename> class Template, typename T> |
23534
b6498c088fca
maint: Don't write '> >' for declaration of templates that use templates.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
84 class is_instance <Template, Template<T>> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
85 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
86 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
87 |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
88 static const bool value = true; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
89 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
90 |
27956
2310164737b3
fix many spelling errors (bug #57613)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
91 // Determine whether a template parameter is a class type. |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
92 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
93 template <typename T1> |
8725 | 94 class is_class_type |
95 { | |
96 private: | |
97 | |
98 typedef char one; | |
99 typedef struct { char c[2]; } two; | |
100 | |
101 // Classes can have pointers to members. | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
102 template <typename T2> static one is_class_type_test (int T2::*); |
8725 | 103 |
104 // Catch everything else. | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
105 template <typename T2> static two is_class_type_test (...); |
8725 | 106 |
107 public: | |
108 | |
109 enum { yes = sizeof (is_class_type_test<T1> (0)) == 1 }; | |
110 enum { no = ! yes }; | |
111 }; | |
112 | |
8726
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
113 // 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
|
114 // type. Otherwise, define it to be T. |
0f6683a8150a
some comments for lo-traits.h
John W. Eaton <jwe@octave.org>
parents:
8725
diff
changeset
|
115 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
116 template <typename T> |
8725 | 117 class ref_param |
118 { | |
119 public: | |
120 | |
121 typedef typename if_then_else<is_class_type<T>::no, T, T const&>::result type; | |
122 }; | |
123 | |
9685 | 124 // Will turn TemplatedClass<T> to T, leave T otherwise. |
125 // Useful for stripping wrapper classes, like octave_int. | |
126 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
127 template <template <typename> class TemplatedClass, typename T> |
9685 | 128 class strip_template_param |
129 { | |
130 public: | |
131 typedef T type; | |
132 }; | |
133 | |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
134 template <template <typename> class TemplatedClass, typename T> |
23534
b6498c088fca
maint: Don't write '> >' for declaration of templates that use templates.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
135 class strip_template_param<TemplatedClass, TemplatedClass<T>> |
9685 | 136 { |
137 public: | |
138 typedef T type; | |
139 }; | |
140 | |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
141 // Will turn TemplatedClass<T> to TemplatedClass<S>, T to S otherwise. |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
142 // Useful for generic promotions. |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
143 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
144 template <template <typename> class TemplatedClass, typename T, typename S> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
145 class subst_template_param |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
146 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
147 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
148 typedef S type; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
149 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
150 |
21139
538b57866b90
consistently use "typename" intead of "class" in template declarations
John W. Eaton <jwe@octave.org>
parents:
20791
diff
changeset
|
151 template <template <typename> class TemplatedClass, typename T, typename S> |
9721
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
152 class subst_template_param<TemplatedClass, TemplatedClass<T>, S> |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
153 { |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
154 public: |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
155 typedef TemplatedClass<S> type; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
156 }; |
192d94cff6c1
improve sum & implement the 'extra' option, refactor some code
Jaroslav Hajek <highegg@gmail.com>
parents:
9685
diff
changeset
|
157 |
8725 | 158 #endif |