1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_base_scalar_h)
27 #define octave_ov_base_scalar_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "lo-mappers.h"
37 #include "lo-utils.h"
38 #include "str-vec.h"
39 #include "MatrixType.h"
40 
41 #include "ov-base.h"
42 #include "ov-typeinfo.h"
43 
44 // Real scalar values.
45 
46 template <typename ST>
47 class
48 octave_base_scalar : public octave_base_value
49 {
50 public:
51 
octave_base_scalar(void)52   octave_base_scalar (void)
53     : octave_base_value (), scalar () { }
54 
octave_base_scalar(const ST & s)55   octave_base_scalar (const ST& s)
56     : octave_base_value (), scalar (s) { }
57 
octave_base_scalar(const octave_base_scalar & s)58   octave_base_scalar (const octave_base_scalar& s)
59     : octave_base_value (), scalar (s.scalar) { }
60 
61   ~octave_base_scalar (void) = default;
62 
squeeze(void)63   octave_value squeeze (void) const { return scalar; }
64 
full_value(void)65   octave_value full_value (void) const { return scalar; }
66 
67   // We don't need to override all three forms of subsref.  The using
68   // declaration will avoid warnings about partially-overloaded virtual
69   // functions.
70   using octave_base_value::subsref;
71 
72   octave_value subsref (const std::string& type,
73                         const std::list<octave_value_list>& idx);
74 
subsref(const std::string & type,const std::list<octave_value_list> & idx,int)75   octave_value_list subsref (const std::string& type,
76                              const std::list<octave_value_list>& idx, int)
77   { return subsref (type, idx); }
78 
79   octave_value subsasgn (const std::string& type,
80                          const std::list<octave_value_list>& idx,
81                          const octave_value& rhs);
82 
is_constant(void)83   bool is_constant (void) const { return true; }
84 
is_defined(void)85   bool is_defined (void) const { return true; }
86 
87   dim_vector dims (void) const;
88 
numel(void)89   octave_idx_type numel (void) const { return 1; }
90 
ndims(void)91   int ndims (void) const { return 2; }
92 
nnz(void)93   octave_idx_type nnz (void) const { return (scalar != ST () ? 1 : 0); }
94 
95   octave_value permute (const Array<int>&, bool = false) const;
96 
97   octave_value reshape (const dim_vector& new_dims) const;
98 
byte_size(void)99   std::size_t byte_size (void) const { return sizeof (ST); }
100 
101   octave_value all (int = 0) const { return (scalar != ST ()); }
102 
103   octave_value any (int = 0) const { return (scalar != ST ()); }
104 
105   octave_value diag (octave_idx_type k = 0) const;
106 
107   octave_value diag (octave_idx_type m, octave_idx_type n) const;
108 
sort(octave_idx_type,sortmode)109   octave_value sort (octave_idx_type, sortmode) const
110   { return octave_value (scalar); }
sort(Array<octave_idx_type> & sidx,octave_idx_type,sortmode)111   octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type,
112                      sortmode) const
113   {
114     sidx.resize (dim_vector (1, 1));
115     sidx(0) = 0;
116     return octave_value (scalar);
117   }
118 
119   sortmode issorted (sortmode mode = UNSORTED) const
120   { return mode == UNSORTED ? ASCENDING : mode; }
121 
sort_rows_idx(sortmode)122   Array<octave_idx_type> sort_rows_idx (sortmode) const
123   {
124     return Array<octave_idx_type> (dim_vector (1, 1),
125                                    static_cast<octave_idx_type> (0));
126   }
127 
128   sortmode is_sorted_rows (sortmode mode = UNSORTED) const
129   { return mode == UNSORTED ? ASCENDING : mode; }
130 
matrix_type(void)131   MatrixType matrix_type (void) const { return MatrixType::Diagonal; }
matrix_type(const MatrixType &)132   MatrixType matrix_type (const MatrixType&) const
133   { return matrix_type (); }
134 
is_scalar_type(void)135   bool is_scalar_type (void) const { return true; }
136 
isnumeric(void)137   bool isnumeric (void) const { return true; }
138 
139   bool is_true (void) const;
140 
141   void print (std::ostream& os, bool pr_as_read_syntax = false);
142 
143   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
144 
145   bool print_name_tag (std::ostream& os, const std::string& name) const;
146 
147   void short_disp (std::ostream& os) const;
148 
149   float_display_format get_edit_display_format (void) const;
150 
151   std::string edit_display (const float_display_format& fmt,
152                             octave_idx_type i, octave_idx_type j) const;
153 
154   // Unsafe.  This function exists to support the MEX interface.
155   // You should not use it anywhere else.
mex_get_data(void)156   void * mex_get_data (void) const { return const_cast<ST *> (&scalar); }
157 
scalar_ref(void)158   const ST& scalar_ref (void) const { return scalar; }
159 
scalar_ref(void)160   ST& scalar_ref (void) { return scalar; }
161 
162   octave_value fast_elem_extract (octave_idx_type n) const;
163 
164   bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
165 
166 protected:
167 
168   // The value of this scalar.
169   ST scalar;
170 };
171 
172 #endif
173