Skip to content

Commit

Permalink
feat: std::format for ecsact IDs (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored Dec 6, 2024
1 parent 6e7f0f9 commit 5c6cfe2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
15 changes: 14 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,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<name> : std::formatter<std::string> { \
auto format(name v, format_context& 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;
}
10 changes: 10 additions & 0 deletions test/ecsact-id-std-format.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include <format>
#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;
}

0 comments on commit 5c6cfe2

Please sign in to comment.