From 977d07a3ae47ef204665d1eda2d642e5064724f3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 24 Jun 2019 12:01:19 +0200 Subject: Split build system into library and driver --- libbuild2/rule-map.hxx | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 libbuild2/rule-map.hxx (limited to 'libbuild2/rule-map.hxx') diff --git a/libbuild2/rule-map.hxx b/libbuild2/rule-map.hxx new file mode 100644 index 0000000..26f80a8 --- /dev/null +++ b/libbuild2/rule-map.hxx @@ -0,0 +1,123 @@ +// file : libbuild2/rule-map.hxx -*- C++ -*- +// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef LIBBUILD2_RULE_MAP_HXX +#define LIBBUILD2_RULE_MAP_HXX + +#include + +#include + +#include +#include + +#include + +namespace build2 +{ + class rule; + + using hint_rule_map = + butl::prefix_map, '.'>; + + using target_type_rule_map = std::map; + + // This is an "indexed map" with operation_id being the index. Entry + // with id 0 is a wildcard. + // + // Note that while we may resize some vectors during non-serial load, this + // is MT-safe since we never cache any references to their elements. + // + class operation_rule_map + { + public: + template + void + insert (operation_id oid, const char* hint, const rule& r) + { + // 3 is the number of builtin operations. + // + if (oid >= map_.size ()) + map_.resize ((oid < 3 ? 3 : oid) + 1); + + map_[oid][&T::static_type].emplace (hint, r); + } + + // Return NULL if not found. + // + const target_type_rule_map* + operator[] (operation_id oid) const + { + return map_.size () > oid ? &map_[oid] : nullptr; + } + + bool + empty () const {return map_.empty ();} + + private: + vector map_; + }; + + // This is another indexed map but this time meta_operation_id is the + // index. The implementation is different, however: here we use a linked + // list with the first, statically-allocated node corresponding to the + // perform meta-operation. The idea is to try and get away with a dynamic + // allocation for the common cases since most rules will be registered + // for perform, at least on non-root scopes. + // + // @@ Redo using small_vector? + // + class rule_map + { + public: + + template + void + insert (action_id a, const char* hint, const rule& r) + { + insert (a >> 4, a & 0x0F, hint, r); + } + + // 0 oid is a wildcard. + // + template + void + insert (meta_operation_id mid, + operation_id oid, + const char* hint, + const rule& r) + { + if (mid_ == mid) + map_.insert (oid, hint, r); + else + { + if (next_ == nullptr) + next_.reset (new rule_map (mid)); + + next_->insert (mid, oid, hint, r); + } + } + + // Return NULL if not found. + // + const operation_rule_map* + operator[] (meta_operation_id mid) const + { + return mid == mid_ ? &map_ : next_ == nullptr ? nullptr : (*next_)[mid]; + } + + explicit + rule_map (meta_operation_id mid = perform_id): mid_ (mid) {} + + bool + empty () const {return map_.empty () && next_ == nullptr;} + + private: + meta_operation_id mid_; + operation_rule_map map_; + unique_ptr next_; + }; +} + +#endif // LIBBUILD2_RULE_MAP_HXX -- cgit v1.1