1/* 2 * Copyright 2014, Rene Gollent, rene@gollent.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7#include "ExpressionInfo.h" 8 9#include "Type.h" 10#include "Value.h" 11#include "ValueNode.h" 12 13 14// #pragma mark - ExpressionResult 15 16 17ExpressionResult::ExpressionResult() 18 : 19 fResultKind(EXPRESSION_RESULT_KIND_UNKNOWN), 20 fPrimitiveValue(NULL), 21 fValueNodeValue(NULL), 22 fTypeResult(NULL) 23{ 24} 25 26 27ExpressionResult::~ExpressionResult() 28{ 29 if (fPrimitiveValue != NULL) 30 fPrimitiveValue->ReleaseReference(); 31 32 if (fValueNodeValue != NULL) 33 fValueNodeValue->ReleaseReference(); 34 35 if (fTypeResult != NULL) 36 fTypeResult->ReleaseReference(); 37} 38 39 40void 41ExpressionResult::SetToPrimitive(Value* value) 42{ 43 _Unset(); 44 45 fPrimitiveValue = value; 46 if (fPrimitiveValue != NULL) { 47 fPrimitiveValue->AcquireReference(); 48 fResultKind = EXPRESSION_RESULT_KIND_PRIMITIVE; 49 } 50} 51 52 53void 54ExpressionResult::SetToValueNode(ValueNodeChild* child) 55{ 56 _Unset(); 57 58 fValueNodeValue = child; 59 if (fValueNodeValue != NULL) { 60 fValueNodeValue->AcquireReference(); 61 fResultKind = EXPRESSION_RESULT_KIND_VALUE_NODE; 62 } 63 64 // if the child has a node with a resolved value, store 65 // it as a primitive, so the consumer of the expression 66 // can use it as-is if desired. 67 68 ValueNode* node = child->Node(); 69 if (node == NULL) 70 return; 71 72 fPrimitiveValue = node->GetValue(); 73 if (fPrimitiveValue != NULL) 74 fPrimitiveValue->AcquireReference(); 75} 76 77 78void 79ExpressionResult::SetToType(Type* type) 80{ 81 _Unset(); 82 83 fTypeResult = type; 84 if (fTypeResult != NULL) { 85 fTypeResult->AcquireReference(); 86 fResultKind = EXPRESSION_RESULT_KIND_TYPE; 87 } 88} 89 90 91void 92ExpressionResult::_Unset() 93{ 94 if (fPrimitiveValue != NULL) { 95 fPrimitiveValue->ReleaseReference(); 96 fPrimitiveValue = NULL; 97 } 98 99 if (fValueNodeValue != NULL) { 100 fValueNodeValue->ReleaseReference(); 101 fValueNodeValue = NULL; 102 } 103 104 if (fTypeResult != NULL) { 105 fTypeResult->ReleaseReference(); 106 fTypeResult = NULL; 107 } 108 109 fResultKind = EXPRESSION_RESULT_KIND_UNKNOWN; 110} 111 112 113// #pragma mark - ExpressionInfo 114 115 116ExpressionInfo::ExpressionInfo() 117 : 118 fExpression() 119{ 120} 121 122 123ExpressionInfo::ExpressionInfo(const ExpressionInfo& other) 124 : 125 fExpression(other.fExpression) 126{ 127} 128 129 130ExpressionInfo::~ExpressionInfo() 131{ 132} 133 134 135ExpressionInfo::ExpressionInfo(const BString& expression) 136 : 137 fExpression(expression) 138{ 139} 140 141 142void 143ExpressionInfo::SetTo(const BString& expression) 144{ 145 fExpression = expression; 146} 147 148 149void 150ExpressionInfo::AddListener(Listener* listener) 151{ 152 fListeners.Add(listener); 153} 154 155 156void 157ExpressionInfo::RemoveListener(Listener* listener) 158{ 159 fListeners.Remove(listener); 160} 161 162 163void 164ExpressionInfo::NotifyExpressionEvaluated(status_t result, 165 ExpressionResult* value) 166{ 167 for (ListenerList::Iterator it = fListeners.GetIterator(); 168 Listener* listener = it.Next();) { 169 listener->ExpressionEvaluated(this, result, value); 170 } 171} 172 173 174// #pragma mark - ExpressionInfo::Listener 175 176 177ExpressionInfo::Listener::~Listener() 178{ 179} 180