yast2-core
Y2AgentComponent.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 
3 /*
4  * Author: Arvin Schnell <arvin@suse.de>
5  */
6 
7 
8 #ifndef Y2AgentComponent_h
9 #define Y2AgentComponent_h
10 
11 
12 #include <ycp/y2log.h>
13 #include <ycp/YCPTerm.h>
14 #include <y2/Y2Component.h>
15 
16 #include <ycp/YCPCode.h>
17 #include <ycp/YCPVoid.h>
18 #include <ycp/YCPTerm.h>
19 
20 class SCRAgent;
21 
22 
26 template <class Agent> class Y2AgentComp : public Y2Component
27 {
28 
29 public:
30 
34  Y2AgentComp (const char*);
35 
39  virtual ~Y2AgentComp ();
40 
44  virtual string name () const { return my_name; }
45 
49  virtual YCPValue evaluate (const YCPValue &command);
50 
54  virtual SCRAgent* getSCRAgent ();
55 
56  virtual YCPValue Read (const YCPPath &path);
57 
58 protected:
59 
63  const char* my_name;
64 
68  Agent* agent;
69 
70 };
71 
72 
73 template <class Agent>
74 Y2AgentComp<Agent>::Y2AgentComp (const char* my_name)
75  : my_name (my_name),
76  agent (0)
77 {
78 }
79 
80 
81 template <class Agent>
83 {
84  if (agent)
85  {
86  delete agent;
87  }
88 }
89 
90 
91 template <class Agent> YCPValue
93 {
94  y2debug ("evaluate (%s)", v->toString ().c_str ());
95 
96  if (!agent)
97  getSCRAgent ();
98 
99  y2debug ("Going to evaluate %s", v->toString ().c_str ());
100 
101  YCPValue value = v;
102  if (value->isCode ())
103  {
104  YCodePtr c = v->asCode ()->code ();
105 
106 
107  if ( c->kind () != YCode::yeTerm)
108  {
109  y2milestone ("Evaluating an expression, not SCR builtin");
110  value = value->asCode ()->evaluate ();
111  return value;
112  }
113 
114  value = value->asCode ()->evaluate ();
115  }
116 
117  if (value.isNull () || value->isVoid ())
118  return value;
119 
120  y2debug ("After code evaluation: %s", value->toString ().c_str ());
121 
122  if( value->isTerm () ) {
123  YCPTerm term = value ->asTerm ();
124  string command = term->name ();
125  YCPList args = term->args ();
126 
127  // evaluate the term in native functions
128  if( command == "Read" ) {
129  return getSCRAgent ()-> Read (args->value (0)->asPath (), args->size() > 1 ? args->value (1) : YCPNull ()) ;
130  }
131  else if( command == "Write" ) {
132  return getSCRAgent ()-> Write (args->value (0)->asPath (), args->value (1), args->size () > 2 ? args->value (2) : YCPNull ()) ;
133  }
134  else if( command == "Dir" ) {
135  return getSCRAgent ()-> Dir (args->value (0)->asPath ()) ;
136  }
137  else if( command == "Error" ) {
138  return getSCRAgent ()-> Error (args->value (0)->asPath ()) ;
139  }
140  else if( command == "Execute" ) {
141  y2debug( "Execute, arg size is %d", args->size() );
142  switch( args->size() ) {
143  case 1:
144  return getSCRAgent ()-> Execute (args->value (0)->asPath ()) ;
145  case 2:
146  return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1)) ;
147  default:
148  return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1), args->value (2)) ;
149  }
150  }
151  else {
152  y2debug( "Passing term to otherCommand" );
153  return getSCRAgent ()-> otherCommand (term);
154  }
155  }
156 #if 0
157  if( value->isCode () ) {
158  y2debug( "Passing (evaluated) code to otherCommand" );
159  return getSCRAgent ()-> otherCommand (value->asCode ()->evaluate ()->asTerm ());
160  }
161 #endif
162 
163  y2error( "Unhandled value (%s): %s", value->valuetype_str (), value->toString ().c_str () );
164 
165  return YCPVoid();
166 }
167 
168 
169 template <class Agent> SCRAgent*
171 {
172  if (!agent)
173  {
174  agent = new Agent ();
175  }
176 
177  return agent;
178 }
179 
180 template <class Agent> YCPValue Y2AgentComp<Agent>::Read (const YCPPath &path)
181 {
182  y2error( "Y2AgentComp::Read" );
183  return getSCRAgent()->Read (path);
184 }
185 
186 #endif // Y2AgentComponent_h
const char * my_name
Definition: Y2AgentComponent.h:63
virtual SCRAgent * getSCRAgent()
Definition: Y2AgentComponent.h:170
virtual ~Y2AgentComp()
Definition: Y2AgentComponent.h:82
virtual string name() const
Definition: Y2AgentComponent.h:44
#define y2milestone(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:110
YCPValue value(int n) const
Definition: YCPList.h:266
Communication handle to a YaST2 component.
Definition: Y2Component.h:262
SuSE Configuration Repository Agent.
Definition: SCRAgent.h:37
Definition: YCode.h:116
string name() const
Definition: YCPTerm.h:185
#define y2error(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:112
Wrapper for YCPListRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPListRep with the arrow operator. See YCPListRep.
Definition: YCPList.h:236
Y2AgentComp(const char *)
Definition: Y2AgentComponent.h:74
virtual YCPValue evaluate(const YCPValue &command)
Definition: Y2AgentComponent.h:92
Wrapper for YCPVoidRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPVoidRep with the arrow operator. See YCPVoidRep.
Definition: YCPVoid.h:75
Wrapper for YCPTermRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPTermRep with the arrow operator. See YCPTermRep.
Definition: YCPTerm.h:177
YCPList args() const
Definition: YCPTerm.h:186
Wrapper for YCPValueRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPValueRep with the arrow operator. See YCPValueRep.
Definition: YCPValue.h:275
Wrapper for YCPPathRep This class realizes an automatic memory management via YCPElement. Access the functionality of YCPPathRep with the arrow operator. See YCPPathRep.
Definition: YCPPath.h:175
virtual YCPValue Read(const YCPPath &path)
Definition: Y2AgentComponent.h:180
Definition: Y2AgentComponent.h:26
#define y2debug(format, args...)
Definition: liby2util-r/src/include/y2util/y2log.h:107
Definition: YCPElement.h:125
Agent * agent
Definition: Y2AgentComponent.h:68
int size() const
Definition: YCPList.h:250

Generated on a sunny day for yast2-core by doxygen 1.8.6