libzypp: zypp/parser/xml/ParseDefConsume.h Source File

libzypp 17.25.10

ParseDefConsume.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PARSER_XML_PARSEDEFCONSUME_H
13 #define ZYPP_PARSER_XML_PARSEDEFCONSUME_H
14
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>
20
21 #include <zypp/parser/xml/Node.h>
22
24 namespace zypp
25 {
27 namespace xml
28 {
29
30 class Node;
31
33 //
34 // CLASS NAME : ParseDefConsume
35 //
38 struct ParseDefConsume
39 {
40 virtual ~ParseDefConsume();
41
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 );
46
47 virtual void startSubnode( const Node & _node );
48 virtual void doneSubnode ( const Node & _node );
49 };
51
53 //
54 // CLASS NAME : ParseDefConsumeRedirect
55 //
60 class ParseDefConsumeRedirect : public ParseDefConsume
61 {
62 public:
63 ParseDefConsumeRedirect();
64 ParseDefConsumeRedirect( const shared_ptr<ParseDefConsume> & target_r );
65 ParseDefConsumeRedirect( ParseDefConsume * allocatedTarget_r );
66 ParseDefConsumeRedirect( ParseDefConsume & target_r );
67
68 virtual ~ParseDefConsumeRedirect();
69
70 public:
71 void setRedirect( const shared_ptr<ParseDefConsume> & target_r );
72 void setRedirect( ParseDefConsume * allocatedTarget_r );
73 void setRedirect( ParseDefConsume & target_r );
74 void cancelRedirect();
75
76 shared_ptr<ParseDefConsume> getRedirect() const;
77
78 public:
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 );
85
86 private:
87 shared_ptr<ParseDefConsume> _target;
88 };
90
92 //
93 // CLASS NAME : ParseDefConsumeCallback
94 //
97 class ParseDefConsumeCallback : public ParseDefConsume
98 {
99 public:
100 typedef function<void(const Node &)> Callback;
101
102 ParseDefConsumeCallback();
103
104 virtual ~ParseDefConsumeCallback();
105
106 public:
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 );
113
114 public:
115 Callback _start;
116 Callback _text;
117 Callback _cdata;
118 Callback _done;
119 Callback _startSubnode;
120 Callback _doneSubnode;
121 };
123
125
126 namespace parse_def_assign
127 {
128 template <class Tp> struct Assigner;
129
130 typedef shared_ptr<Assigner<void> > AssignerRef;
131
133 template <>
134 struct Assigner<void>
135 {
136 virtual ~Assigner()
137 {}
138 virtual void assign( const char * text_r )
139 {}
140 };
141
145 template <class Tp>
146 struct Assigner : public Assigner<void>
147 {
148 Assigner( Tp & value_r )
149 : _value( &value_r )
150 {}
151
152 virtual void assign( const char * text_r )
153 { *_value = Tp( text_r ); }
154
155 private:
156 Tp * _value;
157 };
158
163 template <>
164 inline void Assigner<short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
165 template <>
166 inline void Assigner<int>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
167 template <>
168 inline void Assigner<long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
169 template <>
170 inline void Assigner<long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
171 template <>
172 inline void Assigner<unsigned short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
173 template <>
174 inline void Assigner<unsigned>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
175 template <>
176 inline void Assigner<unsigned long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
177 template <>
178 inline void Assigner<unsigned long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
179 template <>
180 inline void Assigner<bool>::assign( const char * text_r ) { str::strToBoolNodefault( text_r, *_value ); }
182
185 template <class Tp>
186 inline AssignerRef assigner( Tp & value_r )
187 { return AssignerRef( new Assigner<Tp>( value_r ) ); }
188
189 template <class Tp, Tp TInitial>
190 inline AssignerRef assigner( DefaultIntegral<Tp,TInitial> & value_r )
191 { return AssignerRef( new Assigner<Tp>( value_r.get() ) ); }
193
194
212 struct Consumer : public ParseDefConsume
213 {
215 void add( const AssignerRef & assigner_r )
216 { _text.push_back( assigner_r ); }
217
219 void add( const std::string & attr_r, const AssignerRef & assigner_r )
220 { _attr[attr_r].push_back( assigner_r ); }
221
223 void prenotify( function<void ( const Node & )> pre_r )
224 { _pre = pre_r; }
225
227 void postnotify( function<void ( const Node & )> post_r )
228 { _post = post_r; }
229
230 virtual void start( const xml::Node & node_r )
231 {
232 if ( _pre )
233 _pre( node_r );
234
235 if ( ! _attr.empty() )
236 for_( it, _attr.begin(), _attr.end() )
237 assign( it->second, node_r.getAttribute( it->first.c_str() ).c_str() );
238 }
239
240 virtual void text( const xml::Node & node_r )
241 {
242 if ( ! _text.empty() )
243 assign( _text, node_r.value().c_str() );
244 }
245
246 virtual void done( const xml::Node & node_r )
247 {
248 if ( _post )
249 _post( node_r );
250 }
251
252 private:
253 void assign( const std::vector<AssignerRef> & vec_r, const char * value_r )
254 {
255 if ( value_r )
256 for_( it, vec_r.begin(), vec_r.end() )
257 (*it)->assign( value_r );
258 }
259
260 private:
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;
265 };
266
280 struct Builder
281 {
283 Builder()
284 : _ptr( new Consumer )
285 {}
286
288 template <class Tp>
289 Builder( Tp & value_r )
290 : _ptr( new Consumer )
291 { operator()( value_r ); }
292
294 template <class Tp>
295 Builder( const std::string & attr_r, Tp & value_r )
296 : _ptr( new Consumer )
297 { operator()( attr_r, value_r ); }
298
300 template <class Tp>
301 Builder & operator()( Tp & value_r )
302 { _ptr->add( assigner( value_r ) ); return *this; }
303
305 template <class Tp>
306 Builder & operator()( const std::string & attr_r, Tp & value_r )
307 { _ptr->add( attr_r, assigner( value_r ) ); return *this; }
308
310 Builder & operator<<( function<void ( const Node & )> done_r )
311 { _ptr->prenotify( done_r ); return *this; }
312
314 Builder & operator>>( function<void ( const Node & )> done_r )
315 { _ptr->postnotify( done_r ); return *this; }
316
318 operator shared_ptr<ParseDefConsume> () const
319 { return _ptr; }
320
321 private:
322 shared_ptr<Consumer> _ptr;
323 };
325 } // namespace parse_def_assign
327
355 inline parse_def_assign::Builder parseDefAssign()
356 { return parse_def_assign::Builder(); }
357
358 template <class Tp>
359 inline parse_def_assign::Builder parseDefAssign( Tp & value_r )
360 { return parse_def_assign::Builder( value_r ); }
361
362 template <class Tp>
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 ); }
366
368 } // namespace xml
371 } // namespace zypp
373 #endif // ZYPP_PARSER_XML_PARSEDEFCONSUME_H
DefaultIntegral.h
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
Function.h
Hash.h
Node.h
PtrTypes.h
String.h
_value
Edition * _value
Definition: SysContent.cc:311
zypp::xml::Node
xmlTextReader based interface to Reader's current node.
Definition: Node.h:36
zypp::xml::Node::value
XmlString value() const
Provides the text value of the node if present.
Definition: Node.h:143
zypp::xml::Node::getAttribute
XmlString getAttribute(const char *name_r) const
Provides a copy of the attribute value with the specified qualified name.
Definition: Node.h:71
zypp::xml::ParseDefConsumeCallback
ParseDef consumer that invokes callbacks.
Definition: ParseDefConsume.h:98
zypp::xml::ParseDefConsumeCallback::done
virtual void done(const Node &node_r)
Definition: ParseDefConsume.cc:153
zypp::xml::ParseDefConsumeCallback::_startSubnode
Callback _startSubnode
Definition: ParseDefConsume.h:119
zypp::xml::ParseDefConsumeCallback::~ParseDefConsumeCallback
virtual ~ParseDefConsumeCallback()
Definition: ParseDefConsume.cc:132
zypp::xml::ParseDefConsumeCallback::_cdata
Callback _cdata
Definition: ParseDefConsume.h:117
zypp::xml::ParseDefConsumeCallback::_doneSubnode
Callback _doneSubnode
Definition: ParseDefConsume.h:120
zypp::xml::ParseDefConsumeCallback::_start
Callback _start
Definition: ParseDefConsume.h:115
zypp::xml::ParseDefConsumeCallback::_text
Callback _text
Definition: ParseDefConsume.h:116
zypp::xml::ParseDefConsumeCallback::text
virtual void text(const Node &node_r)
Definition: ParseDefConsume.cc:141
zypp::xml::ParseDefConsumeCallback::start
virtual void start(const Node &node_r)
Definition: ParseDefConsume.cc:135
zypp::xml::ParseDefConsumeCallback::ParseDefConsumeCallback
ParseDefConsumeCallback()
Definition: ParseDefConsume.cc:129
zypp::xml::ParseDefConsumeCallback::cdata
virtual void cdata(const Node &node_r)
Definition: ParseDefConsume.cc:147
zypp::xml::ParseDefConsumeCallback::startSubnode
virtual void startSubnode(const Node &node_r)
Definition: ParseDefConsume.cc:159
zypp::xml::ParseDefConsumeCallback::doneSubnode
virtual void doneSubnode(const Node &node_r)
Definition: ParseDefConsume.cc:165
zypp::xml::ParseDefConsumeCallback::Callback
function< void(const Node &)> Callback
Definition: ParseDefConsume.h:100
zypp::xml::ParseDefConsumeCallback::_done
Callback _done
Definition: ParseDefConsume.h:118
zypp::xml::ParseDefConsumeRedirect
ParseDef consumer redirecting all events to another consumer.
Definition: ParseDefConsume.h:61
zypp::xml::ParseDefConsumeRedirect::start
virtual void start(const Node &_node)
Definition: ParseDefConsume.cc:87
zypp::xml::ParseDefConsumeRedirect::startSubnode
virtual void startSubnode(const Node &_node)
Definition: ParseDefConsume.cc:111
zypp::xml::ParseDefConsumeRedirect::doneSubnode
virtual void doneSubnode(const Node &_node)
Definition: ParseDefConsume.cc:117
zypp::xml::ParseDefConsumeRedirect::~ParseDefConsumeRedirect
virtual ~ParseDefConsumeRedirect()
Definition: ParseDefConsume.cc:69
zypp::xml::ParseDefConsumeRedirect::cancelRedirect
void cancelRedirect()
Definition: ParseDefConsume.cc:81
zypp::xml::ParseDefConsumeRedirect::done
virtual void done(const Node &_node)
Definition: ParseDefConsume.cc:105
zypp::xml::ParseDefConsumeRedirect::getRedirect
shared_ptr< ParseDefConsume > getRedirect() const
Definition: ParseDefConsume.cc:84
zypp::xml::ParseDefConsumeRedirect::ParseDefConsumeRedirect
ParseDefConsumeRedirect()
Definition: ParseDefConsume.cc:54
zypp::xml::ParseDefConsumeRedirect::text
virtual void text(const Node &_node)
Definition: ParseDefConsume.cc:93
zypp::xml::ParseDefConsumeRedirect::_target
shared_ptr< ParseDefConsume > _target
Definition: ParseDefConsume.h:87
zypp::xml::ParseDefConsumeRedirect::setRedirect
void setRedirect(const shared_ptr< ParseDefConsume > &target_r)
Definition: ParseDefConsume.cc:72
zypp::xml::ParseDefConsumeRedirect::cdata
virtual void cdata(const Node &_node)
Definition: ParseDefConsume.cc:99
zypp::xml::XmlString::c_str
const char * c_str() const
Explicit conversion to const char *.
Definition: XmlString.h:73
zypp::str::strtonum
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:385
zypp::str::strToBoolNodefault
bool strToBoolNodefault(const C_Str &str, bool &return_r)
Parse str into a bool if it's a legal true or false string.
Definition: String.h:433
zypp::xml::parse_def_assign::AssignerRef
shared_ptr< Assigner< void > > AssignerRef
Definition: ParseDefConsume.h:128
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::xml::ParseDefConsume
Base class for ParseDef consumer.
Definition: ParseDefConsume.h:39
zypp::xml::ParseDefConsume::start
virtual void start(const Node &_node)
Definition: ParseDefConsume.cc:30
zypp::xml::ParseDefConsume::text
virtual void text(const Node &_node)
Definition: ParseDefConsume.cc:33
zypp::xml::ParseDefConsume::cdata
virtual void cdata(const Node &_node)
Definition: ParseDefConsume.cc:36
zypp::xml::ParseDefConsume::~ParseDefConsume
virtual ~ParseDefConsume()
Definition: ParseDefConsume.cc:27
zypp::xml::ParseDefConsume::doneSubnode
virtual void doneSubnode(const Node &_node)
Definition: ParseDefConsume.cc:45
zypp::xml::ParseDefConsume::done
virtual void done(const Node &_node)
Definition: ParseDefConsume.cc:39
zypp::xml::ParseDefConsume::startSubnode
virtual void startSubnode(const Node &_node)
Definition: ParseDefConsume.cc:42
zypp::xml::parse_def_assign::Assigner< void >::~Assigner
virtual ~Assigner()
Definition: ParseDefConsume.h:136
zypp::xml::parse_def_assign::Assigner< void >::assign
virtual void assign(const char *text_r)
Definition: ParseDefConsume.h:138
zypp::xml::parse_def_assign::Assigner
Assigner assigns text to types constructible from char*.
Definition: ParseDefConsume.h:147
zypp::xml::parse_def_assign::Assigner::Assigner
Assigner(Tp &value_r)
Definition: ParseDefConsume.h:148
zypp::xml::parse_def_assign::Assigner::_value
Tp * _value
Definition: ParseDefConsume.h:156
zypp::xml::parse_def_assign::Assigner::assign
virtual void assign(const char *text_r)
Definition: ParseDefConsume.h:152
zypp::xml::parse_def_assign::Builder
Helper class to build a Consumer.
Definition: ParseDefConsume.h:281
zypp::xml::parse_def_assign::Builder::operator<<
Builder & operator<<(function< void(const Node &)> done_r)
Set pre notification callback.
Definition: ParseDefConsume.h:310
zypp::xml::parse_def_assign::Builder::Builder
Builder()
Contruct Consumer.
Definition: ParseDefConsume.h:283
zypp::xml::parse_def_assign::Builder::operator()
Builder & operator()(Tp &value_r)
Extend Consumer.
Definition: ParseDefConsume.h:301
zypp::xml::parse_def_assign::Builder::Builder
Builder(const std::string &attr_r, Tp &value_r)
Contruct Consumer.
Definition: ParseDefConsume.h:295
zypp::xml::parse_def_assign::Builder::operator()
Builder & operator()(const std::string &attr_r, Tp &value_r)
Extend Consumer.
Definition: ParseDefConsume.h:306
zypp::xml::parse_def_assign::Builder::operator>>
Builder & operator>>(function< void(const Node &)> done_r)
Set post notification callback.
Definition: ParseDefConsume.h:314
zypp::xml::parse_def_assign::Builder::_ptr
shared_ptr< Consumer > _ptr
Definition: ParseDefConsume.h:322
zypp::xml::parse_def_assign::Builder::Builder
Builder(Tp &value_r)
Contruct Consumer.
Definition: ParseDefConsume.h:289
zypp::xml::parse_def_assign::Consumer
ParseDef consumer assigning Node text and attribues values to variables.
Definition: ParseDefConsume.h:213
zypp::xml::parse_def_assign::Consumer::done
virtual void done(const xml::Node &node_r)
Definition: ParseDefConsume.h:246
zypp::xml::parse_def_assign::Consumer::add
void add(const AssignerRef &assigner_r)
Extend Consumer.
Definition: ParseDefConsume.h:215
zypp::xml::parse_def_assign::Consumer::_attr
std::unordered_map< std::string, std::vector< AssignerRef > > _attr
Definition: ParseDefConsume.h:261
zypp::xml::parse_def_assign::Consumer::_pre
function< void(const Node &)> _pre
Definition: ParseDefConsume.h:263
zypp::xml::parse_def_assign::Consumer::_text
std::vector< AssignerRef > _text
Definition: ParseDefConsume.h:262
zypp::xml::parse_def_assign::Consumer::add
void add(const std::string &attr_r, const AssignerRef &assigner_r)
Extend Consumer.
Definition: ParseDefConsume.h:219
zypp::xml::parse_def_assign::Consumer::text
virtual void text(const xml::Node &node_r)
Definition: ParseDefConsume.h:240
zypp::xml::parse_def_assign::Consumer::postnotify
void postnotify(function< void(const Node &)> post_r)
Set post notification callback.
Definition: ParseDefConsume.h:227
zypp::xml::parse_def_assign::Consumer::prenotify
void prenotify(function< void(const Node &)> pre_r)
Set pre notification callback.
Definition: ParseDefConsume.h:223
zypp::xml::parse_def_assign::Consumer::start
virtual void start(const xml::Node &node_r)
Definition: ParseDefConsume.h:230
zypp::xml::parse_def_assign::Consumer::_post
function< void(const Node &)> _post
Definition: ParseDefConsume.h:264
zypp::xml::parse_def_assign::Consumer::assign
void assign(const std::vector< AssignerRef > &vec_r, const char *value_r)
Definition: ParseDefConsume.h:253