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 206 207 208 209 210 211 212 213 214 215 216
|
/*
* Modification History
*
* 2001-February-10 Jason Rohrer
* Created.
*
* 2001-September-4 Jason Rohrer
* Changed the destructor to virtual to fix a memory leak.
*
* 2001-September-9 Jason Rohrer
* Changed setArgument to be more intelligent about duplicated calls.
* Added an extractArgument() implemenation.
*/
#ifndef BINARY_OPERATION_EXPRESSION_INCLUDED
#define BINARY_OPERATION_EXPRESSION_INCLUDED
#include "Expression.h"
/**
* Abstract base for an binary operation expression object.
*
* @author Jason Rohrer
*/
class BinaryOperationExpression : public Expression {
public:
/**
* Constructs a binary operation expression.
*
* Both arguments are destroyed when the class is destroyed.
*
* @param inArgumentA the first argument. Defaults to NULL.
* @param inArgumentA the first argument. Defaults to NULL.
*/
BinaryOperationExpression( Expression *inArgumentA = NULL,
Expression *inArgumentB = NULL );
virtual ~BinaryOperationExpression();
/**
* Sets the first argument for the operation.
*
* @param inArgument the first argument. Is destroyed
* when the class is destroyed, or by another
* call to setArgumentA.
*/
void setArgumentA( Expression *inArgument );
/**
* Sets the second argument for the operation.
*
* @param inArgument the second argument. Is destroyed
* when the class is destroyed, or by another
* call to setArgumentB.
*/
void setArgumentB( Expression *inArgument );
/**
* Gets the first argument for the operation.
*
* @return the first argument. Is destroyed
* when the class is destroyed, or by another
* call to setArgumentB. Returns NULL if the
* argument was never set.
*/
Expression *getArgumentA();
/**
* Gets the second argument for the operation.
*
* @return the second argument. Is destroyed
* when the class is destroyed, or by another
* call to setArgumentB. Returns NULL if the
* argument was never set.
*/
Expression *getArgumentB();
// implements the Expression interface
virtual long getNumArguments();
virtual Expression *getArgument( long inArgumentNumber );
virtual void setArgument( long inArgumentNumber,
Expression *inArgument );
virtual Expression *extractArgument( long inArgumentNumber );
protected:
Expression *mArgumentA;
Expression *mArgumentB;
};
inline BinaryOperationExpression::BinaryOperationExpression(
Expression *inArgumentA, Expression *inArgumentB )
: mArgumentA( inArgumentA ), mArgumentB( inArgumentB ) {
}
inline BinaryOperationExpression::~BinaryOperationExpression() {
if( mArgumentA != NULL ) {
delete mArgumentA;
}
if( mArgumentB != NULL ) {
delete mArgumentB;
}
}
inline void BinaryOperationExpression::setArgumentA( Expression *inArgument ) {
if( mArgumentA != NULL && inArgument != mArgumentA ) {
delete mArgumentA;
}
mArgumentA = inArgument;
}
inline void BinaryOperationExpression::setArgumentB( Expression *inArgument ) {
if( mArgumentB != NULL && inArgument != mArgumentB ) {
delete mArgumentB;
}
mArgumentB = inArgument;
}
inline Expression *BinaryOperationExpression::getArgumentA() {
return mArgumentA;
}
inline Expression *BinaryOperationExpression::getArgumentB() {
return mArgumentB;
}
inline long BinaryOperationExpression::getNumArguments() {
return 2;
}
inline Expression *BinaryOperationExpression::getArgument(
long inArgumentNumber ) {
switch( inArgumentNumber ) {
case 0:
return getArgumentA();
break;
case 1:
return getArgumentB();
break;
default:
return NULL;
break;
}
}
inline void BinaryOperationExpression::setArgument( long inArgumentNumber,
Expression *inArgument ) {
switch( inArgumentNumber ) {
case 0:
setArgumentA( inArgument );
return;
break;
case 1:
setArgumentB( inArgument );
return;
break;
default:
return;
break;
}
}
inline Expression *BinaryOperationExpression::extractArgument(
long inArgumentNumber ) {
Expression *returnArg;
switch( inArgumentNumber ) {
case 0:
returnArg = mArgumentA;
mArgumentA = NULL;
return returnArg;
break;
case 1:
returnArg = mArgumentB;
mArgumentB = NULL;
return returnArg;
break;
default:
return NULL;
break;
}
}
#endif
|