1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/xml/ParseDefConsume.h
12 #ifndef ZYPP_PARSER_XML_PARSEDEFCONSUME_H
13 #define ZYPP_PARSER_XML_PARSEDEFCONSUME_H
15 #include "zypp/base/PtrTypes.h"
16 #include "zypp/base/Function.h"
17 #include "zypp/base/Hash.h"
18 #include "zypp/base/String.h"
19 #include "zypp/base/DefaultIntegral.h"
21 #include "zypp/parser/xml/Node.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////
34 // CLASS NAME : ParseDefConsume
36 /** Base class for ParseDef consumer.
38 struct ParseDefConsume
40 virtual ~ParseDefConsume();
42 virtual void start( const Node & _node );
43 virtual void text ( const Node & _node );
44 virtual void cdata( const Node & _node );
45 virtual void done ( const Node & _node );
47 virtual void startSubnode( const Node & _node );
48 virtual void doneSubnode ( const Node & _node );
50 ///////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////
54 // CLASS NAME : ParseDefConsumeRedirect
56 /** ParseDef consumer redirecting all events to another consumer.
57 * \note Allocated <tt>ParseDefConsume *</tt> passed are
58 * immediately wraped into a shared_ptr.
60 class ParseDefConsumeRedirect : public ParseDefConsume
63 ParseDefConsumeRedirect();
64 ParseDefConsumeRedirect( const shared_ptr<ParseDefConsume> & target_r );
65 ParseDefConsumeRedirect( ParseDefConsume * allocatedTarget_r );
66 ParseDefConsumeRedirect( ParseDefConsume & target_r );
68 virtual ~ParseDefConsumeRedirect();
71 void setRedirect( const shared_ptr<ParseDefConsume> & target_r );
72 void setRedirect( ParseDefConsume * allocatedTarget_r );
73 void setRedirect( ParseDefConsume & target_r );
74 void cancelRedirect();
76 shared_ptr<ParseDefConsume> getRedirect() const;
79 virtual void start( const Node & _node );
80 virtual void text ( const Node & _node );
81 virtual void cdata( const Node & _node );
82 virtual void done ( const Node & _node );
83 virtual void startSubnode( const Node & _node );
84 virtual void doneSubnode ( const Node & _node );
87 shared_ptr<ParseDefConsume> _target;
89 ///////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////
93 // CLASS NAME : ParseDefConsumeCallback
95 /** ParseDef consumer that invokes callbacks.
97 class ParseDefConsumeCallback : public ParseDefConsume
100 typedef function<void(const Node &)> Callback;
102 ParseDefConsumeCallback();
104 virtual ~ParseDefConsumeCallback();
107 virtual void start( const Node & node_r );
108 virtual void text( const Node & node_r );
109 virtual void cdata( const Node & node_r );
110 virtual void done( const Node & node_r );
111 virtual void startSubnode( const Node & node_r );
112 virtual void doneSubnode( const Node & node_r );
119 Callback _startSubnode;
120 Callback _doneSubnode;
122 ///////////////////////////////////////////////////////////////////
124 ///////////////////////////////////////////////////////////////////
125 /** \ref parseDefAssign exposed details */
126 namespace parse_def_assign
127 { /////////////////////////////////////////////////////////////////
128 template <class Tp> struct Assigner;
130 typedef shared_ptr<Assigner<void> > AssignerRef;
132 /** Common interface to all Assigner types. */
134 struct Assigner<void>
138 virtual void assign( const char * text_r )
142 /** Assigner assigns text to types constructible from \c char*.
143 * \see \ref assigner convenience constructor.
146 struct Assigner : public Assigner<void>
148 Assigner( Tp & value_r )
152 virtual void assign( const char * text_r )
153 { *_value = Tp( text_r ); }
159 /** \name Assigner specialisation for numeric and boolean values.
164 inline void Assigner<short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
166 inline void Assigner<int>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
168 inline void Assigner<long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
170 inline void Assigner<long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
172 inline void Assigner<unsigned short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
174 inline void Assigner<unsigned>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
176 inline void Assigner<unsigned long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
178 inline void Assigner<unsigned long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
180 inline void Assigner<bool>::assign( const char * text_r ) { str::strToBoolNodefault( text_r, *_value ); }
183 /** \name \relates Assigner Convenience constructor */
186 inline AssignerRef assigner( Tp & value_r )
187 { return AssignerRef( new Assigner<Tp>( value_r ) ); }
189 template <class Tp, Tp TInitial>
190 inline AssignerRef assigner( DefaultIntegral<Tp,TInitial> & value_r )
191 { return AssignerRef( new Assigner<Tp>( value_r.get() ) ); }
195 /** \ref ParseDef consumer assigning \ref Node text and attribues values to variables.
197 * This can be used with all types supported by \ref Assigner.
198 * Basically all types constructible from \c char*, or where a
199 * specialisation exists (e.g. numeric and bool).
201 * You may also set a <tt>void( const Node & )</tt> notification
202 * callback which is invoked after the node was processed.
204 * \note Use and see \ref xml::parseDefAssign convenience constructor.
207 * // parsedef for '<setup attr="13">value</setup>'
208 * ParseDef( "attr", MANDTAORY, xml::parseDefAssign( data.value )
209 * ( "attr", data.attr ) )
212 struct Consumer : public ParseDefConsume
214 /** Extend \ref Consumer. */
215 void add( const AssignerRef & assigner_r )
216 { _text.push_back( assigner_r ); }
218 /** Extend \ref Consumer. */
219 void add( const std::string & attr_r, const AssignerRef & assigner_r )
220 { _attr[attr_r].push_back( assigner_r ); }
222 /** Set pre notification callback. */
223 void prenotify( function<void ( const Node & )> pre_r )
226 /** Set post notification callback. */
227 void postnotify( function<void ( const Node & )> post_r )
230 virtual void start( const xml::Node & node_r )
235 if ( ! _attr.empty() )
236 for_( it, _attr.begin(), _attr.end() )
237 assign( it->second, node_r.getAttribute( it->first.c_str() ).c_str() );
240 virtual void text( const xml::Node & node_r )
242 if ( ! _text.empty() )
243 assign( _text, node_r.value().c_str() );
246 virtual void done( const xml::Node & node_r )
253 void assign( const std::vector<AssignerRef> & vec_r, const char * value_r )
256 for_( it, vec_r.begin(), vec_r.end() )
257 (*it)->assign( value_r );
261 std::unordered_map<std::string, std::vector<AssignerRef> > _attr;
262 std::vector<AssignerRef> _text;
263 function<void ( const Node & )> _pre;
264 function<void ( const Node & )> _post;
267 /** Helper class to build a \ref Consumer.
270 * The class constructs the consumer, allows to extend it via
271 * \ref operator(), and provides a conversion to
272 * \c shared_ptr<ParseDefConsume>, so it can be passed as a
273 * node consumer to \ref ParseDef.
275 * You may also set a <tt>void( const Node & )</tt> notification
276 * callback which is invoked before/after the node was processed.
278 * \note Use and see \ref xml::parseDefAssign convenience constructor.
282 /** Contruct \ref Consumer. */
284 : _ptr( new Consumer )
287 /** Contruct \ref Consumer. */
289 Builder( Tp & value_r )
290 : _ptr( new Consumer )
291 { operator()( value_r ); }
293 /** Contruct \ref Consumer. */
295 Builder( const std::string & attr_r, Tp & value_r )
296 : _ptr( new Consumer )
297 { operator()( attr_r, value_r ); }
299 /** Extend \ref Consumer. */
301 Builder & operator()( Tp & value_r )
302 { _ptr->add( assigner( value_r ) ); return *this; }
304 /** Extend \ref Consumer. */
306 Builder & operator()( const std::string & attr_r, Tp & value_r )
307 { _ptr->add( attr_r, assigner( value_r ) ); return *this; }
309 /** Set pre notification callback. */
310 Builder & operator<<( function<void ( const Node & )> done_r )
311 { _ptr->prenotify( done_r ); return *this; }
313 /** Set post notification callback. */
314 Builder & operator>>( function<void ( const Node & )> done_r )
315 { _ptr->postnotify( done_r ); return *this; }
317 /** Type conversion so this can be passed as node consumer to \ref ParseDef. */
318 operator shared_ptr<ParseDefConsume> () const
322 shared_ptr<Consumer> _ptr;
324 /////////////////////////////////////////////////////////////////
325 } // namespace parse_def_assign
326 ///////////////////////////////////////////////////////////////////
328 /** \name \ref ParseDef consumer assigning \ref Node text and attribues values to variables.
329 * \relates parse_def_assign::Consumer
330 * \relates parse_def_assign::Builder
332 * This function allows convenient contruction of a \ref parse_def_assign::Consumer
333 * to be passed as \ref Node conssumer to \ref ParseDef. Simply list each attributes
334 * name together with the variable it's value should be assigned to. If the attribute
335 * name is omitted, the nodes text value gets assigned.
337 * Target variables can be of any type tsupported by \ref Assigner.
338 * Basically all types constructible from \c char*, or where a
339 * specialisation exists (e.g. numeric and bool).
342 * void setupDone( const xml::Node & _node )
345 * // parsedef for '<setup attr="13">value</setup>'
346 * ParseDef( "attr", MANDTAORY,
347 * xml::parseDefAssign( data.value )
348 * ( "attr", data.attr )
352 * \see \ref xml::rnParse for more example.
355 inline parse_def_assign::Builder parseDefAssign()
356 { return parse_def_assign::Builder(); }
359 inline parse_def_assign::Builder parseDefAssign( Tp & value_r )
360 { return parse_def_assign::Builder( value_r ); }
363 inline parse_def_assign::Builder parseDefAssign( const std::string & attr_r, Tp & value_r )
364 { return parse_def_assign::Builder( attr_r, value_r ); }
367 /////////////////////////////////////////////////////////////////
369 ///////////////////////////////////////////////////////////////////
370 /////////////////////////////////////////////////////////////////
372 ///////////////////////////////////////////////////////////////////
373 #endif // ZYPP_PARSER_XML_PARSEDEFCONSUME_H