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 966df85
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
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
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 966df85

Please sign in to comment.