GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
hook-fcn.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_hook_fcn_h)
24 #define octave_hook_fcn_h 1
25 
26 #include <string>
27 
28 #include "oct-obj.h"
29 #include "ov.h"
30 #include "ov-fcn-handle.h"
31 #include "parse.h"
32 #include "variables.h"
33 
34 class
36 {
37 public:
38 
39  friend class hook_function;
40 
41  base_hook_function (void) : count (1) { }
42 
43  base_hook_function (const base_hook_function&) : count (1) { }
44 
45  virtual ~base_hook_function (void) { }
46 
47  virtual std::string id (void) { return std::string (); }
48 
49  virtual bool is_valid (void) { return false; }
50 
51  virtual void eval (const octave_value_list&) { }
52 
53 protected:
54 
55  size_t count;
56 };
57 
58 class
60 {
61 public:
62 
64  {
65  static base_hook_function nil_rep;
66  rep = &nil_rep;
67  rep->count++;
68  }
69 
71  const octave_value& d = octave_value ());
72 
74  {
75  if (--rep->count == 0)
76  delete rep;
77  }
78 
80  : rep (hf.rep)
81  {
82  rep->count++;
83  }
84 
86  {
87  if (rep != hf.rep)
88  {
89  if (--rep->count == 0)
90  delete rep;
91 
92  rep = hf.rep;
93  rep->count++;
94  }
95 
96  return *this;
97  }
98 
99  std::string id (void) { return rep->id (); }
100 
101  bool is_valid (void) { return rep->is_valid (); }
102 
103  void eval (const octave_value_list& initial_args)
104  {
105  rep->eval (initial_args);
106  }
107 
108 private:
109 
111 };
112 
113 class
115 {
116 public:
117 
118  named_hook_function (const std::string& n, const octave_value& d)
119  : name (n), data (d)
120  { }
121 
122  void eval (const octave_value_list& initial_args)
123  {
124  octave_value_list args = initial_args;
125 
126  if (data.is_defined ())
127  args.append (data);
128 
129  feval (name, args, 0);
130  }
131 
132  std::string id (void) { return name; }
133 
134  bool is_valid (void) { return is_valid_function (name); }
135 
136 private:
137 
138  std::string name;
139 
141 };
142 
143 class
145 {
146 public:
147 
149  : ident (), valid (false), fcn_handle (fh_arg), data (d)
150  {
151  octave_fcn_handle *fh = fcn_handle.fcn_handle_value (true);
152 
153  if (fh)
154  {
155  valid = true;
156 
157  std::ostringstream buf;
158  buf << fh;
159  ident = fh->fcn_name () + ":" + buf.str ();
160  }
161  }
162 
163  void eval (const octave_value_list& initial_args)
164  {
165  octave_value_list args = initial_args;
166 
167  if (data.is_defined ())
168  args.append (data);
169 
170  fcn_handle.do_multi_index_op (0, args);
171  }
172 
173  std::string id (void) { return ident; }
174 
175  bool is_valid (void) { return valid; }
176 
177 private:
178 
179  std::string ident;
180 
181  bool valid;
182 
184 
186 };
187 
188 class
190 {
191 public:
192 
193  typedef std::map<std::string, hook_function> map_type;
194 
195  typedef map_type::iterator iterator;
196  typedef map_type::const_iterator const_iterator;
197 
198  hook_function_list (void) : fcn_map () { }
199 
201 
203  : fcn_map (lst.fcn_map)
204  { }
205 
207  {
208  if (&lst != this)
209  fcn_map = lst.fcn_map;
210 
211  return *this;
212  }
213 
214  bool empty (void) const { return fcn_map.empty (); }
215 
216  void clear (void) { fcn_map.clear (); }
217 
218  void insert (const std::string& id, const hook_function& f)
219  {
220  fcn_map[id] = f;
221  }
222 
223  iterator find (const std::string& id)
224  {
225  return fcn_map.find (id);
226  }
227 
228  const_iterator find (const std::string& id) const
229  {
230  return fcn_map.find (id);
231  }
232 
233  iterator end (void) { return fcn_map.end (); }
234 
235  const_iterator end (void) const { return fcn_map.end (); }
236 
237  void erase (iterator p) { fcn_map.erase (p); }
238 
239  void run (const octave_value_list& initial_args = octave_value_list ())
240  {
241  iterator p = fcn_map.begin ();
242 
243  while (p != fcn_map.end ())
244  {
245  std::string hook_fcn_id = p->first;
246  hook_function hook_fcn = p->second;
247 
248  iterator q = p++;
249 
250  if (hook_fcn.is_valid ())
251  hook_fcn.eval (initial_args);
252  else
253  fcn_map.erase (q);
254  }
255  }
256 
257 private:
258 
259  map_type fcn_map;
260 };
261 
262 #endif
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:163
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:122
std::map< std::string, hook_function > map_type
Definition: hook-fcn.h:193
octave_fcn_handle * fcn_handle_value(bool=false)
hook_function_list(void)
Definition: hook-fcn.h:198
const_iterator find(const std::string &id) const
Definition: hook-fcn.h:228
octave_value_list & append(const octave_value &val)
Definition: oct-obj.cc:85
map_type fcn_map
Definition: hook-fcn.h:259
virtual ~base_hook_function(void)
Definition: hook-fcn.h:45
virtual void eval(const octave_value_list &)
Definition: hook-fcn.h:51
octave_value_list feval(const std::string &name, const octave_value_list &args, int nargout)
Definition: oct-parse.cc:8625
base_hook_function(void)
Definition: hook-fcn.h:41
octave_value data
Definition: hook-fcn.h:140
iterator find(const std::string &id)
Definition: hook-fcn.h:223
std::string id(void)
Definition: hook-fcn.h:132
map_type::const_iterator const_iterator
Definition: hook-fcn.h:196
named_hook_function(const std::string &n, const octave_value &d)
Definition: hook-fcn.h:118
fcn_handle_hook_function(const octave_value &fh_arg, const octave_value &d)
Definition: hook-fcn.h:148
F77_RET_T const double const double double * d
~hook_function_list(void)
Definition: hook-fcn.h:200
void insert(const std::string &id, const hook_function &f)
Definition: hook-fcn.h:218
F77_RET_T const double const double * f
void erase(iterator p)
Definition: hook-fcn.h:237
~hook_function(void)
Definition: hook-fcn.h:73
const_iterator end(void) const
Definition: hook-fcn.h:235
hook_function(void)
Definition: hook-fcn.h:63
virtual bool is_valid(void)
Definition: hook-fcn.h:49
virtual std::string id(void)
Definition: hook-fcn.h:47
iterator end(void)
Definition: hook-fcn.h:233
std::string id(void)
Definition: hook-fcn.h:173
std::string id(void)
Definition: hook-fcn.h:99
bool is_valid(void)
Definition: hook-fcn.h:134
hook_function & operator=(const hook_function &hf)
Definition: hook-fcn.h:85
hook_function_list(const hook_function_list &lst)
Definition: hook-fcn.h:202
bool empty(void) const
Definition: hook-fcn.h:214
void clear(void)
Definition: hook-fcn.h:216
base_hook_function(const base_hook_function &)
Definition: hook-fcn.h:43
base_hook_function * rep
Definition: hook-fcn.h:110
hook_function(const hook_function &hf)
Definition: hook-fcn.h:79
std::string name
Definition: hook-fcn.h:138
octave_function * is_valid_function(const std::string &fcn_name, const std::string &warn_for, bool warn)
Definition: variables.cc:99
bool is_valid(void)
Definition: hook-fcn.h:101
void eval(const octave_value_list &initial_args)
Definition: hook-fcn.h:103
void run(const octave_value_list &initial_args=octave_value_list())
Definition: hook-fcn.h:239
octave_value fcn_handle
Definition: hook-fcn.h:183
map_type::iterator iterator
Definition: hook-fcn.h:195
std::string fcn_name(void) const