libzypp 17.37.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 <utility>
20#include <zypp-core/base/DefaultIntegral>
21
22#include <zypp/parser/xml/Node.h>
23
25 namespace zypp
26{
28 namespace xml
29 {
30
31 class Node;
32
34 //
35 // CLASS NAME : ParseDefConsume
36 //
39 struct ParseDefConsume
40 {
41 virtual ~ParseDefConsume();
42
43 virtual void start( const Node & _node );
44 virtual void text ( const Node & _node );
45 virtual void cdata( const Node & _node );
46 virtual void done ( const Node & _node );
47
48 virtual void startSubnode( const Node & _node );
49 virtual void doneSubnode ( const Node & _node );
50 };
51
52
54 //
55 // CLASS NAME : ParseDefConsumeRedirect
56 //
61 class ParseDefConsumeRedirect : public ParseDefConsume
62 {
63 public:
64 ParseDefConsumeRedirect();
65 ParseDefConsumeRedirect( shared_ptr<ParseDefConsume> target_r );
66 ParseDefConsumeRedirect( ParseDefConsume * allocatedTarget_r );
67 ParseDefConsumeRedirect( ParseDefConsume & target_r );
68
69 ~ParseDefConsumeRedirect() override;
70
71 public:
72 void setRedirect( shared_ptr<ParseDefConsume> target_r );
73 void setRedirect( ParseDefConsume * allocatedTarget_r );
74 void setRedirect( ParseDefConsume & target_r );
75 void cancelRedirect();
76
77 shared_ptr<ParseDefConsume> getRedirect() const;
78
79 public:
80 void start( const Node & _node ) override;
81 void text ( const Node & _node ) override;
82 void cdata( const Node & _node ) override;
83 void done ( const Node & _node ) override;
84 void startSubnode( const Node & _node ) override;
85 void doneSubnode ( const Node & _node ) override;
86
87 private:
88 shared_ptr<ParseDefConsume> _target;
89 };
90
91
93 //
94 // CLASS NAME : ParseDefConsumeCallback
95 //
98 class ParseDefConsumeCallback : public ParseDefConsume
99 {
100 public:
101 using Callback = function<void (const Node &)>;
102
103 ParseDefConsumeCallback();
104
105 ~ParseDefConsumeCallback() override;
106
107 public:
108 void start( const Node & node_r ) override;
109 void text( const Node & node_r ) override;
110 void cdata( const Node & node_r ) override;
111 void done( const Node & node_r ) override;
112 void startSubnode( const Node & node_r ) override;
113 void doneSubnode( const Node & node_r ) override;
114
115 public:
116 Callback _start;
117 Callback _text;
118 Callback _cdata;
119 Callback _done;
120 Callback _startSubnode;
121 Callback _doneSubnode;
122 };
123
124
126
127 namespace parse_def_assign
128 {
129 template <class Tp> struct Assigner;
130
131 using AssignerRef = shared_ptr<Assigner<void>>;
132
134 template <>
135 struct Assigner<void>
136 {
137 virtual ~Assigner()
138 {}
139 virtual void assign( const char * text_r )
140 {}
141 };
142
146 template <class Tp>
147 struct Assigner : public Assigner<void>
148 {
149 Assigner( Tp & value_r )
150 : _value( &value_r )
151 {}
152
153 void assign( const char * text_r ) override
154 { *_value = Tp( text_r ); }
155
156 private:
157 Tp * _value;
158 };
159
164 template <>
165 inline void Assigner<short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
166 template <>
167 inline void Assigner<int>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
168 template <>
169 inline void Assigner<long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
170 template <>
171 inline void Assigner<long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
172 template <>
173 inline void Assigner<unsigned short>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
174 template <>
175 inline void Assigner<unsigned>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
176 template <>
177 inline void Assigner<unsigned long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
178 template <>
179 inline void Assigner<unsigned long long>::assign( const char * text_r ) { str::strtonum( text_r, *_value ); }
180 template <>
181 inline void Assigner<bool>::assign( const char * text_r ) { str::strToBoolNodefault( text_r, *_value ); }
183
186 template <class Tp>
187 inline AssignerRef assigner( Tp & value_r )
188 { return AssignerRef( new Assigner<Tp>( value_r ) ); }
189
190 template <class Tp, Tp TInitial>
191 inline AssignerRef assigner( DefaultIntegral<Tp,TInitial> & value_r )
192 { return AssignerRef( new Assigner<Tp>( value_r.get() ) ); }
194
195
213 struct Consumer : public ParseDefConsume
214 {
216 void add( const AssignerRef & assigner_r )
217 { _text.push_back( assigner_r ); }
218
220 void add( const std::string & attr_r, const AssignerRef & assigner_r )
221 { _attr[attr_r].push_back( assigner_r ); }
222
224 void prenotify( function<void ( const Node & )> pre_r )
225 { _pre = std::move(pre_r); }
226
228 void postnotify( function<void ( const Node & )> post_r )
229 { _post = std::move(post_r); }
230
231 void start( const xml::Node & node_r ) override
232 {
233 if ( _pre )
234 _pre( node_r );
235
236 if ( ! _attr.empty() )
237 for_( it, _attr.begin(), _attr.end() )
238 assign( it->second, node_r.getAttribute( it->first.c_str() ).c_str() );
239 }
240
241 void text( const xml::Node & node_r ) override
242 {
243 if ( ! _text.empty() )
244 assign( _text, node_r.value().c_str() );
245 }
246
247 void done( const xml::Node & node_r ) override
248 {
249 if ( _post )
250 _post( node_r );
251 }
252
253 private:
254 void assign( const std::vector<AssignerRef> & vec_r, const char * value_r )
255 {
256 if ( value_r )
257 for_( it, vec_r.begin(), vec_r.end() )
258 (*it)->assign( value_r );
259 }
260
261 private:
262 std::unordered_map<std::string, std::vector<AssignerRef> > _attr;
263 std::vector<AssignerRef> _text;
264 function<void ( const Node & )> _pre;
265 function<void ( const Node & )> _post;
266 };
267
281 struct Builder
282 {
284 Builder()
285 : _ptr( new Consumer )
286 {}
287
289 template <class Tp>
290 Builder( Tp & value_r )
291 : _ptr( new Consumer )
292 { operator()( value_r ); }
293
295 template <class Tp>
296 Builder( const std::string & attr_r, Tp & value_r )
297 : _ptr( new Consumer )
298 { operator()( attr_r, value_r ); }
299
301 template <class Tp>
302 Builder & operator()( Tp & value_r )
303 { _ptr->add( assigner( value_r ) ); return *this; }
304
306 template <class Tp>
307 Builder & operator()( const std::string & attr_r, Tp & value_r )
308 { _ptr->add( attr_r, assigner( value_r ) ); return *this; }
309
311 Builder & operator<<( function<void ( const Node & )> done_r )
312 { _ptr->prenotify( std::move(done_r) ); return *this; }
313
315 Builder & operator>>( function<void ( const Node & )> done_r )
316 { _ptr->postnotify( std::move(done_r) ); return *this; }
317
319 operator shared_ptr<ParseDefConsume> () const
320 { return _ptr; }
321
322 private:
323 shared_ptr<Consumer> _ptr;
324 };
325
326 } // namespace parse_def_assign
327
328
356 inline parse_def_assign::Builder parseDefAssign()
357 { return parse_def_assign::Builder(); }
358
359 template <class Tp>
360 inline parse_def_assign::Builder parseDefAssign( Tp & value_r )
361 { return parse_def_assign::Builder( value_r ); }
362
363 template <class Tp>
364 inline parse_def_assign::Builder parseDefAssign( const std::string & attr_r, Tp & value_r )
365 { return parse_def_assign::Builder( attr_r, value_r ); }
367
369 } // namespace xml
372} // namespace zypp
374#endif // ZYPP_PARSER_XML_PARSEDEFCONSUME_H
Node.h
shared_ptr
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::_startSubnode
Callback _startSubnode
Definition ParseDefConsume.h:120
zypp::xml::ParseDefConsumeCallback::startSubnode
void startSubnode(const Node &node_r) override
Definition ParseDefConsume.cc:159
zypp::xml::ParseDefConsumeCallback::_cdata
Callback _cdata
Definition ParseDefConsume.h:118
zypp::xml::ParseDefConsumeCallback::_doneSubnode
Callback _doneSubnode
Definition ParseDefConsume.h:121
zypp::xml::ParseDefConsumeCallback::_start
Callback _start
Definition ParseDefConsume.h:116
zypp::xml::ParseDefConsumeCallback::_text
Callback _text
Definition ParseDefConsume.h:117
zypp::xml::ParseDefConsumeCallback::cdata
void cdata(const Node &node_r) override
Definition ParseDefConsume.cc:147
zypp::xml::ParseDefConsumeCallback::start
void start(const Node &node_r) override
Definition ParseDefConsume.cc:135
zypp::xml::ParseDefConsumeCallback::done
void done(const Node &node_r) override
Definition ParseDefConsume.cc:153
zypp::xml::ParseDefConsumeCallback::Callback
function< void(const Node &)> Callback
Definition ParseDefConsume.h:101
zypp::xml::ParseDefConsumeCallback::text
void text(const Node &node_r) override
Definition ParseDefConsume.cc:141
zypp::xml::ParseDefConsumeCallback::ParseDefConsumeCallback
ParseDefConsumeCallback()
Definition ParseDefConsume.cc:129
zypp::xml::ParseDefConsumeCallback::doneSubnode
void doneSubnode(const Node &node_r) override
Definition ParseDefConsume.cc:165
zypp::xml::ParseDefConsumeCallback::~ParseDefConsumeCallback
~ParseDefConsumeCallback() override
Definition ParseDefConsume.cc:132
zypp::xml::ParseDefConsumeCallback::_done
Callback _done
Definition ParseDefConsume.h:119
zypp::xml::ParseDefConsumeRedirect::text
void text(const Node &_node) override
Definition ParseDefConsume.cc:93
zypp::xml::ParseDefConsumeRedirect::cdata
void cdata(const Node &_node) override
Definition ParseDefConsume.cc:99
zypp::xml::ParseDefConsumeRedirect::cancelRedirect
void cancelRedirect()
Definition ParseDefConsume.cc:81
zypp::xml::ParseDefConsumeRedirect::startSubnode
void startSubnode(const Node &_node) override
Definition ParseDefConsume.cc:111
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::_target
shared_ptr< ParseDefConsume > _target
Definition ParseDefConsume.h:88
zypp::xml::ParseDefConsumeRedirect::doneSubnode
void doneSubnode(const Node &_node) override
Definition ParseDefConsume.cc:117
zypp::xml::ParseDefConsumeRedirect::setRedirect
void setRedirect(shared_ptr< ParseDefConsume > target_r)
Definition ParseDefConsume.cc:72
zypp::xml::ParseDefConsumeRedirect::done
void done(const Node &_node) override
Definition ParseDefConsume.cc:105
zypp::xml::ParseDefConsumeRedirect::~ParseDefConsumeRedirect
~ParseDefConsumeRedirect() override
Definition ParseDefConsume.cc:69
zypp::xml::ParseDefConsumeRedirect::start
void start(const Node &_node) override
Definition ParseDefConsume.cc:87
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.
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:507
zypp::xml::parse_def_assign
parseDefAssign exposed details
Definition ParseDefConsume.h:128
zypp::xml::parse_def_assign::AssignerRef
shared_ptr< Assigner< void > > AssignerRef
Definition ParseDefConsume.h:131
zypp::xml
Definition libxmlfwd.cc:22
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::xml::ParseDefConsume
Base class for ParseDef consumer.
Definition ParseDefConsume.h:40
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:137
zypp::xml::parse_def_assign::Assigner< void >::assign
virtual void assign(const char *text_r)
Definition ParseDefConsume.h:139
zypp::xml::parse_def_assign::Assigner
Assigner assigns text to types constructible from char*.
Definition ParseDefConsume.h:148
zypp::xml::parse_def_assign::Assigner< void >::Assigner
Assigner(void &value_r)
Definition ParseDefConsume.h:149
zypp::xml::parse_def_assign::Assigner::assign
void assign(const char *text_r) override
Definition ParseDefConsume.h:153
zypp::xml::parse_def_assign::Assigner::_value
Tp * _value
Definition ParseDefConsume.h:157
zypp::xml::parse_def_assign::Builder
Helper class to build a Consumer.
Definition ParseDefConsume.h:282
zypp::xml::parse_def_assign::Builder::operator<<
Builder & operator<<(function< void(const Node &)> done_r)
Set pre notification callback.
Definition ParseDefConsume.h:311
zypp::xml::parse_def_assign::Builder::Builder
Builder()
Contruct Consumer.
Definition ParseDefConsume.h:284
zypp::xml::parse_def_assign::Builder::operator()
Builder & operator()(const std::string &attr_r, Tp &value_r)
Extend Consumer.
Definition ParseDefConsume.h:307
zypp::xml::parse_def_assign::Builder::operator>>
Builder & operator>>(function< void(const Node &)> done_r)
Set post notification callback.
Definition ParseDefConsume.h:315
zypp::xml::parse_def_assign::Builder::Builder
Builder(const std::string &attr_r, Tp &value_r)
Contruct Consumer.
Definition ParseDefConsume.h:296
zypp::xml::parse_def_assign::Builder::operator()
Builder & operator()(Tp &value_r)
Extend Consumer.
Definition ParseDefConsume.h:302
zypp::xml::parse_def_assign::Builder::_ptr
shared_ptr< Consumer > _ptr
Definition ParseDefConsume.h:323
zypp::xml::parse_def_assign::Builder::Builder
Builder(Tp &value_r)
Contruct Consumer.
Definition ParseDefConsume.h:290
zypp::xml::parse_def_assign::Consumer
ParseDef consumer assigning Node text and attribues values to variables.
Definition ParseDefConsume.h:214
zypp::xml::parse_def_assign::Consumer::add
void add(const AssignerRef &assigner_r)
Extend Consumer.
Definition ParseDefConsume.h:216
zypp::xml::parse_def_assign::Consumer::_attr
std::unordered_map< std::string, std::vector< AssignerRef > > _attr
Definition ParseDefConsume.h:262
zypp::xml::parse_def_assign::Consumer::_pre
function< void(const Node &)> _pre
Definition ParseDefConsume.h:264
zypp::xml::parse_def_assign::Consumer::text
void text(const xml::Node &node_r) override
Definition ParseDefConsume.h:241
zypp::xml::parse_def_assign::Consumer::_text
std::vector< AssignerRef > _text
Definition ParseDefConsume.h:263
zypp::xml::parse_def_assign::Consumer::add
void add(const std::string &attr_r, const AssignerRef &assigner_r)
Extend Consumer.
Definition ParseDefConsume.h:220
zypp::xml::parse_def_assign::Consumer::postnotify
void postnotify(function< void(const Node &)> post_r)
Set post notification callback.
Definition ParseDefConsume.h:228
zypp::xml::parse_def_assign::Consumer::prenotify
void prenotify(function< void(const Node &)> pre_r)
Set pre notification callback.
Definition ParseDefConsume.h:224
zypp::xml::parse_def_assign::Consumer::start
void start(const xml::Node &node_r) override
Definition ParseDefConsume.h:231
zypp::xml::parse_def_assign::Consumer::_post
function< void(const Node &)> _post
Definition ParseDefConsume.h:265
zypp::xml::parse_def_assign::Consumer::done
void done(const xml::Node &node_r) override
Definition ParseDefConsume.h:247
zypp::xml::parse_def_assign::Consumer::assign
void assign(const std::vector< AssignerRef > &vec_r, const char *value_r)
Definition ParseDefConsume.h:254
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Function.h
Hash.h
PtrTypes.h
String.h