382class nested_fcn_handle :
public base_nested_fcn_handle
389 nested_fcn_handle (
const std::string& name =
"",
390 const std::string&
file =
"",
391 const std::string& octaveroot =
"")
392 : base_nested_fcn_handle (name,
file, octaveroot)
395 nested_fcn_handle (
const octave_value& fcn,
const std::string& name,
396 const std::shared_ptr<stack_frame>& stack_context)
397 : base_nested_fcn_handle (fcn, name), m_stack_context (stack_context)
400 m_stack_context->mark_closure_context ();
403 nested_fcn_handle (
const nested_fcn_handle&) =
default;
405 ~nested_fcn_handle () =
default;
407 using base_nested_fcn_handle::is_nested;
409 bool is_nested (
const std::shared_ptr<stack_frame>& frame)
const
411 if (frame == m_stack_context)
415 auto nxt = m_stack_context->access_link ();
420 nxt = nxt->access_link ();
426 nested_fcn_handle * clone ()
const
428 return new nested_fcn_handle (*
this);
437 friend bool is_equal_to (
const nested_fcn_handle& fh1,
438 const nested_fcn_handle& fh2);
440 std::shared_ptr<stack_frame> stack_context ()
const
442 return m_stack_context;
448 std::shared_ptr<stack_frame> m_stack_context;
451class weak_nested_fcn_handle :
public base_nested_fcn_handle
455 weak_nested_fcn_handle (
const nested_fcn_handle& nfh)
456 : base_nested_fcn_handle (nfh), m_stack_context (nfh.stack_context ())
459 weak_nested_fcn_handle (
const weak_nested_fcn_handle&) =
default;
461 ~weak_nested_fcn_handle () =
default;
463 weak_nested_fcn_handle * clone ()
const
465 return new weak_nested_fcn_handle (*
this);
468 bool is_weak_nested ()
const {
return true; }
474 friend bool is_equal_to (
const weak_nested_fcn_handle& fh1,
475 const weak_nested_fcn_handle& fh2);
480 std::weak_ptr<stack_frame> m_stack_context;
490 class_simple_fcn_handle (
const std::string& name,
491 const std::string&
file,
499 class_simple_fcn_handle (
const std::string& class_nm,
500 const std::string& meth_nm);
503 const std::string& class_nm,
504 const std::string& meth_nm);
507 const std::string& class_nm,
508 const std::string& meth_nm);
510 class_simple_fcn_handle (
const class_simple_fcn_handle&) =
default;
512 ~class_simple_fcn_handle () =
default;
514 class_simple_fcn_handle *
clone ()
const
516 return new class_simple_fcn_handle (*
this);
519 std::string
type ()
const {
return "classsimple"; }
532 if (m_fcn.is_defined ())
547 return m_fcn.
is_defined () ? m_fcn.function_value () :
nullptr;
560 std::string dispatch_class ()
const {
return m_dispatch_class; }
566 bool save_binary (std::ostream& os,
bool save_as_floats);
568 bool load_binary (std::istream& is,
bool swap, mach_info::float_format fmt);
571 bool save_as_floats);
576 void print_raw (std::ostream&,
bool pr_as_read_syntax,
577 int current_print_indent_level)
const;
579 friend bool is_equal_to (
const class_simple_fcn_handle& fh1,
580 const class_simple_fcn_handle& fh2);
591 std::string m_dispatch_class;
624 static const std::string anonymous;
631 base_anonymous_fcn_handle (
const std::string& name =
"")
640 base_anonymous_fcn_handle (
const base_anonymous_fcn_handle&) =
default;
642 ~base_anonymous_fcn_handle () =
default;
644 std::string
type ()
const {
return "anonymous"; }
671 bool save_binary (std::ostream& os,
bool save_as_floats);
673 bool load_binary (std::istream& is,
bool swap, mach_info::float_format fmt);
676 bool save_as_floats);
681 void print_raw (std::ostream&,
bool pr_as_read_syntax,
682 int current_print_indent_level)
const;
687 bool parse (
const std::string& fcn_text);
698class anonymous_fcn_handle :
public base_anonymous_fcn_handle
702 using base_anonymous_fcn_handle::anonymous;
709 anonymous_fcn_handle (
const std::string& name =
"")
710 : base_anonymous_fcn_handle (name), m_stack_context ()
715 const std::shared_ptr<stack_frame>& stack_context = std::shared_ptr<stack_frame> ());
717 anonymous_fcn_handle (
const anonymous_fcn_handle&) =
default;
719 ~anonymous_fcn_handle () =
default;
721 anonymous_fcn_handle * clone ()
const
723 return new anonymous_fcn_handle (*
this);
732 friend bool is_equal_to (
const anonymous_fcn_handle& fh1,
733 const anonymous_fcn_handle& fh2);
735 std::shared_ptr<stack_frame> stack_context ()
const
737 return m_stack_context;
743 std::shared_ptr<stack_frame> m_stack_context;
746class weak_anonymous_fcn_handle :
public base_anonymous_fcn_handle
750 using base_anonymous_fcn_handle::anonymous;
752 weak_anonymous_fcn_handle (
const anonymous_fcn_handle& afh)
753 : base_anonymous_fcn_handle (afh), m_stack_context (afh.stack_context ())
756 weak_anonymous_fcn_handle (
const weak_anonymous_fcn_handle&) =
default;
758 ~weak_anonymous_fcn_handle () =
default;
760 weak_anonymous_fcn_handle * clone ()
const
762 return new weak_anonymous_fcn_handle (*
this);
765 bool is_weak_anonymous ()
const {
return true; }
771 friend bool is_equal_to (
const weak_anonymous_fcn_handle& fh1,
772 const weak_anonymous_fcn_handle& fh2);
777 std::weak_ptr<stack_frame> m_stack_context;
780extern bool is_equal_to (
const anonymous_fcn_handle& fh1,
781 const anonymous_fcn_handle& fh2);
784err_invalid_fcn_handle (
const std::string& name)
786 error (
"invalid function handle, unable to find function for @%s",
793 std::string type_str =
type ();
794 error (
"invalid conversion from %s handle to weak nestead handle",
801 std::string type_str =
type ();
802 error (
"invalid conversion from %s handle to weak anonymous handle",
808 const std::list<octave_value_list>& idx,
817 int tmp_nargout = (
type.length () > 1 && nargout == 0) ? 1 : nargout;
819 retval =
call (tmp_nargout, idx.front ());
825 error (
"function handle cannot be indexed with %c",
type[0]);
828 error (
"unexpected: index not '(', '{', or '.' in base_fcn_handle::subsref - please report this bug");
836 retval = retval(0).next_subsref (nargout,
type, idx);
844 std::ostringstream buf;
900 std::string obj_type =
type ();
903 (
"Octave:load-save-unavailable",
904 "%s: loading %s files not available in this version of Octave",
905 obj_type.c_str (), file_type);
911 std::string obj_type =
type ();
914 (
"Octave:load-save-unavailable",
915 "%s: saving %s files not available in this version of Octave",
916 obj_type.c_str (), file_type);
922 std::string htype =
type ();
924 warning (
"%s for %s handles with %s format is not implemented",
925 op, htype.c_str (), fmt);
931 error (
"invalid call to invalid function handle");
939 return interp.
feval (m_fcn, args, nargout);
943internal_fcn_handle::info ()
947 m.
setfield (
"function", fcn_name ());
956 const internal_fcn_handle& fh2)
958 if (fh1.m_name == fh2.m_name
959 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
960 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
982 std::size_t pos = m_name.find (
'.');
984 if (pos != std::string::npos)
1004 std::size_t beg = 0;
1005 std::size_t end = pos;
1007 std::vector<std::string> idx_elts;
1011 end = m_name.find (
'.', beg);
1013 idx_elts.push_back (m_name.substr (beg, end-beg));
1015 if (end == std::string::npos)
1021 std::size_t n_elts = idx_elts.size ();
1023 bool have_object =
false;
1030 partial_expr_val = interp.
varval (idx_elts[0]);
1035 err_invalid_fcn_handle (m_name);
1043 std::list<octave_value_list> arg_list;
1045 for (std::size_t i = 1; i < n_elts; i++)
1050 err_invalid_fcn_handle (m_name);
1053 arg_list.push_back (
ovl (idx_elts[i]));
1060 = partial_expr_val.
subsref (type, arg_list, 0);
1066 err_invalid_fcn_handle (m_name);
1072 err_invalid_fcn_handle (m_name);
1084 err_invalid_fcn_handle (m_name);
1087 arg_list.push_back (
ovl (idx_elts[i]));
1088 arg_list.push_back (args);
1090 return partial_expr_val.
subsref (type, arg_list, nargout);
1093 err_invalid_fcn_handle (m_name);
1099 err_invalid_fcn_handle (m_name);
1101 fcn_to_call = partial_expr_val;
1113 if (m_fcn.is_defined ())
1118 fcn_to_call = m_fcn;
1132 fcn_to_call = ov_fcn;
1141 fcn_to_call = ov_fcn;
1146 err_invalid_fcn_handle (m_name);
1148 return interp.
feval (fcn_to_call, args, nargout);
1152simple_fcn_handle::function_value (
bool)
1157 if (m_fcn.is_defined ())
1168 return m_fcn.
is_defined () ? m_fcn.function_value () :
nullptr;
1172simple_fcn_handle::user_function_value (
bool)
1177 if (m_fcn.is_defined ())
1188 return m_fcn.
is_defined () ? m_fcn.user_function_value () :
nullptr;
1192simple_fcn_handle::fcn_val ()
1194 if (m_fcn.is_defined ())
1209simple_fcn_handle::info ()
1213 m.
setfield (
"function", fcn_name ());
1222simple_fcn_handle::save_ascii (std::ostream& os)
1224 os <<
"# octaveroot: " << config::octave_exec_home () <<
"\n";
1226 std::string fnm = file ();
1228 os <<
"# path: " << fnm <<
"\n";
1230 os <<
"# subtype: " << type () <<
"\n";
1232 os << m_name <<
"\n";
1238simple_fcn_handle::load_ascii (std::istream& is)
1248simple_fcn_handle::save_binary (std::ostream& os,
bool)
1250 std::ostringstream nmbuf;
1255 nmbuf << m_name <<
"@<simple>\n" << config::octave_exec_home ()
1258 std::string buf_str = nmbuf.str ();
1259 int32_t tmp = buf_str.length ();
1260 os.write (
reinterpret_cast<char *
> (&tmp), 4);
1261 os.write (buf_str.c_str (), buf_str.length ());
1267simple_fcn_handle::load_binary (std::istream& is,
bool,
1268 mach_info::float_format)
1274simple_fcn_handle::save_hdf5 (
octave_hdf5_id loc_id,
const char *name,
1277#if defined (HAVE_HDF5)
1282#if defined (HAVE_HDF5_18)
1286 group_hid = H5Gcreate (loc_id, name, 0);
1292 space_hid = data_hid = type_hid = -1;
1295 type_hid = H5Tcopy (H5T_C_S1);
1296 H5Tset_size (type_hid, m_name.length () + 1);
1299 H5Gclose (group_hid);
1306 space_hid = H5Screate_simple (0, hdims,
nullptr);
1309 H5Tclose (type_hid);
1310 H5Gclose (group_hid);
1313#if defined (HAVE_HDF5_18)
1314 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
1318 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
1325 H5Sclose (space_hid);
1326 H5Tclose (type_hid);
1327 H5Gclose (group_hid);
1330 H5Dclose (data_hid);
1332 std::string octaveroot = config::octave_exec_home ();
1337 H5Sclose (space_hid);
1339 hdims[1] = octaveroot.length ();
1340 space_hid = H5Screate_simple (0, hdims,
nullptr);
1343 H5Tclose (type_hid);
1344 H5Gclose (group_hid);
1348 H5Tclose (type_hid);
1349 type_hid = H5Tcopy (H5T_C_S1);
1350 H5Tset_size (type_hid, octaveroot.length () + 1);
1352#if defined (HAVE_HDF5_18)
1353 a_id = H5Acreate (group_hid,
"OCTAVEROOT", type_hid, space_hid,
1356 a_id = H5Acreate (group_hid,
"OCTAVEROOT", type_hid, space_hid,
1362 retval = (H5Awrite (a_id, type_hid, octaveroot.c_str ()) >= 0);
1368 H5Sclose (space_hid);
1369 H5Tclose (type_hid);
1370 H5Gclose (group_hid);
1374 H5Sclose (space_hid);
1376 hdims[1] = fpath.length ();
1377 space_hid = H5Screate_simple (0, hdims,
nullptr);
1380 H5Tclose (type_hid);
1381 H5Gclose (group_hid);
1385 H5Tclose (type_hid);
1386 type_hid = H5Tcopy (H5T_C_S1);
1387 H5Tset_size (type_hid, fpath.length () + 1);
1389#if defined (HAVE_HDF5_18)
1390 a_id = H5Acreate (group_hid,
"FILE", type_hid, space_hid,
1393 a_id = H5Acreate (group_hid,
"FILE", type_hid, space_hid,
1399 retval = (H5Awrite (a_id, type_hid, fpath.c_str ()) >= 0);
1406 H5Sclose (space_hid);
1407 H5Tclose (type_hid);
1408 H5Gclose (group_hid);
1414 octave_unused_parameter (loc_id);
1415 octave_unused_parameter (name);
1429#if defined (HAVE_HDF5)
1431 unimplemented (
"load",
"hdf5");
1433 octave_unused_parameter (group_hid);
1434 octave_unused_parameter (space_hid);
1435 octave_unused_parameter (type_hid);
1441 octave_unused_parameter (group_hid);
1442 octave_unused_parameter (space_hid);
1443 octave_unused_parameter (type_hid);
1451simple_fcn_handle::print_raw (std::ostream& os,
bool pr_as_read_syntax,
1452 int current_print_indent_level)
const
1455 current_print_indent_level);
1459is_equal_to (
const simple_fcn_handle& fh1,
const simple_fcn_handle& fh2)
1461 if (fh1.m_name == fh2.m_name)
1463 if (fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1464 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1466 if (fh1.m_fcn.is_undefined () && fh2.m_fcn.is_undefined ())
1473scoped_fcn_handle::scoped_fcn_handle (
const octave_value& fcn,
1474 const std::string& name,
1475 const std::list<std::string>& parentage)
1480 if (m_fcn.is_defined ())
1488 m_parentage.push_front (name);
1498 if (! m_fcn.is_defined ())
1505 if (! m_fcn.is_defined ())
1506 err_invalid_fcn_handle (m_name);
1508 return interp.
feval (m_fcn, args, nargout);
1512scoped_fcn_handle::info ()
1516 m.
setfield (
"function", fcn_name ());
1526scoped_fcn_handle::save_ascii (std::ostream& os)
1528 os <<
"# octaveroot: " << config::octave_exec_home () <<
"\n";
1530 std::string fnm = file ();
1532 os <<
"# path: " << fnm <<
"\n";
1534 os <<
"# subtype: " << type () <<
"\n";
1536 os << m_name <<
"\n";
1545scoped_fcn_handle::load_ascii (std::istream& is)
1555 m_parentage.push_back (cellstr_val(i));
1562scoped_fcn_handle::save_binary (std::ostream& os,
bool save_as_floats)
1564 std::ostringstream nmbuf;
1566 std::string fnm = file ();
1568 nmbuf << m_name <<
"@<scopedfunction>\n" << config::octave_exec_home ()
1571 std::string buf_str = nmbuf.str ();
1572 int32_t
len = buf_str.length ();
1573 os.write (
reinterpret_cast<char *
> (&
len), 4);
1574 os.write (buf_str.c_str (), buf_str.length ());
1583scoped_fcn_handle::load_binary (std::istream& is,
bool swap,
1584 mach_info::float_format fmt)
1594 m_parentage.push_back (cellstr_val(i));
1601scoped_fcn_handle::save_hdf5 (
octave_hdf5_id loc_id,
const char *name,
1604#if defined (HAVE_HDF5)
1606 unimplemented (
"save",
"hdf5");
1610 octave_unused_parameter (loc_id);
1611 octave_unused_parameter (name);
1617 octave_unused_parameter (loc_id);
1618 octave_unused_parameter (name);
1632#if defined (HAVE_HDF5)
1634 unimplemented (
"load",
"hdf5");
1638 octave_unused_parameter (group_hid);
1639 octave_unused_parameter (space_hid);
1640 octave_unused_parameter (type_hid);
1646 octave_unused_parameter (group_hid);
1647 octave_unused_parameter (space_hid);
1648 octave_unused_parameter (type_hid);
1656scoped_fcn_handle::print_raw (std::ostream& os,
1657 bool pr_as_read_syntax,
1658 int current_print_indent_level)
const
1661 current_print_indent_level);
1665is_equal_to (
const scoped_fcn_handle& fh1,
const scoped_fcn_handle& fh2)
1667 if (fh1.m_name == fh2.m_name
1668 && fh2.m_parentage == fh2.m_parentage
1669 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1670 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1676scoped_fcn_handle::find_function ()
1685 if (m_parentage.size () == 1)
1687 std::string dir_name = sys::file_ops::dirname (m_file);
1689 std::size_t pos = dir_name.find_last_of (sys::file_ops::dir_sep_chars ());
1691 if (pos != std::string::npos)
1692 dir_name = dir_name.substr (0, pos);
1693 else if (dir_name ==
"private")
1696 std::string fcn_name = m_parentage.front ();
1706 std::string primary_parent_name = m_parentage.back ();
1719 std::string oct_home = config::octave_exec_home ();
1721 if (file_name.substr (0, oct_home.size ()) == oct_home)
1722 file_name = file_name.substr (oct_home.size ());
1734base_nested_fcn_handle::info ()
1738 m.
setfield (
"function", fcn_name ());
1741 m.
setfield (
"workspace", workspace ());
1756base_nested_fcn_handle::save_ascii (std::ostream& os)
1758 unimplemented (
"save",
"text");
1760 octave_unused_parameter (os);
1766base_nested_fcn_handle::load_ascii (std::istream& is)
1768 unimplemented (
"load",
"text");
1770 octave_unused_parameter (is);
1776base_nested_fcn_handle::save_binary (std::ostream& os,
1777 bool save_as_floats)
1779 unimplemented (
"save",
"binary");
1781 octave_unused_parameter (os);
1782 octave_unused_parameter (save_as_floats);
1788base_nested_fcn_handle::load_binary (std::istream& is,
bool swap,
1789 mach_info::float_format fmt)
1791 unimplemented (
"load",
"binary");
1793 octave_unused_parameter (is);
1794 octave_unused_parameter (swap);
1795 octave_unused_parameter (fmt);
1802 const char *name,
bool)
1804#if defined (HAVE_HDF5)
1806 unimplemented (
"save",
"hdf5");
1808 octave_unused_parameter (loc_id);
1809 octave_unused_parameter (name);
1815 octave_unused_parameter (loc_id);
1816 octave_unused_parameter (name);
1830#if defined (HAVE_HDF5)
1832 unimplemented (
"load",
"hdf5");
1834 octave_unused_parameter (group_hid);
1835 octave_unused_parameter (space_hid);
1836 octave_unused_parameter (type_hid);
1842 octave_unused_parameter (group_hid);
1843 octave_unused_parameter (space_hid);
1844 octave_unused_parameter (type_hid);
1852base_nested_fcn_handle::print_raw (std::ostream& os,
1853 bool pr_as_read_syntax,
1854 int current_print_indent_level)
const
1857 current_print_indent_level);
1861nested_fcn_handle::make_weak_nested_handle ()
const
1864 (
new weak_nested_fcn_handle (*
this)));
1878 return oct_usr_fcn->
execute (tw, nargout, args);
1882nested_fcn_handle::workspace ()
const
1884 return m_stack_context->workspace ();
1888is_equal_to (
const nested_fcn_handle& fh1,
const nested_fcn_handle& fh2)
1890 if (fh1.m_name == fh2.m_name
1891 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1892 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1904 std::shared_ptr<stack_frame> frames = m_stack_context.
lock ();
1910 return oct_usr_fcn->
execute (tw, nargout, args);
1914weak_nested_fcn_handle::workspace ()
const
1916 std::shared_ptr<stack_frame> frames = m_stack_context.
lock ();
1918 return frames ? frames->workspace () :
octave_value ();
1923 const weak_nested_fcn_handle& fh2)
1925 if (fh1.m_name == fh2.m_name
1926 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
1927 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
1932class_simple_fcn_handle::class_simple_fcn_handle (
const std::string& class_nm,
1933 const std::string& meth_nm)
1935 m_dispatch_class (class_nm)
1938class_simple_fcn_handle::class_simple_fcn_handle (
const octave_value& fcn,
1939 const std::string& class_nm,
1940 const std::string& meth_nm)
1942 m_dispatch_class (class_nm)
1945class_simple_fcn_handle::class_simple_fcn_handle (
const octave_value& obj,
1947 const std::string& class_nm,
1948 const std::string& meth_nm)
1950 m_dispatch_class (class_nm)
1958 if (m_obj.is_defined ())
1963 return interp.
feval (m_fcn, tmp_args, nargout);
1977 if (m_fcn.is_defined ())
1978 return interp.
feval (m_fcn, args, nargout);
1980 return interp.
feval (fcn_name (), args, nargout);
1984class_simple_fcn_handle::info ()
1988 m.
setfield (
"function", fcn_name ());
1991 m.
setfield (
"class", dispatch_class ());
2002class_simple_fcn_handle::save_ascii (std::ostream& os)
2004 unimplemented (
"save",
"text");
2006 octave_unused_parameter (os);
2012class_simple_fcn_handle::load_ascii (std::istream& is)
2014 unimplemented (
"load",
"text");
2016 octave_unused_parameter (is);
2022class_simple_fcn_handle::save_binary (std::ostream& os,
2023 bool save_as_floats)
2025 unimplemented (
"save",
"binary");
2027 octave_unused_parameter (os);
2028 octave_unused_parameter (save_as_floats);
2034class_simple_fcn_handle::load_binary (std::istream& is,
bool swap,
2035 mach_info::float_format fmt)
2037 unimplemented (
"load",
"binary");
2039 octave_unused_parameter (is);
2040 octave_unused_parameter (swap);
2041 octave_unused_parameter (fmt);
2048 const char *name,
bool)
2050#if defined (HAVE_HDF5)
2052 unimplemented (
"save",
"hdf5");
2054 octave_unused_parameter (loc_id);
2055 octave_unused_parameter (name);
2061 octave_unused_parameter (loc_id);
2062 octave_unused_parameter (name);
2076#if defined (HAVE_HDF5)
2078 unimplemented (
"load",
"hdf5");
2080 octave_unused_parameter (group_hid);
2081 octave_unused_parameter (space_hid);
2082 octave_unused_parameter (type_hid);
2088 octave_unused_parameter (group_hid);
2089 octave_unused_parameter (space_hid);
2090 octave_unused_parameter (type_hid);
2098class_simple_fcn_handle::print_raw (std::ostream& os,
2099 bool pr_as_read_syntax,
2100 int current_print_indent_level)
const
2103 current_print_indent_level);
2108 const class_simple_fcn_handle& fh2)
2112 if (fh1.m_name == fh2.m_name
2113 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2114 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2119const std::string base_anonymous_fcn_handle::anonymous (
"@<anonymous>");
2122base_anonymous_fcn_handle::info ()
2126 std::ostringstream buf;
2127 print_raw (buf,
true, 0);
2128 m.
setfield (
"function", buf.str ());
2132 m.
setfield (
"workspace", workspace ());
2133 m.
setfield (
"within_file_path",
"");
2139base_anonymous_fcn_handle::save_ascii (std::ostream& os)
2143 if (m_fcn.is_undefined ())
2146 os << m_name <<
"\n";
2148 print_raw (os,
true, 0);
2151 std::size_t varlen = m_local_vars.size ();
2155 os <<
"# length: " << varlen <<
"\n";
2157 for (
const auto& nm_val : m_local_vars)
2159 if (!
save_text_data (os, nm_val.second, nm_val.first,
false, 0))
2160 return ! os.fail ();
2168base_anonymous_fcn_handle::load_ascii (std::istream& is)
2170 octave::skip_preceeding_newline (is);
2179 buf = octave::read_until_newline (is,
true);
2182 std::streampos pos = is.tellg ();
2208 error (
"load: failed to load anonymous function handle");
2210 m_local_vars[name] = t2;
2227base_anonymous_fcn_handle::save_binary (std::ostream& os,
2228 bool save_as_floats)
2232 if (m_fcn.is_undefined ())
2235 std::ostringstream nmbuf;
2237 std::size_t varlen = m_local_vars.size ();
2241 nmbuf <<
' ' << varlen;
2243 std::string buf_str = nmbuf.str ();
2244 int32_t tmp = buf_str.
length ();
2245 os.write (
reinterpret_cast<char *
> (&tmp), 4);
2246 os.write (buf_str.c_str (), buf_str.length ());
2248 std::ostringstream buf;
2249 print_raw (buf,
true, 0);
2250 std::string stmp = buf.str ();
2251 tmp = stmp.length ();
2252 os.write (
reinterpret_cast<char *
> (&tmp), 4);
2253 os.write (stmp.c_str (), stmp.length ());
2257 for (
const auto& nm_val : m_local_vars)
2260 "", 0, save_as_floats))
2261 return ! os.fail ();
2269base_anonymous_fcn_handle::load_binary (std::istream& is,
bool swap,
2270 mach_info::float_format fmt)
2276 std::size_t anl = anonymous.length ();
2277 if (m_name.length () > anl)
2279 std::istringstream nm_is (m_name.substr (anl));
2291 if (! is.read (
reinterpret_cast<char *
> (&tmp), 4))
2299 is.read (ctmp2, tmp);
2324 error (
"load: failed to load anonymous function handle");
2326 m_local_vars[name] = t2;
2331 return parse (ctmp2);
2339 bool save_as_floats)
2341#if defined (HAVE_HDF5)
2346#if defined (HAVE_HDF5_18)
2350 group_hid = H5Gcreate (loc_id, name, 0);
2356 space_hid = data_hid = type_hid = -1;
2359 type_hid = H5Tcopy (H5T_C_S1);
2360 H5Tset_size (type_hid, m_name.length () + 1);
2363 H5Gclose (group_hid);
2370 space_hid = H5Screate_simple (0, hdims,
nullptr);
2373 H5Tclose (type_hid);
2374 H5Gclose (group_hid);
2377#if defined (HAVE_HDF5_18)
2378 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
2382 data_hid = H5Dcreate (group_hid,
"nm", type_hid, space_hid,
2389 H5Sclose (space_hid);
2390 H5Tclose (type_hid);
2391 H5Gclose (group_hid);
2394 H5Dclose (data_hid);
2396 std::ostringstream buf;
2397 print_raw (buf,
true, 0);
2398 std::string stmp = buf.str ();
2401 H5Tset_size (type_hid, stmp.length () + 1);
2404 H5Sclose (space_hid);
2405 H5Gclose (group_hid);
2409#if defined (HAVE_HDF5_18)
2410 data_hid = H5Dcreate (group_hid,
"fcn", type_hid, space_hid,
2414 data_hid = H5Dcreate (group_hid,
"fcn", type_hid, space_hid,
2421 H5Sclose (space_hid);
2422 H5Tclose (type_hid);
2423 H5Gclose (group_hid);
2427 H5Dclose (data_hid);
2429 std::size_t varlen = m_local_vars.size ();
2438#if defined (HAVE_HDF5_18)
2439 a_id = H5Acreate (group_hid,
"SYMBOL_TABLE",
H5T_NATIVE_IDX, as_id,
2443 a_id = H5Acreate (group_hid,
"SYMBOL_TABLE",
H5T_NATIVE_IDX, as_id,
2460#if defined (HAVE_HDF5_18)
2461 data_hid = H5Gcreate (group_hid,
"symbol table",
2465 data_hid = H5Gcreate (group_hid,
"symbol table", 0);
2469 H5Sclose (space_hid);
2470 H5Tclose (type_hid);
2471 H5Gclose (group_hid);
2475 for (
const auto& nm_val : m_local_vars)
2478 "",
false, save_as_floats))
2482 H5Gclose (data_hid);
2485 H5Sclose (space_hid);
2486 H5Tclose (type_hid);
2487 H5Gclose (group_hid);
2493 octave_unused_parameter (loc_id);
2494 octave_unused_parameter (name);
2495 octave_unused_parameter (save_as_floats);
2509#if defined (HAVE_HDF5)
2511 bool success =
true;
2513#if defined (HAVE_HDF5_18)
2521 H5Sclose (space_hid);
2522 H5Tclose (type_hid);
2523 H5Gclose (group_hid);
2527 H5Tclose (type_hid);
2528 type_hid = H5Dget_type (data_hid);
2531 if (type_class_hid != H5T_STRING)
2533 H5Sclose (space_hid);
2534 H5Tclose (type_hid);
2535 H5Dclose (data_hid);
2536 H5Gclose (group_hid);
2540 H5Sclose (space_hid);
2541 space_hid = H5Dget_space (data_hid);
2542 hsize_t rank = H5Sget_simple_extent_ndims (space_hid);
2546 H5Sclose (space_hid);
2547 H5Tclose (type_hid);
2548 H5Dclose (data_hid);
2549 H5Gclose (group_hid);
2553 int slen = H5Tget_size (type_hid);
2556 H5Sclose (space_hid);
2557 H5Tclose (type_hid);
2558 H5Dclose (data_hid);
2559 H5Gclose (group_hid);
2567 H5Tset_size (st_id, slen);
2574 H5Sclose (space_hid);
2575 H5Tclose (type_hid);
2576 H5Dclose (data_hid);
2577 H5Gclose (group_hid);
2581 H5Dclose (data_hid);
2594#if defined (HAVE_HDF5_18)
2598 H5Eget_auto (&err_fcn, &err_fcn_data);
2599 H5Eset_auto (
nullptr,
nullptr);
2602 octave_hdf5_id attr_id = H5Aopen_name (group_hid,
"SYMBOL_TABLE");
2613#if defined (HAVE_HDF5_18)
2616 H5Eset_auto (err_fcn, err_fcn_data);
2629 if (
len > 0 && success)
2631 hsize_t num_obj = 0;
2632#if defined (HAVE_HDF5_18)
2635 data_hid = H5Gopen (group_hid,
"symbol table");
2637 H5Gget_num_objs (data_hid, &num_obj);
2638 H5Gclose (data_hid);
2640 if (num_obj !=
static_cast<hsize_t
> (
len))
2641 error (
"load: failed to load anonymous function handle");
2644 int current_item = 0;
2649 error (
"load: failed to load anonymous function handle");
2651 m_local_vars[dsub.
name] = dsub.
tc;
2656 return parse (fcn_tmp);
2662 octave_unused_parameter (group_hid);
2663 octave_unused_parameter (space_hid);
2664 octave_unused_parameter (type_hid);
2672base_anonymous_fcn_handle::print_raw (std::ostream& os,
bool,
int)
const
2679 error (
"invalid anonymous function handle");
2701 error (
"invalid anonymous function handle");
2708 error (
"invalid anonymous function handle");
2710 tpc.print_fcn_handle_body (e);
2714base_anonymous_fcn_handle::parse (
const std::string& fcn_text)
2735 = interp.
eval_string (fcn_text,
true, parse_status);
2737 if (parse_status != 0)
2760anonymous_fcn_handle::anonymous_fcn_handle (
const octave_value& fcn,
2762 const std::shared_ptr<stack_frame>& stack_context)
2763 : base_anonymous_fcn_handle (fcn, local_vars),
2764 m_stack_context (stack_context)
2766 if (m_stack_context)
2767 m_stack_context->mark_closure_context ();
2771anonymous_fcn_handle::make_weak_anonymous_handle ()
const
2774 (
new weak_anonymous_fcn_handle (*
this)));
2788 return oct_usr_fcn->
execute (tw, nargout, args);
2792anonymous_fcn_handle::workspace ()
const
2796 for (
const auto& nm_val : m_local_vars)
2797 local_vars_map.assign (nm_val.first, nm_val.second);
2805 if (m_stack_context)
2807 octave_value ov_frames = m_stack_context->workspace ();
2813 Cell retval =
Cell (num_frames+1, 1);
2814 retval(0) = m_local_vars;
2816 retval(i+1) = cell_frames(i);
2823 const anonymous_fcn_handle& fh2)
2825 if (fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2826 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2838 std::shared_ptr<stack_frame> frames = m_stack_context.
lock ();
2844 return oct_usr_fcn->
execute (tw, nargout, args);
2848weak_anonymous_fcn_handle::workspace ()
const
2852 for (
const auto& nm_val : m_local_vars)
2853 local_vars_map.assign (nm_val.first, nm_val.second);
2859 std::shared_ptr<stack_frame> frames = m_stack_context.lock ();
2873 Cell retval =
Cell (num_frames+1, 1);
2874 retval(0) = m_local_vars;
2876 retval(i+1) = cell_frames(i);
2883 const weak_anonymous_fcn_handle& fh2)
2885 if (fh1.m_name == fh2.m_name
2886 && fh1.m_fcn.is_defined () && fh2.m_fcn.is_defined ())
2887 return fh1.m_fcn.is_copy_of (fh2.m_fcn);
2892OCTAVE_END_NAMESPACE(octave)
2907 const std::string& name)
2912 const std::string& meth_nm)
2914 m_rep (new octave::class_simple_fcn_handle (class_nm, meth_nm))
2918 const std::string& class_nm,
2919 const std::string& meth_nm)
2921 m_rep (new octave::class_simple_fcn_handle (fcn, class_nm, meth_nm))
2926 const std::string& class_nm,
2927 const std::string& meth_nm)
2929 m_rep (new octave::class_simple_fcn_handle (obj, fcn, class_nm, meth_nm))
2933 const std::string& name,
2934 const std::list<std::string>& parentage)
2936 m_rep (new octave::scoped_fcn_handle (fcn, name, parentage))
2940 const std::string& name,
2941 const std::shared_ptr<octave::stack_frame>& stack_context)
2943 m_rep (new octave::nested_fcn_handle (fcn, name, stack_context))
2947 const octave::stack_frame::local_vars_map& local_vars,
2948 const std::shared_ptr<octave::stack_frame>& stack_context)
2950 m_rep (new octave::anonymous_fcn_handle (fcn, local_vars, stack_context))
2960 m_rep.reset (fh.m_rep->clone ());
2973 return m_rep->save_ascii (os);
2979 std::shared_ptr<octave::base_fcn_handle> new_rep;
2984 std::streampos pos = is.tellg ();
2987 if (octaveroot.empty ())
3002 if (! (octaveroot.empty () || fpath.empty ()))
3004 std::size_t
len = octaveroot.size ();
3005 if (octaveroot == fpath.substr (0,
len))
3006 fpath = octave::config::octave_exec_home () + fpath.substr (
len);
3012 if (subtype.empty ())
3024 new_rep.reset (
new octave::anonymous_fcn_handle ());
3026 new_rep.reset (
new octave::simple_fcn_handle (name, fpath, octaveroot));
3032 if (subtype ==
"simple")
3037 new_rep.reset (
new octave::simple_fcn_handle (name, fpath,
3040 else if (subtype ==
"scopedfunction")
3045 new_rep.reset (
new octave::scoped_fcn_handle (name, fpath,
3048 else if (subtype ==
"anonymous")
3049 new_rep.reset (
new octave::anonymous_fcn_handle ());
3050 else if (subtype ==
"nested")
3055 new_rep.reset (
new octave::nested_fcn_handle (name, fpath,
3058 else if (subtype ==
"classsimple")
3063 new_rep.reset (
new octave::class_simple_fcn_handle (name, fpath,
3071 if (! new_rep->load_ascii (is))
3082 return m_rep->save_binary (os, save_as_floats);
3087 octave::mach_info::float_format fmt)
3093 if (! is.read (
reinterpret_cast<char *
> (&tmp), 4))
3101 is.read (ctmp1, tmp);
3103 std::string name (ctmp1);
3108 std::shared_ptr<octave::base_fcn_handle> new_rep;
3112 if (name.length () >= anl && name.substr (0, anl) ==
anonymous)
3119 new_rep.reset (
new octave::anonymous_fcn_handle (name));
3127 std::string octaveroot;
3129 std::string subtype =
"simple";
3131 if (name.find_first_of (
'\n') != std::string::npos)
3133 std::size_t pos1 = name.find_first_of (
'\n');
3134 std::size_t pos2 = name.find_first_of (
'\n', pos1 + 1);
3135 octaveroot = name.substr (pos1 + 1, pos2 - pos1 - 1);
3136 fpath = name.substr (pos2 + 1);
3137 name = name.substr (0, pos1);
3140 std::size_t pos1 = name.find (
'@');
3141 if (pos1 != std::string::npos)
3143 if (name[pos1+1] ==
'<')
3145 std::size_t pos2 = name.find (
'>', pos1 + 2);
3147 if (pos2 != std::string::npos)
3148 subtype = name.substr (pos1 + 2, pos2 - pos1 - 2);
3151 name = name.substr (0, pos1);
3157 if (subtype ==
"simple")
3158 new_rep.reset (
new octave::simple_fcn_handle (name, fpath, octaveroot));
3159 else if (subtype ==
"scopedfunction")
3160 new_rep.reset (
new octave::scoped_fcn_handle (name, fpath, octaveroot));
3161 else if (subtype ==
"nested")
3162 new_rep.reset (
new octave::nested_fcn_handle (name, fpath, octaveroot));
3163 else if (subtype ==
"classsimple")
3164 new_rep.reset (
new octave::class_simple_fcn_handle (name, fpath,
3171 if (! new_rep->load_binary (is, swap, fmt))
3181 bool save_as_floats)
3183 return m_rep->save_hdf5 (loc_id, name, save_as_floats);
3189#if defined (HAVE_HDF5)
3191#if defined (HAVE_HDF5_18)
3199#if defined (HAVE_HDF5_18)
3207 H5Gclose (group_hid);
3214 if (type_class_hid != H5T_STRING)
3216 H5Tclose (type_hid);
3217 H5Dclose (data_hid);
3218 H5Gclose (group_hid);
3223 hsize_t rank = H5Sget_simple_extent_ndims (space_hid);
3227 H5Sclose (space_hid);
3228 H5Tclose (type_hid);
3229 H5Dclose (data_hid);
3230 H5Gclose (group_hid);
3234 int slen = H5Tget_size (type_hid);
3237 H5Sclose (space_hid);
3238 H5Tclose (type_hid);
3239 H5Dclose (data_hid);
3240 H5Gclose (group_hid);
3248 H5Tset_size (st_id, slen);
3255 H5Sclose (space_hid);
3256 H5Tclose (type_hid);
3257 H5Dclose (data_hid);
3258 H5Gclose (group_hid);
3262 H5Dclose (data_hid);
3264 std::string name (nm_tmp);
3266 std::shared_ptr<octave::base_fcn_handle> new_rep;
3273 new_rep.reset (
new octave::anonymous_fcn_handle ());
3281 std::string octaveroot;
3283 std::string subtype =
"simple";
3285 if (name.find_first_of (
'\n') != std::string::npos)
3287 std::size_t pos1 = name.find_first_of (
'\n');
3288 std::size_t pos2 = name.find_first_of (
'\n', pos1 + 1);
3289 octaveroot = name.substr (pos1 + 1, pos2 - pos1 - 1);
3290 fpath = name.substr (pos2 + 1);
3291 name = name.substr (0, pos1);
3294 std::size_t pos1 = name.find (
'@');
3295 if (pos1 != std::string::npos)
3297 if (name[pos1+1] ==
'<')
3299 std::size_t pos2 = name.find (
'>', pos1 + 2);
3301 if (pos2 != std::string::npos)
3302 subtype = name.substr (pos1 + 2, pos2 - pos1 - 2);
3305 name = name.substr (0, pos1);
3311 if (subtype ==
"simple")
3312 new_rep.reset (
new octave::simple_fcn_handle (name, fpath, octaveroot));
3313 else if (subtype ==
"scopedfunction")
3314 new_rep.reset (
new octave::scoped_fcn_handle (name, fpath, octaveroot));
3315 else if (subtype ==
"nested")
3316 new_rep.reset (
new octave::nested_fcn_handle (name, fpath, octaveroot));
3317 else if (subtype ==
"classsimple")
3318 new_rep.reset (
new octave::class_simple_fcn_handle (name, fpath,
3322 bool status =
false;
3324 if (new_rep && new_rep->load_hdf5 (group_hid, space_hid, type_hid))
3332 H5Tclose (type_hid);
3333 H5Sclose (space_hid);
3334 H5Gclose (group_hid);
3340 octave_unused_parameter (loc_id);
3341 octave_unused_parameter (name_arg);
3463 return is_equal_to (*
dynamic_cast<octave::internal_fcn_handle *
> (fh1.get_rep ()),
3464 *
dynamic_cast<octave::internal_fcn_handle *
> (fh2.get_rep ()));
3466 return is_equal_to (*
dynamic_cast<octave::simple_fcn_handle *
> (fh1.get_rep ()),
3467 *
dynamic_cast<octave::simple_fcn_handle *
> (fh2.get_rep ()));
3469 return is_equal_to (*
dynamic_cast<octave::scoped_fcn_handle *
> (fh1.get_rep ()),
3470 *
dynamic_cast<octave::scoped_fcn_handle *
> (fh2.get_rep ()));
3472 return is_equal_to (*
dynamic_cast<octave::nested_fcn_handle *
> (fh1.get_rep ()),
3473 *
dynamic_cast<octave::nested_fcn_handle *
> (fh2.get_rep ()));
3475 return is_equal_to (*
dynamic_cast<octave::class_simple_fcn_handle *
> (fh1.get_rep ()),
3476 *
dynamic_cast<octave::class_simple_fcn_handle *
> (fh2.get_rep ()));
3478 return is_equal_to (*
dynamic_cast<octave::anonymous_fcn_handle *
> (fh1.get_rep ()),
3479 *
dynamic_cast<octave::anonymous_fcn_handle *
> (fh2.get_rep ()));