Project

General

Profile

displaying custom data types in a table Wt::WTableView

Added by Anton Erykalin about 1 year ago

Hello.
Help please.
I have defined my class based on std::string class.

class IpString : public std::string
{
    using base = std::string;
public:
    using base::base;
    static std::vector<int> ipV4StringToInt(IpString ipString);


    friend bool operator< (const IpString &left,  const IpString &right)
    {
        std::vector<int> lToInt = IpString::ipV4StringToInt(left);
        std::vector<int> rToInt = IpString::ipV4StringToInt(right);
        return (lToInt[0]<=rToInt[0]) && (lToInt[1]<=rToInt[1]) && (lToInt[2]<=rToInt[2]) && (lToInt[3]<rToInt[3]);
    }
    friend bool operator> (const IpString &left,  const IpString &right)
    {
        std::vector<int> lToInt = IpString::ipV4StringToInt(left);
        std::vector<int> rToInt = IpString::ipV4StringToInt(right);
        return (lToInt[0]>=rToInt[0]) && (lToInt[1]>=rToInt[1]) && (lToInt[2]>=rToInt[2]) && (lToInt[3]>rToInt[3]);
    }
    friend bool operator<= (const IpString &left,  const IpString &right)
    {
        std::vector<int> lToInt = IpString::ipV4StringToInt(left);
        std::vector<int> rToInt = IpString::ipV4StringToInt(right);
        return (lToInt[0]<=rToInt[0]) && (lToInt[1]<=rToInt[1]) && (lToInt[2]<=rToInt[2]) && (lToInt[3]<=rToInt[3]);
    }
    friend bool operator>= (const IpString &left,  const IpString &right)
    {
        std::vector<int> lToInt = IpString::ipV4StringToInt(left);
        std::vector<int> rToInt = IpString::ipV4StringToInt(right);
        return (lToInt[0]>=rToInt[0]) && (lToInt[1]>=rToInt[1]) && (lToInt[2]>=rToInt[2]) && (lToInt[3]>=rToInt[3]);
    }
};

I am giving a dataset of objects of this class to a table, but nothing is showing up in the table. That is, I get just empty fields equal to the number of front rows for the table. Perhaps I forgot to override / define some method in my class that is needed to display in the table. And is it possible to give Wt::WTableView custom data types?


Replies (3)

RE: displaying custom data types in a table Wt::WTableView - Added by Anton Erykalin about 1 year ago

Here is an example for a better understanding of what is happening to me, for the elements of the first column I passed my class IpString

RE: displaying custom data types in a table Wt::WTableView - Added by Roel Standaert about 1 year ago

You'll have to make sure any_traits is properly implemented for your type, and register it.

Wt::asString(...) is only implemented by default for exactly std::string, not derived classes of it.

Deriving from any STL classes, like std::string, is a bad idea, too. std::string does not have a virtual destructor, so you can get undefined behavior by destroying it through a std::string* (see Item 7 from Effective C++). Also, your IpString type appears to me to add an invariant to std::string, but there's all sorts of functions, like operator+=, that you could call on it and invalidate this invariant.

RE: displaying custom data types in a table Wt::WTableView - Added by Anton Erykalin about 1 year ago

Yes, you are absolutely right, I completely forgot about the absence of a virtual destructor. I'm just trying to fix the sorting of the IP strings. I believe that I have only two options left, to make a class based on a Wt::WString or somehow give my own sorting rule.
Problem I'm trying to solve is the correct sorting of strings with IP addresses. now if I sort for example in ascending order, then I will get something like this:

  1. 192.168.1.11
  2. 192.168.1.20
  3. 192.168.1.3

That is, the sorting occurred character-by-character as for simple strings.
And this behavior does not suit me. I would like the rows to be sorted like this:

  1. 192.168.1.3
  2. 192.168.1.11
  3. 192.168.1.20
    (1-3/3)