From 966df85f9e09c527c133948ba770e32d3c8cded2 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Fri, 6 Dec 2024 12:18:00 -0800 Subject: [PATCH] feat: std::format for ecsact IDs (#271) --- MODULE.bazel | 6 +++--- ecsact/runtime/common.h | 15 ++++++++++++++- test/BUILD.bazel | 21 ++++++++++++++++++++- test/c-compliant.c | 6 ++++++ test/ecsact-id-std-format.cc | 10 ++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test/c-compliant.c create mode 100644 test/ecsact-id-std-format.cc diff --git a/MODULE.bazel b/MODULE.bazel index 20c75a7f..62ae2c37 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -12,7 +12,7 @@ bazel_dep(name = "abseil-cpp", version = "20230802.0") bazel_dep(name = "boost.dll", version = "1.83.0.bzl.2") bazel_dep(name = "boost.process", version = "1.83.0.bzl.2") -bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True) +bazel_dep(name = "toolchains_llvm", version = "1.2.0", dev_dependency = True) bazel_dep(name = "hedron_compile_commands", dev_dependency = True) git_override( module_name = "hedron_compile_commands", @@ -21,8 +21,8 @@ git_override( ) llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True) -llvm.toolchain(llvm_version = "17.0.6") -use_repo(llvm, "llvm_toolchain") +llvm.toolchain(llvm_version = "19.1.0") +use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm") register_toolchains( "@llvm_toolchain//:all", diff --git a/ecsact/runtime/common.h b/ecsact/runtime/common.h index 1dc4d349..c07e5886 100644 --- a/ecsact/runtime/common.h +++ b/ecsact/runtime/common.h @@ -2,6 +2,9 @@ #define ECSACT_RUNTIME_COMMON_H #include +#ifdef __cplusplus +# include +#endif #ifdef __cplusplus # define ECSACT_DEPRECATED(Reason) [[deprecated(Reason)]] @@ -12,7 +15,17 @@ #define ECSACT_INVALID_ID(ID_TYPE) ((ecsact_##ID_TYPE##_id)(-1)) #ifdef __cplusplus -# define ECSACT_TYPED_ID(name) enum class name : int32_t +# define ECSACT_TYPED_ID(name) \ + enum class name : int32_t; \ + template<> \ + struct std::formatter : std::formatter { \ + auto format(name v, format_context& ctx) const { \ + return formatter::format( \ + std::format(#name "({})", static_cast(v)), \ + ctx \ + ); \ + } \ + } #else # define ECSACT_TYPED_ID(name) typedef int32_t name #endif diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 2f0093a7..7cf87565 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -1,5 +1,5 @@ -load("@rules_cc//cc:defs.bzl", "cc_test") load("@ecsact_runtime//bazel:copts.bzl", "copts") +load("@rules_cc//cc:defs.bzl", "cc_test") cc_test( name = "for_each_macros_test", @@ -11,3 +11,22 @@ cc_test( "@googletest//:gtest_main", ], ) + +cc_test( + name = "c-compliant", + srcs = ["c-compliant.c"], + # intentionally not set! shouldn't need any flags for c compile + # copts = copts, + deps = [ + "@ecsact_runtime", + ], +) + +cc_test( + name = "ecsact-id-std-format", + srcs = ["ecsact-id-std-format.cc"], + copts = copts, + deps = [ + "@ecsact_runtime", + ], +) diff --git a/test/c-compliant.c b/test/c-compliant.c new file mode 100644 index 00000000..616a4e99 --- /dev/null +++ b/test/c-compliant.c @@ -0,0 +1,6 @@ +#include "ecsact/runtime/common.h" + +int main() { + ecsact_registry_id id; + return 0; +} diff --git a/test/ecsact-id-std-format.cc b/test/ecsact-id-std-format.cc new file mode 100644 index 00000000..baffc2df --- /dev/null +++ b/test/ecsact-id-std-format.cc @@ -0,0 +1,10 @@ +#include +#include +#include "ecsact/runtime/common.h" + +auto main() -> int { + auto id = ecsact_registry_id{}; + std::cout << std::format("the id is {}\n", id); + std::cout << std::format("invalid id is {}\n", ECSACT_INVALID_ID(registry)); + return 0; +}