MyraMath
NullaryFunctionExpression.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_EXPRESSION_NULLARYFUNCTIONEXPRESSION_H
7 #define MYRAMATH_EXPRESSION_NULLARYFUNCTIONEXPRESSION_H
8 
16 
18 
19 namespace myra {
20 namespace detail {
21 
22 // Implementation detail, an Expression that takes zero arguments.
23 template<class Function,int Arity> class NullaryFunctionExpression : public ExpressionBase<Arity,typename Function::Output>
24  {
25  public:
26 
27  // Reflect types from underlying Function.
28  typedef typename Function::Output Output;
29 
30  // Constructor, requires Function f and input Expression's (A,B,C).
31  NullaryFunctionExpression(const Function& in_f, Index<Arity> in_fsize)
32  : f(in_f), fsize(in_fsize) { }
33 
34  protected:
35 
36  // Size inspector.
37  virtual Index<Arity> size() const
38  { return fsize; }
39 
40  // Given an Index i, returns f()
41  virtual Output evaluate(Index<Arity> i) const
42  { return f.evaluate(); }
43 
44  // Virtual copy constructor.
45  virtual ExpressionBase<Arity,Output>* clone() const
46  { return new NullaryFunctionExpression<Function,Arity>(f,fsize); }
47 
48  private:
49 
50  // Underlying Function instance.
51  Function f;
52 
53  // Underlying size.
54  Index<Arity> fsize;
55  };
56 
57 // Helper factory function, returns f().
58 template<class Function, int Arity>
60 make_NullaryFunctionExpression(const Function& f, Index<Arity> size)
62 
63 } // namespace detail
64 
65 } // namespace myra
66 
67 #endif
Implementation detail, polymorphic base type contained/erased by Expression.
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
virtual Output evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: NullaryFunctionExpression.h:41
virtual Index< Arity > size() const
Size inspector.
Definition: NullaryFunctionExpression.h:37
Definition: Expression.h:25
Definition: syntax.dox:1
Definition: random.cpp:45
Definition: NullaryFunctionExpression.h:23
Returns std::string representation of an Index<N>