Open fpagliughi opened 1 year ago
After going through some of this, it appears that it would be difficult to provide the same usability and level of detail that some exceptions provide if we get rid of them. So, it seems that making exceptions a build option for the library might be a more appropriate way to handle this... at least for now. As the result<>
type creeps in and evolves, this may change, making exceptions less useful.
So, for now, exceptions will be included at build time by default, but can be turned off with the CMake build option, SOCKPP_WITH_EXCEPTIONS
, like:
$ cmake -DSOCKPP_WITH_EXCEPTIONS=OFF ..
Here's how I think things will play out...
std::error_code
and result<T>
will be introduced in v0.9, but will only be used in a limited fashion, where useful. errno
will now return a result<T>
instead of throwing. These will be marked noexcept
.error_code&
parameter to return any failure state if encountered. So the application can choose to completely avoid exceptions if desired.get_last_error()
and make sockets somewhat more thread friendly by removing the cached error state.
Commit 784dc0958c08f4894258c8a2f5b411e36b38fad4 in PR #17 introduced stateless I/O functions, like
read_r()
andwritre_r()
, which return anioresult
type that wrap the success and specific error values derived fromerrno
orGetLastError()
.On an unrelated note, in Issue #72, @JakeSays suggested getting rid of exceptions in the few places they're used, and @ldgeng, suggested using std::error_code.
I was not even aware
std::error_code
existed, but it looks very appropriate for this library. It would be a good way to eliminate platform-specific differences of checking errors on Win32 that has been plaguing us already. And it would present a road forward for handling errors when TLS libraries are introduced in a future version of sockpp.So, taken all together, I'm thinking of doing this...
result<T>
type whereT
is the success variant anderror_code
is the error.ioresult
would be a specialization ofresult<int>
:result<T>
in their place. The whole API will benoexcept
.ioresult
. The only explicit need for the state would be checking the object after construction.The last point would allow a single socket to be thread-safe for two threads where one reads from the socket and the other writes to it. This is a common pattern in socket apps in C, but couldn't be done in sockpp since
get_last_error()
is not thread safe.