X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/33aec8491c4c337352f570c24f8a76298e3a319c..b8df87e176f27b25534f27d7e240defa32ca35bc:/include/xbt/system_error.hpp diff --git a/include/xbt/system_error.hpp b/include/xbt/system_error.hpp index f580e750ce..262bfa83f4 100644 --- a/include/xbt/system_error.hpp +++ b/include/xbt/system_error.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2016. The SimGrid Team. +/* Copyright (c) 2016-2019. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -14,22 +14,72 @@ namespace simgrid { namespace xbt { +/** A `error_category` suitable to be used with `errno` + * + * It is not clear which error we are supposed to generate + * when getting a errno: + * + * * `system_error` clearly cannot be used for this on Windows; + * + * * `generic_error` might not be used for non-standard `errno`. + * + * Let's just define a function which gives us the correct + * category. + */ inline const std::error_category& errno_category() noexcept { return std::generic_category(); } +/** Create a `error_code` from an `errno` value + * + * This is expected to to whatever is right to create a + * `error_code` from a given `errno` value. + */ +inline +std::error_code errno_code(int errnum) +{ + return std::error_code(errnum, errno_category()); +} + +/** Create an `error_code` from `errno` (and clear it) */ +inline +std::error_code errno_code() +{ + int errnum = errno; + errno = 0; + return errno_code(errnum); +} + +/** Create a `system_error` from an `errno` value + * + * This is expected to to whatever is right to create a + * `system_error` from a given `errno` value. + */ inline std::system_error errno_error(int errnum) { - return std::system_error(errnum, errno_category()); + return std::system_error(errno_code(errnum)); } inline std::system_error errno_error(int errnum, const char* what) { - return std::system_error(errnum, errno_category(), what); + return std::system_error(errno_code(errnum), what); +} + +/** Create a `system_code` from `errno` (and clear it) */ +inline +std::system_error errno_error() +{ + return std::system_error(errno_code()); +} + +inline +std::system_error errno_error(const char* what) +{ + return std::system_error(errno_code(), what); } }