Allow initializer_list to define PluginFrame header values
authorMichael Andres <ma@suse.de>
Wed, 20 Nov 2013 15:17:32 +0000 (16:17 +0100)
committerMichael Andres <ma@suse.de>
Wed, 20 Nov 2013 15:23:38 +0000 (16:23 +0100)
tests/zypp/PluginFrame_test.cc
zypp/PluginFrame.cc
zypp/PluginFrame.h

index c64cf43..cac4f3b 100644 (file)
@@ -50,12 +50,14 @@ BOOST_AUTO_TEST_CASE(PluginFrameCtorAssign)
   h.addHeader( "" ); // empty KV in header is ok, if you like it
   BOOST_CHECK_EQUAL( (g == h), false );
 
-  h.addHeader( "a", "a1" );
-  h.addHeader( "a", "a2" );
-  h.addHeader( "b", "b1" );
-  h.addHeader( "b", "b2" );
-  h.addHeader( "c", "c1" );
-  h.addHeader( "c", "c1" );
+  h.addHeader({
+    { "a", "a1" },
+    { "a", "a2" },
+    { "b", "b1" },
+    { "b", "b2" },
+    { "c", "c1" },
+    { "c", "c1" }
+  });
   BOOST_CHECK_EQUAL( h.headerSize(), 7 );
 
   h.setHeader( "b", "b" ); // replaces existing 'b:" headers
index b05815b..6e4e4ad 100644 (file)
@@ -17,6 +17,9 @@
 
 using std::endl;
 
+#undef  ZYPP_BASE_LOGGER_LOGGROUP
+#define ZYPP_BASE_LOGGER_LOGGROUP "zypp::plugin"
+
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
@@ -35,10 +38,17 @@ namespace zypp
       Impl( const std::string & command_r )
       { setCommand( command_r ); }
 
-      Impl( const std::string & command_r, const std::string body_r )
+      Impl( const std::string & command_r, const std::string body_r )
        : _body( body_r )
       { setCommand( command_r ); }
 
+      Impl( const std::string & command_r, HeaderInitializerList contents_r )
+      { setCommand( command_r ); addHeader( contents_r ); }
+
+      Impl( const std::string & command_r, const std::string & body_r, HeaderInitializerList contents_r )
+       : _body( body_r )
+      { setCommand( command_r ); addHeader( contents_r ); }
+
       Impl( std::istream & stream_r );
 
     public:
@@ -122,6 +132,12 @@ namespace zypp
        _header.insert( mkHeaderPair( key_r, value_r ) );
       }
 
+      void addHeader( HeaderInitializerList contents_r )
+      {
+       for ( const auto & el : contents_r )
+         addHeader( el.first, el.second );
+      }
+
       void clearHeader( const std::string & key_r )
       {
        _header.erase( key_r );
@@ -234,6 +250,12 @@ namespace zypp
     return _val;
   }
 
+  const std::string & PluginFrame::enomethodCommand()
+  {
+    static std::string _val( "_ENOMETHOD" );
+    return _val;
+  }
+
   PluginFrame::PluginFrame()
     : _pimpl( Impl::nullimpl() )
   {}
@@ -242,10 +264,18 @@ namespace zypp
     : _pimpl( new Impl( command_r ) )
   {}
 
-  PluginFrame::PluginFrame( const std::string & command_r, const std::string body_r )
+  PluginFrame::PluginFrame( const std::string & command_r, const std::string body_r )
     : _pimpl( new Impl( command_r, body_r ) )
   {}
 
+  PluginFrame::PluginFrame( const std::string & command_r, HeaderInitializerList contents_r )
+    : _pimpl( new Impl( command_r, contents_r ) )
+  {}
+
+  PluginFrame::PluginFrame( const std::string & command_r, const std::string & body_r, HeaderInitializerList contents_r )
+    : _pimpl( new Impl( command_r, body_r, contents_r ) )
+  {}
+
   PluginFrame::PluginFrame( std::istream & stream_r )
     : _pimpl( new Impl( stream_r ) )
   {}
@@ -292,6 +322,9 @@ namespace zypp
   void PluginFrame::addHeader( const std::string & key_r, const std::string & value_r )
   { _pimpl->addHeader( key_r, value_r ); }
 
+  void PluginFrame::addHeader( HeaderInitializerList contents_r )
+  { _pimpl->addHeader( contents_r ); }
+
   void PluginFrame::clearHeader( const std::string & key_r )
   { _pimpl->clearHeader( key_r ); }
 
index 92fddf7..8683fe2 100644 (file)
@@ -42,11 +42,15 @@ namespace zypp
     friend std::ostream & operator<<( std::ostream & str, const PluginFrame & obj );
     friend bool operator==( const PluginFrame & lhs, const PluginFrame & rhs );
 
+    typedef const std::initializer_list<std::pair<std::string,std::string>> & HeaderInitializerList;
+
     public:
       /** "ACK" command. */
       static const std::string & ackCommand();
       /** "ERROR" command. */
       static const std::string & errorCommand();
+      /** "_ENOMETHOD" command. */
+      static const std::string & enomethodCommand();
 
     public:
       /** Default exception type */
@@ -63,7 +67,17 @@ namespace zypp
       /** Ctor taking command and body
        * \throw PluginFrameException If \ref setCommand throws
        */
-      PluginFrame( const std::string & command_r, const std::string body_r );
+      PluginFrame( const std::string & command_r, const std::string & body_r );
+
+      /** Ctor taking the command and a HeaderInitializerList
+       * \throw PluginFrameException If \ref setCommand throws
+       */
+      PluginFrame( const std::string & command_r, HeaderInitializerList contents_r );
+
+      /** Ctor taking command, body and a HeaderInitializerList
+       * \throw PluginFrameException If \ref setCommand throws
+       */
+      PluginFrame( const std::string & command_r, const std::string & body_r, HeaderInitializerList contents_r );
 
       /** Ctor reading frame data from a stream
        * \throw PluginFrameException On error reading from stream
@@ -96,6 +110,10 @@ namespace zypp
       bool isErrorCommand() const
       {return command() == errorCommand(); }
 
+      /** Convenience to identify an _ENOMETHOD command. */
+      bool isEnomethodCommand() const
+      {return command() == enomethodCommand(); }
+
       /** Return the frame body. */
       const std::string & body() const;
 
@@ -189,11 +207,20 @@ namespace zypp
        */
       void setHeader( const std::string & key_r, const std::string & value_r = std::string() );
 
+      /** Set a new header list
+       * \throw PluginFrameException If key contains illegal chars (\c NL or \c :)
+       * \throw PluginFrameException If value contains illegal chars (\c NL)
+       */
+      void setHeader( HeaderInitializerList contents_r )
+      { headerList().clear(); addHeader( contents_r ); }
+
       /** Add header for \c key_r leaving already existing headers for \c key_r unchanged.
        * \throw PluginFrameException If key contains illegal chars (\c NL or \c :)
        * \throw PluginFrameException If value contains illegal chars (\c NL)
        */
       void addHeader( const std::string & key_r, const std::string & value_r = std::string() );
+      /** \overload taking an initializer_list */
+      void addHeader( HeaderInitializerList contents_r );
 
       /** Remove all headers for \c key_r. */
       void clearHeader( const std::string & key_r );