2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_scalar_h) |
|
24 #define octave_scalar_h 1 |
|
25 |
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <cstdlib> |
|
31 |
|
32 #include <string> |
|
33 |
|
34 class ostream; |
|
35 |
2889
|
36 #include "lo-mappers.h" |
2376
|
37 #include "lo-utils.h" |
|
38 #include "mx-base.h" |
2477
|
39 #include "oct-alloc.h" |
2376
|
40 #include "str-vec.h" |
|
41 |
|
42 #include "ov-base.h" |
|
43 #include "ov-typeinfo.h" |
|
44 |
|
45 class Octave_map; |
|
46 class octave_value_list; |
|
47 |
|
48 class tree_walker; |
|
49 |
|
50 // Real scalar values. |
|
51 |
|
52 class |
|
53 octave_scalar : public octave_base_value |
|
54 { |
|
55 public: |
|
56 |
|
57 octave_scalar (void) |
|
58 : octave_base_value (), scalar (0.0) { } |
|
59 |
|
60 octave_scalar (double d) |
|
61 : octave_base_value (), scalar (d) { } |
|
62 |
|
63 octave_scalar (const octave_scalar& s) |
|
64 : octave_base_value (), scalar (s.scalar) { } |
|
65 |
|
66 ~octave_scalar (void) { } |
|
67 |
|
68 octave_value *clone (void) { return new octave_scalar (*this); } |
|
69 |
2477
|
70 void *operator new (size_t size) |
|
71 { return allocator.alloc (size); } |
|
72 |
|
73 void operator delete (void *p, size_t size) |
|
74 { allocator.free (p, size); } |
2376
|
75 |
|
76 octave_value index (const octave_value_list& idx) const; |
|
77 |
|
78 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
79 |
|
80 int rows (void) const { return 1; } |
|
81 int columns (void) const { return 1; } |
|
82 |
|
83 bool is_defined (void) const { return true; } |
|
84 bool is_real_scalar (void) const { return true; } |
|
85 |
|
86 octave_value all (void) const { return (scalar != 0.0); } |
|
87 octave_value any (void) const { return (scalar != 0.0); } |
|
88 |
|
89 bool is_real_type (void) const { return true; } |
|
90 bool is_scalar_type (void) const { return true; } |
|
91 bool is_numeric_type (void) const { return true; } |
|
92 |
|
93 bool valid_as_scalar_index (void) const |
|
94 { return (! xisnan (scalar) && NINT (scalar) == 1); } |
|
95 |
|
96 bool valid_as_zero_index (void) const |
|
97 { return (! xisnan (scalar) && NINT (scalar) == 0); } |
|
98 |
|
99 bool is_true (void) const { return (scalar != 0.0); } |
|
100 |
|
101 double double_value (bool = false) const { return scalar; } |
|
102 |
2916
|
103 double scalar_value (bool = false) const { return scalar; } |
|
104 |
2376
|
105 Matrix matrix_value (bool = false) const { return Matrix (1, 1, scalar); } |
|
106 |
|
107 Complex complex_value (bool = false) const { return scalar; } |
|
108 |
|
109 ComplexMatrix complex_matrix_value (bool = false) const |
|
110 { return ComplexMatrix (1, 1, Complex (scalar)); } |
|
111 |
|
112 octave_value not (void) const { return octave_value (! scalar); } |
|
113 |
|
114 octave_value uminus (void) const { return octave_value (- scalar); } |
|
115 |
|
116 octave_value transpose (void) const { return octave_value (scalar); } |
|
117 |
|
118 octave_value hermitian (void) const { return octave_value (scalar); } |
|
119 |
|
120 void increment (void) { ++scalar; } |
|
121 |
|
122 void decrement (void) { --scalar; } |
|
123 |
|
124 octave_value convert_to_str (void) const; |
|
125 |
2901
|
126 void print (ostream& os, bool pr_as_read_syntax = false) const; |
|
127 |
|
128 void print_raw (ostream& os, bool pr_as_read_syntax = false) const; |
|
129 |
|
130 bool print_name_tag (ostream& os, const string& name) const; |
2376
|
131 |
|
132 int type_id (void) const { return t_id; } |
|
133 |
|
134 string type_name (void) const { return t_name; } |
|
135 |
|
136 static int static_type_id (void) { return t_id; } |
|
137 |
|
138 static void register_type (void) |
|
139 { t_id = octave_value_typeinfo::register_type (t_name); } |
|
140 |
|
141 private: |
|
142 |
2477
|
143 // The value of this scalar. |
2376
|
144 double scalar; |
|
145 |
2477
|
146 // For custom memory management. |
|
147 static octave_allocator allocator; |
|
148 |
|
149 // Type id of scalar objects, set by register_type(). |
2376
|
150 static int t_id; |
|
151 |
2477
|
152 // Type name of scalar objects, defined in ov-scalar.cc. |
2376
|
153 static const string t_name; |
|
154 }; |
|
155 |
|
156 #endif |
|
157 |
|
158 /* |
|
159 ;;; Local Variables: *** |
|
160 ;;; mode: C++ *** |
|
161 ;;; End: *** |
|
162 */ |