ov-class.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 John W. Eaton
00004 Copyright (C) 2009 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_class_h)
00025 #define octave_class_h 1
00026 
00027 #include <cstdlib>
00028 
00029 #include <iosfwd>
00030 #include <string>
00031 
00032 #include "mx-base.h"
00033 #include "str-vec.h"
00034 
00035 #include "error.h"
00036 #include "oct-alloc.h"
00037 #include "oct-map.h"
00038 #include "ov-base.h"
00039 #include "ov-typeinfo.h"
00040 
00041 class octave_value_list;
00042 
00043 class tree_walker;
00044 
00045 // Data structures.
00046 
00047 class
00048 octave_class : public octave_base_value
00049 {
00050 public:
00051 
00052   octave_class (void)
00053     : octave_base_value (), map (), c_name (),
00054       parent_list (), obsolete_copies (0)
00055     { }
00056 
00057   octave_class (const octave_map& m, const std::string& id,
00058                 const std::list<std::string>& plist)
00059     : octave_base_value (), map (m), c_name (id),
00060       parent_list (plist), obsolete_copies (0)
00061     { }
00062 
00063   octave_class (const octave_map& m, const std::string& id,
00064                 const octave_value_list& parents);
00065 
00066   octave_class (const octave_class& s)
00067     : octave_base_value (s), map (s.map), c_name (s.c_name),
00068       parent_list (s.parent_list), obsolete_copies (0)  { }
00069 
00070   ~octave_class (void) { }
00071 
00072   octave_base_value *clone (void) const { return new octave_class (*this); }
00073 
00074   octave_base_value *unique_clone (void);
00075 
00076   octave_base_value *empty_clone (void) const
00077   {
00078     return new octave_class (octave_map (map.keys ()), c_name, parent_list);
00079   }
00080 
00081   Cell dotref (const octave_value_list& idx);
00082 
00083   Matrix size (void);
00084 
00085   octave_idx_type numel (const octave_value_list&);
00086 
00087   octave_value subsref (const std::string& type,
00088                         const std::list<octave_value_list>& idx)
00089     {
00090       octave_value_list tmp = subsref (type, idx, 1);
00091       return tmp.length () > 0 ? tmp(0) : octave_value ();
00092     }
00093 
00094   octave_value_list subsref (const std::string& type,
00095                              const std::list<octave_value_list>& idx,
00096                              int nargout);
00097 
00098   octave_value_list
00099   do_multi_index_op (int nargout, const octave_value_list& idx)
00100   {
00101     return subsref ("(", std::list<octave_value_list> (1, idx), nargout);
00102   }
00103 
00104   static octave_value numeric_conv (const Cell& val,
00105                                     const std::string& type);
00106 
00107   void assign(const std::string& k, const octave_value& rhs)
00108   { map.assign (k, rhs); };
00109 
00110   octave_value subsasgn (const std::string& type,
00111                          const std::list<octave_value_list>& idx,
00112                          const octave_value& rhs);
00113 
00114   octave_value undef_subsasgn (const std::string& type,
00115                                const std::list<octave_value_list>& idx,
00116                                const octave_value& rhs);
00117 
00118   idx_vector index_vector (void) const;
00119 
00120   dim_vector dims (void) const { return map.dims (); }
00121 
00122   size_t byte_size (void) const;
00123 
00124   // This is the number of elements in each field.  The total number
00125   // of elements is numel () * nfields ().
00126   octave_idx_type numel (void) const
00127   {
00128     dim_vector dv = dims ();
00129     return dv.numel ();
00130   }
00131 
00132   octave_idx_type nfields (void) const { return map.nfields (); }
00133 
00134   size_t nparents (void) const { return parent_list.size (); }
00135 
00136   octave_value reshape (const dim_vector& new_dims) const
00137     {
00138       octave_class retval = octave_class (*this);
00139       retval.map = retval.map_value().reshape (new_dims);
00140       return octave_value (new octave_class (retval));
00141     }
00142 
00143   octave_value resize (const dim_vector& dv, bool = false) const
00144     {
00145       octave_class retval = octave_class (*this);
00146       retval.map.resize (dv);
00147       return octave_value (new octave_class (retval));
00148     }
00149 
00150   bool is_defined (void) const { return true; }
00151 
00152   bool is_map (void) const { return false; }
00153 
00154   bool is_object (void) const { return true; }
00155 
00156   octave_map map_value (void) const { return map; }
00157 
00158   string_vector map_keys (void) const;
00159 
00160   std::list<std::string> parent_class_name_list (void) const
00161     { return parent_list; }
00162 
00163   string_vector parent_class_names (void) const
00164     { return string_vector (parent_list); }
00165 
00166   octave_base_value *find_parent_class (const std::string&);
00167 
00168   octave_base_value *unique_parent_class (const std::string&);
00169 
00170   string_vector all_strings (bool pad) const;
00171 
00172   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00173 
00174   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
00175 
00176   bool print_name_tag (std::ostream& os, const std::string& name) const;
00177 
00178   void print_with_name (std::ostream& os, const std::string& name,
00179                         bool print_padding = true);
00180 
00181   bool reconstruct_exemplar (void);
00182 
00183   static void clear_exemplar_map (void);
00184 
00185   bool reconstruct_parents (void);
00186 
00187   bool save_ascii (std::ostream& os);
00188 
00189   bool load_ascii (std::istream& is);
00190 
00191   bool save_binary (std::ostream& os, bool& save_as_floats);
00192 
00193   bool load_binary (std::istream& is, bool swap,
00194                     oct_mach_info::float_format fmt);
00195 
00196 #if defined (HAVE_HDF5)
00197   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00198 
00199   bool load_hdf5 (hid_t loc_id, const char *name);
00200 #endif
00201 
00202   mxArray *as_mxArray (void) const;
00203 
00204 private:
00205 
00206   octave_map map;
00207 
00208   DECLARE_OCTAVE_ALLOCATOR
00209 
00210 public:
00211   int type_id (void) const { return t_id; }
00212   std::string type_name (void) const { return t_name; }
00213   std::string class_name (void) const { return c_name; }
00214 
00215   static int static_type_id (void) { return t_id; }
00216   static std::string static_type_name (void) { return t_name; }
00217   static std::string static_class_name (void) { return "<unknown>"; }
00218   static void register_type (void);
00219 
00220 private:
00221   static int t_id;
00222 
00223   static const std::string t_name;
00224   std::string c_name;
00225   std::list<std::string> parent_list;
00226 
00227   bool in_class_method (void);
00228   std::string get_current_method_class (void);
00229 
00230   octave_value subsasgn_common (const octave_value& obj,
00231                                 const std::string& type,
00232                                 const std::list<octave_value_list>& idx,
00233                                 const octave_value& rhs);
00234 
00235   int obsolete_copies;
00236 
00237 public:
00238   // The list of field names and parent classes defines a class.  We
00239   // keep track of each class that has been created so that we know
00240   class exemplar_info
00241   {
00242   public:
00243 
00244     exemplar_info (void) : field_names (), parent_class_names () { }
00245 
00246     exemplar_info (const octave_value& obj);
00247 
00248     exemplar_info (const exemplar_info& x)
00249       : field_names (x.field_names),
00250         parent_class_names (x.parent_class_names) { }
00251 
00252     exemplar_info& operator = (const exemplar_info& x)
00253     {
00254       if (&x != this)
00255         {
00256           field_names = x.field_names;
00257           parent_class_names = x.parent_class_names;
00258         }
00259       return *this;
00260     }
00261 
00262     octave_idx_type nfields (void) const { return field_names.length (); }
00263 
00264     size_t nparents (void) const { return parent_class_names.size (); }
00265 
00266     string_vector fields (void) const { return field_names; }
00267 
00268     std::list<std::string> parents (void) const { return parent_class_names; }
00269 
00270     bool compare (const octave_value& obj) const;
00271 
00272   private:
00273 
00274     string_vector field_names;
00275     std::list<std::string> parent_class_names;
00276   };
00277 
00278   // A map from class names to lists of fields.
00279   static std::map<std::string, exemplar_info> exemplar_map;
00280 
00281   typedef std::map<std::string, exemplar_info>::iterator exemplar_iterator;
00282   typedef std::map<std::string, exemplar_info>::const_iterator exemplar_const_iterator;
00283 };
00284 
00285 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines