27 #ifndef FUNCTION_EXPRESSION_HPP_ 28 #define FUNCTION_EXPRESSION_HPP_ 30 #include "expression.hpp" 31 #include <boost/foreach.hpp> 33 #include <boost/mpl/begin.hpp> 34 #include <boost/mpl/end.hpp> 35 #include <boost/mpl/next.hpp> 36 #include <boost/mpl/deref.hpp> 38 namespace fusion = boost::fusion;
39 namespace mpl = boost::mpl;
40 namespace bft = boost::function_types;
44 typedef std::list<ExpressionVariant> ExpressionList;
46 template<
typename vtype>
49 typedef typename boost::shared_ptr<Expression<vtype> > VarExpressionPointer;
50 ExpressionVariant operator()(VarExpressionPointer e)
55 template<
class ExpressionPo
inter>
56 ExpressionVariant operator()(ExpressionPointer e)
58 typedef typename ExpressionPointer::element_type::ValueType vtype_orig;
64 template<
class vtype,
typename func>
68 typedef typename bft::result_type<func>::type rtype;
77 struct isConstantVisitor :
public boost::static_visitor<bool>
79 template<
class ExpressionPo
inter>
80 bool operator()(ExpressionPointer e) {
return e->
isConstant(); }
95 DependencyList getDependencies();
101 typename from =
typename mpl::begin< bft::parameter_types<func> >::type,
102 typename to =
typename mpl::end< bft::parameter_types<func> >::type
106 template<
class vtype,
typename func,
typename to>
109 typedef typename bft::result_type<func>::type rtype;
110 static void makeList(ExpressionList::iterator var, ExpressionList::iterator end, ExpressionList &args)
115 template<
typename ArgType>
116 static rtype evaluate(func f, ExpressionList::iterator var, ArgType
const &sArgs)
118 return fusion::invoke(f, sArgs);
122 template<
class vtype,
typename func,
typename from,
typename to>
125 typedef typename bft::result_type<func>::type rtype;
126 typedef typename mpl::deref<from>::type arg_type;
127 typedef typename mpl::next<from>::type next_type_iter;
129 static void makeList(ExpressionList::iterator var, ExpressionList::iterator end, ExpressionList &args)
134 args.push_back(boost::apply_visitor(visit, (*var)));
137 Con::makeList(var, end, args);
141 template<
typename ArgType>
142 static rtype evaluate(func f, ExpressionList::iterator var, ArgType
const &sArgs)
144 typedef boost::shared_ptr< Expression<arg_type> > pExprType;
145 pExprType expr = boost::get<pExprType>(*var);
152 template<
class vtype,
typename func>
154 : f(f_), updateAlways(updateAlways_)
159 template<
class vtype,
typename func>
165 template<
class vtype,
typename func>
168 if (updateAlways)
return false;
170 isConstantVisitor visit;
171 BOOST_FOREACH(ExpressionVariant ex, args)
173 result = result && boost::apply_visitor(visit, ex);
179 template<
class vtype,
typename func>
182 DependencyList result;
185 BOOST_FOREACH(ExpressionVariant ex, args)
187 DependencyList dep = boost::apply_visitor(visit, ex);
188 result.insert(dep.begin(), dep.end());
190 if (updateAlways) result.insert(-1);
201 virtual ExpressionVariant getExpression(ExpressionList &) = 0;
204 typedef boost::shared_ptr<EntryBase> pEntryBase;
206 template<
typename func>
207 class Entry :
public EntryBase
210 typedef typename bft::result_type<func>::type rtype;
216 Entry(func f_,
bool updateAlways_) : f(f_), updateAlways(updateAlways_) {}
218 ExpressionVariant getExpression(ExpressionList &args)
226 typedef std::map<std::string, pEntryBase> FExprMap;
227 boost::shared_ptr<FExprMap> funcs;
233 template<
typename func>
234 void registerFunction(std::string fname, func f,
bool updateAlways =
false)
236 pEntryBase eB(
new Entry<func>(f, updateAlways));
237 (*funcs)[fname] = eB;
240 ExpressionVariant getExpression(std::string fname, ExpressionList &args)
243 return (*funcs)[fname]->getExpression(args);
274 #endif // FUNCTION_EXPRESSION_HPP_ Definition: function_expression.hpp:47
Definition: expression.hpp:68
Definition: function_expression.hpp:104
Definition: function_expression.hpp:65
Definition: function_expression.hpp:194
Definition: expression.hpp:399
Definition: expression.hpp:91
Definition: expression.hpp:62
vtype eval()
Return the modified value.
Definition: function_expression.hpp:160
virtual bool isConstant()=0
Returns whether the expression can be evaluated, are all the references also constant.
bool isConstant()
Returns whether the expression can be evaluated, are all the references also constant.
Definition: function_expression.hpp:166
Definition: expression.hpp:431