Instruction.h

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/calculator/Instruction.h#3 $
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 // Instruction.h
00023 // Include this file if you intend to manipulate instructions directly,
00024 // otherwise include Calculator.h
00025 //
00026 */
00027 #ifndef Fennel_Instruction_Included
00028 #define Fennel_Instruction_Included
00029 
00030 #include <string>
00031 #include "fennel/calculator/InstructionSignature.h"
00032 #include "fennel/calculator/RegisterReference.h"
00033 #include "fennel/tuple/StandardTypeDescriptor.h"
00034 #include "fennel/calculator/InstructionFactory.h"
00035 #include "fennel/calculator/CalcMessage.h"
00036 
00037 FENNEL_BEGIN_NAMESPACE
00038 
00039 using namespace std;
00040 
00041 
00042 // Instruction
00043 // Not a pure abstract base class, but nearly so.
00044 class FENNEL_CALCULATOR_EXPORT Instruction
00045 {
00046 public:
00047     explicit
00048     Instruction () {}
00049 
00050     virtual
00051     ~Instruction() {}
00052 
00053     virtual void describe(string& out, bool values) const = 0;
00054 
00055     // Subclasses must also implement a create() call described by
00056     // typedef InstructionCreateFunction. Since this member function
00057     // must be static (as the object doesn't exist to create it) it
00058     // cannot also be virtual, due to a limitations of C++ that
00059     // prevents a "static virtual". Consider this a virtual func() =
00060     // 0; in the sense that it requires this function to be
00061     // implemented by each derived concrete class.
00062 
00063 protected:
00064     friend class fennel::Calculator;
00065 
00066     virtual void exec(long &pc) const = 0;
00067 
00068     void describeHelper(
00069         string &out,
00070         bool values,
00071         const char* longName,
00072         const char* shortName,
00073         RegisterReference* result,
00074         RegisterReference* op1,
00075         RegisterReference* op2) const
00076     {
00077         out = longName;
00078         out += ": ";
00079         out += result->toString();
00080         if (values) {
00081             out += " ( ";
00082             if (result->isNull()) {
00083                 out += "NULL";
00084             } else {
00085                 out += result->valueToString();
00086             }
00087             out += " ) ";
00088         }
00089         out += " = ";
00090 
00091         if (!op2 || !op2->isValid()) {  // if < 2 operands
00092             out += " ";
00093             out += shortName;
00094             out += " ";
00095         }
00096         if (op1 && op1->isValid()) {  // 1 or 2 operands
00097             out += op1->toString();
00098             if (values) {
00099                 out += " ( ";
00100                 if (op1->isNull()) {
00101                     out += "NULL";
00102                 } else {
00103                     out += op1->valueToString();
00104                 }
00105                 out += " ) ";
00106             }
00107         }
00108         if (op2 && op2->isValid()) {  // 2 operands
00109             out += " ";
00110             out += shortName;
00111             out += " ";
00112             out += op2->toString();
00113             if (values) {
00114                 out += " ( ";
00115                 if (op2->isNull()) {
00116                     out += "NULL";
00117                 } else {
00118                     out += op2->valueToString();
00119                 }
00120                 out += " ) ";
00121             }
00122         }
00123     }
00124 };
00125 
00126 
00132 
00133 class FENNEL_CALCULATOR_EXPORT InstructionRegister
00134 {
00135 protected:
00136     template < typename TYPE1,
00137                template <typename> class INSTCLASS >
00138     static void
00139     registerInstance(StandardTypeDescriptorOrdinal type)
00140     {
00141         StringToCreateFn* instMap = InstructionFactory::getInstructionTable();
00142         (*instMap)[INSTCLASS<TYPE1>::signature(type).compute()] =
00143             &INSTCLASS<TYPE1>::create;
00144     }
00145 
00146     template < typename TYPE1,
00147                typename TYPE2,
00148                template <typename, typename> class INSTCLASS >
00149     static void
00150     registerInstance2(
00151         StandardTypeDescriptorOrdinal type1,
00152         StandardTypeDescriptorOrdinal type2)
00153     {
00154         StringToCreateFn* instMap = InstructionFactory::getInstructionTable();
00155         (*instMap)[INSTCLASS<TYPE1,TYPE2>::signature(type1, type2).compute()] =
00156             &INSTCLASS<TYPE1,TYPE2>::create;
00157     }
00158 
00159     template < typename IGNOREDDATATYPE, class INSTCLASS >
00160     static void
00161     registerInstance(StandardTypeDescriptorOrdinal type)
00162     {
00163         StringToCreateFn* instMap = InstructionFactory::getInstructionTable();
00164         (*instMap)[INSTCLASS::signature(type).compute()] =
00165             &INSTCLASS::create;
00166     }
00167 };
00168 
00169 
00170 FENNEL_END_NAMESPACE
00171 
00172 #endif
00173 
00174 // End Instruction.h
00175 

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