27 #ifndef FUNCTION_EXPRESSION_HPP_ 28 #define FUNCTION_EXPRESSION_HPP_ 30 #include "expression.hpp" 32 #include <boost/mpl/begin.hpp> 33 #include <boost/mpl/end.hpp> 34 #include <boost/mpl/next.hpp> 35 #include <boost/mpl/deref.hpp> 37 namespace fusion = boost::fusion;
38 namespace mpl = boost::mpl;
39 namespace bft = boost::function_types;
43 typedef std::list<ExpressionVariant> ExpressionList;
45 template<
typename vtype>
48 typedef typename std::shared_ptr<Expression<vtype> > VarExpressionPointer;
49 ExpressionVariant operator()(VarExpressionPointer e)
54 template<
class ExpressionPo
inter>
55 ExpressionVariant operator()(ExpressionPointer e)
57 typedef typename ExpressionPointer::element_type::ValueType vtype_orig;
63 template<
class vtype,
typename func>
67 typedef typename bft::result_type<func>::type rtype;
76 struct isConstantVisitor :
public boost::static_visitor<bool>
78 template<
class ExpressionPo
inter>
79 bool operator()(ExpressionPointer e) {
return e->
isConstant(); }
94 DependencyList getDependencies();
100 typename from =
typename mpl::begin< bft::parameter_types<func> >::type,
101 typename to =
typename mpl::end< bft::parameter_types<func> >::type
105 template<
class vtype,
typename func,
typename to>
108 typedef typename bft::result_type<func>::type rtype;
109 static void makeList(ExpressionList::iterator var, ExpressionList::iterator end, ExpressionList &)
114 template<
typename ArgType>
115 static rtype evaluate(func f, ExpressionList::iterator, ArgType
const &sArgs)
117 return fusion::invoke(f, sArgs);
121 template<
class vtype,
typename func,
typename from,
typename to>
124 typedef typename bft::result_type<func>::type rtype;
125 typedef typename mpl::deref<from>::type arg_type;
126 typedef typename mpl::next<from>::type next_type_iter;
128 static void makeList(ExpressionList::iterator var, ExpressionList::iterator end, ExpressionList &args)
133 args.push_back(boost::apply_visitor(visit, (*var)));
136 Con::makeList(var, end, args);
140 template<
typename ArgType>
141 static rtype evaluate(func f, ExpressionList::iterator var, ArgType
const &sArgs)
143 typedef std::shared_ptr< Expression<arg_type> > pExprType;
144 pExprType expr = boost::get<pExprType>(*var);
151 template<
class vtype,
typename func>
153 : f(f_), updateAlways(updateAlways_)
158 template<
class vtype,
typename func>
164 template<
class vtype,
typename func>
167 if (updateAlways)
return false;
169 isConstantVisitor visit;
170 for (ExpressionVariant ex: args)
172 result = result && boost::apply_visitor(visit, ex);
178 template<
class vtype,
typename func>
181 DependencyList result;
184 for (ExpressionVariant ex: args)
186 DependencyList dep = boost::apply_visitor(visit, ex);
187 result.insert(dep.begin(), dep.end());
189 if (updateAlways) result.insert(-1);
200 virtual ~EntryBase() {}
201 virtual ExpressionVariant getExpression(ExpressionList &) = 0;
204 typedef std::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 std::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:46
Definition: expression.hpp:73
Definition: function_expression.hpp:103
Definition: function_expression.hpp:64
Definition: function_expression.hpp:193
Definition: expression.hpp:404
Definition: expression.hpp:96
Definition: expression.hpp:67
vtype eval()
Return the modified value.
Definition: function_expression.hpp:159
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:165
Definition: expression.hpp:436