Skip to content
Snippets Groups Projects
Commit 1bd51e39 authored by Adam Wardynski's avatar Adam Wardynski
Browse files

Change static consts True and False to functions.

Somewhat nicer to use and guards against static init order fiasco.
parent 9e72a1da
Branches
No related merge requests found
......@@ -9,10 +9,10 @@ And::BaseRetValPtr And::apply_internal(const FunExecContext& context) const
{
foreach(boost::shared_ptr< Function<Bool> > expression, *expressions_) {
if(!(expression->apply(context)->get_value())) {
return Predicate::False->apply(context);
return Predicate::False(context);
}
}
return Predicate::True->apply(context);
return Predicate::True(context);
}
const std::string And::raw_operator_name() const {
......
......@@ -47,9 +47,9 @@ protected:
boost::shared_ptr<T> arg1 = this->arg1_expr_->apply(context);
boost::shared_ptr<T> arg2 = this->arg2_expr_->apply(context);
if(arg1->equals(*arg2)) {
return Predicate::True->apply(context);
return Predicate::True(context);
}
return Predicate::False->apply(context);
return Predicate::False(context);
}
};
......@@ -94,17 +94,17 @@ protected:
boost::shared_ptr<Position> arg1 = this->arg1_expr_->apply(context);
boost::shared_ptr<Position> arg2 = this->arg2_expr_->apply(context);
if(arg1->equals(*arg2)) {
return Predicate::True->apply(context);
return Predicate::True(context);
} else {
//in the given context both positions can still point nowhere
//even if they have different underlying value
int abs_pos1 = context.get_abs_position(*arg1);
int abs_pos2 = context.get_abs_position(*arg2);
if(!context.is_inside(abs_pos1) && !context.is_inside(abs_pos2)) {
return Predicate::True->apply(context);
return Predicate::True(context);
}
}
return Predicate::False->apply(context);
return Predicate::False(context);
}
};
......
......@@ -30,9 +30,9 @@ protected:
boost::shared_ptr<T> set1 = this->set1_expr_->apply(context);
boost::shared_ptr<T> set2 = this->set2_expr_->apply(context);
if(set1->is_subset_of(*set2)) {
return Predicate::True->apply(context);
return Predicate::True(context);
}
return Predicate::False->apply(context);
return Predicate::False(context);
}
};
......
......@@ -35,10 +35,10 @@ protected:
if(!possible_subset->empty()) {
boost::shared_ptr<T> set_compared_to = this->set2_expr_->apply(context);
if(possible_subset->is_subset_of(*set_compared_to)) {
return Predicate::True->apply(context);
return Predicate::True(context);
}
}
return Predicate::False->apply(context);
return Predicate::False(context);
}
};
......
......@@ -8,10 +8,10 @@ Nor::BaseRetValPtr Nor::apply_internal(const FunExecContext& context) const
{
foreach(BoolFunctionPtr expression, *expressions_) {
if(expression->apply(context)->get_value()) {
return Predicate::False->apply(context);
return Predicate::False(context);
}
}
return Predicate::True->apply(context);
return Predicate::True(context);
}
const std::string Nor::raw_operator_name() const {
......
......@@ -8,10 +8,10 @@ Or::BaseRetValPtr Or::apply_internal(const FunExecContext& context) const
{
foreach(BoolFunctionPtr expression, *expressions_) {
if(expression->apply(context)->get_value()) {
return Predicate::True->apply(context);
return Predicate::True(context);
}
}
return Predicate::False->apply(context);
return Predicate::False(context);
}
const std::string Or::raw_operator_name() const {
......
......@@ -2,8 +2,16 @@
namespace Wccl {
const boost::scoped_ptr< Constant<Bool> > Predicate::True(new Constant<Bool>(Bool(true)));
Predicate::RetValPtr Predicate::True(const FunExecContext& context)
{
static Constant<Bool> true_constant(Bool(true));
return true_constant.apply(context);
}
const boost::scoped_ptr< Constant<Bool> > Predicate::False(new Constant<Bool>(Bool(false)));
Predicate::RetValPtr Predicate::False(const FunExecContext& context)
{
static Constant<Bool> false_constant(Bool(false));
return false_constant.apply(context);
}
} /* end ns Wccl */
......@@ -14,13 +14,13 @@ namespace Wccl {
class Predicate : public Function<Bool> {
public:
/**
* Constant<Bool> holding true value, to use by predicates when returning value
* Helper function returing True, to use by predicates when returning value
*/
static const boost::scoped_ptr< Constant<Bool> > True;
static RetValPtr True(const FunExecContext& context);
/**
* Constant<Bool> holding false value, to use by predicates when returning value
* Helper function returing False, to use by predicates when returning value
*/
static const boost::scoped_ptr< Constant<Bool> > False;
static RetValPtr False(const FunExecContext& context);
};
} /* end ns Wccl */
......
......@@ -75,25 +75,25 @@ std::string Regex::to_raw_string() const {
Regex::BaseRetValPtr Regex::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<const StrSet>& set = strset_expr_->apply(context);
if(set->empty()) {
return Predicate::False->apply(context);
return Predicate::False(context);
}
foreach(const UnicodeString& s, set->contents()) {
UErrorCode status = U_ZERO_ERROR;
boost::scoped_ptr<RegexMatcher> matcher(pattern_->matcher(s, status));
if(status != U_ZERO_ERROR) {
BOOST_ASSERT(status == U_ZERO_ERROR);
return Predicate::False->apply(context);
return Predicate::False(context);
}
bool matched = matcher->matches(status);
if(status != U_ZERO_ERROR) {
BOOST_ASSERT(status == U_ZERO_ERROR);
return Predicate::False->apply(context);
return Predicate::False(context);
}
if(!matched) {
return Predicate::False->apply(context);
return Predicate::False(context);
}
}
return Predicate::True->apply(context);
return Predicate::True(context);
}
} /* end ns Wccl */
......@@ -40,8 +40,8 @@ struct PredFix
BOOST_FIXTURE_TEST_CASE(predicate_constants, PredFix)
{
BOOST_CHECK_EQUAL(true, Predicate::True->apply(cx)->get_value());
BOOST_CHECK_EQUAL(false, Predicate::False->apply(cx)->get_value());
BOOST_CHECK_EQUAL(true, Predicate::True(cx)->get_value());
BOOST_CHECK_EQUAL(false, Predicate::False(cx)->get_value());
}
BOOST_FIXTURE_TEST_CASE(and_1arg, PredFix)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment