[vte] lib: Add to_string() debug functions for helper classes



commit d698ede30f5daa2a3bea125f305e6bbff650ab73
Author: Christian Persch <chpe gnome org>
Date:   Thu Dec 10 18:42:18 2015 +0100

    lib: Add to_string() debug functions for helper classes
    
    Plain old char* buffers, not an overloaded operator<< since we
    still don't depend on libstdc++.

 src/vtetypes.cc |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/vtetypes.hh |   15 +++++++++++
 2 files changed, 87 insertions(+), 0 deletions(-)
---
diff --git a/src/vtetypes.cc b/src/vtetypes.cc
index 8dbd2fd..3d88910 100644
--- a/src/vtetypes.cc
+++ b/src/vtetypes.cc
@@ -112,6 +112,63 @@ vte::color::rgb::parse(char const* spec)
        return retval;
 }
 
+#ifdef VTE_DEBUG
+
+#define DEBUG_STRING_SIZE (256)
+#define DEBUG_STRING_SLICES (64)
+
+static char*
+debug_get_buf(void)
+{
+        static char *buf = NULL;
+        static gsize offset = 0;
+
+        if (buf != NULL) {
+                offset = (offset + 1) % DEBUG_STRING_SLICES;
+        } else {
+                buf = g_new0(char, DEBUG_STRING_SIZE * DEBUG_STRING_SLICES);
+        }
+        return buf + offset * DEBUG_STRING_SIZE;
+}
+
+char const*
+vte::grid::coords::to_string() const
+{
+        char *buf = debug_get_buf();
+        g_snprintf(buf, DEBUG_STRING_SIZE, "grid[%ld,%ld]", row(), column());
+        return buf;
+}
+
+char const*
+vte::grid::span::to_string() const
+{
+        if (empty())
+                return "grid[empty]";
+
+        char *buf = debug_get_buf();
+        g_snprintf(buf, DEBUG_STRING_SIZE, "grid[%ld,%ld .. %ld,%ld]",
+                   start_row(), start_column(), end_row(), end_column());
+        return buf;
+}
+
+char const*
+vte::view::coords::to_string() const
+{
+        char *buf = debug_get_buf();
+        g_snprintf(buf, DEBUG_STRING_SIZE, "view[%ld,%ld]", x, y);
+        return buf;
+}
+
+char const*
+vte::color::rgb::to_string() const
+{
+        char *buf = debug_get_buf();
+        g_snprintf(buf, DEBUG_STRING_SIZE, "rgb(%04x,%04x,%04x)", red, green, blue);
+        return buf;
+}
+
+#endif /* VTE_DEBUG */
+
 #ifdef MAIN
 
 #include <glib.h>
@@ -164,6 +221,11 @@ test_grid_coords (void)
         g_assert_true (coords(42, 42) <  coords(43, 160));
         g_assert_false(coords(42, 42) >= coords(43, 160));
         g_assert_false(coords(42, 42) >  coords(43, 160));
+
+#ifdef VTE_DEBUG
+        /* to_string() */
+        g_assert_cmpstr(vte::grid::coords(17, 42).to_string(), ==, "grid[17,42]");
+#endif
 }
 
 static void
@@ -257,6 +319,11 @@ test_grid_span (void)
         g_assert_false(s8.box_contains(coords(33, 15)));
         g_assert_false(s8.box_contains(coords(33, 24)));
         g_assert_false(s8.box_contains(coords(3, 42)));
+
+#ifdef VTE_DEBUG
+        /* to_string() */
+        g_assert_cmpstr(vte::grid::span(17, 42, 18, 3).to_string(), ==, "grid[17,42 .. 18,3]");
+#endif
 }
 
 static void
@@ -284,6 +351,11 @@ test_view_coords (void)
         p5.swap(p3);
         g_assert_true(p3 == p4);
         g_assert_true(p5 == p2);
+
+#ifdef VTE_DEBUG
+        /* to_string() */
+        g_assert_cmpstr(vte::view::coords(256, 512).to_string(), ==, "view[256,512]");
+#endif
 }
 
 
diff --git a/src/vtetypes.hh b/src/vtetypes.hh
index 108dded..4d6bcb6 100644
--- a/src/vtetypes.hh
+++ b/src/vtetypes.hh
@@ -21,6 +21,12 @@
 #include <gdk/gdk.h>
 #include <errno.h>
 
+#ifdef VTE_DEBUG
+#define IFDEF_DEBUG(str) str
+#else
+#define IFDEF_DEBUG(str)
+#endif
+
 namespace vte {
 
 namespace grid {
@@ -47,6 +53,8 @@ namespace grid {
                 inline bool operator >  (coords const& rhs) const { return m_row > rhs.m_row || (m_row == 
rhs.m_row && m_column >  rhs.m_column); }
                 inline bool operator >= (coords const& rhs) const { return m_row > rhs.m_row || (m_row == 
rhs.m_row && m_column >= rhs.m_column); }
 
+                IFDEF_DEBUG(char const* to_string() const);
+
         private:
                 row_t m_row;
                 column_t m_column;
@@ -79,6 +87,8 @@ namespace grid {
                 inline bool box_contains(coords const& p) const { return m_start.row() <= p.row() && p.row() 
<= m_end.row() &&
                                                                          m_start.column() <= p.column() && 
p.column() <= m_end.column(); }
 
+                IFDEF_DEBUG(char const* to_string() const);
+
         private:
                 coords m_start;
                 coords m_end;
@@ -101,6 +111,9 @@ namespace view {
                 inline bool operator != (coords const& rhs) const { return x != rhs.x || y != rhs.y; }
 
                 void swap(coords &rhs) { coords tmp = rhs; rhs = *this; *this = tmp; }
+
+                IFDEF_DEBUG(char const* to_string() const);
+
         public:
                 coord_t x;
                 coord_t y;
@@ -130,6 +143,8 @@ namespace color {
                 inline bool operator == (rgb const& rhs) const {
                         return red == rhs.red && green == rhs.green && blue == rhs.blue;
                 }
+
+                IFDEF_DEBUG(char const* to_string() const);
         };
 
 } /* namespace color */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]