1 //////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (C) 1999-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_cell_h) 27 #define octave_ov_cell_h 1 28 29 #include "octave-config.h" 30 31 #include <cstdlib> 32 33 #include <iosfwd> 34 #include <string> 35 #include <memory> 36 37 #include "mx-base.h" 38 #include "str-vec.h" 39 40 #include "Cell.h" 41 #include "error.h" 42 #include "ov-base-mat.h" 43 #include "ov-typeinfo.h" 44 45 class octave_value_list; 46 47 // Cells. 48 49 class 50 octave_cell : public octave_base_matrix<Cell> 51 { 52 public: 53 octave_cell(void)54 octave_cell (void) 55 : octave_base_matrix<Cell> (), cellstr_cache () { } 56 octave_cell(const Cell & c)57 octave_cell (const Cell& c) 58 : octave_base_matrix<Cell> (c), cellstr_cache () { } 59 octave_cell(const Array<std::string> & str)60 octave_cell (const Array<std::string>& str) 61 : octave_base_matrix<Cell> (Cell (str)), 62 cellstr_cache (new Array<std::string> (str)) { } 63 octave_cell(const octave_cell & c)64 octave_cell (const octave_cell& c) 65 : octave_base_matrix<Cell> (c), cellstr_cache () { } 66 67 ~octave_cell (void) = default; 68 clone(void)69 octave_base_value * clone (void) const { return new octave_cell (*this); } empty_clone(void)70 octave_base_value * empty_clone (void) const { return new octave_cell (); } 71 72 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame); 73 74 #if 0 75 octave_base_value * try_narrowing_conversion (void); 76 #endif 77 subsref(const std::string & type,const std::list<octave_value_list> & idx)78 octave_value subsref (const std::string& type, 79 const std::list<octave_value_list>& idx) 80 { 81 octave_value_list tmp = subsref (type, idx, 1); 82 return tmp.length () > 0 ? tmp(0) : octave_value (); 83 } 84 85 octave_value_list subsref (const std::string& type, 86 const std::list<octave_value_list>& idx, 87 int nargout); 88 89 octave_value subsref (const std::string& type, 90 const std::list<octave_value_list>& idx, 91 bool auto_add); 92 93 octave_value subsasgn (const std::string& type, 94 const std::list<octave_value_list>& idx, 95 const octave_value& rhs); 96 97 // FIXME: should we import the functions from the base class and 98 // overload them here, or should we use a different name so we don't 99 // have to do this? Without the using declaration or a name change, 100 // the base class functions will be hidden. That may be OK, but it 101 // can also cause some confusion. 102 using octave_base_value::assign; 103 104 void assign (const octave_value_list& idx, const Cell& rhs); 105 106 void assign (const octave_value_list& idx, const octave_value& rhs); 107 108 void delete_elements (const octave_value_list& idx); 109 110 std::size_t byte_size (void) const; 111 112 octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; 113 114 octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, 115 sortmode mode = ASCENDING) const; 116 117 sortmode issorted (sortmode mode = UNSORTED) const; 118 119 Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const; 120 121 sortmode is_sorted_rows (sortmode mode = UNSORTED) const; 122 is_matrix_type(void)123 bool is_matrix_type (void) const { return false; } 124 isnumeric(void)125 bool isnumeric (void) const { return false; } 126 is_defined(void)127 bool is_defined (void) const { return true; } 128 is_constant(void)129 bool is_constant (void) const { return true; } 130 iscell(void)131 bool iscell (void) const { return true; } 132 builtin_type(void)133 builtin_type_t builtin_type (void) const { return btyp_cell; } 134 135 bool iscellstr (void) const; 136 137 bool is_true (void) const; 138 cell_value(void)139 Cell cell_value (void) const { return matrix; } 140 141 octave_value_list list_value (void) const; 142 convert_to_str_internal(bool pad,bool,char type)143 octave_value convert_to_str_internal (bool pad, bool, char type) const 144 { return octave_value (string_vector_value (pad), type); } 145 146 string_vector string_vector_value (bool pad = false) const; 147 148 Array<std::string> cellstr_value (void) const; 149 150 Array<std::string> cellstr_value (const char *fmt, ...) const; 151 152 bool print_as_scalar (void) const; 153 154 void print (std::ostream& os, bool pr_as_read_syntax = false); 155 156 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; 157 158 bool print_name_tag (std::ostream& os, const std::string& name) const; 159 160 void short_disp (std::ostream& os) const; 161 162 bool save_ascii (std::ostream& os); 163 164 bool load_ascii (std::istream& is); 165 166 bool save_binary (std::ostream& os, bool save_as_floats); 167 168 bool load_binary (std::istream& is, bool swap, 169 octave::mach_info::float_format fmt); 170 171 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats); 172 173 bool load_hdf5 (octave_hdf5_id loc_id, const char *name); 174 175 octave_value map (unary_mapper_t umap) const; 176 177 mxArray * as_mxArray (void) const; 178 179 // Unsafe. This function exists to support the MEX interface. 180 // You should not use it anywhere else. 181 void * mex_get_data (void) const; 182 183 private: 184 clear_cellstr_cache(void)185 void clear_cellstr_cache (void) const 186 { cellstr_cache.reset (); } 187 188 mutable std::unique_ptr<Array<std::string>> cellstr_cache; 189 190 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA 191 }; 192 193 #endif 194