Skip to content

Commit 0b9d377

Browse files
authored
feat: std::format for ecsact IDs (#271)
1 parent 6e7f0f9 commit 0b9d377

File tree

6 files changed

+58
-6
lines changed

6 files changed

+58
-6
lines changed

MODULE.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ bazel_dep(name = "abseil-cpp", version = "20230802.0")
1212
bazel_dep(name = "boost.dll", version = "1.83.0.bzl.2")
1313
bazel_dep(name = "boost.process", version = "1.83.0.bzl.2")
1414

15-
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
15+
bazel_dep(name = "toolchains_llvm", version = "1.2.0", dev_dependency = True)
1616
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
1717
git_override(
1818
module_name = "hedron_compile_commands",
@@ -21,8 +21,8 @@ git_override(
2121
)
2222

2323
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)
24-
llvm.toolchain(llvm_version = "17.0.6")
25-
use_repo(llvm, "llvm_toolchain")
24+
llvm.toolchain(llvm_version = "19.1.0")
25+
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")
2626

2727
register_toolchains(
2828
"@llvm_toolchain//:all",

ecsact/runtime/common.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define ECSACT_RUNTIME_COMMON_H
33

44
#include <stdint.h>
5+
#ifdef __cplusplus
6+
# include <format>
7+
#endif
58

69
#ifdef __cplusplus
710
# define ECSACT_DEPRECATED(Reason) [[deprecated(Reason)]]
@@ -12,7 +15,18 @@
1215
#define ECSACT_INVALID_ID(ID_TYPE) ((ecsact_##ID_TYPE##_id)(-1))
1316

1417
#ifdef __cplusplus
15-
# define ECSACT_TYPED_ID(name) enum class name : int32_t
18+
# define ECSACT_TYPED_ID(name) \
19+
enum class name : int32_t; \
20+
template<> \
21+
struct std::formatter<name> : std::formatter<std::string> { \
22+
template<typename FormatContext> \
23+
auto format(name v, FormatContext& ctx) const { \
24+
return formatter<string>::format( \
25+
std::format(#name "({})", static_cast<int32_t>(v)), \
26+
ctx \
27+
); \
28+
} \
29+
}
1630
#else
1731
# define ECSACT_TYPED_ID(name) typedef int32_t name
1832
#endif

test/BUILD.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_test")
21
load("@ecsact_runtime//bazel:copts.bzl", "copts")
2+
load("@rules_cc//cc:defs.bzl", "cc_test")
33

44
cc_test(
55
name = "for_each_macros_test",
@@ -11,3 +11,22 @@ cc_test(
1111
"@googletest//:gtest_main",
1212
],
1313
)
14+
15+
cc_test(
16+
name = "c-compliant",
17+
srcs = ["c-compliant.c"],
18+
# intentionally not set! shouldn't need any flags for c compile
19+
# copts = copts,
20+
deps = [
21+
"@ecsact_runtime",
22+
],
23+
)
24+
25+
cc_test(
26+
name = "ecsact-id-std-format",
27+
srcs = ["ecsact-id-std-format.cc"],
28+
copts = copts,
29+
deps = [
30+
"@ecsact_runtime",
31+
],
32+
)

test/c-compliant.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "ecsact/runtime/common.h"
2+
3+
int main() {
4+
ecsact_registry_id id;
5+
return 0;
6+
}

test/ecsact-id-std-format.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
#include <format>
3+
#include "ecsact/runtime/common.h"
4+
5+
using test_formatter =
6+
decltype(std::declval<std::formatter<ecsact_registry_id>>());
7+
8+
auto main() -> int {
9+
auto id = ecsact_registry_id{};
10+
std::cout << std::format("the id is {}\n", id);
11+
std::cout << std::format("invalid id is {}\n", ECSACT_INVALID_ID(registry));
12+
return 0;
13+
}

test/for_each_macros_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
TEST(ForEach, ForEachLambda) {
66
#define AS_PTR_VARIABLE(method_name, unused) \
7-
decltype(&method_name) method_name##_ptr = nullptr;
7+
[[maybe_unused]] decltype(&method_name) method_name##_ptr = nullptr;
88

99
FOR_EACH_ECSACT_API_FN(AS_PTR_VARIABLE, unused);
1010

0 commit comments

Comments
 (0)