1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Modification History
*
* 2003-May-2 Jason Rohrer
* Created.
*
* 2003-May-3 Jason Rohrer
* Fixed a few compile-time errors.
*/
#ifndef BINARY_LOGIC_EXPRESSION_INCLUDED
#define BINARY_LOGIC_EXPRESSION_INCLUDED
#include "Expression.h"
#include "BinaryOperationExpression.h"
/**
* Expression implementation of a binary logic operation.
*
* Evaluating this expression returns 1 if the logic operation is true
* and 0 if it is false.
*
* During evaluation, positive values are treated as logical true,
* while all other values (zero and negative) are treated as false.
*
* @author Jason Rohrer
*/
class BinaryLogicExpression : public BinaryOperationExpression {
public:
static const int LOGIC_AND = 0;
static const int LOGIC_OR = 1;
static const int LOGIC_XOR = 2;
/**
* Constructs a binary logic operation expression.
*
* Both arguments are destroyed when the class is destroyed.
*
* @param inLogicOperation the logical operation, one of
* LOGIC_AND, LOGIC_OR, LOGIC_XOR.
* @param inArgumentA the first argument.
* @param inArgumentB the second argument.
*/
BinaryLogicExpression( int inLogicOperation,
Expression *inArgumentA,
Expression *inArgumentB );
/**
* Gets the logical operation.
*
* @return one of LOGIC_AND, LOGIC_OR, LOGIC_XOR.
*/
int getLogicOperation();
/**
* Sets the logical operation
*
* @param inLogicOperation LOGIC_AND, LOGIC_OR, LOGIC_XOR.
*/
void setLogicOperation( int inLogicOperation );
/**
* A static version of getID().
*/
static long staticGetID();
// implements the Expression interface
virtual double evaluate();
virtual long getID();
virtual void print();
virtual Expression *copy();
protected:
static long mID;
int mLogicOperation;
};
// static init
long BinaryLogicExpression::mID = 16;
inline BinaryLogicExpression::BinaryLogicExpression(
int inLogicOperation,
Expression *inArgumentA,
Expression *inArgumentB )
: BinaryOperationExpression( inArgumentA, inArgumentB ),
mLogicOperation( inLogicOperation ) {
}
inline int BinaryLogicExpression::getLogicOperation() {
return mLogicOperation;
}
inline void BinaryLogicExpression::setLogicOperation( int inLogicOperation ) {
mLogicOperation = inLogicOperation;
}
inline double BinaryLogicExpression::evaluate() {
if( mLogicOperation == LOGIC_AND ) {
if( mArgumentA->evaluate() > 0 && mArgumentB->evaluate() > 0 ) {
return 1;
}
else {
return 0;
}
}
else if( mLogicOperation == LOGIC_OR ) {
if( mArgumentA->evaluate() > 0 || mArgumentB->evaluate() > 0 ) {
return 1;
}
else {
return 0;
}
}
else if( mLogicOperation == LOGIC_XOR ) {
double valA = mArgumentA->evaluate();
double valB = mArgumentB->evaluate();
if( ( valA > 0 && valB <= 0 ) || ( valA <= 0 && valB > 0 ) ) {
return 1;
}
else {
return 0;
}
}
else {
// unknown operation type
// default to false
return 0;
}
}
inline long BinaryLogicExpression::getID() {
return mID;
}
inline long BinaryLogicExpression::staticGetID() {
return mID;
}
inline void BinaryLogicExpression::print() {
printf( "( " );
mArgumentA->print();
if( mLogicOperation == LOGIC_AND ) {
printf( " and " );
}
else if( mLogicOperation == LOGIC_OR ) {
printf( " or " );
}
else if( mLogicOperation == LOGIC_XOR ) {
printf( " xor " );
}
else {
printf( " UNKNOWN_LOGIC_OPERATION " );
}
mArgumentB->print();
printf( " )" );
}
inline Expression *BinaryLogicExpression::copy() {
BinaryLogicExpression *copy =
new BinaryLogicExpression( mLogicOperation,
mArgumentA->copy(),
mArgumentB->copy() );
return copy;
}
#endif
|