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

Const contract on function return Value.

Generally we want to avoid accidental change of value of a constant or of a variable. Originally I'd copy value on access, but this changed it so a const Value is returned instead and copy is done only when it's needed.
parent cb4be50a
No related merge requests found
......@@ -25,7 +25,7 @@ Affix::BaseRetValPtr Affix::apply_internal(const FunExecContext& context) const
if(affix_length_ == 0) {
return strset_expr_->apply(context);
}
const boost::shared_ptr<StrSet>& set = strset_expr_->apply(context);
const boost::shared_ptr<const StrSet>& set = strset_expr_->apply(context);
boost::shared_ptr<StrSet> a_set = boost::shared_ptr<StrSet>(new StrSet());
if(affix_length_ < 0) {
foreach(const UnicodeString& s, set->contents()) {
......
#ifndef LIBWCCL_OPS_CONSTANT_H
#define LIBWCCL_OPS_CONSTANT_H
#include <boost/scoped_ptr.hpp>
#include <boost/concept_check.hpp>
#include <libwccl/ops/functions.h>
......@@ -45,12 +44,12 @@ protected :
/**
* Applying Constant function returns the held value of a constant
*/
virtual BaseRetValPtr apply_internal(const FunExecContext&) const {
return BaseRetValPtr (new T(*value_));
BaseRetValPtr apply_internal(const FunExecContext&) const {
return value_;
}
private:
const boost::scoped_ptr<const T> value_;
const boost::shared_ptr<const T> value_;
};
......
......@@ -17,9 +17,9 @@ namespace Wccl {
*/
class FunctionBase : public Operator {
protected:
typedef boost::shared_ptr<Value> BaseRetValPtr;
typedef boost::shared_ptr<const Value> BaseRetValPtr;
/**
* Applies the function, given the sentence context.
* Applies the function for the given execution context.
*/
virtual BaseRetValPtr apply_internal(const FunExecContext& context) const = 0;
};
......@@ -37,7 +37,7 @@ public:
* Type returned after application of function (shared pointer to
* a variable of the specified return type)
*/
typedef boost::shared_ptr<T> RetValPtr;
typedef boost::shared_ptr<const T> RetValPtr;
/**
* Applies the function, given the sentence context, returning specific
......@@ -45,7 +45,7 @@ public:
* be specified in derived classes.
*/
RetValPtr apply(const FunExecContext& context) const {
RetValPtr v = boost::dynamic_pointer_cast<T>(apply_internal(context));
RetValPtr v = boost::dynamic_pointer_cast<const T>(apply_internal(context));
BOOST_ASSERT(v);
return v;
}
......
......@@ -73,7 +73,7 @@ std::string Regex::to_raw_string() const {
}
Regex::BaseRetValPtr Regex::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<StrSet>& set = strset_expr_->apply(context);
const boost::shared_ptr<const StrSet>& set = strset_expr_->apply(context);
if(set->empty()) {
return Predicate::False->apply(context);
}
......
......@@ -12,8 +12,8 @@ std::string ToLower::to_raw_string() const {
return UnaryFunctionFormatter::to_raw_string(*this, *strset_expr_);
}
ToLower::BaseRetValPtr ToLower::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<StrSet >& set = strset_expr_->apply(context);
ToLower::BaseRetValPtr ToLower::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<const StrSet >& set = strset_expr_->apply(context);
boost::shared_ptr<StrSet > l_set = boost::make_shared<StrSet>();
//TODO: should tolower be a method of StrSet as well?
foreach(const UnicodeString& s, set->contents()) {
......
......@@ -13,7 +13,7 @@ std::string ToUpper::to_raw_string() const {
}
ToUpper::BaseRetValPtr ToUpper::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<StrSet >& set = strset_expr_->apply(context);
const boost::shared_ptr<const StrSet >& set = strset_expr_->apply(context);
boost::shared_ptr<StrSet > u_set = boost::make_shared<StrSet>();
//TODO: should tolower be a method of StrSet as well?
foreach(const UnicodeString& s, set->contents()) {
......
......@@ -58,14 +58,4 @@ BOOST_FIXTURE_TEST_CASE(bool_to_raw_string, BoolFix)
BOOST_CHECK_EQUAL(false_value.to_raw_string(), false_constant.to_raw_string());
}
BOOST_FIXTURE_TEST_CASE(bool_value_preserved, BoolFix)
{
boost::shared_ptr<Bool> v = true_constant.apply(cx);
v->set_value(false);
BOOST_CHECK_EQUAL(true, true_constant.apply(cx)->get_value());
v = false_constant.apply(cx);
v->set_value(true);
BOOST_CHECK_EQUAL(false, false_constant.apply(cx)->get_value());
}
BOOST_AUTO_TEST_SUITE_END()
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