From 50bf3956c88ee6341d0023a421d502d604e3da4f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 16 Aug 2020 09:16:04 +0200 Subject: Redo modules map as vector --- libbuild2/module.hxx | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'libbuild2/module.hxx') diff --git a/libbuild2/module.hxx b/libbuild2/module.hxx index f97bc60..0305e2c 100644 --- a/libbuild2/module.hxx +++ b/libbuild2/module.hxx @@ -42,7 +42,7 @@ namespace build2 // struct module_boot_extra { - shared_ptr& module; // Module instance (out). + shared_ptr module; // Module instance (out). // Convenience functions. // @@ -53,9 +53,8 @@ namespace build2 T& module_as () {assert (module); return static_cast (*module);} }; - // Return true if the module should be initialized first (the order of - // initialization within the resulting two groups of modules is - // unspecified). + // Return true if the module should be initialized first (within the + // resulting two groups the modules are initializated in the order loaded). // using module_boot_function = bool (scope& root, @@ -66,8 +65,8 @@ namespace build2 // struct module_init_extra { - shared_ptr& module; // Module instance (in/out). - const variable_map& hints; // Configuration hints (see below). + shared_ptr module; // Module instance (in/out). + const variable_map& hints; // Configuration hints (see below). // Convenience functions. // @@ -125,20 +124,37 @@ namespace build2 { bool boot; // True if the module boot'ed but not yet init'ed. bool first; // True if the boot'ed module must be init'ed first. + const string name; module_init_function* init; shared_ptr module; location_value loc; // Boot location. }; - struct module_map: std::map + struct module_map: vector { + iterator + find (const string& name) + { + return find_if ( + begin (), end (), + [&name] (const module_state& s) {return s.name == name;}); + } + + const_iterator + find (const string& name) const + { + return find_if ( + begin (), end (), + [&name] (const module_state& s) {return s.name == name;}); + } + template T* find_module (const string& name) const { auto i (find (name)); return i != end () - ? static_cast (i->second.module.get ()) + ? static_cast (i->module.get ()) : nullptr; } }; @@ -152,7 +168,8 @@ namespace build2 // parser but also by some modules to init prerequisite modules. Return a // pointer to the corresponding module state if the module was both // successfully loaded and configured and NULL otherwise (which can only - // happen if optional is true). Note that the return can be used as a bool. + // happen if optional is true). Note that the result can be used as a bool + // but should not be assumed stable (vector resize). // // The config_hints variable map can be used to pass configuration hints // from one module to another. For example, the cxx modude may pass the @@ -170,18 +187,18 @@ namespace build2 // A wrapper over init_module() to use from other modules that incorporates // the .loaded variable check (use init_module() directly to sidestep - // this check). Return a pointer to the pointer to the module instance if it - // was both successfully loaded and configured and NULL otherwise (so can be - // used as bool). + // this check). Return an optional pointer to the module instance that is + // present if the module was both successfully loaded and configured and + // absent otherwise (so can be used as bool). // - // Note also that NULL can be returned even of optional is false which + // Note also that absent can be returned even of optional is false which // happens if the requested module has already been loaded but failed to // configure. The function could have issued diagnostics but the caller can // normally provide additional information. // // Note: for non-optional modules use the versions below. // - LIBBUILD2_SYMEXPORT const shared_ptr* + LIBBUILD2_SYMEXPORT optional> load_module (scope& root, scope& base, const string& name, @@ -191,7 +208,7 @@ namespace build2 // As above but always load and return a pointer to the module instance. // - LIBBUILD2_SYMEXPORT const shared_ptr& + LIBBUILD2_SYMEXPORT shared_ptr load_module (scope& root, scope& base, const string& name, -- cgit v1.1