From 9fb791e9fad6c63fc1dac49f4d05ae63b8a3db9b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Jan 2016 11:55:15 +0200 Subject: Rename build directory/namespace to build2 --- build2/rule-map | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 build2/rule-map (limited to 'build2/rule-map') diff --git a/build2/rule-map b/build2/rule-map new file mode 100644 index 0000000..f86ef16 --- /dev/null +++ b/build2/rule-map @@ -0,0 +1,115 @@ +// file : build2/rule-map -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BUILD2_RULE_MAP +#define BUILD2_RULE_MAP + +#include +#include +#include +#include // unique_ptr +#include // reference_wrapper + +#include + +#include +#include + +namespace build2 +{ + class rule; + + using target_type_rule_map = std::map< + const target_type*, + butl::prefix_map, '.'>>; + + // This is an "indexed map" with operation_id being the index. Entry + // with id 0 is a wildcard. + // + class operation_rule_map + { + public: + template + void + insert (operation_id oid, const char* hint, 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: + std::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. + // + class rule_map + { + public: + + template + void + insert (action_id a, const char* hint, rule& r) + { + insert (a >> 4, a & 0x0F, hint, r); + } + + template + void + insert (meta_operation_id mid, operation_id oid, const char* hint, 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_; + std::unique_ptr next_; + }; +} + +#endif // BUILD2_RULE_MAP -- cgit v1.1