[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: [RFC] kdebug patch
From:       Thomas Braxton <brax108 () cox ! net>
Date:       2005-09-02 3:52:56
Message-ID: 200509012252.56229.brax108 () cox ! net
[Download RAW message or body]

On Thursday 01 September 2005 01:06 pm, David Faure wrote:
> > 2) The implementation of operator<< for most classes should modify
> > d->output directly instead of calling operator<<(QString).
> > If you want I can get these changed right away, then I think I'll be done
> > with kdebug
>
> Interesting - sounds like a speedup, but also it would avoid the repeated
> area name in front of every newline in the printed string, AFAICS?
> Feel free to submit a patch for that (or to ask for a SVN account if you
> plan on doing more work on KDE ;)
here is the patch for this.

["kdebug_cpp_cleanup.diff" (text/x-diff)]

Index: kdecore/kdebug.cpp
===================================================================
--- kdecore/kdebug.cpp	(revision 456029)
+++ kdecore/kdebug.cpp	(working copy)
@@ -444,7 +444,7 @@
 
 kdbgstream& kdbgstream::operator<<(const QString& string)
 {
-    if (!d->print)
+    if ( !d->print )
         return *this;
 
     d->output += string;
@@ -458,54 +458,54 @@
   if (!d->print)
       return *this;
 
-  QString string;
-  // -----
-  if(widget==0)
-    {
-      string = QString::fromAscii("[Null pointer]");
-    } else {
-      string = QString::fromAscii("[%1 pointer(0x%2)").arg(QString::fromUtf8(widget->className()))
-                    .arg(QString::number(ulong(widget), 16)
-		         .rightJustified(8, QLatin1Char('0')));
+  if(widget==0) {
+      d->output += QLatin1String("[Null pointer]");
+  } else {
+      d->output += QString::fromAscii("[%1 pointer(0x%2)")
+                         .arg(QString::fromUtf8(widget->className()))
+                         .arg(QString::number(ulong(widget), 16)
+		              .rightJustified(8, QLatin1Char('0')));
       if(widget->objectName().isEmpty()) {
-	  string += QString::fromAscii( " to unnamed widget, " );
+	  d->output += QLatin1String( " to unnamed widget, " );
       } else {
-	  string += QString::fromAscii(" to widget %1, ").arg(widget->objectName());
+	  d->output += QString::fromAscii(" to widget %1, ")
+	                    .arg(widget->objectName());
       }
-      string += QString::fromAscii("geometry=%1x%2+%3+%4]").arg(widget->width())
-                .arg(widget->height())
-                .arg(widget->x())
-                .arg(widget->y());
-    }
-  return *this << string;
+      d->output += QString::fromAscii("geometry=%1x%2+%3+%4]")
+                       .arg(widget->width()).arg(widget->height())
+                       .arg(widget->x()).arg(widget->y());
+  }
+  return *this;
 }
-/*
- * either use 'output' directly and do the flush if needed
- * or use the QString operator which calls the char* operator
- *
- */
 kdbgstream& kdbgstream::operator<<( const QDateTime& time) {
-    *this << time.toString();
+    if ( d->print )
+        d->output += time.toString();
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QDate& date) {
-    *this << date.toString();
+    if ( d->print )
+        d->output += date.toString();
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QTime& time ) {
-    *this << time.toString();
+    if ( d->print )
+        d->output += time.toString();
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( KDBGFUNC f ) {
-    if (!d->print) return *this;
-    return (*f)(*this);
+    if ( d->print )
+        return (*f)(*this);
+    return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QPoint& p ) {
-    *this << QString::fromAscii("(%1, %2)").arg(p.x()).arg(p.y());
+    if ( d->print )
+        d->output += QString::fromAscii("(%1, %2)").arg(p.x()).arg(p.y());
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QSize& s ) {
-    *this << QString::fromAscii("[%1x%2]").arg(s.width()).arg(s.height());
+    if ( d->print )
+        d->output += QString::fromAscii("[%1x%2]").arg(s.width())
+	                 .arg(s.height());
     return *this;
 }
 static QString s_rectString(const QRect& r)
@@ -514,21 +514,27 @@
     return str.arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
 }
 kdbgstream& kdbgstream::operator<<( const QRect& r ) {
-    *this << s_rectString( r );
+    if( d->print )
+        d->output += s_rectString( r );
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QRegion& reg ) {
-    *this<< "[ ";
+    if( !d->print )
+        return *this;
 
+    d->output += QLatin1String( "[ " );
+
     QVector<QRect>rs=reg.rects();
     for (int i=0;i<rs.size();++i)
-        *this << s_rectString( rs[i] ) + QLatin1Char( ' ' );
+        d->output += s_rectString( rs[i] ) + QLatin1Char( ' ' );
 
-    *this <<"]";
+    d->output += QLatin1String( "]" );
+
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const KURL& u ) {
-    *this << u.prettyURL();
+    if ( d->print )
+        d->output += u.prettyURL();
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QStringList& l ) {
@@ -536,13 +542,15 @@
     return *this << static_cast<QList<QString> >(l);
 }
 static QString s_makeColorName(const QColor& c) {
-    QString s = QString::fromAscii("(invalid/default)");
+    QString s = QLatin1String("(invalid/default)");
     if ( c.isValid() )
         s = c.name();
     return s;
 }
 kdbgstream& kdbgstream::operator<<( const QColor& c ) {
-    return *this << s_makeColorName( c );
+    if ( d->print )
+        d->output += s_makeColorName( c );
+    return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QPen& p ) {
     static const char* const s_penStyles[] = {
@@ -550,15 +558,21 @@
         "DashDotDotLine" };
     static const char* const s_capStyles[] = {
         "FlatCap", "SquareCap", "RoundCap" };
-    *this << "[ style:" << s_penStyles[ p.style() ];
-    *this << " width:" << p.width();
-    *this << " color:" << s_makeColorName( p.color() );
+
+    if ( !d->print )
+        return *this;
+
+    d->output += QLatin1String("[ style:");
+    d->output += QLatin1String(s_penStyles[ p.style() ]);
+    d->output += QString::fromAscii(" width:%1").arg(p.width());
+    d->output += QLatin1String(" color:") + s_makeColorName( p.color() );
     if ( p.width() > 0 ) // cap style doesn't matter, otherwise
     {
-        *this << " capstyle:" << s_capStyles[ p.capStyle() >> 4 ];
+        d->output += QLatin1String(" capstyle:") +
+	             QLatin1String(s_capStyles[ p.capStyle() >> 4 ]);
         // join style omitted
     }
-    *this <<" ]";
+    d->output += QLatin1String(" ]");
     return *this;
 }
 kdbgstream& kdbgstream::operator<<( const QBrush& b) {
@@ -570,21 +584,28 @@
         "RadialGradientPattern", "TexturePattern"
     };
 
-    *this <<"[ style: " << s_brushStyles[ b.style() ];
-    *this <<" color: " << s_makeColorName( b.color() );
+    d->output += QLatin1String("[ style: ");
+    d->output += QLatin1String(s_brushStyles[ b.style() ]);
+    d->output += QLatin1String(" color: ");
+    d->output += s_makeColorName( b.color() );
     if ( b.pixmap() )
-        *this <<" has a pixmap";
-    *this <<" ]";
+        d->output += QLatin1String(" has a pixmap");
+    d->output += QLatin1String(" ]");
     return *this;
 }
 
 kdbgstream& kdbgstream::operator<<( const QVariant& v) {
-    *this << "[variant: " << v.typeName();
+    if ( !d->print )
+        return *this;
+
+    d->output += QLatin1String("[variant: ") +
+                 QLatin1String( v.typeName() );
+    
     // For now we just attempt a conversion to string.
     // Feel free to switch(v.type()) and improve the output.
     if ( v.canConvert(QVariant::String) )
-        *this << " toString=" << v.toString();
-    *this << "]";
+        d->output += QLatin1String(" toString=") + v.toString();
+    d->output += QLatin1String("]");
     return *this;
 }
 
@@ -607,7 +628,7 @@
             d->output += QLatin1String("...");
         d->output += QLatin1Char(']');
     } else {
-        d->output += QString::fromAscii( data ); // using ascii as advertised
+        d->output += QLatin1String( data ); // using ascii as advertised
     }
     return *this;
 }
@@ -628,8 +649,8 @@
 
     for (int i = 0; i < n; ++i)
         s += QString::number(i) +
-             QString::fromAscii(": ") +
-             QString::fromLatin1(strings[i]) + QString::fromAscii("\n");
+             QLatin1String(": ") +
+             QString::fromLatin1(strings[i]) + QLatin1String("\n");
     s += QLatin1String("]\n");
     if (strings)
         free (strings);


 =

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscrib=
e <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic