ov-bool.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_bool_h)
00024 #define octave_bool_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "lo-utils.h"
00032 #include "mx-base.h"
00033 #include "oct-alloc.h"
00034 #include "str-vec.h"
00035 
00036 #include "oct-stream.h"
00037 #include "ov-base.h"
00038 #include "ov-base-scalar.h"
00039 #include "ov-bool-mat.h"
00040 #include "ov-scalar.h"
00041 #include "ov-typeinfo.h"
00042 
00043 class octave_value_list;
00044 
00045 class tree_walker;
00046 
00047 // Real scalar values.
00048 
00049 class
00050 OCTINTERP_API
00051 octave_bool : public octave_base_scalar<bool>
00052 {
00053 public:
00054 
00055   octave_bool (void)
00056     : octave_base_scalar<bool> (false) { }
00057 
00058   octave_bool (bool b)
00059     : octave_base_scalar<bool> (b) { }
00060 
00061   octave_bool (const octave_bool& s)
00062     : octave_base_scalar<bool> (s) { }
00063 
00064   ~octave_bool (void) { }
00065 
00066   octave_base_value *clone (void) const { return new octave_bool (*this); }
00067   octave_base_value *empty_clone (void) const { return new octave_bool_matrix (); }
00068 
00069   type_conv_info numeric_conversion_function (void) const;
00070 
00071   octave_value do_index_op (const octave_value_list& idx,
00072                             bool resize_ok = false);
00073 
00074   idx_vector index_vector (void) const { return idx_vector (scalar); }
00075 
00076   builtin_type_t builtin_type (void) const { return btyp_bool; }
00077 
00078   bool is_real_scalar (void) const { return true; }
00079 
00080   bool is_bool_scalar (void) const { return true; }
00081 
00082   bool is_bool_type (void) const { return true; }
00083 
00084   bool is_real_type (void) const { return true; }
00085 
00086   bool is_numeric_type (void) const { return false; }
00087 
00088   bool is_true (void) const { return scalar; }
00089 
00090   int8NDArray
00091   int8_array_value (void) const
00092     { return int8NDArray (dim_vector (1, 1), scalar); }
00093 
00094   int16NDArray
00095   int16_array_value (void) const
00096     { return int16NDArray (dim_vector (1, 1), scalar); }
00097 
00098   int32NDArray
00099   int32_array_value (void) const
00100     { return int32NDArray (dim_vector (1, 1), scalar); }
00101 
00102   int64NDArray
00103   int64_array_value (void) const
00104     { return int64NDArray (dim_vector (1, 1), scalar); }
00105 
00106   uint8NDArray
00107   uint8_array_value (void) const
00108     { return uint8NDArray (dim_vector (1, 1), scalar); }
00109 
00110   uint16NDArray
00111   uint16_array_value (void) const
00112     { return uint16NDArray (dim_vector (1, 1), scalar); }
00113 
00114   uint32NDArray
00115   uint32_array_value (void) const
00116     { return uint32NDArray (dim_vector (1, 1), scalar); }
00117 
00118   uint64NDArray
00119   uint64_array_value (void) const
00120     { return uint64NDArray (dim_vector (1, 1), scalar); }
00121 
00122   octave_int8
00123   int8_scalar_value (void) const { return octave_int8 (scalar); }
00124 
00125   octave_int16
00126   int16_scalar_value (void) const { return octave_int16 (scalar); }
00127 
00128   octave_int32
00129   int32_scalar_value (void) const { return octave_int32 (scalar); }
00130 
00131   octave_int64
00132   int64_scalar_value (void) const { return octave_int64 (scalar); }
00133 
00134   octave_uint8
00135   uint8_scalar_value (void) const { return octave_uint8 (scalar); }
00136 
00137   octave_uint16
00138   uint16_scalar_value (void) const { return octave_uint16 (scalar); }
00139 
00140   octave_uint32
00141   uint32_scalar_value (void) const { return octave_uint32 (scalar); }
00142 
00143   octave_uint64
00144   uint64_scalar_value (void) const { return octave_uint64 (scalar); }
00145 
00146   double double_value (bool = false) const { return scalar; }
00147 
00148   float float_value (bool = false) const { return scalar; }
00149 
00150   double scalar_value (bool = false) const { return scalar; }
00151 
00152   float float_scalar_value (bool = false) const { return scalar; }
00153 
00154   Matrix matrix_value (bool = false) const
00155     { return Matrix (1, 1, scalar); }
00156 
00157   FloatMatrix float_matrix_value (bool = false) const
00158     { return FloatMatrix (1, 1, scalar); }
00159 
00160   NDArray array_value (bool = false) const
00161     { return NDArray (dim_vector (1, 1), static_cast<double> (scalar)); }
00162 
00163   FloatNDArray float_array_value (bool = false) const
00164     { return FloatNDArray (dim_vector (1, 1), static_cast<double> (scalar)); }
00165 
00166   Complex complex_value (bool = false) const { return scalar; }
00167 
00168   FloatComplex float_complex_value (bool = false) const { return scalar; }
00169 
00170   ComplexMatrix complex_matrix_value (bool = false) const
00171     { return ComplexMatrix (1, 1, Complex (scalar)); }
00172 
00173   FloatComplexMatrix float_complex_matrix_value (bool = false) const
00174     { return FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
00175 
00176   ComplexNDArray complex_array_value (bool = false) const
00177     { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
00178 
00179   FloatComplexNDArray float_complex_array_value (bool = false) const
00180     { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
00181 
00182   SparseMatrix sparse_matrix_value (bool = false) const
00183     { return SparseMatrix (Matrix (1, 1, scalar)); }
00184 
00185   // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
00186   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00187     { return SparseComplexMatrix (sparse_matrix_value ()); }
00188 
00189   SparseBoolMatrix sparse_bool_matrix_value (bool = false) const
00190     { return SparseBoolMatrix (boolMatrix (1, 1, scalar)); }
00191 
00192   charNDArray
00193   char_array_value (bool = false) const
00194   {
00195     charNDArray retval (dim_vector (1, 1));
00196     retval(0) = static_cast<char> (scalar);
00197     return retval;
00198   }
00199 
00200   bool bool_value (bool = false) const { return scalar; }
00201 
00202   boolMatrix bool_matrix_value (bool = false) const
00203     { return boolMatrix (1, 1, scalar); }
00204 
00205   boolNDArray bool_array_value (bool = false) const
00206     { return boolNDArray (dim_vector (1, 1), scalar); }
00207 
00208   octave_value resize (const dim_vector& dv, bool fill = false) const;
00209 
00210   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
00211 
00212   bool save_ascii (std::ostream& os);
00213 
00214   bool load_ascii (std::istream& is);
00215 
00216   bool save_binary (std::ostream& os, bool& save_as_floats);
00217 
00218   bool load_binary (std::istream& is, bool swap,
00219                     oct_mach_info::float_format fmt);
00220 
00221 #if defined (HAVE_HDF5)
00222   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00223 
00224   bool load_hdf5 (hid_t loc_id, const char *name);
00225 #endif
00226 
00227   int write (octave_stream& os, int block_size,
00228              oct_data_conv::data_type output_type, int skip,
00229              oct_mach_info::float_format flt_fmt) const
00230     {
00231       return os.write (bool_array_value (), block_size, output_type,
00232                        skip, flt_fmt);
00233     }
00234 
00235   mxArray *as_mxArray (void) const;
00236 
00237   // Mapper functions are converted to double for treatment
00238   octave_value map (unary_mapper_t umap) const
00239     {
00240       octave_scalar m (scalar_value ());
00241       return m.map (umap);
00242     }
00243 
00244 private:
00245 
00246   DECLARE_OCTAVE_ALLOCATOR
00247 
00248   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00249 };
00250 
00251 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines