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 (HAVE_CONFIG_H)
27 #  include "config.h"
28 #endif
29 
30 #include <istream>
31 #include <ostream>
32 
33 #include "oct-inttypes.h"
34 
35 #include "mx-base.h"
36 
37 #include "errwarn.h"
38 #include "mxarray.h"
39 #include "oct-hdf5.h"
40 #include "ovl.h"
41 #include "ops.h"
42 #include "ov-bool.h"
43 #include "ov-bool-mat.h"
44 #include "ov-base.h"
45 #include "ov-base-scalar.h"
46 #include "ov-base-scalar.cc"
47 #include "ov-re-mat.h"
48 #include "ov-scalar.h"
49 #include "pr-output.h"
50 
51 #include "ls-oct-text.h"
52 #include "ls-hdf5.h"
53 
54 // Prevent implicit instantiations on some systems (Windows, others?)
55 // that can lead to duplicate definitions of static data members.
56 
57 extern template class OCTINTERP_API octave_base_scalar<double>;
58 
59 template class octave_base_scalar<bool>;
60 
61 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool, "bool", "logical");
62 
63 static octave_base_value *
default_numeric_conversion_function(const octave_base_value & a)64 default_numeric_conversion_function (const octave_base_value& a)
65 {
66   const octave_bool& v = dynamic_cast<const octave_bool&> (a);
67 
68   return new octave_scalar (v.bool_value ());
69 }
70 
71 octave_base_value::type_conv_info
numeric_conversion_function(void) const72 octave_bool::numeric_conversion_function (void) const
73 {
74   return octave_base_value::type_conv_info (default_numeric_conversion_function,
75                                             octave_scalar::static_type_id ());
76 
77 }
78 
79 octave_value
do_index_op(const octave_value_list & idx,bool resize_ok)80 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
81 {
82   // FIXME: this doesn't solve the problem of
83   //
84   //   a = 1; a([1,1], [1,1], [1,1])
85   //
86   // and similar constructions.  Hmm...
87 
88   // FIXME: using this constructor avoids narrowing the
89   // 1x1 matrix back to a scalar value.  Need a better solution
90   // to this problem.
91 
92   octave_value tmp (new octave_bool_matrix (bool_matrix_value ()));
93 
94   return tmp.do_index_op (idx, resize_ok);
95 }
96 
97 octave_value
as_double(void) const98 octave_bool::as_double (void) const
99 {
100   return static_cast<double> (scalar);
101 }
102 
103 octave_value
as_single(void) const104 octave_bool::as_single (void) const
105 {
106   return static_cast<float> (scalar);
107 }
108 
109 octave_value
as_int8(void) const110 octave_bool::as_int8 (void) const
111 {
112   return octave_int8 (scalar);
113 }
114 
115 octave_value
as_int16(void) const116 octave_bool::as_int16 (void) const
117 {
118   return octave_int16 (scalar);
119 }
120 
121 octave_value
as_int32(void) const122 octave_bool::as_int32 (void) const
123 {
124   return octave_int32 (scalar);
125 }
126 
127 octave_value
as_int64(void) const128 octave_bool::as_int64 (void) const
129 {
130   return octave_int64 (scalar);
131 }
132 
133 octave_value
as_uint8(void) const134 octave_bool::as_uint8 (void) const
135 {
136   return octave_uint8 (scalar);
137 }
138 
139 octave_value
as_uint16(void) const140 octave_bool::as_uint16 (void) const
141 {
142   return octave_uint16 (scalar);
143 }
144 
145 octave_value
as_uint32(void) const146 octave_bool::as_uint32 (void) const
147 {
148   return octave_uint32 (scalar);
149 }
150 
151 octave_value
as_uint64(void) const152 octave_bool::as_uint64 (void) const
153 {
154   return octave_uint64 (scalar);
155 }
156 
157 octave_value
resize(const dim_vector & dv,bool fill) const158 octave_bool::resize (const dim_vector& dv, bool fill) const
159 {
160   if (fill)
161     {
162       boolNDArray retval (dv, false);
163       if (dv.numel ())
164         retval(0) = scalar;
165       return retval;
166     }
167   else
168     {
169       boolNDArray retval (dv);
170       if (dv.numel ())
171         retval(0) = scalar;
172       return retval;
173     }
174 }
175 
176 octave_value
convert_to_str_internal(bool,bool,char type) const177 octave_bool::convert_to_str_internal (bool, bool, char type) const
178 {
179   char s[2];
180   s[0] = static_cast<char> (scalar);
181   s[1] = '\0';
182 
183   return octave_value (s, type);
184 }
185 
186 bool
save_ascii(std::ostream & os)187 octave_bool::save_ascii (std::ostream& os)
188 {
189   double d = double_value ();
190 
191   octave_write_double (os, d);
192   os << "\n";
193 
194   return true;
195 }
196 
197 bool
load_ascii(std::istream & is)198 octave_bool::load_ascii (std::istream& is)
199 {
200   scalar = (octave_read_value<double> (is) != 0.0);
201 
202   if (! is)
203     error ("load: failed to load scalar constant");
204 
205   return true;
206 }
207 
208 bool
save_binary(std::ostream & os,bool)209 octave_bool::save_binary (std::ostream& os, bool /* save_as_floats */)
210 {
211   char tmp = (scalar ? 1 : 0);
212   os.write (reinterpret_cast<char *> (&tmp), 1);
213 
214   return true;
215 }
216 
217 bool
load_binary(std::istream & is,bool,octave::mach_info::float_format)218 octave_bool::load_binary (std::istream& is, bool /* swap */,
219                           octave::mach_info::float_format /* fmt */)
220 {
221   char tmp;
222   if (! is.read (reinterpret_cast<char *> (&tmp), 1))
223     return false;
224   scalar = (tmp ? 1 : 0);
225   return true;
226 }
227 
228 bool
save_hdf5(octave_hdf5_id loc_id,const char * name,bool)229 octave_bool::save_hdf5 (octave_hdf5_id loc_id, const char *name,
230                         bool /* save_as_floats */)
231 {
232   bool retval = false;
233 
234 #if defined (HAVE_HDF5)
235 
236   hsize_t dimens[3];
237   hid_t space_hid, data_hid;
238   space_hid = data_hid = -1;
239 
240   space_hid = H5Screate_simple (0, dimens, nullptr);
241   if (space_hid < 0) return false;
242 #if defined (HAVE_HDF5_18)
243   data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
244                         octave_H5P_DEFAULT, octave_H5P_DEFAULT, octave_H5P_DEFAULT);
245 #else
246   data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
247                         octave_H5P_DEFAULT);
248 #endif
249   if (data_hid < 0)
250     {
251       H5Sclose (space_hid);
252       return false;
253     }
254 
255   double tmp = double_value ();
256   retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
257                      octave_H5P_DEFAULT, &tmp) >= 0;
258 
259   H5Dclose (data_hid);
260   H5Sclose (space_hid);
261 
262 #else
263   octave_unused_parameter (loc_id);
264   octave_unused_parameter (name);
265 
266   warn_save ("hdf5");
267 #endif
268 
269   return retval;
270 }
271 
272 bool
load_hdf5(octave_hdf5_id loc_id,const char * name)273 octave_bool::load_hdf5 (octave_hdf5_id loc_id, const char *name)
274 {
275 #if defined (HAVE_HDF5)
276 
277 #if defined (HAVE_HDF5_18)
278   hid_t data_hid = H5Dopen (loc_id, name, octave_H5P_DEFAULT);
279 #else
280   hid_t data_hid = H5Dopen (loc_id, name);
281 #endif
282   hid_t space_id = H5Dget_space (data_hid);
283 
284   hsize_t rank = H5Sget_simple_extent_ndims (space_id);
285 
286   if (rank != 0)
287     {
288       H5Dclose (data_hid);
289       return false;
290     }
291 
292   double dtmp;
293   if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, octave_H5S_ALL, octave_H5S_ALL,
294                octave_H5P_DEFAULT, &dtmp) < 0)
295     {
296       H5Dclose (data_hid);
297       return false;
298     }
299 
300   scalar = (dtmp != 0.0);
301 
302   H5Dclose (data_hid);
303 
304 #else
305   octave_unused_parameter (loc_id);
306   octave_unused_parameter (name);
307 
308   warn_load ("hdf5");
309 #endif
310 
311   return true;
312 }
313 
314 mxArray *
as_mxArray(void) const315 octave_bool::as_mxArray (void) const
316 {
317   mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL);
318 
319   bool *pr = static_cast<bool *> (retval->get_data ());
320 
321   pr[0] = scalar;
322 
323   return retval;
324 }
325