fnCall.h

00001 /***************************************************************
00002  *
00003  * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
00004  * University of Wisconsin-Madison, WI.
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License"); you
00007  * may not use this file except in compliance with the License.  You may
00008  * obtain a copy of the License at
00009  * 
00010  *    http://www.apache.org/licenses/LICENSE-2.0
00011  * 
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  *
00018  ***************************************************************/
00019 
00020 
00021 #ifndef __CLASSAD_FN_CALL_H__
00022 #define __CLASSAD_FN_CALL_H__
00023 
00024 #include <map>
00025 #include <vector>
00026 #include "classad/classad.h"
00027 
00028 BEGIN_NAMESPACE( classad )
00029 
00030 typedef std::vector<ExprTree*> ArgumentList;
00031 
00032 typedef struct 
00033 {
00034     std::string       functionName;
00035 
00036     // The function should be a ClassAdFunc. Because we use this structure
00037     // as part of the interface with a shared library that uses an extern
00038     // C interface, we keep it as a void*, unfortunately.
00039     // The onus is on you to make sure that it's really a ClassAdFunc!
00040     void         *function;
00041     
00042     // We are likely to add some flags here in the future.  These
00043     // flags will describe things like how to cache results of the
00044     // function. Currently they are unused, so set it to zero. 
00045     int          flags;
00046 } ClassAdFunctionMapping;
00047 
00048 // If you create a shared library, you have to provide a function
00049 // named "Initialize" that conforms to this prototype. You will return
00050 // a table of function mappings.  The last mapping in the table should
00051 // have a NULL function pointer. It will be ignored.
00052 typedef  ClassAdFunctionMapping *(*ClassAdSharedLibraryInit)(void);
00053 
00055 class FunctionCall : public ExprTree
00056 {
00057  public:
00058     typedef bool(*ClassAdFunc)(const char*, const ArgumentList&, EvalState&,
00059                                Value&);
00060 
00062     FunctionCall(FunctionCall &functioncall);
00063 
00065     virtual ~FunctionCall ();
00066     
00067     FunctionCall & operator=(FunctionCall &functioncall);
00068 
00074     static FunctionCall *MakeFunctionCall( const std::string &fnName, 
00075                                            std::vector<ExprTree*> &argList );
00076     
00081     void GetComponents( std::string &, std::vector<ExprTree*> &) const;
00082     
00084     virtual ExprTree* Copy( ) const;
00085     
00086     bool CopyFrom(const FunctionCall &functioncall);
00087 
00088     virtual bool SameAs(const ExprTree *tree) const;
00089 
00090     friend bool operator==(const FunctionCall &fn1, const FunctionCall &fn2);
00091 
00092     static void RegisterFunction(std::string &functionName, ClassAdFunc function);
00093     static void RegisterFunctions(ClassAdFunctionMapping *functions);
00094 
00095     static bool RegisterSharedLibraryFunctions(const char *shared_library_path);
00096 
00097  protected:
00099     FunctionCall ();
00100     
00101     typedef std::map<std::string, void*, CaseIgnLTStr > FuncTable;
00102     
00103  private:
00104     virtual void _SetParentScope( const ClassAd* );
00105     virtual bool _Evaluate( EvalState &, Value & ) const;
00106     virtual bool _Evaluate( EvalState &, Value &, ExprTree *& ) const;
00107     virtual bool _Flatten( EvalState&, Value&, ExprTree*&, int* ) const;
00108     
00109     // information common to all function calls a mapping from
00110     // function names (char*) to static methods We have a function to
00111     // return the object--it's static, and we want to make sure that
00112     // it's constructor has been called whenever we need to use it.
00113     static FuncTable &getFunctionTable(void);
00114     static bool      initialized;
00115     
00116     // function call specific information
00117     std::string     functionName;
00118     ClassAdFunc     function;
00119     ArgumentList    arguments;
00120 
00121     
00122     // function implementations
00123     // type predicates
00124     static bool isType(const char*,const ArgumentList&,EvalState&,Value&);
00125     
00126     // list membership predicates
00127     static bool testMember(const char*,const ArgumentList&,EvalState&,
00128                            Value&);
00129     
00130     // sum, average and bounds (max and min)
00131     /*
00132       static bool sumAvgFrom(const char*,const ArgumentList&,EvalState&,
00133       Value&);
00134       static bool boundFrom(const char*,const ArgumentList&,EvalState&,
00135       Value&);
00136     */
00137     static bool size(const char*,const ArgumentList&,EvalState&,
00138                      Value&);
00139     static bool sumAvg(const char*,const ArgumentList&,EvalState&,
00140                        Value&);
00141     static bool minMax(const char*,const ArgumentList&,EvalState&,
00142                        Value&);
00143     static bool listCompare(const char*,const ArgumentList&,EvalState&,
00144                             Value&);
00145     
00146     // time management (constructors)
00147 
00148 
00149     static bool epochTime (const char *, const ArgumentList &argList, 
00150                            EvalState &, Value &val);
00151     static bool currentTime(const char*,const ArgumentList&,EvalState&,
00152                             Value&);
00153     static bool timeZoneOffset(const char*,const ArgumentList&,EvalState&,
00154                                Value&);
00155     static bool dayTime(const char*,const ArgumentList&,EvalState&,Value&);
00156     static bool makeTime(const char*,const ArgumentList&,EvalState&,Value&);
00157     /* Unused
00158     static bool makeDate(const char*,const ArgumentList&,EvalState&,Value&);
00159     */
00160     // time management (selectors)
00161     static bool getField(const char*,const ArgumentList&,EvalState&,Value&);
00162     static bool splitTime(const char*,const ArgumentList&,EvalState&,Value&);
00163     static bool formatTime(const char*,const ArgumentList&,EvalState&,Value&);
00164     // time management (conversions)
00165     /* Unused
00166     static bool inTimeUnits(const char*,const ArgumentList&,EvalState&,
00167                             Value&);
00168     */
00169 
00170     // string management
00171     static bool strCat(const char*,const ArgumentList&,EvalState&,Value&);
00172     static bool changeCase(const char*,const ArgumentList&,EvalState&,
00173                            Value&);
00174     static bool subString(const char*,const ArgumentList&,EvalState&,
00175                           Value&);
00176     static bool compareString(const char*,const ArgumentList&,EvalState&,
00177                           Value&);
00178     
00179     // pattern matching
00180     static bool matchPattern(const char*,const ArgumentList&,EvalState&,
00181                              Value&);
00182     static bool substPattern( const char*,const ArgumentList &argList,EvalState &state, Value &result );
00183 
00184     static bool matchPatternMember(const char*,const ArgumentList &argList,EvalState &state,
00185                               Value &result);
00186 
00187     // type conversion
00188     static bool convInt(const char*,const ArgumentList&,EvalState&,Value&);
00189     static bool convReal(const char*,const ArgumentList&,EvalState&,Value&);
00190     static bool convString(const char*,const ArgumentList&,EvalState&,
00191                            Value&);
00192 
00193     static bool convBool(const char*,const ArgumentList&,EvalState&,Value&);
00194     static bool convTime(const char*,const ArgumentList&,EvalState&,Value&);
00195     
00196     // math (floor, ceil, round)
00197     static bool doMath(const char*,const ArgumentList&,EvalState&,Value&);
00198     static bool random(const char*,const ArgumentList&,EvalState&,Value&);
00199 
00200     static bool ifThenElse( const char* name,const ArgumentList &argList,EvalState &state,Value &result );
00201     
00202     static bool interval( const char* name,const ArgumentList &argList,EvalState &state,Value &result );
00203 
00204     static bool eval( const char* name,const ArgumentList &argList,EvalState &state,Value &result );
00205 
00206     //static bool doReal(const char*,const ArgumentList&,EvalState&,Value&);
00207 };
00208 
00209 END_NAMESPACE // classad
00210 
00211 #endif//__CLASSAD_FN_CALL_H__
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends