GNU Octave  9.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-2024 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  OCTAVE_DEFAULT_CONSTRUCT_COPY (base_hook_function)
46 
47  virtual ~base_hook_function () = default;
48 
49  virtual std::string id () const { return ""; }
50 
51  virtual bool is_valid () const { return false; }
52 
53  virtual void eval (const octave_value_list&) { }
54 };
55 
57 {
58 public:
59 
61  {
62  static std::shared_ptr<base_hook_function>
63  nil_rep (new base_hook_function ());
64 
65  m_rep = nil_rep;
66  }
67 
69  const octave_value& d = octave_value ());
70 
71  OCTAVE_DEFAULT_COPY (hook_function)
72 
73  ~hook_function () = default;
74 
75  std::string id () const { return m_rep->id (); }
76 
77  bool is_valid () const { return m_rep->is_valid (); }
78 
79  void eval (const octave_value_list& initial_args)
80  {
81  m_rep->eval (initial_args);
82  }
83 
84 private:
85 
86  std::shared_ptr<base_hook_function> m_rep;
87 };
88 
90 {
91 public:
92 
93  named_hook_function () = delete;
94 
95  named_hook_function (const std::string& n, const octave_value& d)
96  : m_name (n), m_data (d)
97  { }
98 
99  OCTAVE_DEFAULT_COPY_DELETE (named_hook_function)
100 
101  void eval (const octave_value_list& initial_args);
102 
103  std::string id () const { return m_name; }
104 
105  bool is_valid () const { return is_valid_function (m_name); }
106 
107 private:
108 
109  std::string m_name;
110 
111  octave_value m_data;
112 };
113 
115 {
116 public:
117 
119 
121  : m_ident (), m_valid (false), m_fcn_handle (fh_arg), m_data (d)
122  {
123  octave_fcn_handle *fh = m_fcn_handle.fcn_handle_value (true);
124 
125  if (fh)
126  {
127  m_valid = true;
128 
129  std::ostringstream buf;
130  buf << fh;
131  m_ident = fh->fcn_name () + ':' + buf.str ();
132  }
133  }
134 
135  OCTAVE_DEFAULT_COPY_DELETE (fcn_handle_hook_function)
136 
137  void eval (const octave_value_list& initial_args);
138 
139  std::string id () const { return m_ident; }
140 
141  bool is_valid () const { return m_valid; }
142 
143 private:
144 
145  std::string m_ident;
146 
147  bool m_valid;
148 
149  octave_value m_fcn_handle;
150 
151  octave_value m_data;
152 };
153 
155 {
156 public:
157 
158  typedef std::map<std::string, hook_function> map_type;
159 
160  typedef map_type::iterator iterator;
161  typedef map_type::const_iterator const_iterator;
162 
163  OCTAVE_DEFAULT_CONSTRUCT_COPY_DELETE (hook_function_list)
164 
165  bool empty () const { return m_fcn_map.empty (); }
166 
167  void clear () { m_fcn_map.clear (); }
168 
169  void insert (const std::string& id, const hook_function& f)
170  {
171  m_fcn_map[id] = f;
172  }
173 
174  iterator find (const std::string& id)
175  {
176  return m_fcn_map.find (id);
177  }
178 
179  const_iterator find (const std::string& id) const
180  {
181  return m_fcn_map.find (id);
182  }
183 
184  iterator end () { return m_fcn_map.end (); }
185 
186  const_iterator end () const { return m_fcn_map.end (); }
187 
188  void erase (iterator p) { m_fcn_map.erase (p); }
189 
190  void run (const octave_value_list& initial_args = octave_value_list ())
191  {
192  auto p = m_fcn_map.begin ();
193 
194  while (p != m_fcn_map.end ())
195  {
196  std::string hook_fcn_id = p->first;
197  hook_function hook_fcn = p->second;
198 
199  auto q = p++;
200 
201  if (hook_fcn.is_valid ())
202  hook_fcn.eval (initial_args);
203  else
204  m_fcn_map.erase (q);
205  }
206  }
207 
208 private:
209 
210  map_type m_fcn_map;
211 };
212 
213 OCTAVE_END_NAMESPACE(octave)
214 
215 #endif
virtual void eval(const octave_value_list &)
Definition: hook-fcn.h:53
virtual bool is_valid() const
Definition: hook-fcn.h:51
std::string id() const
Definition: hook-fcn.h:139
fcn_handle_hook_function(const octave_value &fh_arg, const octave_value &d)
Definition: hook-fcn.h:120
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.cc:66
bool is_valid() const
Definition: hook-fcn.h:141
const_iterator find(const std::string &id) const
Definition: hook-fcn.h:179
map_type::iterator iterator
Definition: hook-fcn.h:160
map_type::const_iterator const_iterator
Definition: hook-fcn.h:161
void run(const octave_value_list &initial_args=octave_value_list())
Definition: hook-fcn.h:190
std::map< std::string, hook_function > map_type
Definition: hook-fcn.h:158
const_iterator end() const
Definition: hook-fcn.h:186
iterator end()
Definition: hook-fcn.h:184
bool empty() const
Definition: hook-fcn.h:165
void erase(iterator p)
Definition: hook-fcn.h:188
iterator find(const std::string &id)
Definition: hook-fcn.h:174
void insert(const std::string &id, const hook_function &f)
Definition: hook-fcn.h:169
~hook_function()=default
std::string id() const
Definition: hook-fcn.h:75
bool is_valid() const
Definition: hook-fcn.h:77
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:79
named_hook_function()=delete
named_hook_function(const std::string &n, const octave_value &d)
Definition: hook-fcn.h:95
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.cc:53
std::string id() const
Definition: hook-fcn.h:103
bool is_valid() const
Definition: hook-fcn.h:105
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:761
octave_function * is_valid_function(const std::string &fcn_name, const std::string &warn_for, bool warn)
Definition: variables.cc:76