Opened 9 years ago

Closed 8 years ago

#8846 closed Patches (fixed)

Improvements to Spirit Qi Qt QString custom_string.cpp example

Reported by: Mateusz Loskot Owned by: Joel de Guzman
Milestone: To Be Determined Component: spirit
Version: Boost Development Trunk Severity: Optimization
Keywords: spirit qi qt qstring Cc: tim@…

Description

Tim Angus asked me to submit this patch (Boost Trac was complaining for him with "Submission rejected as potential spam").

There is an example of how to use Qi with a custom string (in this case a QString from the Qt framework). This is very helpful but it doesn't work in conjunction with the debug facilities. The following patch fixes this by adding a couple of template specialisations.

Patch:

Index: libs/spirit/example/qi/custom_string.cpp
===================================================================
--- libs/spirit/example/qi/custom_string.cpp	(revision 85071)
+++ libs/spirit/example/qi/custom_string.cpp	(working copy)
@@ -34,6 +34,26 @@
             return true;
         }
     };
+
+    // Test if a QString is empty (required for debug)
+    template <>
+    struct is_empty_container<QString>
+    {
+        static bool call(QString const& c)
+        {
+            return c.isEmpty();
+        }
+    };
+
+    // Define how to stream a QString (required for debug)
+    template <typename Out, typename Enable>
+    struct print_attribute_debug<Out, QString, Enable>
+    {
+        static void call(Out& out, QString const& val)
+        {
+            out << val.toStdString();
+        }
+    };

Change History (4)

comment:1 by anonymous, 8 years ago

This is great ! I has exactly the same problem (debugging QStrings did no longer work after a Qt update) and this solved it. Many thanks !

comment:2 by Joel de Guzman, 8 years ago

Could you please prepare a pull request instead: https://github.com/boostorg/spirit

comment:3 by Mateusz Loskot, 8 years ago

comment:4 by Joel de Guzman, 8 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.