From de45de01a81e0f9d6559f706cef482e5fc2eb8d2 Mon Sep 17 00:00:00 2001 From: Daniel Burrows Date: Mon, 19 Apr 2010 22:01:27 -0700 Subject: Uniformly treat NULL pointers to incremental expressions as incremental expressions that are always "true". I considered trying to optimize by stripping NULLs out of "and" and "or" expressions, but that seems a bit tricky. It might be more straightforward if I introduce "true" and "false" expressions explicitly, though (part of the trouble is that it's probably bad if creation routines return NULL, and they can't currently return false). --- src/generic/problemresolver/incremental_expression.cc | 14 +++++++++----- src/generic/problemresolver/incremental_expression.h | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) (limited to 'src/generic/problemresolver') diff --git a/src/generic/problemresolver/incremental_expression.cc b/src/generic/problemresolver/incremental_expression.cc index 8f5c1547..7285cefc 100644 --- a/src/generic/problemresolver/incremental_expression.cc +++ b/src/generic/problemresolver/incremental_expression.cc @@ -1,7 +1,7 @@ /** \file incremental_expression.cc */ // -*-c++-*- -// Copyright (C) 2009 Daniel Burrows +// Copyright (C) 2009-2010 Daniel Burrows // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -27,7 +27,7 @@ void counting_bool_e::init_num_true() num_true = 0; for(std::vector > >::const_iterator it = children.begin(); it != children.end(); ++it) - if((*it)->get_value()) + if(!it->valid() || (*it)->get_value()) ++num_true; } @@ -53,7 +53,7 @@ void counting_bool_e::child_modified(const cwidget::util::ref_ptr > &child) { - if(child->get_value()) + if(!child.valid() || child->get_value()) { bool old_value = get_value(); @@ -71,7 +71,7 @@ void counting_bool_e::add_child(const cwidget::util::ref_ptr > void counting_bool_e::remove_child(const cwidget::util::ref_ptr > &child) { - if(child->get_value()) + if(!child.valid() || child->get_value()) { bool old_value = get_value(); @@ -116,7 +116,11 @@ void not_e::child_modified(const cwidget::util::ref_ptr > &chil bool not_e::get_value() { - return !get_child()->get_value(); + if(get_child().valid()) + return !get_child()->get_value(); + else + // NULL pointers count as "true". + return false; } void not_e::dump(std::ostream &out) diff --git a/src/generic/problemresolver/incremental_expression.h b/src/generic/problemresolver/incremental_expression.h index 683e4cba..da235208 100644 --- a/src/generic/problemresolver/incremental_expression.h +++ b/src/generic/problemresolver/incremental_expression.h @@ -403,7 +403,8 @@ public: { for(typename std::vector > >::const_iterator it = get_children().begin(); it != get_children().end(); ++it) - (*it)->add_parent(this); + if(it->valid()) + (*it)->add_parent(this); } const std::vector > > &get_children() const @@ -417,14 +418,17 @@ public: children.push_back(new_child); } - /** \brief Remove a child from this container. */ + /** \brief Remove one copy of a child from this container. */ virtual void remove_child(const cwidget::util::ref_ptr > &child) { + if(child.valid()) + child->remove_parent(this); + typename std::vector > >::iterator - new_end = std::remove(children.begin(), children.end(), child); + found = std::find(children.begin(), children.end(), child); - child->remove_parent(this); - children.erase(new_end, children.end()); + if(found != children.end()) + children.erase(found); } virtual std::string get_name() = 0; @@ -438,7 +442,10 @@ public: if(it != get_children().begin()) out << ", "; - (*it)->dump(out); + if(it->valid()) + (*it)->dump(out); + else + out << "(null)"; } out << ")"; } -- cgit v1.2.3