ov-struct.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_struct_h)
00024 #define octave_struct_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "mx-base.h"
00032 #include "str-vec.h"
00033 
00034 #include "error.h"
00035 #include "oct-alloc.h"
00036 #include "oct-map.h"
00037 #include "ov-base.h"
00038 #include "ov-typeinfo.h"
00039 
00040 class octave_value_list;
00041 
00042 class tree_walker;
00043 
00044 // Data structures.
00045 
00046 class
00047 octave_struct : public octave_base_value
00048 {
00049 public:
00050 
00051   octave_struct (void)
00052     : octave_base_value (), map () { }
00053 
00054   octave_struct (const octave_map& m)
00055     : octave_base_value (), map (m) { }
00056 
00057   octave_struct (const octave_struct& s)
00058     : octave_base_value (), map (s.map) { }
00059 
00060   ~octave_struct (void) { }
00061 
00062   octave_base_value *clone (void) const { return new octave_struct (*this); }
00063   octave_base_value *empty_clone (void) const { return new octave_struct (); }
00064 
00065   octave_base_value *try_narrowing_conversion (void);
00066 
00067   Cell dotref (const octave_value_list& idx, bool auto_add = false);
00068 
00069   octave_value subsref (const std::string& type,
00070                         const std::list<octave_value_list>& idx)
00071     {
00072       octave_value_list tmp = subsref (type, idx, 1);
00073       return tmp.length () > 0 ? tmp(0) : octave_value ();
00074     }
00075 
00076   octave_value_list subsref (const std::string&,
00077                              const std::list<octave_value_list>&, int);
00078 
00079   octave_value subsref (const std::string& type,
00080                         const std::list<octave_value_list>& idx,
00081                         bool auto_add);
00082 
00083   static octave_value numeric_conv (const octave_value& val,
00084                                     const std::string& type);
00085 
00086   octave_value subsasgn (const std::string& type,
00087                          const std::list<octave_value_list>& idx,
00088                          const octave_value& rhs);
00089 
00090   octave_value squeeze (void) const { return map.squeeze (); }
00091 
00092   octave_value permute (const Array<int>& vec, bool inv = false) const
00093     { return map.permute (vec, inv); }
00094 
00095   octave_value do_index_op (const octave_value_list& idx,
00096                             bool resize_ok = false);
00097 
00098   dim_vector dims (void) const { return map.dims (); }
00099 
00100   size_t byte_size (void) const;
00101 
00102   // This is the number of elements in each field.  The total number
00103   // of elements is numel () * nfields ().
00104   octave_idx_type numel (void) const
00105   {
00106     return map.numel ();
00107   }
00108 
00109   octave_idx_type nfields (void) const { return map.nfields (); }
00110 
00111   octave_value reshape (const dim_vector& new_dims) const
00112     { return map.reshape (new_dims); }
00113 
00114   octave_value resize (const dim_vector& dv, bool fill = false) const
00115     { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
00116 
00117   bool is_defined (void) const { return true; }
00118 
00119   bool is_constant (void) const { return true; }
00120 
00121   bool is_map (void) const { return true; }
00122 
00123   builtin_type_t builtin_type (void) const { return btyp_struct; }
00124 
00125   octave_map map_value (void) const { return map; }
00126 
00127   string_vector map_keys (void) const { return map.fieldnames (); }
00128 
00129   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00130 
00131   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00132 
00133   bool print_name_tag (std::ostream& os, const std::string& name) const;
00134 
00135   bool save_ascii (std::ostream& os);
00136 
00137   bool load_ascii (std::istream& is);
00138 
00139   bool save_binary (std::ostream& os, bool& save_as_floats);
00140 
00141   bool load_binary (std::istream& is, bool swap,
00142                     oct_mach_info::float_format fmt);
00143 
00144 #if defined (HAVE_HDF5)
00145   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00146 
00147   bool load_hdf5 (hid_t loc_id, const char *name);
00148 #endif
00149 
00150   mxArray *as_mxArray (void) const;
00151 
00152   octave_value
00153   fast_elem_extract (octave_idx_type n) const;
00154 
00155   bool
00156   fast_elem_insert (octave_idx_type n, const octave_value& x);
00157 
00158 protected:
00159 
00160   // The associative array used to manage the structure data.
00161   octave_map map;
00162 
00163 private:
00164 
00165   DECLARE_OCTAVE_ALLOCATOR
00166 
00167   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00168 };
00169 
00170 class
00171 octave_scalar_struct : public octave_base_value
00172 {
00173 public:
00174 
00175   octave_scalar_struct (void)
00176     : octave_base_value (), map () { }
00177 
00178   octave_scalar_struct (const octave_scalar_map& m)
00179     : octave_base_value (), map (m) { }
00180 
00181   octave_scalar_struct (const octave_scalar_struct& s)
00182     : octave_base_value (), map (s.map) { }
00183 
00184   ~octave_scalar_struct (void) { }
00185 
00186   octave_base_value *clone (void) const { return new octave_scalar_struct (*this); }
00187   octave_base_value *empty_clone (void) const { return new octave_scalar_struct (); }
00188 
00189   octave_value dotref (const octave_value_list& idx, bool auto_add = false);
00190 
00191   octave_value subsref (const std::string& type,
00192                         const std::list<octave_value_list>& idx);
00193 
00194   octave_value_list subsref (const std::string& type,
00195                              const std::list<octave_value_list>& idx, int);
00196 
00197 
00198   octave_value subsref (const std::string& type,
00199                         const std::list<octave_value_list>& idx,
00200                         bool auto_add);
00201 
00202   static octave_value numeric_conv (const octave_value& val,
00203                                     const std::string& type);
00204 
00205   octave_value subsasgn (const std::string& type,
00206                          const std::list<octave_value_list>& idx,
00207                          const octave_value& rhs);
00208 
00209   octave_value squeeze (void) const { return map; }
00210 
00211   octave_value permute (const Array<int>& vec, bool inv = false) const
00212     { return octave_map (map).permute (vec, inv); }
00213 
00214   octave_value do_index_op (const octave_value_list& idx,
00215                             bool resize_ok = false);
00216 
00217   dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
00218 
00219   size_t byte_size (void) const;
00220 
00221   // This is the number of elements in each field.  The total number
00222   // of elements is numel () * nfields ().
00223   octave_idx_type numel (void) const
00224   {
00225     return 1;
00226   }
00227 
00228   octave_idx_type nfields (void) const { return map.nfields (); }
00229 
00230   octave_value reshape (const dim_vector& new_dims) const
00231     { return octave_map (map).reshape (new_dims); }
00232 
00233   octave_value resize (const dim_vector& dv, bool fill = false) const
00234     { octave_map tmap = map; tmap.resize (dv, fill); return tmap; }
00235 
00236   bool is_defined (void) const { return true; }
00237 
00238   bool is_constant (void) const { return true; }
00239 
00240   bool is_map (void) const { return true; }
00241 
00242   builtin_type_t builtin_type (void) const { return btyp_struct; }
00243 
00244   octave_map map_value (void) const { return map; }
00245 
00246   octave_scalar_map scalar_map_value (void) const { return map; }
00247 
00248   string_vector map_keys (void) const { return map.fieldnames (); }
00249 
00250   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00251 
00252   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00253 
00254   bool print_name_tag (std::ostream& os, const std::string& name) const;
00255 
00256   bool save_ascii (std::ostream& os);
00257 
00258   bool load_ascii (std::istream& is);
00259 
00260   bool save_binary (std::ostream& os, bool& save_as_floats);
00261 
00262   bool load_binary (std::istream& is, bool swap,
00263                     oct_mach_info::float_format fmt);
00264 
00265 #if defined (HAVE_HDF5)
00266   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00267 
00268   bool load_hdf5 (hid_t loc_id, const char *name);
00269 #endif
00270 
00271   mxArray *as_mxArray (void) const;
00272 
00273   bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
00274 
00275 protected:
00276 
00277   // The associative array used to manage the structure data.
00278   octave_scalar_map map;
00279 
00280 private:
00281 
00282   octave_value to_array (void);
00283 
00284   DECLARE_OCTAVE_ALLOCATOR
00285 
00286   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00287 };
00288 
00289 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines