MyraMath
UserExpression.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_USEREXPRESSION_H
7 #define MYRAMATH_EXPRESSION_USEREXPRESSION_H
8 
17 
18 namespace myra {
19 namespace detail {
20 
21 // Implementation detail.
22 template<class UserClass> class UserExpression : public ExpressionBase<UserClass::Arity,typename UserClass::Number>
23  {
24  public:
25 
26  // Reflect the Arity and Number type, both must be present in the user's class.
27  const static int Arity = UserClass::Arity;
28  typedef typename UserClass::Number Number;
29 
30  // Constructor, requires a copy constructible UserClass.
31  UserExpression(const UserClass& in_u)
32  : u(in_u) { }
33 
34  protected:
35 
37  virtual Index<Arity> size() const
38  { return u.size(); }
39 
41  virtual Number evaluate(Index<Arity> i) const
42  { return u.evaluate(i); }
43 
44  // Virtual copy constructor.
45  virtual ExpressionBase<Arity,Number>* clone() const
46  { return new UserExpression<UserClass>(u); }
47 
48  private:
49 
50  // Deep copy of user's class.
51  UserClass u;
52 
53  };
54 
55 } // namespace detail
56 
60 
61 } // namespace myra
62 
63 #endif
Expression< UserClass::Arity, typename UserClass::Number > make_UserExpression(const UserClass &u)
Helper function to adapt some user code (encapsulated in a class) into an Expression.
Definition: UserExpression.h:58
Implementation detail, polymorphic base type contained/erased by Expression.
An interface used to fill containers from Expression&#39;s (see Matrix::evaluate(), for example)...
Definition: Expression.h:25
Definition: syntax.dox:1
virtual Index< Arity > size() const
Size inspector.
Definition: UserExpression.h:37
Definition: random.cpp:45
Various utility functions/classes related to scalar Number types.
virtual Number evaluate(Index< Arity > i) const
Given an Index i, returns a Number.
Definition: UserExpression.h:41
Given an index (i,j,etc), returns a value.
Definition: arithmetic.h:19
Definition: UserExpression.h:22