{"id":18081,"date":"2024-12-17T19:14:13","date_gmt":"2024-12-17T19:14:13","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18081"},"modified":"2024-12-17T19:38:20","modified_gmt":"2024-12-17T19:38:20","slug":"pretty-printer-own-type","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18081","title":{"rendered":"Pretty printer &#8211; Own type"},"content":{"rendered":"<p><strong>Example code<\/strong><\/p>\n<pre class=\"brush: cpp; title: main.cpp; notranslate\" title=\"main.cpp\">\r\nstruct List\r\n{\r\n    struct Node\r\n    {\r\n        char val{};\r\n        Node* next{};\r\n    };\r\n    Node* first{};\r\n    Node* last{};\r\n\r\n    void add(char val)\r\n    {\r\n        Node* node = new Node;\r\n        node-&gt;val = val;\r\n\r\n        if (!first) { first = node; }\r\n        if (last) { last-&gt;next = node; }\r\n        last = node;\r\n    }\r\n};\r\n\r\nint main()\r\n{\r\n    List list;\r\n    list.add(&#039;A&#039;);\r\n    list.add(&#039;B&#039;);\r\n    list.add(&#039;C&#039;);\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Without pretty printer<\/strong><\/p>\n<pre>\r\n(gdb) b 29\r\n(gdb) r\r\nBreakpoint 1, main () at main.cpp:29\r\n29\t\treturn 0;\r\n(gdb) p list\r\n$1 = {first = 0x55555556b2b0, last = 0x55555556b2f0}\r\n<\/pre>\n<hr>\n<p><strong>With pretty printer<\/strong><\/p>\n<pre class=\"brush: python; title: list_printer.py; notranslate\" title=\"list_printer.py\">\r\nimport gdb\r\n\r\nclass ListPrinter:\r\n    def __init__(self, val):\r\n        self.val = val\r\n\r\n    def to_string(self):\r\n        elements = []\r\n        node = self.val[&#039;first&#039;]\r\n        while node:\r\n            char_val = chr(int(node[&#039;val&#039;]))\r\n            elements.append(f&quot;{char_val}&quot;)\r\n            node = node[&#039;next&#039;]\r\n        return &quot;[ &quot; + &quot;, &quot;.join(elements) + &quot; ]&quot;\r\n\r\ndef register_printers(obj):\r\n    if obj is None:\r\n        obj = gdb\r\n    obj.pretty_printers.append(lookup_printer)\r\n\r\ndef lookup_printer(val):\r\n    if str(val.type) == &#039;List&#039;:\r\n        return ListPrinter(val)\r\n    return None\r\n\r\nregister_printers(None)\r\n<\/pre>\n<pre>\r\n(gdb) source list_printer.py\r\n(gdb) b 29\r\n(gdb) r\r\nBreakpoint 1, main () at main.cpp:29\r\n29\t    return 0;\r\n(gdb) p list\r\n$1 = [ A, B, C ]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example code Without pretty printer (gdb) b 29 (gdb) r Breakpoint 1, main () at main.cpp:29 29 return 0; (gdb)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18081"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18081"}],"version-history":[{"count":12,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18081\/revisions"}],"predecessor-version":[{"id":18130,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18081\/revisions\/18130"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18081"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}