GNU Octave  8.1.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-2023 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 <memory>
32 #include <string>
33 
34 #include "ovl.h"
35 #include "ov.h"
36 #include "ov-fcn-handle.h"
37 #include "variables.h"
38 
40 
42 {
43 public:
44 
45  base_hook_function (void) = default;
46 
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 
59 {
60 public:
61 
63  {
64  static std::shared_ptr<base_hook_function>
65  nil_rep (new base_hook_function ());
66 
67  m_rep = nil_rep;
68  }
69 
71  const octave_value& d = octave_value ());
72 
73  ~hook_function (void) = default;
74 
75  hook_function (const hook_function& hf) = default;
76 
77  hook_function& operator = (const hook_function& hf) = default;
78 
79  std::string id (void) const { return m_rep->id (); }
80 
81  bool is_valid (void) const { return m_rep->is_valid (); }
82 
83  void eval (const octave_value_list& initial_args)
84  {
85  m_rep->eval (initial_args);
86  }
87 
88 private:
89 
90  std::shared_ptr<base_hook_function> m_rep;
91 };
92 
94 {
95 public:
96 
97  named_hook_function (const std::string& n, const octave_value& d)
98  : m_name (n), m_data (d)
99  { }
100 
101  void eval (const octave_value_list& initial_args);
102 
103  std::string id (void) const { return m_name; }
104 
105  bool is_valid (void) const { return is_valid_function (m_name); }
106 
107 private:
108 
109  std::string m_name;
110 
112 };
113 
115 {
116 public:
117 
119  : m_ident (), m_valid (false), m_fcn_handle (fh_arg), m_data (d)
120  {
122 
123  if (fh)
124  {
125  m_valid = true;
126 
127  std::ostringstream buf;
128  buf << fh;
129  m_ident = fh->fcn_name () + ':' + buf.str ();
130  }
131  }
132 
133  void eval (const octave_value_list& initial_args);
134 
135  std::string id (void) const { return m_ident; }
136 
137  bool is_valid (void) const { return m_valid; }
138 
139 private:
140 
141  std::string m_ident;
142 
143  bool m_valid;
144 
146 
148 };
149 
151 {
152 public:
153 
154  typedef std::map<std::string, hook_function> map_type;
155 
156  typedef map_type::iterator iterator;
157  typedef map_type::const_iterator const_iterator;
158 
159  hook_function_list (void) = default;
160 
161  ~hook_function_list (void) = default;
162 
163  hook_function_list (const hook_function_list& lst) = default;
164 
166 
167  bool empty (void) const { return m_fcn_map.empty (); }
168 
169  void clear (void) { m_fcn_map.clear (); }
170 
171  void insert (const std::string& id, const hook_function& f)
172  {
173  m_fcn_map[id] = f;
174  }
175 
176  iterator find (const std::string& id)
177  {
178  return m_fcn_map.find (id);
179  }
180 
181  const_iterator find (const std::string& id) const
182  {
183  return m_fcn_map.find (id);
184  }
185 
186  iterator end (void) { return m_fcn_map.end (); }
187 
188  const_iterator end (void) const { return m_fcn_map.end (); }
189 
190  void erase (iterator p) { m_fcn_map.erase (p); }
191 
192  void run (const octave_value_list& initial_args = octave_value_list ())
193  {
194  auto p = m_fcn_map.begin ();
195 
196  while (p != m_fcn_map.end ())
197  {
198  std::string hook_fcn_id = p->first;
199  hook_function hook_fcn = p->second;
200 
201  auto q = p++;
202 
203  if (hook_fcn.is_valid ())
204  hook_fcn.eval (initial_args);
205  else
206  m_fcn_map.erase (q);
207  }
208  }
209 
210 private:
211 
213 };
214 
216 
217 #if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
218 OCTAVE_DEPRECATED (7, "use 'octave::base_hook_function' instead")
220 
221 OCTAVE_DEPRECATED (7, "use 'octave::hook_function' instead")
223 
224 OCTAVE_DEPRECATED (7, "use 'octave::named_hook_function' instead")
226 
227 OCTAVE_DEPRECATED (7, "use 'octave::fcn_handle_hook_function' instead")
229 
230 OCTAVE_DEPRECATED (7, "use 'octave::hook_function_list' instead")
232 #endif
233 
234 #endif
OCTAVE_END_NAMESPACE(octave)
virtual void eval(const octave_value_list &)
Definition: hook-fcn.h:55
virtual bool is_valid(void) const
Definition: hook-fcn.h:53
base_hook_function(void)=default
virtual ~base_hook_function(void)=default
virtual std::string id(void) const
Definition: hook-fcn.h:51
base_hook_function(const base_hook_function &)=default
octave_value m_data
Definition: hook-fcn.h:147
std::string id(void) const
Definition: hook-fcn.h:135
fcn_handle_hook_function(const octave_value &fh_arg, const octave_value &d)
Definition: hook-fcn.h:118
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.cc:61
bool is_valid(void) const
Definition: hook-fcn.h:137
octave_value m_fcn_handle
Definition: hook-fcn.h:145
const_iterator find(const std::string &id) const
Definition: hook-fcn.h:181
map_type::iterator iterator
Definition: hook-fcn.h:156
~hook_function_list(void)=default
void clear(void)
Definition: hook-fcn.h:169
map_type::const_iterator const_iterator
Definition: hook-fcn.h:157
map_type m_fcn_map
Definition: hook-fcn.h:212
const_iterator end(void) const
Definition: hook-fcn.h:188
void run(const octave_value_list &initial_args=octave_value_list())
Definition: hook-fcn.h:192
std::map< std::string, hook_function > map_type
Definition: hook-fcn.h:154
hook_function_list(const hook_function_list &lst)=default
bool empty(void) const
Definition: hook-fcn.h:167
hook_function_list & operator=(const hook_function_list &lst)=default
void erase(iterator p)
Definition: hook-fcn.h:190
iterator find(const std::string &id)
Definition: hook-fcn.h:176
iterator end(void)
Definition: hook-fcn.h:186
void insert(const std::string &id, const hook_function &f)
Definition: hook-fcn.h:171
hook_function_list(void)=default
hook_function(void)
Definition: hook-fcn.h:62
hook_function & operator=(const hook_function &hf)=default
bool is_valid(void) const
Definition: hook-fcn.h:81
std::shared_ptr< base_hook_function > m_rep
Definition: hook-fcn.h:90
std::string id(void) const
Definition: hook-fcn.h:79
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:83
~hook_function(void)=default
hook_function(const hook_function &hf)=default
bool is_valid(void) const
Definition: hook-fcn.h:105
named_hook_function(const std::string &n, const octave_value &d)
Definition: hook-fcn.h:97
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.cc:51
octave_value m_data
Definition: hook-fcn.h:111
std::string id(void) const
Definition: hook-fcn.h:103
std::string m_name
Definition: hook-fcn.h:109
OCTINTERP_API octave_fcn_handle * fcn_handle_value(bool silent=false) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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