How do you see the value of your class String?



  • He wrote his class of line-- String♪ But to see the variable type. String Total Visual Studio or Qt Creator I can't. std::string)

    How do IDE show the meaning of such variables?



  • Look.

    For new versions, Visual Studio (since 2012) is best used in natvis. At the same time, you can, for example, include visualization of your types directly into the project, and Visual Studio will even cover the visual changes right into the summer, during the rendezvous.

    Let's figure out a simple example. Let's create a simple caste class representing the line.

    #include "stdafx.h"
    #include <cstring>
    

    namespace utils
    {
    class MyString
    {
    size_t length;
    char* payload;

    public:
        MyString() : length(0), payload(nullptr) { }
        MyString(char* data) : length(strlen(data)), payload(new char[length + 1])
        { strcpy_s(payload, length + 1, data); }
    
        ~MyString() { delete[] payload; }
    };
    

    }

    And the simplest test:

    int main()
    {
    utils::MyString s1;
    utils::MyString s2("I am string");
    }

    Now we add our caste visualization. We're going out the project window.

    добавить файл...

    and add a new natvis file. Call him MyString.natvis.

    ... добавили

    We'll get the next file:

    <?xml version="1.0" encoding="utf-8"?>
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    </AutoVisualizer>

    We'll add to it our visualization. Put it down. Type with the name of our type (including namespace). Substrate DisplayString Makes me wonder how the guy's gonna look in one line. A condition may be addedDisplayString Condition=whichever is different, the value will be different. Substrate Expand Manages how the data will be displayed in a &quot; dry &quot; form. (HML, don't forget that < and > For templates, you're going to have to introduce &lt; and &gt;like Name="std::vector&lt;*&gt;"♪ The sound is used to indicate the arbitrary argument of the template.)

    <?xml version="1.0" encoding="utf-8"?>
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
    <Type Name="utils::MyString">
    <DisplayString Condition="payload == 0">[empty string]</DisplayString>
    <DisplayString>{payload,s}, len={length,d}</DisplayString>
    <Expand>
    <Item Name="Length">length,d</Item>
    <Item Name="Content">payload, sb</Item>
    </Expand>
    </Type>
    </AutoVisualizer>

    We get this picture in the window Watch:

    а вот и результат

    Additional reading on:

    • Detailed information on the syntax of natvis and its phishes is available https://msdn.microsoft.com/en-us/library/jj620914.aspx#Anchor_8 ♪
    • Review of new opportunities for natvis in Visual Studio 2015 https://blogs.msdn.microsoft.com/vcblog/2015/09/28/debug-visualizers-in-visual-c-2015/ ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2