|
yast2-core
|
00001 // -*- c++ -*- 00002 00003 /* 00004 * Author: Arvin Schnell <arvin@suse.de> 00005 */ 00006 00007 00008 #ifndef Y2AgentComponent_h 00009 #define Y2AgentComponent_h 00010 00011 00012 #include <ycp/y2log.h> 00013 #include <ycp/YCPTerm.h> 00014 #include <y2/Y2Component.h> 00015 00016 #include <ycp/YCPCode.h> 00017 #include <ycp/YCPVoid.h> 00018 #include <ycp/YCPTerm.h> 00019 00020 class SCRAgent; 00021 00022 00026 template <class Agent> class Y2AgentComp : public Y2Component 00027 { 00028 00029 public: 00030 00034 Y2AgentComp (const char*); 00035 00039 ~Y2AgentComp (); 00040 00044 string name () const { return my_name; } 00045 00049 YCPValue evaluate (const YCPValue &command); 00050 00054 SCRAgent* getSCRAgent (); 00055 00056 YCPValue Read (const YCPPath &path); 00057 00058 private: 00059 00063 const char* my_name; 00064 00068 Agent* agent; 00069 00070 }; 00071 00072 00073 template <class Agent> 00074 Y2AgentComp<Agent>::Y2AgentComp (const char* my_name) 00075 : my_name (my_name), 00076 agent (0) 00077 { 00078 } 00079 00080 00081 template <class Agent> 00082 Y2AgentComp<Agent>::~Y2AgentComp () 00083 { 00084 if (agent) 00085 { 00086 delete agent; 00087 } 00088 } 00089 00090 00091 template <class Agent> YCPValue 00092 Y2AgentComp<Agent>::evaluate (const YCPValue& v) 00093 { 00094 y2debug ("evaluate (%s)", v->toString ().c_str ()); 00095 00096 if (!agent) 00097 getSCRAgent (); 00098 00099 y2debug ("Going to evaluate %s", v->toString ().c_str ()); 00100 00101 YCPValue value = v; 00102 if (value->isCode ()) 00103 { 00104 YCodePtr c = v->asCode ()->code (); 00105 00106 00107 if ( c->kind () != YCode::yeTerm) 00108 { 00109 y2milestone ("Evaluating an expression, not SCR builtin"); 00110 value = value->asCode ()->evaluate (); 00111 return value; 00112 } 00113 00114 value = value->asCode ()->evaluate (); 00115 } 00116 00117 if (value.isNull () || value->isVoid ()) 00118 return value; 00119 00120 y2debug ("After code evaluation: %s", value->toString ().c_str ()); 00121 00122 if( value->isTerm () ) { 00123 YCPTerm term = value ->asTerm (); 00124 string command = term->name (); 00125 YCPList args = term->args (); 00126 00127 // evaluate the term in native functions 00128 if( command == "Read" ) { 00129 return getSCRAgent ()-> Read (args->value (0)->asPath (), args->size() > 1 ? args->value (1) : YCPNull ()) ; 00130 } 00131 else if( command == "Write" ) { 00132 return getSCRAgent ()-> Write (args->value (0)->asPath (), args->value (1), args->size () > 2 ? args->value (2) : YCPNull ()) ; 00133 } 00134 else if( command == "Dir" ) { 00135 return getSCRAgent ()-> Dir (args->value (0)->asPath ()) ; 00136 } 00137 else if( command == "Error" ) { 00138 return getSCRAgent ()-> Error (args->value (0)->asPath ()) ; 00139 } 00140 else if( command == "Execute" ) { 00141 y2debug( "Execute, arg size is %d", args->size() ); 00142 switch( args->size() ) { 00143 case 1: 00144 return getSCRAgent ()-> Execute (args->value (0)->asPath ()) ; 00145 case 2: 00146 return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1)) ; 00147 default: 00148 return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1), args->value (2)) ; 00149 } 00150 } 00151 else { 00152 y2debug( "Passing term to otherCommand" ); 00153 return getSCRAgent ()-> otherCommand (term); 00154 } 00155 } 00156 #if 0 00157 if( value->isCode () ) { 00158 y2debug( "Passing (evaluated) code to otherCommand" ); 00159 return getSCRAgent ()-> otherCommand (value->asCode ()->evaluate ()->asTerm ()); 00160 } 00161 #endif 00162 00163 y2error( "Unhandled value (%s): %s", value->valuetype_str (), value->toString ().c_str () ); 00164 00165 return YCPVoid(); 00166 } 00167 00168 00169 template <class Agent> SCRAgent* 00170 Y2AgentComp<Agent>::getSCRAgent () 00171 { 00172 if (!agent) 00173 { 00174 agent = new Agent (); 00175 } 00176 00177 return agent; 00178 } 00179 00180 template <class Agent> YCPValue Y2AgentComp<Agent>::Read (const YCPPath &path) 00181 { 00182 y2error( "Y2AgentComp::Read" ); 00183 return getSCRAgent()->Read (path); 00184 } 00185 00186 #endif // Y2AgentComponent_h
1.7.3