diff --git a/src/autoload.cpp b/src/autoload.cpp index c2423785b..29347029b 100644 --- a/src/autoload.cpp +++ b/src/autoload.cpp @@ -46,12 +46,14 @@ file_access_attempt_t access_file(const wcstring &path, int mode) { return result; } -autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t *const scripts, - size_t script_count) +autoload_t::autoload_t(const wcstring &env_var_name_var, + command_removed_function_t cmd_removed_callback, + const builtin_script_t *const scripts, size_t script_count) : lock(), env_var_name(env_var_name_var), builtin_scripts(scripts), - builtin_script_count(script_count) { + builtin_script_count(script_count), + command_removed(cmd_removed_callback) { pthread_mutex_init(&lock, NULL); } diff --git a/src/autoload.h b/src/autoload.h index 7c56588ba..c7a3aec17 100644 --- a/src/autoload.h +++ b/src/autoload.h @@ -65,6 +65,9 @@ class autoload_t : public lru_cache_t { /// A table containing all the files that are currently being loaded. /// This is here to help prevent recursion. std::set is_loading_set; + // Function invoked when a command is removed + typedef void (*command_removed_function_t)(const wcstring &); + const command_removed_function_t command_removed; void remove_all_functions() { this->evict_all_nodes(); } @@ -74,19 +77,17 @@ class autoload_t : public lru_cache_t { autoload_function_t *get_autoloaded_function_with_creation(const wcstring &cmd, bool allow_eviction); - protected: - /// Overridable callback for when a command is removed. - virtual void command_removed(const wcstring &cmd) { UNUSED(cmd); } - public: // CRTP override void entry_was_evicted(wcstring key, autoload_function_t node); // Create an autoload_t for the given environment variable name. - autoload_t(const wcstring &env_var_name_var, const builtin_script_t *scripts, - size_t script_count); + autoload_t(const wcstring &env_var_name_var, + command_removed_function_t callback, + const builtin_script_t *scripts = NULL, + size_t script_count = 0); - virtual ~autoload_t(); + ~autoload_t(); /// Autoload the specified file, if it exists in the specified path. Do not load it multiple /// times unless its timestamp changes or parse_util_unload is called. diff --git a/src/complete.cpp b/src/complete.cpp index 3e5ccc7bb..3de8c81b1 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -346,23 +346,14 @@ class completer_t { } }; -/// Autoloader for completions. -class completion_autoload_t : public autoload_t { - public: - completion_autoload_t(); - virtual void command_removed(const wcstring &cmd); -}; - -static completion_autoload_t completion_autoloader; - -// Constructor -completion_autoload_t::completion_autoload_t() : autoload_t(L"fish_complete_path", NULL, 0) {} - -/// Callback when an autoloaded completion is removed. -void completion_autoload_t::command_removed(const wcstring &cmd) { +// Callback when an autoloaded completion is removed. +static void autoloaded_completion_removed(const wcstring &cmd) { complete_remove_all(cmd, false /* not a path */); } +// Autoloader for completions +static autoload_t completion_autoloader(L"fish_complete_path", autoloaded_completion_removed); + /// Create a new completion entry. void append_completion(std::vector *completions, const wcstring &comp, const wcstring &desc, complete_flags_t flags, string_fuzzy_match_t match) { diff --git a/src/function.cpp b/src/function.cpp index 3103b9968..da5fc30b5 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -37,25 +37,16 @@ static std::set function_tombstones; /// Lock for functions. static pthread_mutex_t functions_lock; -/// Autoloader for functions. -class function_autoload_t : public autoload_t { - public: - function_autoload_t(); - virtual void command_removed(const wcstring &cmd); -}; - -static function_autoload_t function_autoloader; - -/// Constructor -function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path", NULL, 0) {} - static bool function_remove_ignore_autoload(const wcstring &name, bool tombstone = true); /// Callback when an autoloaded function is removed. -void function_autoload_t::command_removed(const wcstring &cmd) { +void autoloaded_function_removed(const wcstring &cmd) { function_remove_ignore_autoload(cmd, false); } +// Function autoloader +static autoload_t function_autoloader(L"fish_function_path", autoloaded_function_removed); + /// Kludgy flag set by the load function in order to tell function_add that the function being /// defined is autoloaded. There should be a better way to do this... static bool is_autoload = false;