Passing Data From Static Classes To Main Dialog Class

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
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 125
Joined: Wed Oct 28, 2015 9:48 pm

Passing Data From Static Classes To Main Dialog Class

Post by Everydaydiesel »

Hello,

I have a dialog application that has a main thread and a second std:thread.

The std::thread starts a http listener and has its own static class
https://www.boost.org/doc/libs/1_69_0/l ... _small.cpp

My question is, when a event fires inside the static class, how can i call a function that belongs to the dialog.

Code: Select all

    
void create_response()
{
       string sDataToPass = "test";
       BeastTestDialog::ProcessData(sDataToPass);
       // want to call 
}

void BeastTestDialog::ProcessData(string sData)
{
    // need to get the data inside this procedure
}
Thanks in advance
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Passing Data From Static Classes To Main Dialog Class

Post by doublemax »

So you want to call a non-static method of the dialog? Then you need a pointer to an instance of the dialog. Usually when working with callbacks, you have the option to provive a void* that will be passed to the callback as additional parameter.
Use the source, Luke!
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 125
Joined: Wed Oct 28, 2015 9:48 pm

Re: Passing Data From Static Classes To Main Dialog Class

Post by Everydaydiesel »

Thank you for the reply.

I tried this first and got hung up on this problem. I pass in a reference to 's' and it acts like it cant see s at all. I suspect this has something to do with lambda but I am not good with them at all.

Code: Select all

    // Asynchronously receive a complete request message.
    void read_request(Beast_1_69Dialog &s)
    {
        cout << "read_request" << endl;
        if (s.m_bExitWebServer)
        {
            //boost::beast::error_code ec2;
            //socket.shutdown(tcp::socket::shutdown_send, ec2);
            return;
        }
        else
        {
            auto self = shared_from_this();

            http::async_read(
                socket_,
                buffer_,
                request_,
                [self](boost::beast::error_code ec,
                    std::size_t bytes_transferred)
                {
                    boost::ignore_unused(bytes_transferred);
                    if(!ec)
                        self->process_request(s);       // error is here ##################
                });
        }
    }

error

Code: Select all

-------------- Build: Debug in Beast 1.69 (compiler: GNU GCC Compiler)---------------

g++ -Wall -std=c++11 -I/usr/lib/x86_64-linux-gnu/wx/include/gtk2-unicode-3.0 -I/usr/include/wx-3.0 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread -g -Iinclude -c "Beast 1.69/Beast_1_69Main.cpp" -o obj/Debug/Beast_1_69Main.o
Beast 1.69/Beast_1_69Main.cpp: In lambda function:
Beast 1.69/Beast_1_69Main.cpp:199:47: error: ‘s’ is not captured
                         self->process_request(s);
                                               ^
Beast 1.69/Beast_1_69Main.cpp:194:22: note: the lambda has no capture-default
                 [self](boost::beast::error_code ec,
                      ^
Beast 1.69/Beast_1_69Main.cpp:177:41: note: ‘Beast_1_69Dialog& s’ declared here
     void read_request(Beast_1_69Dialog &s)//bool bExitListener)
                                         ^
Process terminated with status 1 (0 minute(s), 3 second(s))
1 error(s), 0 warning(s) (0 minute(s), 3 second(s))
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Passing Data From Static Classes To Main Dialog Class

Post by doublemax »

Code: Select all

auto self = shared_from_this();
What type is "self"? (And here we see why i will never use auto. It saves a few keystrokes, but your code loses a lot of readability).

Code: Select all

[self](boost::beast::error_code ec, std::size_t bytes_transferred)
The [self] determines which variables will be visible inside the Lambda function, in this case only "self" and nothing else. Therefore you can't use "s".
https://en.cppreference.com/w/cpp/langu ... da_capture
Use the source, Luke!
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 125
Joined: Wed Oct 28, 2015 9:48 pm

Re: Passing Data From Static Classes To Main Dialog Class

Post by Everydaydiesel »

compiler tells me it is a
std::shared_ptr<http_connection> self = shared_from_this();

it was actually taken from this example ( I wish i could convert that example without shared and lambdas but I have tried and failed)
https://www.boost.org/doc/libs/1_69_0/l ... _small.cpp


thank you for the link
changing [self] to [self, &s] works!
Post Reply