C++ Logo

std-proposals

Advanced search

[std-proposals] Real World Programming, private = public

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 1 Sep 2023 22:28:16 +0100
This past week I've been talking about C++ as a real-world programming
language where we sometimes have to improvise and do a repair job, and
so I suggested marking a function as 'verbose' to tell the compiler to
disable all optimisations. All of us who work a 9 to 5 writing code
need to improvise once in a while, whether we're compensating for a
bug, or avoiding an ABI break, or compensating for somebody else
having avoided an ABI break.

Today I'm programming an Arduino microcontroller in C++ that's running
a webserver over a wifi connection. It's fine for small files... even
as big as 64 megabytes. But you can forget about going up near a
gigabyte... the transfer will always finish with bytes missing.

The problem is that the HTTP server is operating a TCP connection that
does not wait for the ACK before sending out another segment, and so
sometimes two packets get sent out without an ACK in between. This is
leading to data loss.

I have access to the header files for the web server, and I can see
where the HTTP class contains a handle to the TCP connection, but of
course the internals of the HTTP class are all private. I need access
to the TCP handle though because I want to make sure that:
    count_segments_sent == count_segments_acknowleged
before I send out more data.

My solution is as follows:

    #define private public
    #define protected public
    #include "WebServer.h"

This works fine on my own particular compiler here, but on another
compiler it might not work if the names get mangled differently or if
the class layout changes.

What if we were to standardise this hack? You'd hope we would never
have to use it but those of us programming in the real world have to
do this stuff once in a while. We would have to standardise it in such
a way that it doesn't break SFINAE.

So let's say you have a class called 'WebServer', and you want to have
access to all of its members, well how about if we could use typedef
as follows:

    typedef WebServer WebServerFreeForAll : private=public;

Then we could use it as follows:

    typedef WebServer WebServerFreeForAll : private=public, protected=public;

or even:

    typedef WebServer WebServerNotSureWhy : public=protected;

And so then in our code, let's say we have a function that manipulates
a webserver, well we could do:

    void Manipulate( WebServer &web )
    {
        static_cast<WebServerFreeForAll&>(web).some_private_member = 77;
    }

Received on 2023-09-01 21:28:28