GNU Octave  3.8.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 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