Skip to content

Commit

Permalink
For the C make successful result, special case the implementation
Browse files Browse the repository at this point in the history
hard coding it into an inlined definition.
  • Loading branch information
ned14 committed Oct 17, 2024
1 parent 21ffac4 commit 0a91b4e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
6 changes: 3 additions & 3 deletions include/outcome/detail/revision.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Distributed under the Boost Software License, Version 1.0.
*/

// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define OUTCOME_PREVIOUS_COMMIT_REF 2a8ff2b0ca37e4627b0d9919b653fee7d4cc6968
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 14:48:43 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 2a8ff2b0
#define OUTCOME_PREVIOUS_COMMIT_REF 84e9e394f800b73b5c4f4f901752998cd97aa767
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-10-17 18:48:13 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 84e9e394
46 changes: 12 additions & 34 deletions include/outcome/experimental/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ Distributed under the Boost Software License, Version 1.0.
#endif
#endif

#ifndef OUTCOME_C_INLINE
#if __STDC_VERSION__ >= 199900L || __cplusplus > 0
#define OUTCOME_C_INLINE inline
#elif defined(__GNUC__) || defined(__clang__)
#define OUTCOME_C_INLINE __inline
#endif
#endif

#include "../outcome_gdb.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -111,7 +119,6 @@ extern "C"

#define CXX_STATUS_CODE(ident) struct cxx_status_code_##ident

extern OUTCOME_C_WEAK void outcome_make_result_status_code_success(void *out, size_t bytes, size_t offset, const void *toset, size_t tosetbytes);
extern OUTCOME_C_WEAK void outcome_make_result_status_code_failure_posix(void *out, size_t bytes, size_t offset, int errcode);
extern OUTCOME_C_WEAK void outcome_make_result_status_code_failure_system(void *out, size_t bytes, size_t offset, intptr_t errcode);
extern int outcome_status_code_equal(const void *a, const void *b);
Expand Down Expand Up @@ -169,15 +176,13 @@ extern "C"
unsigned flags; \
S error; \
}; \
OUTCOME_C_NODISCARD_EXTERN_C OUTCOME_C_WEAK struct cxx_result_status_code_##ident outcome_make_result_##ident##_success(R value) \
{ \
OUTCOME_C_NODISCARD static OUTCOME_C_INLINE struct cxx_result_status_code_##ident outcome_make_result_##ident##_success(R value) \
{ /* We special case this so it inlines efficiently */ \
struct cxx_result_status_code_##ident ret; \
assert(outcome_make_result_status_code_success); /* If this fails, you need to compile this file at least once in C++. */ \
outcome_make_result_status_code_success((void *) &ret, sizeof(ret), offsetof(struct cxx_result_status_code_##ident, flags), (const void *) &value, \
sizeof(value)); \
ret.value = value; \
ret.flags = 1 /* have_value */; \
return ret; \
} \
OUTCOME_C_MSVC_FORCE_EMIT(outcome_make_result_##ident##_success) \
OUTCOME_C_NODISCARD_EXTERN_C OUTCOME_C_WEAK struct cxx_result_status_code_##ident outcome_make_result_##ident##_failure_posix(int errcode) \
{ \
struct cxx_result_status_code_##ident ret; \
Expand Down Expand Up @@ -290,33 +295,6 @@ extern "C"
#endif

// You need to include this C header in at least one C++ source file to have these C helper functions be implemented
extern "C" OUTCOME_C_WEAK void outcome_make_result_status_code_success(void *out, size_t bytes, size_t offset, const void *toset, size_t tosetbytes)
{
union type_punner_t
{
OUTCOME_V2_NAMESPACE::experimental::status_result<intptr_t> cpp;
struct cxx_status_code
{
intptr_t value;
unsigned flags;
cxx_status_code_system error;
} c;

type_punner_t()
: cpp(0)
{
}
~type_punner_t() {}
} pun;
static_assert(sizeof(pun.cpp) == sizeof(pun.c), "");
static constexpr size_t punoffset = offsetof(type_punner_t::cxx_status_code, flags);
assert(bytes - tosetbytes >= sizeof(pun.cpp) - punoffset);
const size_t tocopy = std::min(bytes - tosetbytes, sizeof(pun.c) - punoffset);
memcpy(out, toset, tosetbytes);
memcpy((void *) ((char *) out + offset), (const void *) ((const char *) &pun.c + punoffset), tocopy);
}
OUTCOME_C_MSVC_FORCE_EMIT(outcome_make_result_status_code_success)

extern "C" OUTCOME_C_WEAK void outcome_make_result_status_code_failure_posix(void *out, size_t bytes, size_t offset, int errcode)
{
using value_type = OUTCOME_V2_NAMESPACE::experimental::posix_code::value_type;
Expand Down
6 changes: 3 additions & 3 deletions single-header/outcome-basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,9 +1019,9 @@ Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt)
*/
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define OUTCOME_PREVIOUS_COMMIT_REF 36beb5b34b24cf25d93ac421370ef5578f2f65ed
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 13:14:36 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 36beb5b3
#define OUTCOME_PREVIOUS_COMMIT_REF 84e9e394f800b73b5c4f4f901752998cd97aa767
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-10-17 18:48:13 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 84e9e394
#define OUTCOME_V2 (QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2))
#ifdef _DEBUG
#define OUTCOME_V2_CXX_MODULE_NAME QUICKCPPLIB_BIND_NAMESPACE((QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2d)))
Expand Down
6 changes: 3 additions & 3 deletions single-header/outcome-experimental.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,9 @@ Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt)
*/
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define OUTCOME_PREVIOUS_COMMIT_REF 36beb5b34b24cf25d93ac421370ef5578f2f65ed
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 13:14:36 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 36beb5b3
#define OUTCOME_PREVIOUS_COMMIT_REF 84e9e394f800b73b5c4f4f901752998cd97aa767
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-10-17 18:48:13 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 84e9e394
#define OUTCOME_V2 (QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2))
#ifdef _DEBUG
#define OUTCOME_V2_CXX_MODULE_NAME QUICKCPPLIB_BIND_NAMESPACE((QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2d)))
Expand Down
6 changes: 3 additions & 3 deletions single-header/outcome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,9 +1043,9 @@ Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt)
*/
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define OUTCOME_PREVIOUS_COMMIT_REF 36beb5b34b24cf25d93ac421370ef5578f2f65ed
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-09-04 13:14:36 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 36beb5b3
#define OUTCOME_PREVIOUS_COMMIT_REF 84e9e394f800b73b5c4f4f901752998cd97aa767
#define OUTCOME_PREVIOUS_COMMIT_DATE "2024-10-17 18:48:13 +00:00"
#define OUTCOME_PREVIOUS_COMMIT_UNIQUE 84e9e394
#define OUTCOME_V2 (QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2))
#ifdef _DEBUG
#define OUTCOME_V2_CXX_MODULE_NAME QUICKCPPLIB_BIND_NAMESPACE((QUICKCPPLIB_BIND_NAMESPACE_VERSION(outcome_v2d)))
Expand Down

0 comments on commit 0a91b4e

Please sign in to comment.