Cafu Engine
Expression.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
8 #define CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
9 
10 #include <string>
11 #include "Templates/Array.hpp"
12 
13 
14 class TextParserT;
15 
16 
17 class TableT
18 {
19  public:
20 
21  struct RowT
22  {
23  bool RowSnap;
24  bool RowClamp;
25  ArrayT<float> Data;
26  };
27 
28  std::string Name;
29  bool ColumnSnap;
30  bool ColumnClamp;
31  ArrayT<RowT> Rows;
32 
33 
34  /// Constructor.
35  TableT() : ColumnSnap(false), ColumnClamp(false) { }
36 
37  /// Looks up the table value at x.
38  float Lookup(float x) const;
39 };
40 
41 
42 /// A typed expression class.
44 {
45  public:
46 
47  /// The type of the expression.
48  /// If bit 0x10000 is set, the type is the generic float variable whose index is obtained from the 16 lower (0x0FFFF) bits.
49  /// If bit 0x20000 is set, the type is the generic int variable whose index is obtained from the 16 lower (0x0FFFF) bits.
50  enum TypeT { FloatNumber, IntNumber, SymbolTime, SymbolALRed, SymbolALGreen, SymbolALBlue, TableLookup,
51  CastInt, Add, Sub, Mul, Div, GENERIC_FLOAT_START=0x10000, GENERIC_INT_START=0x20000 };
52 
53  /// A helper structure for returning both the type and the value of an expression.
54  struct ResultT
55  {
56  TypeT Type; ///< The type of the result, can only be FloatNumber or IntNumber.
57  float ValueFloat; ///< If Type==FloatNumber, this is the value of the result. Otherwise it is undefined.
58  int ValueInt; ///< If Type==IntNumber, this is the value of the result. Otherwise it is undefined.
59 
60  ResultT(float Value) : Type(FloatNumber), ValueFloat(Value), ValueInt(0 ) { } ///< Constructor.
61  ResultT(int Value) : Type(IntNumber ), ValueFloat(0.0 ), ValueInt(Value) { } ///< Constructor.
62 
63  float GetAsFloat() const { return Type==FloatNumber ? ValueFloat : ValueInt; }
64  int GetAsInt () const { return Type==FloatNumber ? int(ValueFloat) : ValueInt; }
65  };
66 
67  /// This structure contains the values for the "variables" of an expression.
68  /// An instance of it must be passed to the Evaluate() method.
69  struct SymbolsT
70  {
71  SymbolsT();
72 
73  float Time;
74  float AmbientLightColor[3];
75  ArrayT<float> GenFloat; ///< Generic / general purpose float variables/symbols.
76  ArrayT<int> GenInt; ///< Generic / general purpose int variables/symbols.
77  };
78 
79 
80  /// Constructor.
81  ExpressionT(float FloatNumberValue_=0.0);
82 
83  /// Constructor.
84  ExpressionT(int IntNumberValue_);
85 
86  /// This constructor creates an ExpressionT of a given type.
87  /// Note that only those types are meaningful for which no other parameter is required,
88  /// such as \c SymbolTime, \c SymbolALRed, \c SymbolALGreen and \c SymbolALBlue.
89  ExpressionT(TypeT Type_);
90 
91  /// Constructor.
92  ExpressionT(TextParserT& TP, const ArrayT<TableT*>& ListOfTables) /*throw (TextParserT::ParseError)*/;
93 
94  /// Copy Constructor (Law of the Big Three).
95  ExpressionT(const ExpressionT& Source);
96 
97  /// Destructor (Law of the Big Three).
98  ~ExpressionT();
99 
100  /// Assignment Operator (Law of the Big Three).
101  ExpressionT& operator = (const ExpressionT& Source);
102 
103  /// Returns a string description of this ExpressionT.
104  std::string GetString() const;
105 
106  /// Evaluates this expression and returns the result.
107  ResultT Evaluate(const SymbolsT& Symbols) const;
108 
109 
110  private:
111 
112  TypeT Type;
113  float FloatNumberValue;
114  int IntNumberValue;
115  TableT* Table;
116  ExpressionT* Child1;
117  ExpressionT* Child2;
118 
119  /// The equal operator is not defined.
120  bool operator == (const ExpressionT& rhs) const;
121 };
122 
123 #endif
ResultT(float Value)
Constructor.
Definition: Expression.hpp:60
~ExpressionT()
Destructor (Law of the Big Three).
Definition: Expression.cpp:255
TypeT Type
The type of the result, can only be FloatNumber or IntNumber.
Definition: Expression.hpp:56
int ValueInt
If Type==IntNumber, this is the value of the result. Otherwise it is undefined.
Definition: Expression.hpp:58
float Lookup(float x) const
Looks up the table value at x.
Definition: Expression.cpp:17
TypeT
The type of the expression.
Definition: Expression.hpp:50
ExpressionT(float FloatNumberValue_=0.0)
Constructor.
Definition: Expression.cpp:88
Definition: Expression.hpp:17
A helper structure for returning both the type and the value of an expression.
Definition: Expression.hpp:54
ResultT(int Value)
Constructor.
Definition: Expression.hpp:61
A typed expression class.
Definition: Expression.hpp:43
ExpressionT & operator=(const ExpressionT &Source)
Assignment Operator (Law of the Big Three).
Definition: Expression.cpp:264
float ValueFloat
If Type==FloatNumber, this is the value of the result. Otherwise it is undefined. ...
Definition: Expression.hpp:57
ArrayT< int > GenInt
Generic / general purpose int variables/symbols.
Definition: Expression.hpp:76
ArrayT< float > GenFloat
Generic / general purpose float variables/symbols.
Definition: Expression.hpp:75
This structure contains the values for the "variables" of an expression.
Definition: Expression.hpp:69
ResultT Evaluate(const SymbolsT &Symbols) const
Evaluates this expression and returns the result.
Definition: Expression.cpp:319
std::string GetString() const
Returns a string description of this ExpressionT.
Definition: Expression.cpp:283
Definition: Expression.hpp:21
TableT()
Constructor.
Definition: Expression.hpp:35
This is a class for parsing text.
Definition: TextParser.hpp:21