GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
hook-fcn.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_hook_fcn_h)
24 #define octave_hook_fcn_h 1
25 
26 #include "octave-config.h"
27 
28 #include <string>
29 
30 #include "ovl.h"
31 #include "ov.h"
32 #include "ov-fcn-handle.h"
33 #include "parse.h"
34 #include "variables.h"
35 
36 class
38 {
39 public:
40 
41  friend class hook_function;
42 
43  base_hook_function (void) : count (1) { }
44 
45  base_hook_function (const base_hook_function&) : count (1) { }
46 
47  virtual ~base_hook_function (void) = default;
48 
49  virtual std::string id (void) { return ""; }
50 
51  virtual bool is_valid (void) { return false; }
52 
53  virtual void eval (const octave_value_list&) { }
54 
55 protected:
56 
57  size_t count;
58 };
59 
60 class
62 {
63 public:
64 
66  {
67  static base_hook_function nil_rep;
68  rep = &nil_rep;
69  rep->count++;
70  }
71 
73  const octave_value& d = octave_value ());
74 
76  {
77  if (--rep->count == 0)
78  delete rep;
79  }
80 
82  : rep (hf.rep)
83  {
84  rep->count++;
85  }
86 
88  {
89  if (rep != hf.rep)
90  {
91  if (--rep->count == 0)
92  delete rep;
93 
94  rep = hf.rep;
95  rep->count++;
96  }
97 
98  return *this;
99  }
100 
101  std::string id (void) { return rep->id (); }
102 
103  bool is_valid (void) { return rep->is_valid (); }
104 
105  void eval (const octave_value_list& initial_args)
106  {
107  rep->eval (initial_args);
108  }
109 
110 private:
111 
113 };
114 
115 class
117 {
118 public:
119 
121  : name (n), data (d)
122  { }
123 
124  void eval (const octave_value_list& initial_args)
125  {
126  octave_value_list args = initial_args;
127 
128  if (data.is_defined ())
129  args.append (data);
130 
131  octave::feval (name, args, 0);
132  }
133 
134  std::string id (void) { return name; }
135 
136  bool is_valid (void) { return is_valid_function (name); }
137 
138 private:
139 
141 
143 };
144 
145 class
147 {
148 public:
149 
151  : ident (), valid (false), fcn_handle (fh_arg), data (d)
152  {
153  octave_fcn_handle *fh = fcn_handle.fcn_handle_value (true);
154 
155  if (fh)
156  {
157  valid = true;
158 
159  std::ostringstream buf;
160  buf << fh;
161  ident = fh->fcn_name () + ':' + buf.str ();
162  }
163  }
164 
165  void eval (const octave_value_list& initial_args)
166  {
167  octave_value_list args = initial_args;
168 
169  if (data.is_defined ())
170  args.append (data);
171 
172  octave::feval (fcn_handle, args, 0);
173  }
174 
175  std::string id (void) { return ident; }
176 
177  bool is_valid (void) { return valid; }
178 
179 private:
180 
182 
183  bool valid;
184 
186 
188 };
189 
190 class
192 {
193 public:
194 
195  typedef std::map<std::string, hook_function> map_type;
196 
197  typedef map_type::iterator iterator;
198  typedef map_type::const_iterator const_iterator;
199 
200  hook_function_list (void) : fcn_map () { }
201 
202  ~hook_function_list (void) = default;
203 
205  : fcn_map (lst.fcn_map)
206  { }
207 
209  {
210  if (&lst != this)
211  fcn_map = lst.fcn_map;
212 
213  return *this;
214  }
215 
216  bool empty (void) const { return fcn_map.empty (); }
217 
218  void clear (void) { fcn_map.clear (); }
219 
220  void insert (const std::string& id, const hook_function& f)
221  {
222  fcn_map[id] = f;
223  }
224 
226  {
227  return fcn_map.find (id);
228  }
229 
230  const_iterator find (const std::string& id) const
231  {
232  return fcn_map.find (id);
233  }
234 
235  iterator end (void) { return fcn_map.end (); }
236 
237  const_iterator end (void) const { return fcn_map.end (); }
238 
239  void erase (iterator p) { fcn_map.erase (p); }
240 
241  void run (const octave_value_list& initial_args = octave_value_list ())
242  {
243  iterator p = fcn_map.begin ();
244 
245  while (p != fcn_map.end ())
246  {
247  std::string hook_fcn_id = p->first;
248  hook_function hook_fcn = p->second;
249 
250  iterator q = p++;
251 
252  if (hook_fcn.is_valid ())
253  hook_fcn.eval (initial_args);
254  else
255  fcn_map.erase (q);
256  }
257  }
258 
259 private:
260 
262 };
263 
264 #endif
uint32_t id
Definition: graphics.cc:12193
OCTINTERP_API octave_value_list feval(const std::string &name, const octave_value_list &args=octave_value_list(), int nargout=0)
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:165
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:124
std::map< std::string, hook_function > map_type
Definition: hook-fcn.h:195
octave_fcn_handle * fcn_handle_value(bool=false)
hook_function_list(void)
Definition: hook-fcn.h:200
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:83
const_iterator find(const std::string &id) const
Definition: hook-fcn.h:230
map_type fcn_map
Definition: hook-fcn.h:261
virtual void eval(const octave_value_list &)
Definition: hook-fcn.h:53
base_hook_function(void)
Definition: hook-fcn.h:43
octave_value data
Definition: hook-fcn.h:142
iterator find(const std::string &id)
Definition: hook-fcn.h:225
std::string id(void)
Definition: hook-fcn.h:134
map_type::const_iterator const_iterator
Definition: hook-fcn.h:198
named_hook_function(const std::string &n, const octave_value &d)
Definition: hook-fcn.h:120
fcn_handle_hook_function(const octave_value &fh_arg, const octave_value &d)
Definition: hook-fcn.h:150
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
bool empty(void) const
Definition: hook-fcn.h:216
void insert(const std::string &id, const hook_function &f)
Definition: hook-fcn.h:220
nd deftypefn *std::string name
Definition: sysdep.cc:647
std::string fcn_name(void) const
void erase(iterator p)
Definition: hook-fcn.h:239
~hook_function(void)
Definition: hook-fcn.h:75
is false
Definition: cellfun.cc:400
const_iterator end(void) const
Definition: hook-fcn.h:237
hook_function(void)
Definition: hook-fcn.h:65
virtual bool is_valid(void)
Definition: hook-fcn.h:51
virtual std::string id(void)
Definition: hook-fcn.h:49
iterator end(void)
Definition: hook-fcn.h:235
std::string id(void)
Definition: hook-fcn.h:175
std::string id(void)
Definition: hook-fcn.h:101
bool is_valid(void)
Definition: hook-fcn.h:136
hook_function & operator=(const hook_function &hf)
Definition: hook-fcn.h:87
hook_function_list(const hook_function_list &lst)
Definition: hook-fcn.h:204
p
Definition: lu.cc:138
void clear(void)
Definition: hook-fcn.h:218
base_hook_function(const base_hook_function &)
Definition: hook-fcn.h:45
base_hook_function * rep
Definition: hook-fcn.h:112
hook_function(const hook_function &hf)
Definition: hook-fcn.h:81
std::string name
Definition: hook-fcn.h:140
octave_function * is_valid_function(const std::string &fcn_name, const std::string &warn_for, bool warn)
Definition: variables.cc:78
bool is_valid(void)
Definition: hook-fcn.h:103
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:105
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
void run(const octave_value_list &initial_args=octave_value_list())
Definition: hook-fcn.h:241
octave_value fcn_handle
Definition: hook-fcn.h:185
map_type::iterator iterator
Definition: hook-fcn.h:197