what does piece of code do?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
newname
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 12, 2020 10:13 am

what does piece of code do?

Post by newname »

Hi,
Thank you for reading.

Here is a piece of code from https://github.com/dlasalle/wx-calc

Code: Select all

double WxCalcWindow::getCurrentValue()
{
  try {
    return std::stod(std::string(m_display-[b][u]>GetValue().mb_str())[/u][/b]);
    
  } catch (const std::invalid_argument& e) {
    // assume unparsable equals 0
    return 0.0;
  }
}
As far as I know, GetValue returns a string object, which invokes the mb_str method, which "Returns the multibyte (C string) representation of the string using conv's wxMBConv::cWC2MB method and returns wxCharBuffer. " according to the manual.

Do I understand the code correctly? and what does mb_str return in this case?
P.S
I even deleted mb_str() and the program still compiled and is running fine.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: what does piece of code do?

Post by catalin »

GetValue() returns a wxString, which is not [necessarily] a std::string, so some mechanism might be needed for conversion, and using mb_str() is one of them.
mb_str() returns wxCharBuffer, which is convertible to char*.
If conversion works for you without the call to mb_str(), you might be using wxWidgets libs compiled with wxUSE_STL=1.
newname
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 12, 2020 10:13 am

Re: what does piece of code do?

Post by newname »

Thank you for the reply. But
1. if GetValue doesn't return a string, how could we call mb_str? I think function chaining is only possible with c++ objects. I am saying this because I don't yet know much about c++ as I've only finished reading c++ primer plus.

2. I haven't look at the source code for GetValue, but do you mean there are two versions of GetValue, one that uses the standard c++ string and one that is designed for wxwidgets only?

P.S
as for the wxUSE_STL=1, I use Visual Studio Community 2019 and built wxWidgets from source. I just clicked on the sln file and that's all.
newname
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 12, 2020 10:13 am

Re: what does piece of code do?

Post by newname »

Oh and one more question.

I have looked at textctrl.cpp

Code: Select all

wxString wxTextCtrl::GetValue() const
{
    // range 0..-1 is special for GetRange() and means to retrieve all text
    return GetRange(0, -1);
}

wxString wxTextCtrl::GetRange(long from, long to) const
{
    wxString str;

    if ( from >= to && to != -1 )
    {
        // nothing to retrieve
        return str;
    }
str is local to GetRange. How is it possible that it should contain the numerical string I entered previously?

Thank you very much.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: what does piece of code do?

Post by doublemax »

newname wrote: Thu Aug 13, 2020 12:32 am 1. if GetValue doesn't return a string, how could we call mb_str? I think function chaining is only possible with c++ objects. I am saying this because I don't yet know much about c++ as I've only finished reading c++ primer plus.
GetValue() always returns a wxString.
newname wrote: Thu Aug 13, 2020 12:32 am2. I haven't look at the source code for GetValue, but do you mean there are two versions of GetValue, one that uses the standard c++ string and one that is designed for wxwidgets only?
Not exactly. But if wxUSE_STL=1 (by default it's 0), then implicit conversions from wxString to std::string exist. This is not enabled by default, see explanation below.

std::string is a "stupid" byte container. wxString is a little more complex than that and you should treat is as a "black box" that stores a Unicode(!) string. Normally you should not care about in which format wxString stores the string internally.

If you want to convert a Unicode wxString to std::string, some kind of conversion needs to happen, because not all Unicode characters can by represented with 8 bits. That's why the original code you posted contains the mb_str() call. Without parameters it's called as mb_str(wxConvLocal). Which means that it tries to encode the Unicode string using the local encoding. This is somewhat "dangerous" as it could fail and return an empty string, e.g. if the string contained Japanese characters.

Please read the intro of the wxString documentation:
https://docs.wxwidgets.org/trunk/classwx_string.html
Use the source, Luke!
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: what does piece of code do?

Post by doublemax »

str is local to GetRange. How is it possible that it should contain the numerical string I entered previously?
You only posted the code part that checks if the passed range is valid, and if not, it just returns an empty string. Further down, "str" is filled with a part of the string.
Use the source, Luke!
newname
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 12, 2020 10:13 am

Re: what does piece of code do?

Post by newname »

Thank you very much for your help.
Post Reply