Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Prev Up HomeNext

Declare a Result

// Declare to C a Result with a happy value of intptr_t
BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM(result_int, intptr_t)

// Save oneself typing out BOOST_OUTCOME_C_RESULT_SYSTEM(result_int) all the time
typedef BOOST_OUTCOME_C_RESULT_SYSTEM(result_int) result;

// Our custom C enum
enum c_enum
{
  c_enum_not_found,
  c_enum_bad_argument
};

// Make a custom status code domain for this C enum
BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM_FROM_ENUM(result_int,                                // The C Result type declared above
                                    c_enum,                                    // The C enum we wish to wrap
                                    "{74ceb994-7622-3a21-07f0-b016aa705585}",  // Unique UUID for this domain
                                    // Mappings of C enum values to textual description and semantic equivalances to generic codes
                                    {c_enum::c_enum_not_found, "item not found", {errc::no_such_file_or_directory}},
                                    {c_enum::c_enum_bad_argument, "invoked wrong", {errc::invalid_argument}})

// Make helper macros
#define SUCCESS(v) BOOST_OUTCOME_C_MAKE_RESULT_SYSTEM_SUCCESS(result_int, (v))
#define FAILURE(v) BOOST_OUTCOME_C_MAKE_RESULT_SYSTEM_FROM_ENUM(result_int, c_enum, (v))
View this code on Github

The key to making C programming easy is to alias the long complex things into short easy thing. Obviously SUCCESS(expr) and FAILURE(expr) is too generic, but for the purposes of this documentation it makes thing easier.

Last revised: July 16, 2024 at 21:33:35 +0100


Prev Up HomeNext