BoolNativeInstruction.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/BoolNativeInstruction.h#5 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2004-2009 SQLstream, Inc.
00006 // Copyright (C) 2009-2009 LucidEra, Inc.
00007 //
00008 // This program is free software; you can redistribute it and/or modify it
00009 // under the terms of the GNU General Public License as published by the Free
00010 // Software Foundation; either version 2 of the License, or (at your option)
00011 // any later version approved by The Eigenbase Project.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 #ifndef Fennel_BoolNativeInstruction_Included
00023 #define Fennel_BoolNativeInstruction_Included
00024 
00025 #include "fennel/calculator/NativeInstruction.h"
00026 
00027 FENNEL_BEGIN_NAMESPACE
00028 
00034 template<typename TMPLT>
00035 class BoolNativeInstruction
00036     : public NativeInstruction<TMPLT>
00037 {
00038 public:
00039     explicit
00040     BoolNativeInstruction(
00041         RegisterRef<bool>* result,
00042         RegisterRef<TMPLT>* op1,
00043         StandardTypeDescriptorOrdinal nativeType)
00044         : NativeInstruction<TMPLT>(op1, nativeType),
00045           mResult(result)
00046     {}
00047     explicit
00048     BoolNativeInstruction(
00049         RegisterRef<bool>* result,
00050         RegisterRef<TMPLT>* op1,
00051         RegisterRef<TMPLT>* op2,
00052         StandardTypeDescriptorOrdinal nativeType)
00053         : NativeInstruction<TMPLT>(op1, op2, nativeType),
00054           mResult(result)
00055     {}
00056     virtual
00057     ~BoolNativeInstruction() {}
00058 
00059 protected:
00060     RegisterRef<bool>* mResult;
00061 };
00062 
00063 template <typename TMPLT>
00064 class BoolNativeEqual
00065     : public BoolNativeInstruction<TMPLT>
00066 {
00067 public:
00068     explicit
00069     BoolNativeEqual(
00070         RegisterRef<bool>* result,
00071         RegisterRef<TMPLT>* op1,
00072         RegisterRef<TMPLT>* op2,
00073         StandardTypeDescriptorOrdinal nativeType)
00074         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00075     {}
00076     virtual
00077     ~BoolNativeEqual() {}
00078 
00079     virtual void exec(TProgramCounter& pc) const {
00080         pc++;
00081         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00082             NativeInstruction<TMPLT>::mOp2->isNull()) {
00083             BoolNativeInstruction<TMPLT>::mResult->toNull();
00084         } else if (NativeInstruction<TMPLT>::mOp1->value() ==
00085                    NativeInstruction<TMPLT>::mOp2->value()) {
00086             BoolNativeInstruction<TMPLT>::mResult->value(true);
00087         } else {
00088             BoolNativeInstruction<TMPLT>::mResult->value(false);
00089         }
00090     }
00091 
00092     static const char * longName()
00093     {
00094         return "BoolNativeEqual";
00095     }
00096 
00097     static const char * shortName()
00098     {
00099         return "EQ";
00100     }
00101 
00102     static int numArgs()
00103     {
00104         return 3;
00105     }
00106 
00107     void describe(string& out, bool values) const {
00108         describeHelper(
00109             out, values, longName(), shortName(),
00110             BoolNativeInstruction<TMPLT>::mResult,
00111             NativeInstruction<TMPLT>::mOp1,
00112             NativeInstruction<TMPLT>::mOp2);
00113     }
00114 
00115     static InstructionSignature
00116     signature(StandardTypeDescriptorOrdinal type) {
00117         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00118         v[0] = STANDARD_TYPE_BOOL;
00119         return InstructionSignature(shortName(), v);
00120     }
00121 
00122     static Instruction*
00123     create(InstructionSignature const & sig)
00124     {
00125         assert(sig.size() == numArgs());
00126         return new
00127             BoolNativeEqual(
00128                 static_cast<RegisterRef<bool>*> (sig[0]),
00129                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00130                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00131                 (sig[1])->type());
00132     }
00133 };
00134 
00135 template <typename TMPLT>
00136 class BoolNativeNotEqual : public BoolNativeInstruction<TMPLT>
00137 {
00138 public:
00139     explicit
00140     BoolNativeNotEqual(
00141         RegisterRef<bool>* result,
00142         RegisterRef<TMPLT>* op1,
00143         RegisterRef<TMPLT>* op2,
00144         StandardTypeDescriptorOrdinal nativeType)
00145         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00146     {}
00147     virtual
00148     ~BoolNativeNotEqual() {}
00149 
00150     virtual void exec(TProgramCounter& pc) const {
00151         pc++;
00152         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00153             NativeInstruction<TMPLT>::mOp2->isNull()) {
00154             BoolNativeInstruction<TMPLT>::mResult->toNull();
00155         } else if (NativeInstruction<TMPLT>::mOp1->value() ==
00156                    NativeInstruction<TMPLT>::mOp2->value()) {
00157             BoolNativeInstruction<TMPLT>::mResult->value(false);
00158         } else {
00159             BoolNativeInstruction<TMPLT>::mResult->value(true);
00160         }
00161     }
00162 
00163     static const char * longName()
00164     {
00165         return "BoolNativeNotEqual";
00166     }
00167 
00168     static const char * shortName()
00169     {
00170         return "NE";
00171     }
00172 
00173     static int numArgs()
00174     {
00175         return 3;
00176     }
00177 
00178     void describe(string& out, bool values) const {
00179         describeHelper(
00180             out, values, longName(), shortName(),
00181             BoolNativeInstruction<TMPLT>::mResult,
00182             NativeInstruction<TMPLT>::mOp1,
00183             NativeInstruction<TMPLT>::mOp2);
00184     }
00185 
00186     static InstructionSignature
00187     signature(StandardTypeDescriptorOrdinal type) {
00188         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00189         v[0] = STANDARD_TYPE_BOOL;
00190         return InstructionSignature(shortName(), v);
00191     }
00192 
00193     static Instruction*
00194     create(InstructionSignature const & sig)
00195     {
00196         assert(sig.size() == numArgs());
00197         return new
00198             BoolNativeNotEqual(
00199                 static_cast<RegisterRef<bool>*> (sig[0]),
00200                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00201                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00202                 (sig[1])->type());
00203     }
00204 };
00205 
00206 template <typename TMPLT>
00207 class BoolNativeGreater : public BoolNativeInstruction<TMPLT>
00208 {
00209 public:
00210     explicit
00211     BoolNativeGreater(
00212         RegisterRef<bool>* result,
00213         RegisterRef<TMPLT>* op1,
00214         RegisterRef<TMPLT>* op2,
00215         StandardTypeDescriptorOrdinal nativeType)
00216         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00217     {}
00218     virtual
00219     ~BoolNativeGreater() {}
00220 
00221     virtual void exec(TProgramCounter& pc) const {
00222         pc++;
00223         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00224             NativeInstruction<TMPLT>::mOp2->isNull()) {
00225             BoolNativeInstruction<TMPLT>::mResult->toNull();
00226         } else if (NativeInstruction<TMPLT>::mOp1->value() >
00227                    NativeInstruction<TMPLT>::mOp2->value()) {
00228             BoolNativeInstruction<TMPLT>::mResult->value(true);
00229         } else {
00230             BoolNativeInstruction<TMPLT>::mResult->value(false);
00231         }
00232     }
00233 
00234     static const char * longName()
00235     {
00236         return "BoolNativeGreater";
00237     }
00238 
00239     static const char * shortName()
00240     {
00241         return "GT";
00242     }
00243 
00244     static int numArgs()
00245     {
00246         return 3;
00247     }
00248 
00249     void describe(string& out, bool values) const {
00250         describeHelper(
00251             out, values, longName(), shortName(),
00252             BoolNativeInstruction<TMPLT>::mResult,
00253             NativeInstruction<TMPLT>::mOp1,
00254             NativeInstruction<TMPLT>::mOp2);
00255     }
00256 
00257     static InstructionSignature
00258     signature(StandardTypeDescriptorOrdinal type) {
00259         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00260         v[0] = STANDARD_TYPE_BOOL;
00261         return InstructionSignature(shortName(), v);
00262     }
00263 
00264     static Instruction*
00265     create(InstructionSignature const & sig)
00266     {
00267         assert(sig.size() == numArgs());
00268         return new
00269             BoolNativeGreater(
00270                 static_cast<RegisterRef<bool>*> (sig[0]),
00271                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00272                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00273                 (sig[1])->type());
00274     }
00275 };
00276 
00277 template <typename TMPLT>
00278 class BoolNativeGreaterEqual : public BoolNativeInstruction<TMPLT>
00279 {
00280 public:
00281     explicit
00282     BoolNativeGreaterEqual(
00283         RegisterRef<bool>* result,
00284         RegisterRef<TMPLT>* op1,
00285         RegisterRef<TMPLT>* op2,
00286         StandardTypeDescriptorOrdinal nativeType)
00287         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00288     {}
00289     virtual
00290     ~BoolNativeGreaterEqual() {}
00291 
00292     virtual void exec(TProgramCounter& pc) const {
00293         pc++;
00294         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00295             NativeInstruction<TMPLT>::mOp2->isNull()) {
00296             BoolNativeInstruction<TMPLT>::mResult->toNull();
00297         } else if (NativeInstruction<TMPLT>::mOp1->value() >=
00298                    NativeInstruction<TMPLT>::mOp2->value()) {
00299             BoolNativeInstruction<TMPLT>::mResult->value(true);
00300         } else {
00301             BoolNativeInstruction<TMPLT>::mResult->value(false);
00302         }
00303     }
00304 
00305     static const char * longName()
00306     {
00307         return "BoolNativeGreaterEqual";
00308     }
00309 
00310     static const char * shortName()
00311     {
00312         return "GE";
00313     }
00314 
00315     static int numArgs()
00316     {
00317         return 3;
00318     }
00319 
00320     void describe(string& out, bool values) const {
00321         describeHelper(
00322             out, values, longName(), shortName(),
00323             BoolNativeInstruction<TMPLT>::mResult,
00324             NativeInstruction<TMPLT>::mOp1,
00325             NativeInstruction<TMPLT>::mOp2);
00326     }
00327 
00328     static InstructionSignature
00329     signature(StandardTypeDescriptorOrdinal type) {
00330         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00331         v[0] = STANDARD_TYPE_BOOL;
00332         return InstructionSignature(shortName(), v);
00333     }
00334 
00335     static Instruction*
00336     create(InstructionSignature const & sig)
00337     {
00338         assert(sig.size() == numArgs());
00339         return new
00340             BoolNativeGreaterEqual(
00341                 static_cast<RegisterRef<bool>*> (sig[0]),
00342                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00343                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00344                 (sig[1])->type());
00345     }
00346 };
00347 
00348 template <typename TMPLT>
00349 class BoolNativeLess : public BoolNativeInstruction<TMPLT>
00350 {
00351 public:
00352     explicit
00353     BoolNativeLess(
00354         RegisterRef<bool>* result,
00355         RegisterRef<TMPLT>* op1,
00356         RegisterRef<TMPLT>* op2,
00357         StandardTypeDescriptorOrdinal nativeType)
00358         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00359     {}
00360     virtual
00361     ~BoolNativeLess() {}
00362 
00363     virtual void exec(TProgramCounter& pc) const {
00364         pc++;
00365         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00366             NativeInstruction<TMPLT>::mOp2->isNull()) {
00367             BoolNativeInstruction<TMPLT>::mResult->toNull();
00368         } else if (NativeInstruction<TMPLT>::mOp1->value() <
00369                    NativeInstruction<TMPLT>::mOp2->value()) {
00370             BoolNativeInstruction<TMPLT>::mResult->value(true);
00371         } else {
00372             BoolNativeInstruction<TMPLT>::mResult->value(false);
00373         }
00374     }
00375 
00376     static const char * longName()
00377     {
00378         return "BoolNativeLess";
00379     }
00380 
00381     static const char * shortName()
00382     {
00383         return "LT";
00384     }
00385 
00386     static int numArgs()
00387     {
00388         return 3;
00389     }
00390 
00391     void describe(string& out, bool values) const {
00392         describeHelper(
00393             out, values, longName(), shortName(),
00394             BoolNativeInstruction<TMPLT>::mResult,
00395             NativeInstruction<TMPLT>::mOp1,
00396             NativeInstruction<TMPLT>::mOp2);
00397     }
00398 
00399     static InstructionSignature
00400     signature(StandardTypeDescriptorOrdinal type) {
00401         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00402         v[0] = STANDARD_TYPE_BOOL;
00403         return InstructionSignature(shortName(), v);
00404     }
00405 
00406     static Instruction*
00407     create(InstructionSignature const & sig)
00408     {
00409         assert(sig.size() == numArgs());
00410         return new
00411             BoolNativeLess(
00412                 static_cast<RegisterRef<bool>*> (sig[0]),
00413                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00414                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00415                 (sig[1])->type());
00416     }
00417 };
00418 
00419 template <typename TMPLT>
00420 class BoolNativeLessEqual : public BoolNativeInstruction<TMPLT>
00421 {
00422 public:
00423     explicit
00424     BoolNativeLessEqual(
00425         RegisterRef<bool>* result,
00426         RegisterRef<TMPLT>* op1,
00427         RegisterRef<TMPLT>* op2,
00428         StandardTypeDescriptorOrdinal nativeType)
00429         : BoolNativeInstruction<TMPLT>(result, op1, op2, nativeType)
00430     {}
00431     virtual
00432     ~BoolNativeLessEqual() {}
00433 
00434     virtual void exec(TProgramCounter& pc) const {
00435         pc++;
00436         if (NativeInstruction<TMPLT>::mOp1->isNull() ||
00437             NativeInstruction<TMPLT>::mOp2->isNull()) {
00438             BoolNativeInstruction<TMPLT>::mResult->toNull();
00439         } else if (NativeInstruction<TMPLT>::mOp1->value() <=
00440                    NativeInstruction<TMPLT>::mOp2->value()) {
00441             BoolNativeInstruction<TMPLT>::mResult->value(true);
00442         } else {
00443             BoolNativeInstruction<TMPLT>::mResult->value(false);
00444         }
00445     }
00446 
00447     static const char * longName()
00448     {
00449         return "BoolNativeLessEqual";
00450     }
00451 
00452     static const char * shortName()
00453     {
00454         return "LE";
00455     }
00456 
00457     static int numArgs()
00458     {
00459         return 3;
00460     }
00461 
00462     void describe(string& out, bool values) const {
00463         describeHelper(
00464             out, values, longName(), shortName(),
00465             BoolNativeInstruction<TMPLT>::mResult,
00466             NativeInstruction<TMPLT>::mOp1,
00467             NativeInstruction<TMPLT>::mOp2);
00468     }
00469 
00470     static InstructionSignature
00471     signature(StandardTypeDescriptorOrdinal type) {
00472         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00473         v[0] = STANDARD_TYPE_BOOL;
00474         return InstructionSignature(shortName(), v);
00475     }
00476 
00477     static Instruction*
00478     create(InstructionSignature const & sig)
00479     {
00480         assert(sig.size() == numArgs());
00481         return new
00482             BoolNativeLessEqual(
00483                 static_cast<RegisterRef<bool>*> (sig[0]),
00484                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00485                 static_cast<RegisterRef<TMPLT>*> (sig[2]),
00486                 (sig[1])->type());
00487     }
00488 };
00489 
00490 template <typename TMPLT>
00491 class BoolNativeIsNull : public BoolNativeInstruction<TMPLT>
00492 {
00493 public:
00494     explicit
00495     BoolNativeIsNull(
00496         RegisterRef<bool>* result,
00497         RegisterRef<TMPLT>* op1,
00498         StandardTypeDescriptorOrdinal nativeType)
00499         : BoolNativeInstruction<TMPLT>(result, op1, nativeType)
00500     {}
00501     virtual
00502     ~BoolNativeIsNull() {}
00503 
00504     virtual void exec(TProgramCounter& pc) const {
00505         pc++;
00506         if (NativeInstruction<TMPLT>::mOp1->isNull()) {
00507             BoolNativeInstruction<TMPLT>::mResult->value(true);
00508         } else {
00509             BoolNativeInstruction<TMPLT>::mResult->value(false);
00510         }
00511     }
00512 
00513     static const char * longName()
00514     {
00515         return "BoolNativeIsNull";
00516     }
00517 
00518     static const char * shortName()
00519     {
00520         return "ISNULL";
00521     }
00522 
00523     static int numArgs()
00524     {
00525         return 2;
00526     }
00527 
00528     void describe(string& out, bool values) const {
00529         describeHelper(
00530             out, values, longName(), shortName(),
00531             BoolNativeInstruction<TMPLT>::mResult,
00532             NativeInstruction<TMPLT>::mOp1,
00533             NativeInstruction<TMPLT>::mOp2);
00534     }
00535 
00536     static InstructionSignature
00537     signature(StandardTypeDescriptorOrdinal type) {
00538         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00539         v[0] = STANDARD_TYPE_BOOL;
00540         return InstructionSignature(shortName(), v);
00541     }
00542 
00543     static Instruction*
00544     create(InstructionSignature const & sig)
00545     {
00546         assert(sig.size() == numArgs());
00547         return new BoolNativeIsNull(
00548             static_cast<RegisterRef<bool>*> (sig[0]),
00549             static_cast<RegisterRef<TMPLT>*> (sig[1]),
00550             (sig[1])->type());
00551     }
00552 };
00553 
00554 template <typename TMPLT>
00555 class BoolNativeIsNotNull : public BoolNativeInstruction<TMPLT>
00556 {
00557 public:
00558     explicit
00559     BoolNativeIsNotNull(
00560         RegisterRef<bool>* result,
00561         RegisterRef<TMPLT>* op1,
00562         StandardTypeDescriptorOrdinal nativeType)
00563         : BoolNativeInstruction<TMPLT>(result, op1, nativeType)
00564     {}
00565     virtual
00566     ~BoolNativeIsNotNull() {}
00567 
00568     virtual void exec(TProgramCounter& pc) const {
00569         pc++;
00570         if (NativeInstruction<TMPLT>::mOp1->isNull()) {
00571             BoolNativeInstruction<TMPLT>::mResult->value(false);
00572         } else {
00573             BoolNativeInstruction<TMPLT>::mResult->value(true);
00574         }
00575     }
00576 
00577     static const char * longName()
00578     {
00579         return "BoolNativeIsNotNull";
00580     }
00581 
00582     static const char * shortName()
00583     {
00584         return "ISNOTNULL";
00585     }
00586 
00587     static int numArgs()
00588     {
00589         return 2;
00590     }
00591 
00592     void describe(string& out, bool values) const {
00593         describeHelper(
00594             out, values, longName(), shortName(),
00595             BoolNativeInstruction<TMPLT>::mResult,
00596             NativeInstruction<TMPLT>::mOp1,
00597             NativeInstruction<TMPLT>::mOp2);
00598     }
00599 
00600     static InstructionSignature
00601     signature(StandardTypeDescriptorOrdinal type) {
00602         vector<StandardTypeDescriptorOrdinal> v(numArgs(), type);
00603         v[0] = STANDARD_TYPE_BOOL;
00604         return InstructionSignature(shortName(), v);
00605     }
00606 
00607     static Instruction*
00608     create(InstructionSignature const & sig)
00609     {
00610         assert(sig.size() == numArgs());
00611         return new
00612             BoolNativeIsNotNull(
00613                 static_cast<RegisterRef<bool>*> (sig[0]),
00614                 static_cast<RegisterRef<TMPLT>*> (sig[1]),
00615                 (sig[1])->type());
00616     }
00617 };
00618 
00619 class FENNEL_CALCULATOR_EXPORT BoolNativeInstructionRegister
00620     : InstructionRegister {
00621 
00622     // TODO: Refactor registerTypes to class InstructionRegister
00623     template < template <typename> class INSTCLASS2 >
00624     static void
00625     registerTypes(vector<StandardTypeDescriptorOrdinal> const &t)
00626     {
00627         for (uint i = 0; i < t.size(); i++) {
00628             StandardTypeDescriptorOrdinal type = t[i];
00629             // Type <char> below is a placeholder and is ignored.
00630             InstructionSignature sig = INSTCLASS2<char>::signature(type);
00631             switch (type) {
00632 #define Fennel_InstructionRegisterSwitch_NativeNotBool 1
00633 #include "fennel/calculator/InstructionRegisterSwitch.h"
00634             default:
00635                 throw std::logic_error("Default InstructionRegister");
00636             }
00637         }
00638     }
00639 
00640 public:
00641     static void
00642     registerInstructions()
00643     {
00644         vector<StandardTypeDescriptorOrdinal> t;
00645         t = InstructionSignature::typeVector
00646             (StandardTypeDescriptor::isNativeNotBool);
00647 
00648         // Have to do full fennel:: qualification of template
00649         // arguments below to prevent template argument 'TMPLT', of
00650         // this encapsulating class, from perverting NativeAdd into
00651         // NativeAdd<TMPLT> or something like
00652         // that. Anyway. Fennel::NativeAdd works just fine.
00653         registerTypes<fennel::BoolNativeEqual>(t);
00654         registerTypes<fennel::BoolNativeNotEqual>(t);
00655         registerTypes<fennel::BoolNativeGreater>(t);
00656         registerTypes<fennel::BoolNativeGreaterEqual>(t);
00657         registerTypes<fennel::BoolNativeLess>(t);
00658         registerTypes<fennel::BoolNativeLessEqual>(t);
00659         registerTypes<fennel::BoolNativeIsNull>(t);
00660         registerTypes<fennel::BoolNativeIsNotNull>(t);
00661     }
00662 };
00663 
00664 FENNEL_END_NAMESPACE
00665 
00666 #endif
00667 
00668 // End BoolNativeInstruction.h
00669 

Generated on Mon Jun 22 04:00:16 2009 for Fennel by  doxygen 1.5.1