Mercurial > hg > octave-nkf
annotate src/load-path.h @ 8008:4d13a7a2f6ab
dir_path: use singleton class for static data members
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 05 Aug 2008 12:04:10 -0400 |
parents | dd5cc5016487 |
children | a14bdf90be55 |
rev | line source |
---|---|
5832 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2006, 2007 John W. Eaton |
5832 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
5832 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
5832 | 20 |
21 */ | |
22 | |
23 #if !defined (octave_load_path_h) | |
24 #define octave_load_path_h 1 | |
25 | |
26 #include <iostream> | |
27 #include <list> | |
28 #include <map> | |
29 #include <string> | |
30 | |
5835 | 31 #include "pathsearch.h" |
5832 | 32 #include "str-vec.h" |
33 | |
34 class | |
6241 | 35 OCTINTERP_API |
5832 | 36 load_path |
37 { | |
38 protected: | |
39 | |
7336 | 40 load_path (void) : dir_info_list (), fcn_map (), method_map () { } |
5832 | 41 |
42 public: | |
43 | |
7336 | 44 typedef void (*hook_fcn_ptr) (const std::string& dir); |
5832 | 45 |
46 ~load_path (void) { } | |
47 | |
6365 | 48 static void initialize (bool set_initial_path = false) |
5832 | 49 { |
50 if (instance_ok ()) | |
6365 | 51 instance->do_initialize (set_initial_path); |
5832 | 52 } |
53 | |
54 static void clear (void) | |
55 { | |
56 if (instance_ok ()) | |
57 instance->do_clear (); | |
58 } | |
59 | |
5867 | 60 static void set (const std::string& p, bool warn = false) |
5832 | 61 { |
62 if (instance_ok ()) | |
5867 | 63 instance->do_set (p, warn); |
5832 | 64 } |
65 | |
5867 | 66 static void append (const std::string& dir, bool warn = false) |
5832 | 67 { |
68 if (instance_ok ()) | |
5867 | 69 instance->do_append (dir, warn); |
5832 | 70 } |
71 | |
5867 | 72 static void prepend (const std::string& dir, bool warn = false) |
5832 | 73 { |
74 if (instance_ok ()) | |
5867 | 75 instance->do_prepend (dir, warn); |
5832 | 76 } |
77 | |
78 static bool remove (const std::string& dir) | |
79 { | |
80 return instance_ok () ? instance->do_remove (dir) : false; | |
81 } | |
82 | |
83 static void update (void) | |
84 { | |
85 if (instance_ok ()) | |
86 instance->do_update (); | |
87 } | |
88 | |
7336 | 89 static std::string find_method (const std::string& class_name, |
90 const std::string& meth, | |
91 std::string& dir_name) | |
92 { | |
93 return instance_ok () | |
94 ? instance->do_find_method (class_name, meth, dir_name) : std::string (); | |
95 } | |
96 | |
97 static std::string find_method (const std::string& class_name, | |
98 const std::string& meth) | |
99 { | |
100 std::string dir_name; | |
101 return find_method (class_name, meth, dir_name); | |
102 } | |
103 | |
104 static std::list<std::string> methods (const std::string& class_name) | |
105 { | |
106 return instance_ok () | |
107 ? instance->do_methods (class_name) : std::list<std::string> (); | |
108 } | |
109 | |
110 static std::string find_fcn (const std::string& fcn, std::string& dir_name) | |
111 { | |
112 return instance_ok () | |
113 ? instance->do_find_fcn (fcn, dir_name) : std::string (); | |
114 } | |
115 | |
5832 | 116 static std::string find_fcn (const std::string& fcn) |
117 { | |
7336 | 118 std::string dir_name; |
119 return find_fcn (fcn, dir_name); | |
120 } | |
121 | |
122 static std::string find_private_fcn (const std::string& dir, | |
123 const std::string& fcn) | |
124 { | |
5832 | 125 return instance_ok () |
7336 | 126 ? instance->do_find_private_fcn (dir, fcn) : std::string (); |
5832 | 127 } |
128 | |
129 static std::string find_fcn_file (const std::string& fcn) | |
130 { | |
7336 | 131 std::string dir_name; |
132 | |
5832 | 133 return instance_ok () ? |
7336 | 134 instance->do_find_fcn (fcn, dir_name, M_FILE) : std::string (); |
5832 | 135 } |
136 | |
137 static std::string find_oct_file (const std::string& fcn) | |
138 { | |
7336 | 139 std::string dir_name; |
140 | |
5832 | 141 return instance_ok () ? |
7336 | 142 instance->do_find_fcn (fcn, dir_name, OCT_FILE) : std::string (); |
5832 | 143 } |
144 | |
5864 | 145 static std::string find_mex_file (const std::string& fcn) |
146 { | |
7336 | 147 std::string dir_name; |
148 | |
5864 | 149 return instance_ok () ? |
7336 | 150 instance->do_find_fcn (fcn, dir_name, MEX_FILE) : std::string (); |
5864 | 151 } |
152 | |
5832 | 153 static std::string find_file (const std::string& file) |
154 { | |
155 return instance_ok () | |
156 ? instance->do_find_file (file) : std::string (); | |
157 } | |
158 | |
159 static std::string find_first_of (const string_vector& files) | |
160 { | |
161 return instance_ok () ? | |
162 instance->do_find_first_of (files) : std::string (); | |
163 } | |
164 | |
165 static string_vector find_all_first_of (const string_vector& files) | |
166 { | |
167 return instance_ok () ? | |
168 instance->do_find_all_first_of (files) : string_vector (); | |
169 } | |
170 | |
171 static string_vector dirs (void) | |
172 { | |
173 return instance_ok () ? instance->do_dirs () : string_vector (); | |
174 } | |
175 | |
176 static std::list<std::string> dir_list (void) | |
177 { | |
178 return instance_ok () | |
179 ? instance->do_dir_list () : std::list<std::string> (); | |
180 } | |
181 | |
182 static string_vector files (const std::string& dir) | |
183 { | |
184 return instance_ok () ? instance->do_files (dir) : string_vector (); | |
185 } | |
186 | |
187 static string_vector fcn_names (void) | |
188 { | |
189 return instance_ok () ? instance->do_fcn_names () : string_vector (); | |
190 } | |
191 | |
192 static std::string path (void) | |
193 { | |
194 return instance_ok () ? instance->do_path () : std::string (); | |
195 } | |
196 | |
197 static void display (std::ostream& os) | |
198 { | |
199 if (instance_ok ()) | |
200 instance->do_display (os); | |
201 } | |
202 | |
7336 | 203 static void set_add_hook (hook_fcn_ptr f) { add_hook = f; } |
5832 | 204 |
7336 | 205 static void set_remove_hook (hook_fcn_ptr f) { remove_hook = f; } |
5832 | 206 |
207 static void set_command_line_path (const std::string& p) | |
208 { | |
5835 | 209 if (command_line_path.empty ()) |
210 command_line_path = p; | |
211 else | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
7971
diff
changeset
|
212 command_line_path += dir_path::path_sep_str () + p; |
5832 | 213 } |
214 | |
6626 | 215 static std::string system_path (void) |
216 { | |
6630 | 217 return instance_ok () ? instance->do_system_path () : std::string (); |
6626 | 218 } |
219 | |
5832 | 220 private: |
221 | |
222 static const int M_FILE = 1; | |
223 static const int OCT_FILE = 2; | |
5864 | 224 static const int MEX_FILE = 4; |
5832 | 225 |
226 class dir_info | |
227 { | |
228 public: | |
229 | |
7336 | 230 // <FCN_NAME, TYPE> |
231 typedef std::map<std::string, int> fcn_file_map_type; | |
232 | |
233 typedef fcn_file_map_type::const_iterator const_fcn_file_map_iterator; | |
234 typedef fcn_file_map_type::iterator fcn_file_map_iterator; | |
235 | |
7971
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
236 struct class_info |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
237 { |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
238 fcn_file_map_type method_file_map; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
239 fcn_file_map_type private_file_map; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
240 }; |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
241 |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
242 // <CLASS_NAME, CLASS_INFO> |
dd5cc5016487
handle private functions in class directories
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
243 typedef std::map<std::string, class_info> method_file_map_type; |
7336 | 244 |
245 typedef method_file_map_type::const_iterator const_method_file_map_iterator; | |
246 typedef method_file_map_type::iterator method_file_map_iterator; | |
247 | |
5832 | 248 dir_info (const std::string& d) : dir_name (d) { initialize (); } |
249 | |
250 dir_info (const dir_info& di) | |
251 : dir_name (di.dir_name), is_relative (di.is_relative), | |
252 dir_mtime (di.dir_mtime), all_files (di.all_files), | |
253 fcn_files (di.fcn_files), | |
7336 | 254 private_file_map (di.private_file_map), |
255 method_file_map (di.method_file_map) { } | |
5832 | 256 |
257 ~dir_info (void) { } | |
258 | |
259 dir_info& operator = (const dir_info& di) | |
260 { | |
261 if (&di != this) | |
262 { | |
263 dir_name = di.dir_name; | |
264 is_relative = di.is_relative; | |
265 dir_mtime = di.dir_mtime; | |
266 all_files = di.all_files; | |
267 fcn_files = di.fcn_files; | |
7336 | 268 private_file_map = di.private_file_map; |
269 method_file_map = di.method_file_map; | |
5832 | 270 } |
271 | |
272 return *this; | |
273 } | |
274 | |
275 void update (void); | |
276 | |
277 std::string dir_name; | |
278 bool is_relative; | |
279 octave_time dir_mtime; | |
280 string_vector all_files; | |
281 string_vector fcn_files; | |
7336 | 282 fcn_file_map_type private_file_map; |
283 method_file_map_type method_file_map; | |
5832 | 284 |
285 private: | |
286 | |
287 void initialize (void); | |
288 | |
7336 | 289 void get_file_list (const std::string& d); |
290 | |
291 void get_private_file_map (const std::string& d); | |
5832 | 292 |
7336 | 293 void get_method_file_map (const std::string& d, |
294 const std::string& class_name); | |
295 | |
296 friend fcn_file_map_type get_fcn_files (const std::string& d); | |
5832 | 297 }; |
298 | |
299 class file_info | |
300 { | |
301 public: | |
302 | |
303 file_info (const std::string& d, int t) : dir_name (d), types (t) { } | |
304 | |
305 file_info (const file_info& fi) | |
306 : dir_name (fi.dir_name), types (fi.types) { } | |
307 | |
308 ~file_info (void) { } | |
309 | |
310 file_info& operator = (const file_info& fi) | |
311 { | |
312 if (&fi != this) | |
313 { | |
314 dir_name = fi.dir_name; | |
315 types = fi.types; | |
316 } | |
317 | |
318 return *this; | |
319 } | |
320 | |
321 std::string dir_name; | |
322 int types; | |
323 }; | |
324 | |
325 // We maintain two ways of looking at the same information. | |
326 // | |
327 // First, a list of directories and the set of "public" files and | |
328 // private files (those found in the special "private" subdirectory) | |
329 // in each directory. | |
330 // | |
331 // Second, a map from file names (the union of all "public" files for all | |
7336 | 332 // directories, but without filename extensions) to a list of |
5832 | 333 // corresponding information (directory name and file types). This |
334 // way, we can quickly find shadowed file names and look up all | |
335 // overloaded functions (in the "@" directories used to implement | |
336 // classes). | |
337 | |
7336 | 338 typedef std::list<dir_info> dir_info_list_type; |
339 | |
340 typedef dir_info_list_type::const_iterator const_dir_info_list_iterator; | |
341 typedef dir_info_list_type::iterator dir_info_list_iterator; | |
342 | |
343 typedef std::list<file_info> file_info_list_type; | |
344 | |
345 typedef file_info_list_type::const_iterator const_file_info_list_iterator; | |
346 typedef file_info_list_type::iterator file_info_list_iterator; | |
347 | |
348 // <FCN_NAME, FILE_INFO_LIST> | |
349 typedef std::map<std::string, file_info_list_type> fcn_map_type; | |
350 | |
351 typedef fcn_map_type::const_iterator const_fcn_map_iterator; | |
352 typedef fcn_map_type::iterator fcn_map_iterator; | |
5832 | 353 |
7336 | 354 // <DIR_NAME, <FCN_NAME, TYPE>> |
355 typedef std::map<std::string, dir_info::fcn_file_map_type> private_fcn_map_type; | |
356 | |
357 typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator; | |
358 typedef private_fcn_map_type::iterator private_fcn_map_iterator; | |
359 | |
360 // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>> | |
361 typedef std::map<std::string, fcn_map_type> method_map_type; | |
362 | |
363 typedef method_map_type::const_iterator const_method_map_iterator; | |
364 typedef method_map_type::iterator method_map_iterator; | |
365 | |
366 mutable dir_info_list_type dir_info_list; | |
367 | |
368 mutable fcn_map_type fcn_map; | |
369 | |
370 mutable private_fcn_map_type private_fcn_map; | |
371 | |
372 mutable method_map_type method_map; | |
5832 | 373 |
374 static load_path *instance; | |
375 | |
7336 | 376 static hook_fcn_ptr add_hook; |
5832 | 377 |
7336 | 378 static hook_fcn_ptr remove_hook; |
5832 | 379 |
380 static std::string command_line_path; | |
381 | |
6626 | 382 static std::string sys_path; |
383 | |
5832 | 384 static bool instance_ok (void); |
385 | |
386 const_dir_info_list_iterator find_dir_info (const std::string& dir) const; | |
387 dir_info_list_iterator find_dir_info (const std::string& dir); | |
388 | |
389 bool contains (const std::string& dir) const; | |
390 | |
7336 | 391 void move_fcn_map (const std::string& dir, |
392 const string_vector& fcn_files, bool at_end); | |
393 | |
394 void move_method_map (const std::string& dir, bool at_end); | |
395 | |
5832 | 396 void move (std::list<dir_info>::iterator i, bool at_end); |
397 | |
6365 | 398 void do_initialize (bool set_initial_path); |
5832 | 399 |
400 void do_clear (void); | |
401 | |
5867 | 402 void do_set (const std::string& p, bool warn); |
403 | |
404 void do_append (const std::string& dir, bool warn); | |
5832 | 405 |
5867 | 406 void do_prepend (const std::string& dir, bool warn); |
5832 | 407 |
5867 | 408 void do_add (const std::string& dir, bool at_end, bool warn); |
5832 | 409 |
7336 | 410 void remove_fcn_map (const std::string& dir, const string_vector& fcn_files); |
411 | |
412 void remove_private_fcn_map (const std::string& dir); | |
413 | |
414 void remove_method_map (const std::string& dir); | |
415 | |
5832 | 416 bool do_remove (const std::string& dir); |
417 | |
418 void do_update (void) const; | |
419 | |
7336 | 420 static bool |
421 check_file_type (std::string& fname, int type, int possible_types, | |
422 const std::string& fcn, const char *who); | |
423 | |
5832 | 424 std::string do_find_fcn (const std::string& fcn, |
7336 | 425 std::string& dir_name, |
5864 | 426 int type = M_FILE | OCT_FILE | MEX_FILE) const; |
5832 | 427 |
7336 | 428 std::string do_find_private_fcn (const std::string& dir, |
429 const std::string& fcn, | |
430 int type = M_FILE | OCT_FILE | MEX_FILE) const; | |
431 | |
432 std::string do_find_method (const std::string& class_name, | |
433 const std::string& meth, | |
434 std::string& dir_name, | |
435 int type = M_FILE | OCT_FILE | MEX_FILE) const; | |
436 | |
437 std::list<std::string> do_methods (const std::string& class_name) const; | |
438 | |
5832 | 439 std::string do_find_file (const std::string& file) const; |
440 | |
441 std::string do_find_first_of (const string_vector& files) const; | |
442 | |
443 string_vector do_find_all_first_of (const string_vector& files) const; | |
444 | |
445 string_vector do_dirs (void) const; | |
446 | |
447 std::list<std::string> do_dir_list (void) const; | |
448 | |
449 string_vector do_files (const std::string& dir) const; | |
450 | |
451 string_vector do_fcn_names (void) const; | |
452 | |
453 std::string do_path (void) const; | |
454 | |
7336 | 455 friend void print_types (std::ostream& os, int types); |
456 | |
457 friend string_vector get_file_list (const dir_info::fcn_file_map_type& lst); | |
458 | |
459 friend void | |
460 print_fcn_list (std::ostream& os, const dir_info::fcn_file_map_type& lst); | |
461 | |
5832 | 462 void do_display (std::ostream& os) const; |
463 | |
6629 | 464 std::string do_system_path (void) const { return sys_path; } |
6626 | 465 |
5832 | 466 void add_to_fcn_map (const dir_info& di, bool at_end) const; |
7336 | 467 |
468 void add_to_private_fcn_map (const dir_info& di) const; | |
469 | |
470 void add_to_method_map (const dir_info& di, bool at_end) const; | |
471 | |
472 friend dir_info::fcn_file_map_type get_fcn_files (const std::string& d); | |
5832 | 473 }; |
474 | |
475 extern std::string | |
476 genpath (const std::string& dir, const string_vector& skip = "private"); | |
477 | |
478 extern void execute_pkg_add (const std::string& dir); | |
479 extern void execute_pkg_del (const std::string& dir); | |
480 | |
481 #endif | |
482 | |
483 /* | |
484 ;;; Local Variables: *** | |
485 ;;; mode: C++ *** | |
486 ;;; page-delimiter: "^/\\*" *** | |
487 ;;; End: *** | |
488 */ |