Skip to content

feat: std::format for ecsact IDs #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion ecsact/runtime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define ECSACT_RUNTIME_COMMON_H

#include <stdint.h>
#ifdef __cplusplus
# include <format>
#endif

#ifdef __cplusplus
# define ECSACT_DEPRECATED(Reason) [[deprecated(Reason)]]
Expand All @@ -12,7 +15,18 @@
#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<name> : std::formatter<std::string> { \
template<typename FormatContext> \
auto format(name v, FormatContext& ctx) const { \
return formatter<string>::format( \
std::format(#name "({})", static_cast<int32_t>(v)), \
ctx \
); \
} \
}
#else
# define ECSACT_TYPED_ID(name) typedef int32_t name
#endif
Expand Down
21 changes: 20 additions & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
],
)
6 changes: 6 additions & 0 deletions test/c-compliant.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "ecsact/runtime/common.h"

int main() {
ecsact_registry_id id;
return 0;
}
13 changes: 13 additions & 0 deletions test/ecsact-id-std-format.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <format>
#include "ecsact/runtime/common.h"

using test_formatter =
decltype(std::declval<std::formatter<ecsact_registry_id>>());

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;
}
2 changes: 1 addition & 1 deletion test/for_each_macros_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

TEST(ForEach, ForEachLambda) {
#define AS_PTR_VARIABLE(method_name, unused) \
decltype(&method_name) method_name##_ptr = nullptr;
[[maybe_unused]] decltype(&method_name) method_name##_ptr = nullptr;

FOR_EACH_ECSACT_API_FN(AS_PTR_VARIABLE, unused);

Expand Down
Loading