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