daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #ifndef _CONSTANT_UNION_INCLUDED_ |
| 8 | #define _CONSTANT_UNION_INCLUDED_ |
| 9 | |
| 10 | |
| 11 | class constUnion { |
| 12 | public: |
| 13 | |
| 14 | POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) |
| 15 | void setIConst(int i) {iConst = i; type = EbtInt; } |
| 16 | void setFConst(float f) {fConst = f; type = EbtFloat; } |
| 17 | void setBConst(bool b) {bConst = b; type = EbtBool; } |
| 18 | |
| 19 | int getIConst() { return iConst; } |
| 20 | float getFConst() { return fConst; } |
| 21 | bool getBConst() { return bConst; } |
| 22 | int getIConst() const { return iConst; } |
| 23 | float getFConst() const { return fConst; } |
| 24 | bool getBConst() const { return bConst; } |
| 25 | |
| 26 | bool operator==(const int i) const |
| 27 | { |
| 28 | if (i == iConst) |
| 29 | return true; |
| 30 | |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | bool operator==(const float f) const |
| 35 | { |
| 36 | if (f == fConst) |
| 37 | return true; |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | bool operator==(const bool b) const |
| 43 | { |
| 44 | if (b == bConst) |
| 45 | return true; |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | bool operator==(const constUnion& constant) const |
| 51 | { |
| 52 | if (constant.type != type) |
| 53 | return false; |
| 54 | |
| 55 | switch (type) { |
| 56 | case EbtInt: |
| 57 | if (constant.iConst == iConst) |
| 58 | return true; |
| 59 | |
| 60 | break; |
| 61 | case EbtFloat: |
| 62 | if (constant.fConst == fConst) |
| 63 | return true; |
| 64 | |
| 65 | break; |
| 66 | case EbtBool: |
| 67 | if (constant.bConst == bConst) |
| 68 | return true; |
| 69 | |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool operator!=(const int i) const |
| 77 | { |
| 78 | return !operator==(i); |
| 79 | } |
| 80 | |
| 81 | bool operator!=(const float f) const |
| 82 | { |
| 83 | return !operator==(f); |
| 84 | } |
| 85 | |
| 86 | bool operator!=(const bool b) const |
| 87 | { |
| 88 | return !operator==(b); |
| 89 | } |
| 90 | |
| 91 | bool operator!=(const constUnion& constant) const |
| 92 | { |
| 93 | return !operator==(constant); |
| 94 | } |
| 95 | |
| 96 | bool operator>(const constUnion& constant) const |
| 97 | { |
| 98 | assert(type == constant.type); |
| 99 | switch (type) { |
| 100 | case EbtInt: |
| 101 | if (iConst > constant.iConst) |
| 102 | return true; |
| 103 | |
| 104 | return false; |
| 105 | case EbtFloat: |
| 106 | if (fConst > constant.fConst) |
| 107 | return true; |
| 108 | |
| 109 | return false; |
| 110 | default: |
| 111 | assert(false && "Default missing"); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | bool operator<(const constUnion& constant) const |
| 119 | { |
| 120 | assert(type == constant.type); |
| 121 | switch (type) { |
| 122 | case EbtInt: |
| 123 | if (iConst < constant.iConst) |
| 124 | return true; |
| 125 | |
| 126 | return false; |
| 127 | case EbtFloat: |
| 128 | if (fConst < constant.fConst) |
| 129 | return true; |
| 130 | |
| 131 | return false; |
| 132 | default: |
| 133 | assert(false && "Default missing"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | constUnion operator+(const constUnion& constant) const |
| 141 | { |
| 142 | constUnion returnValue; |
| 143 | assert(type == constant.type); |
| 144 | switch (type) { |
| 145 | case EbtInt: returnValue.setIConst(iConst + constant.iConst); break; |
| 146 | case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break; |
| 147 | default: assert(false && "Default missing"); |
| 148 | } |
| 149 | |
| 150 | return returnValue; |
| 151 | } |
| 152 | |
| 153 | constUnion operator-(const constUnion& constant) const |
| 154 | { |
| 155 | constUnion returnValue; |
| 156 | assert(type == constant.type); |
| 157 | switch (type) { |
| 158 | case EbtInt: returnValue.setIConst(iConst - constant.iConst); break; |
| 159 | case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break; |
| 160 | default: assert(false && "Default missing"); |
| 161 | } |
| 162 | |
| 163 | return returnValue; |
| 164 | } |
| 165 | |
| 166 | constUnion operator*(const constUnion& constant) const |
| 167 | { |
| 168 | constUnion returnValue; |
| 169 | assert(type == constant.type); |
| 170 | switch (type) { |
| 171 | case EbtInt: returnValue.setIConst(iConst * constant.iConst); break; |
| 172 | case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break; |
| 173 | default: assert(false && "Default missing"); |
| 174 | } |
| 175 | |
| 176 | return returnValue; |
| 177 | } |
| 178 | |
| 179 | constUnion operator%(const constUnion& constant) const |
| 180 | { |
| 181 | constUnion returnValue; |
| 182 | assert(type == constant.type); |
| 183 | switch (type) { |
| 184 | case EbtInt: returnValue.setIConst(iConst % constant.iConst); break; |
| 185 | default: assert(false && "Default missing"); |
| 186 | } |
| 187 | |
| 188 | return returnValue; |
| 189 | } |
| 190 | |
| 191 | constUnion operator>>(const constUnion& constant) const |
| 192 | { |
| 193 | constUnion returnValue; |
| 194 | assert(type == constant.type); |
| 195 | switch (type) { |
| 196 | case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break; |
| 197 | default: assert(false && "Default missing"); |
| 198 | } |
| 199 | |
| 200 | return returnValue; |
| 201 | } |
| 202 | |
| 203 | constUnion operator<<(const constUnion& constant) const |
| 204 | { |
| 205 | constUnion returnValue; |
| 206 | assert(type == constant.type); |
| 207 | switch (type) { |
| 208 | case EbtInt: returnValue.setIConst(iConst << constant.iConst); break; |
| 209 | default: assert(false && "Default missing"); |
| 210 | } |
| 211 | |
| 212 | return returnValue; |
| 213 | } |
| 214 | |
| 215 | constUnion operator&(const constUnion& constant) const |
| 216 | { |
| 217 | constUnion returnValue; |
| 218 | assert(type == constant.type); |
| 219 | switch (type) { |
| 220 | case EbtInt: returnValue.setIConst(iConst & constant.iConst); break; |
| 221 | default: assert(false && "Default missing"); |
| 222 | } |
| 223 | |
| 224 | return returnValue; |
| 225 | } |
| 226 | |
| 227 | constUnion operator|(const constUnion& constant) const |
| 228 | { |
| 229 | constUnion returnValue; |
| 230 | assert(type == constant.type); |
| 231 | switch (type) { |
| 232 | case EbtInt: returnValue.setIConst(iConst | constant.iConst); break; |
| 233 | default: assert(false && "Default missing"); |
| 234 | } |
| 235 | |
| 236 | return returnValue; |
| 237 | } |
| 238 | |
| 239 | constUnion operator^(const constUnion& constant) const |
| 240 | { |
| 241 | constUnion returnValue; |
| 242 | assert(type == constant.type); |
| 243 | switch (type) { |
| 244 | case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break; |
| 245 | default: assert(false && "Default missing"); |
| 246 | } |
| 247 | |
| 248 | return returnValue; |
| 249 | } |
| 250 | |
| 251 | constUnion operator&&(const constUnion& constant) const |
| 252 | { |
| 253 | constUnion returnValue; |
| 254 | assert(type == constant.type); |
| 255 | switch (type) { |
| 256 | case EbtBool: returnValue.setBConst(bConst && constant.bConst); break; |
| 257 | default: assert(false && "Default missing"); |
| 258 | } |
| 259 | |
| 260 | return returnValue; |
| 261 | } |
| 262 | |
| 263 | constUnion operator||(const constUnion& constant) const |
| 264 | { |
| 265 | constUnion returnValue; |
| 266 | assert(type == constant.type); |
| 267 | switch (type) { |
| 268 | case EbtBool: returnValue.setBConst(bConst || constant.bConst); break; |
| 269 | default: assert(false && "Default missing"); |
| 270 | } |
| 271 | |
| 272 | return returnValue; |
| 273 | } |
| 274 | |
| 275 | TBasicType getType() { return type; } |
| 276 | private: |
| 277 | |
| 278 | union { |
| 279 | int iConst; // used for ivec, scalar ints |
| 280 | bool bConst; // used for bvec, scalar bools |
| 281 | float fConst; // used for vec, mat, scalar floats |
| 282 | } ; |
| 283 | |
| 284 | TBasicType type; |
| 285 | }; |
| 286 | |
| 287 | #endif // _CONSTANT_UNION_INCLUDED_ |