File: cdef-method.cc

package info (click to toggle)
octave 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124,192 kB
  • sloc: cpp: 322,665; ansic: 68,088; fortran: 20,980; objc: 8,121; sh: 7,719; yacc: 4,266; lex: 4,123; perl: 1,530; java: 1,366; awk: 1,257; makefile: 424; xml: 147
file content (234 lines) | stat: -rw-r--r-- 6,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2012-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING.  If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include <algorithm>
#include <iomanip>

#include "cdef-class.h"
#include "cdef-manager.h"
#include "cdef-utils.h"
#include "errwarn.h"
#include "interpreter-private.h"
#include "interpreter.h"
#include "load-path.h"
#include "ov-builtin.h"
#include "ov-classdef.h"
#include "ov-fcn-handle.h"
#include "ov-usr-fcn.h"
#include "parse.h"
#include "pt-assign.h"
#include "pt-classdef.h"
#include "pt-idx.h"
#include "pt-misc.h"
#include "pt-stmt.h"
#include "pt-walk.h"

namespace octave
{
  OCTAVE_NORETURN static
  void
  err_method_access (const std::string& from, const cdef_method& meth)
  {
    octave_value acc = meth.get ("Access");
    std::string acc_s;

    if (acc.is_string ())
      acc_s = acc.string_value ();
    else
      acc_s = "class-restricted";

    error ("%s: method '%s' has %s access and cannot be run in this context",
           from.c_str (), meth.get_name ().c_str (), acc_s.c_str ());
  }

  void
  cdef_method::cdef_method_rep::check_method (void)
  {
    if (is_external ())
      {
        if (is_dummy_method (function))
          {
            load_path& lp
              = __get_load_path__ ("cdef_method::cdef_method_rep::check_method");

            std::string name = get_name ();
            std::string cls_name = dispatch_type;
            std::string pack_name;

            size_t pos = cls_name.rfind ('.');

            if (pos != std::string::npos)
              {
                pack_name = cls_name.substr (0, pos);
                cls_name = cls_name.substr (pos + 1);
              }

            std::string dir_name;
            std::string file_name = lp.find_method (cls_name, name,
                                                    dir_name, pack_name);

            if (! file_name.empty ())
              {
                octave_value ov_fcn
                  = load_fcn_from_file (file_name, dir_name,
                                        dispatch_type, pack_name);

                if (ov_fcn.is_defined ())
                  {
                    function = ov_fcn;

                    make_function_of_class (dispatch_type, function);
                  }
              }
          }
        else
          {
            // FIXME: check out-of-date status
          }

        if (is_dummy_method (function))
          error ("no definition found for method '%s' of class '%s'",
                 get_name ().c_str (), dispatch_type.c_str ());
      }
  }

  octave_value_list
  cdef_method::cdef_method_rep::execute (const octave_value_list& args,
                                         int nargout, bool do_check_access,
                                         const std::string& who)
  {
    octave_value_list retval;

    if (do_check_access && ! check_access ())
      err_method_access (who, wrap ());

    if (get ("Abstract").bool_value ())
      error ("%s: cannot execute abstract method",
             get ("Name").string_value ().c_str ());

    check_method ();

    if (function.is_defined ())
      retval = feval (function, args, nargout);

    return retval;
  }

  octave_value_list
  cdef_method::cdef_method_rep::execute (const cdef_object& obj,
                                         const octave_value_list& args,
                                         int nargout, bool do_check_access,
                                         const std::string& who)
  {
    octave_value_list retval;

    if (do_check_access && ! check_access ())
      err_method_access (who, wrap ());

    if (get ("Abstract").bool_value ())
      error ("%s: cannot execute abstract method",
             get ("Name").string_value ().c_str ());

    check_method ();

    if (function.is_defined ())
      {
        octave_value_list new_args;

        new_args.resize (args.length () + 1);

        new_args(0) = to_ov (obj);
        for (int i = 0; i < args.length (); i++)
          new_args(i+1) = args(i);

        retval = feval (function, new_args, nargout);
      }

    return retval;
  }

  bool
  cdef_method::cdef_method_rep::is_constructor (void) const
  {
    if (function.is_function())
      return function.function_value ()->is_classdef_constructor ();

    return false;
  }

  bool
  cdef_method::cdef_method_rep::is_defined_in_class (const std::string &cname) const
  {
    return (function.is_function ()
            ? function.function_value ()->dispatch_class () == cname
            : false);
  }

  std::string
  cdef_method::cdef_method_rep::get_doc_string (void)
  {
    check_method ();

    octave_function *fcn = function.function_value ();

    return fcn ? fcn->doc_string () : "";
  }

  bool
  cdef_method::cdef_method_rep::check_access (void) const
  {
    cdef_class cls (to_cdef (get ("DefiningClass")));

    return octave::check_access (cls, get ("Access"), get_name ());
  }

  octave_value_list
  cdef_method::cdef_method_rep::meta_subsref
  (const std::string& type, const std::list<octave_value_list>& idx,
   int nargout)
  {
    octave_value_list retval;

    switch (type[0])
      {
      case '(':
        retval = (execute (idx.front (), type.length () > 1 ? 1 : nargout, true));
        break;

      default:
        error ("invalid meta.method indexing");
        break;
      }

    if (type.length () > 1 && idx.size () > 1 && ! retval.empty ())
      retval = retval(0).next_subsref (nargout, type, idx, 1);

    return retval;
  }
}