libzypp  15.28.6
PluginFrame.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PLUGINFRAME_H
13 #define ZYPP_PLUGINFRAME_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <map>
18 
19 #include "zypp/base/PtrTypes.h"
20 
22 
24 namespace zypp
25 {
26 
41  {
42  friend std::ostream & operator<<( std::ostream & str, const PluginFrame & obj );
43  friend bool operator==( const PluginFrame & lhs, const PluginFrame & rhs );
44 
45  typedef const std::initializer_list<std::pair<std::string,std::string>> & HeaderInitializerList;
46 
47  public:
49  static const std::string & ackCommand();
51  static const std::string & errorCommand();
53  static const std::string & enomethodCommand();
54 
55  public:
58 
60  PluginFrame();
61 
65  PluginFrame( const std::string & command_r );
66 
70  PluginFrame( const std::string & command_r, const std::string & body_r );
71 
75  PluginFrame( const std::string & command_r, HeaderInitializerList contents_r );
76 
80  PluginFrame( const std::string & command_r, const std::string & body_r, HeaderInitializerList contents_r );
81 
86  PluginFrame( std::istream & stream_r );
87 
88  public:
90  bool empty() const;
91 
93  explicit operator bool() const
94  { return !empty(); }
95 
96  public:
98  const std::string & command() const;
99 
103  void setCommand( const std::string & command_r );
104 
106  bool isAckCommand() const
107  { return command() == ackCommand(); }
108 
110  bool isErrorCommand() const
111  {return command() == errorCommand(); }
112 
114  bool isEnomethodCommand() const
115  {return command() == enomethodCommand(); }
116 
118  const std::string & body() const;
119 
128  std::string & bodyRef();
129 
131  void setBody( const std::string & body_r );
132 
133  public:
135  typedef std::multimap<std::string, std::string> HeaderList;
136 
138  typedef HeaderList::const_iterator HeaderListIterator;
139 
140  private:
143 
144  public:
146  const HeaderList & headerList() const;
147 
149  bool headerEmpty() const
150  { return headerList().empty(); }
151 
153  unsigned headerSize() const
154  { return headerList().size(); }
155 
158  { return headerList().begin(); }
159 
162  { return headerList().end(); }
163 
165  void headerClear()
166  { headerList().clear(); }
167 
168 
170  bool hasKey( const std::string & key_r ) const
171  { return ! keyEmpty( key_r ); }
172 
174  bool keyEmpty( const std::string & key_r ) const
175  { return headerList().find( key_r ) == headerEnd(); }
176 
178  bool keySize( const std::string & key_r ) const
179  { return headerList().count( key_r ); }
180 
182  HeaderListIterator keyBegin( const std::string & key_r ) const
183  { return headerList().lower_bound( key_r ); }
184 
186  HeaderListIterator keyEnd( const std::string & key_r ) const
187  { return headerList().upper_bound( key_r ); }
188 
189 
194  const std::string & getHeader( const std::string & key_r ) const;
195 
199  const std::string & getHeader( const std::string & key_r, const std::string & default_r ) const;
200 
202  const std::string & getHeaderNT( const std::string & key_r, const std::string & default_r = std::string() ) const;
203 
208  void setHeader( const std::string & key_r, const std::string & value_r = std::string() );
209 
214  void setHeader( HeaderInitializerList contents_r )
215  { headerList().clear(); addHeader( contents_r ); }
216 
221  void addHeader( const std::string & key_r, const std::string & value_r = std::string() );
223  void addHeader( HeaderInitializerList contents_r );
224 
226  void clearHeader( const std::string & key_r );
227 
228  public:
232  std::ostream & writeTo( std::ostream & stream_r ) const;
233 
235  static std::ostream & writeTo( std::ostream & stream_r, const PluginFrame & frame_r )
236  { return frame_r.writeTo( stream_r ); }
237 
241  std::istream & readFrom( std::istream & stream_r )
242  { *this = PluginFrame( stream_r ); return stream_r; }
243 
245  static std::istream & readFrom( std::istream & stream_r, PluginFrame & frame_r )
246  { frame_r = PluginFrame( stream_r ); return stream_r; }
247 
248  public:
250  class Impl;
251  private:
254  };
255 
257  std::ostream & operator<<( std::ostream & str, const PluginFrame & obj );
258 
260  inline std::ostream & dumpOn( std::ostream & str, const PluginFrame & obj )
261  { if ( str ) try { PluginFrame::writeTo( str, obj ); } catch(...){}; return str; }
262 
264  inline std::istream & operator>>( std::istream & str, PluginFrame & obj )
265  { return PluginFrame::readFrom( str, obj ); }
266 
268  bool operator==( const PluginFrame & lhs, const PluginFrame & rhs );
269 
271  inline bool operator!=( const PluginFrame & lhs, const PluginFrame & rhs )
272  { return( ! operator==( lhs, rhs ) ); }
273 
275 } // namespace zypp
277 #endif // ZYPP_PLUGINFRAME_H
bool keySize(const std::string &key_r) const
Return number of header entries for key_r.
Definition: PluginFrame.h:178
friend bool operator==(const PluginFrame &lhs, const PluginFrame &rhs)
Definition: PluginFrame.cc:336
PluginFrame()
Default ctor (empty frame)
Definition: PluginFrame.cc:259
Command frame for communication with PluginScript.
Definition: PluginFrame.h:40
void setCommand(const std::string &command_r)
Set the frame command.
Definition: PluginFrame.cc:289
const std::string & command() const
Return the frame command.
Definition: PluginFrame.cc:286
std::istream & operator>>(std::istream &str, PluginFrame &obj)
Definition: PluginFrame.h:264
HeaderListIterator keyEnd(const std::string &key_r) const
Return iterator pointing behind the last header for key_r.
Definition: PluginFrame.h:186
std::ostream & dumpOn(std::ostream &str, const PluginFrame &obj)
Definition: PluginFrame.h:260
bool isEnomethodCommand() const
Convenience to identify an _ENOMETHOD command.
Definition: PluginFrame.h:114
bool operator!=(const PluginFrame &lhs, const PluginFrame &rhs)
Definition: PluginFrame.h:271
HeaderListIterator keyBegin(const std::string &key_r) const
Return iterator pointing to the 1st header for key_r (or keyEnd(key_r))
Definition: PluginFrame.h:182
void addHeader(const std::string &key_r, const std::string &value_r=std::string())
Add header for key_r leaving already existing headers for key_r unchanged.
Definition: PluginFrame.cc:322
bool isErrorCommand() const
Convenience to identify an ERROR command.
Definition: PluginFrame.h:110
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
static std::istream & readFrom(std::istream &stream_r, PluginFrame &frame_r)
Definition: PluginFrame.h:245
static const std::string & errorCommand()
"ERROR" command.
Definition: PluginFrame.cc:247
void headerClear()
Clear the list of headers.
Definition: PluginFrame.h:165
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
static const std::string & enomethodCommand()
"_ENOMETHOD" command.
Definition: PluginFrame.cc:253
bool keyEmpty(const std::string &key_r) const
Definition: PluginFrame.h:174
unsigned headerSize() const
Return size of the header list.
Definition: PluginFrame.h:153
Base class for PluginFrame Exception.
const std::initializer_list< std::pair< std::string, std::string > > & HeaderInitializerList
Definition: PluginFrame.h:45
bool isAckCommand() const
Convenience to identify an ACK command.
Definition: PluginFrame.h:106
friend std::ostream & operator<<(std::ostream &str, const PluginFrame &obj)
Definition: PluginFrame.cc:333
bool headerEmpty() const
Whether header list is empty.
Definition: PluginFrame.h:149
static const std::string & ackCommand()
"ACK" command.
Definition: PluginFrame.cc:241
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: PluginFrame.h:250
void setBody(const std::string &body_r)
Set the frame body.
Definition: PluginFrame.cc:298
HeaderListIterator headerEnd() const
Return iterator pointing behind the last header.
Definition: PluginFrame.h:161
std::multimap< std::string, std::string > HeaderList
The header list.
Definition: PluginFrame.h:135
void setHeader(HeaderInitializerList contents_r)
Set a new header list.
Definition: PluginFrame.h:214
const std::string & getHeader(const std::string &key_r) const
Return header value for key_r.
Definition: PluginFrame.cc:310
bool hasKey(const std::string &key_r) const
Whether the header list contains at least one entry for key_r.
Definition: PluginFrame.h:170
HeaderList::const_iterator HeaderListIterator
Header list iterator.
Definition: PluginFrame.h:138
HeaderListIterator headerBegin() const
Return iterator pointing to the 1st header (or headerEnd)
Definition: PluginFrame.h:157
void setHeader(const std::string &key_r, const std::string &value_r=std::string())
Set header for key_r removing all other occurrences of key_r.
Definition: PluginFrame.cc:319
HeaderList & headerList()
Modifyalble header list for internal use only.
Definition: PluginFrame.cc:304
PluginFrame implementation.
Definition: PluginFrame.cc:32
std::ostream & writeTo(std::ostream &stream_r) const
Write frame to stream.
Definition: PluginFrame.cc:301
std::istream & readFrom(std::istream &stream_r)
Read frame from stream.
Definition: PluginFrame.h:241
std::string & bodyRef()
Return a reference to the frame body.
Definition: PluginFrame.cc:295
bool empty() const
Whether this is an empty frame.
Definition: PluginFrame.cc:283
void clearHeader(const std::string &key_r)
Remove all headers for key_r.
Definition: PluginFrame.cc:328
static std::ostream & writeTo(std::ostream &stream_r, const PluginFrame &frame_r)
Definition: PluginFrame.h:235
PluginFrameException Exception
Default exception type.
Definition: PluginFrame.h:57
const std::string & getHeaderNT(const std::string &key_r, const std::string &default_r=std::string()) const
Not throwing version returing one of the matching header values or default_r string.
Definition: PluginFrame.cc:316
const std::string & body() const
Return the frame body.
Definition: PluginFrame.cc:292