From 68f96f9213e849d0d7c4cedf3edeaec99743ee27 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 24 Aug 2015 09:51:15 +0200 Subject: New variable architecture --- build/variable.ixx | 403 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 345 insertions(+), 58 deletions(-) (limited to 'build/variable.ixx') diff --git a/build/variable.ixx b/build/variable.ixx index b97815e..16ea7c0 100644 --- a/build/variable.ixx +++ b/build/variable.ixx @@ -4,108 +4,395 @@ namespace build { - // value_proxy + // value // - inline bool value_proxy:: - empty () const {return as ().empty ();} + template + inline void + assign (value& v, const variable& var) + { + auto t (&value_traits::value_type); + + if (v.type != t) + assign (v, t, var); + } + + template + inline typename value_traits::type + as (value& v) + { + return value_traits::as (v); + } + + template + inline typename value_traits::const_type + as (const value& v) + { + return value_traits::as (v); + } + + template + inline bool + assign (name& n) + { + return value_traits::assign (n); + } + + template + inline typename value_traits::type + as (name& n) + { + return value_traits::as (n); + } - inline const value_proxy& value_proxy:: - operator= (value_ptr v) const + template + inline typename value_traits::const_type + as (const name& n) { - *p = std::move (v); + return value_traits::as (n); + } + + template + inline value& value:: + operator= (T v) + { + value_traits::assign (*this, std::move (v)); + return *this; + } + + template + inline value& value:: + operator+= (T v) + { + value_traits::append (*this, std::move (v)); return *this; } - inline const value_proxy& value_proxy:: - operator= (const value_proxy& v) const + inline void value:: + assign (names v, const variable& var) + { + data_ = std::move (v); + state_ = (type != nullptr && type->assign != nullptr + ? type->assign (data_, var) + : !data_.empty ()) + ? state_type::filled + : state_type::empty; + } + + // bool value + // + inline bool_value value_traits:: + as (value& v) + { + assert (v.type == bool_type); + return bool_value (v.data_.front ()); + } + + inline bool_value value_traits:: + as (const value& v) { - if (this != &v) + assert (v.type == bool_type); + return bool_value (v.data_.front ()); + } + + inline void value_traits:: + assign (value& v, bool x) + { + if (v.null ()) { - if (v) - *p = v.as ().clone (); - else - p->reset (); + if (v.type == nullptr) + v.type = bool_type; + v.data_.emplace_back (name ()); + v.state_ = value::state_type::empty; } - return *this; + as (v) = x; + v.state_ = value::state_type::filled; } - inline const value_proxy& value_proxy:: - operator= (list_value v) const + inline void value_traits:: + append (value& v, bool x) { - if (*p == nullptr) - p->reset (new list_value (std::move (v))); + if (v.null ()) + assign (v, x); else - //@@ Assuming it is a list_value. - // - as () = std::move (v); + as (v) += x; // Cannot be empty. + } - return *this; + // string value + // + inline std::string& value_traits:: + as (value& v) + { + assert (v.type == string_type); + return v.data_.front ().value; } - inline const value_proxy& value_proxy:: - operator= (std::string v) const + inline const std::string& value_traits:: + as (const value& v) { - // In most cases this is used to initialize a new variable, so - // don't bother trying to optimize for the case where p is not - // NULL. - // - p->reset (new list_value (std::move (v))); + assert (v.type == string_type); + return v.data_.front ().value; + } + + inline void value_traits:: + assign (value& v, std::string x) + { + if (v.null ()) + { + if (v.type == nullptr) + v.type = string_type; + v.data_.emplace_back (name ()); + v.state_ = value::state_type::empty; + } + + v.state_ = (as (v) = std::move (x)).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + inline void value_traits:: + append (value& v, std::string x) + { + if (v.null ()) + assign (v, std::move (x)); + else + v.state_ = (as (v) += std::move (x)).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + // dir_path value + // + inline dir_path& value_traits:: + as (value& v) + { + assert (v.type == dir_path_type); + return v.data_.front ().dir; + } + + inline const dir_path& value_traits:: + as (const value& v) + { + assert (v.type == dir_path_type); + return v.data_.front ().dir; + } + + inline void value_traits:: + assign (value& v, dir_path x) + { + if (v.null ()) + { + if (v.type == nullptr) + v.type = dir_path_type; + v.data_.emplace_back (name ()); + v.state_ = value::state_type::empty; + } + + v.state_ = (as (v) = std::move (x)).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + inline void value_traits:: + append (value& v, dir_path x) + { + if (v.null ()) + assign (v, std::move (x)); + else + v.state_ = (as (v) /= std::move (x)).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + // name value + // + inline name& value_traits:: + as (value& v) + { + assert (v.type == name_type); + return v.data_.front (); + } + + inline const name& value_traits:: + as (const value& v) + { + assert (v.type == name_type); + return v.data_.front (); + } + + inline void value_traits:: + assign (value& v, name x) + { + if (v.null ()) + { + if (v.type == nullptr) + v.type = name_type; + v.data_.emplace_back (name ()); + v.state_ = value::state_type::empty; + } + + v.state_ = (as (v) = std::move (x)).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + // vector value + // + template + inline vector_value& vector_value:: + assign (std::vector v) + { + d->clear (); + d->insert (d->end (), + std::make_move_iterator (v.begin ()), + std::make_move_iterator (v.end ())); return *this; } - inline const value_proxy& value_proxy:: - operator= (dir_path v) const + template + template + inline vector_value& vector_value:: + assign (const vector_value& v) { - p->reset (new list_value (std::move (v))); + d->clear (); + d->insert (d->end (), v.begin (), v.end ()); return *this; } - inline const value_proxy& value_proxy:: - operator= (std::nullptr_t) const + template + template + inline vector_value& vector_value:: + append (const vector_value& v) { - p->reset (); + d->insert (d->end (), v.begin (), v.end ()); return *this; } - inline const value_proxy& value_proxy:: - operator+= (const value_proxy& v) const + template + inline vector_value value_traits>:: + as (value& v) + { + assert (v.type == &value_traits>::value_type); + return vector_value (v.data_); + } + + template + inline vector_value value_traits>:: + as (const value& v) { - if (v && this != &v) + assert (v.type == &value_traits>::value_type); + return vector_value (v.data_); + } + + template + template + inline void value_traits>:: + assign (value& v, V x) + { + if (v.null ()) { - if (*p == nullptr) - *this = v; - else - //@@ Assuming it is a list_value. - // - *this += v.as (); + if (v.type == nullptr) + v.type = &value_traits>::value_type; + v.state_ = value::state_type::empty; } - return *this; + v.state_ = (as (v).assign (std::move (x))).empty () + ? value::state_type::empty + : value::state_type::filled; } - inline const value_proxy& value_proxy:: - operator+= (const list_value& v) const + template + template + inline void value_traits>:: + append (value& v, V x) { - if (*p == nullptr) - *this = v; + if (v.null ()) + assign (v, std::move (x)); else + v.state_ = (as (v).append (std::move (x))).empty () + ? value::state_type::empty + : value::state_type::filled; + } + + // map value + // + template + inline map_value value_traits>:: + as (value& v) + { + assert ((v.type == &value_traits>::value_type)); + return map_value (v.data_); + } + + template + inline map_value value_traits>:: + as (const value& v) + { + assert ((v.type == &value_traits>::value_type)); + return map_value (v.data_); + } + + template + template + inline void value_traits>:: + assign (value& v, M x) + { + if (v.null ()) { - list_value& lv (as ()); - lv.insert (lv.end (), v.begin (), v.end ()); + if (v.type == nullptr) + v.type = &value_traits>::value_type; + v.state_ = value::state_type::empty; } - return *this; + v.state_ = (as (v).assign (std::move (x))).empty () + ? value::state_type::empty + : value::state_type::filled; } - inline const value_proxy& value_proxy:: - operator+= (std::string v) const + template + template + inline void value_traits>:: + append (value& v, M x) { - if (*p == nullptr) - *this = v; + if (v.null ()) + assign (v, std::move (x)); else - as ().emplace_back (std::move (v)); + v.state_ = (as (v).append (std::move (x))).empty () + ? value::state_type::empty + : value::state_type::filled; + } - return *this; + // variable_map::iterator_adapter + // + template + inline typename I::reference variable_map::iterator_adapter:: + operator* () const + { + auto& r (I::operator* ()); + const variable& var (r.first); + auto& val (r.second); + + // First access after being assigned a type? + // + if (var.type != nullptr && val.type != var.type) + build::assign (const_cast (val), var.type, var); + + return r; + } + + template + inline typename I::pointer variable_map::iterator_adapter:: + operator-> () const + { + auto p (I::operator-> ()); + const variable& var (p->first); + auto& val (p->second); + + // First access after being assigned a type? + // + if (var.type != nullptr && val.type != var.type) + build::assign (const_cast (val), var.type, var); + + return p; } } -- cgit v1.1