comparison src/ov-fcn-handle.h @ 10321:97b4bd6f0925

partially rewrite function handles
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 13 Feb 2010 08:17:21 +0100
parents f3b65e1ae355
children 21551cc88061
comparison
equal deleted inserted replaced
10320:907d470e261b 10321:97b4bd6f0925
42 { 42 {
43 private: 43 private:
44 44
45 typedef std::map<std::string, octave_value> str_ov_map; 45 typedef std::map<std::string, octave_value> str_ov_map;
46 46
47 octave_fcn_handle (const octave_value& f, const std::string& n,
48 str_ov_map *sdisp)
49 : fcn (f), nm (n), disp (sdisp) { }
50
51 public: 47 public:
52 48
53 static const std::string anonymous; 49 static const std::string anonymous;
54 50
55 octave_fcn_handle (void) 51 octave_fcn_handle (void)
56 : fcn (), nm () { } 52 : fcn (), nm (), has_overloads (false) { }
57 53
58 octave_fcn_handle (const std::string& n) 54 octave_fcn_handle (const std::string& n)
59 : fcn (), nm (n) { } 55 : fcn (), nm (n), has_overloads (false) { }
60 56
61 octave_fcn_handle (const octave_value& f, const std::string& n = anonymous); 57 octave_fcn_handle (const octave_value& f, const std::string& n = anonymous);
62 58
63 octave_fcn_handle (const octave_fcn_handle& fh) 59 octave_fcn_handle (const octave_fcn_handle& fh)
64 : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm) 60 : octave_base_value (fh), fcn (fh.fcn), nm (fh.nm),
65 { 61 has_overloads (fh.has_overloads)
66 if (fh.disp.get ()) 62 {
67 disp.reset (new str_ov_map (*fh.disp)); 63 for (int i = 0; i < btyp_num_types; i++)
64 builtin_overloads[i] = fh.builtin_overloads[i];
65
66 overloads = fh.overloads;
68 } 67 }
69 68
70 ~octave_fcn_handle (void) { } 69 ~octave_fcn_handle (void) { }
71 70
72 octave_base_value *clone (void) const { return new octave_fcn_handle (*this); } 71 octave_base_value *clone (void) const { return new octave_fcn_handle (*this); }
90 89
91 bool is_function_handle (void) const { return true; } 90 bool is_function_handle (void) const { return true; }
92 91
93 builtin_type_t builtin_type (void) const { return btyp_func_handle; } 92 builtin_type_t builtin_type (void) const { return btyp_func_handle; }
94 93
95 bool is_overloaded (void) const { return disp.get () && ! disp->empty (); } 94 bool is_overloaded (void) const { return has_overloads; }
96 95
97 dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; } 96 dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
98 97
99 octave_function *function_value (bool = false) 98 octave_function *function_value (bool = false)
100 { return fcn.function_value (); } 99 { return fcn.function_value (); }
105 octave_fcn_handle *fcn_handle_value (bool = false) { return this; } 104 octave_fcn_handle *fcn_handle_value (bool = false) { return this; }
106 105
107 octave_value fcn_val (void) const { return fcn; } 106 octave_value fcn_val (void) const { return fcn; }
108 107
109 std::string fcn_name (void) const { return nm; } 108 std::string fcn_name (void) const { return nm; }
109
110 void set_overload (builtin_type_t btyp, const octave_value& ov_fcn)
111 {
112 if (btyp != btyp_unknown)
113 {
114 has_overloads = true;
115 builtin_overloads[btyp] = ov_fcn;
116 }
117
118 }
119
120 void set_overload (const std::string& dispatch_type, const octave_value& ov_fcn)
121 {
122 has_overloads = true;
123 overloads[dispatch_type] = ov_fcn;
124 }
110 125
111 bool save_ascii (std::ostream& os); 126 bool save_ascii (std::ostream& os);
112 127
113 bool load_ascii (std::istream& is); 128 bool load_ascii (std::istream& is);
114 129
141 octave_value fcn; 156 octave_value fcn;
142 157
143 // The name of the handle, including the "@". 158 // The name of the handle, including the "@".
144 std::string nm; 159 std::string nm;
145 160
146 // A pointer to statical dispatch to standard classes. If null, we don't want 161 // Whether the function is overloaded at all.
147 // to dispatch at all. 162 bool has_overloads;
148 std::auto_ptr<str_ov_map> disp; 163
164 // Overloads for builtin types. We use array to make lookup faster.
165 octave_value builtin_overloads[btyp_num_types];
166
167 // Overloads for other classes.
168 str_ov_map overloads;
149 169
150 friend octave_value make_fcn_handle (const std::string &, bool); 170 friend octave_value make_fcn_handle (const std::string &, bool);
151 }; 171 };
152 172
153 extern octave_value make_fcn_handle (const std::string& nm, 173 extern octave_value make_fcn_handle (const std::string& nm,