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.
7 #ifndef CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
8 #define CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
11 #include "Templates/Array.hpp"
35 TableT() : ColumnSnap(false), ColumnClamp(false) { }
37 /// Looks up the table value at x.
38 float Lookup(float x
) const;
42 /// A typed expression class.
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 };
53 /// A helper structure for returning both the type and the value of an expression.
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.
60 ResultT(float Value
) : Type(FloatNumber
), ValueFloat(Value
), ValueInt(0 ) { } ///< Constructor.
61 ResultT(int Value
) : Type(IntNumber
), ValueFloat(0.0 ), ValueInt(Value
) { } ///< Constructor.
63 float GetAsFloat() const { return Type
==FloatNumber
? ValueFloat
: ValueInt
; }
64 int GetAsInt () const { return Type
==FloatNumber
? int(ValueFloat
) : ValueInt
; }
67 /// This structure contains the values for the "variables" of an expression.
68 /// An instance of it must be passed to the Evaluate() method.
74 float AmbientLightColor
[3];
75 ArrayT
<float> GenFloat
; ///< Generic / general purpose float variables/symbols.
76 ArrayT
<int> GenInt
; ///< Generic / general purpose int variables/symbols.
81 ExpressionT(float FloatNumberValue_
=0.0);
84 ExpressionT(int IntNumberValue_
);
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_
);
92 ExpressionT(TextParserT
& TP
, const ArrayT
<TableT
*>& ListOfTables
) /*throw (TextParserT::ParseError)*/;
94 /// Copy Constructor (Law of the Big Three).
95 ExpressionT(const ExpressionT
& Source
);
97 /// Destructor (Law of the Big Three).
100 /// Assignment Operator (Law of the Big Three).
101 ExpressionT
& operator = (const ExpressionT
& Source
);
103 /// Returns a string description of this ExpressionT.
104 std::string
GetString() const;
106 /// Evaluates this expression and returns the result.
107 ResultT
Evaluate(const SymbolsT
& Symbols
) const;
113 float FloatNumberValue
;
119 /// The equal operator is not defined.
120 bool operator == (const ExpressionT
& rhs
) const;