Skip to content

refactor: replace object-like #define constants with constexpr - #351

Open
zzcgumn wants to merge 3 commits into
developfrom
chore/refactor_to_constexpr
Open

refactor: replace object-like #define constants with constexpr#351
zzcgumn wants to merge 3 commits into
developfrom
chore/refactor_to_constexpr

Conversation

@zzcgumn

@zzcgumn zzcgumn commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Context

Numeric and string constants across the library were defined as object-like
preprocessor macros (#define MAXNOOFBOARDS 200). Macros carry no type, ignore
scope, and don't show up in the debugger. library/src/utility/constants.h
already established the target style (constexpr int DDS_STRAINS = 5;); this
change extends it to the remaining object-like constant macros in library/src
and library/tests.

ALL_CAPS names are kept — renaming the legacy public C API identifiers
(DDS_VERSION, RETURN_NO_FAULT, ...) would be a real API break. The ABI is
unaffected (a constexpr int of the same value emits no symbol), and the
api/dll.h header already cannot compile as C (C++ trailing-return syntax, and
it includes the already-constexpr constants.h).

Changes

File Constants
library/src/api/dll.h DDS_VERSION, MAXNOOFBOARDS, MAXNOOFTABLES, 28 RETURN_*constexpr int; 28 TEXT_*inline constexpr const char* const (TEXT_SUIT_OR_RANK \-continuation collapsed)
library/src/api/dds.h THREADMEM_*, MAXNODE, MINNODE, SIMILARDEALLIMIT, SIMILARMAXWINNODES
library/src/ab_stats.hpp DDS_MAXDEPTH
library/src/system/scheduler.hpp HASH_MAX
library/src/system/system.cpp DDS_SYSTEM_THREAD_* (10) — [[maybe_unused]], since most sit behind platform #ifdefs
library/src/system/timer_group.cpp TIMER_DEPTH
library/src/trans_table/trans_table_s.cpp NSIZE, WSIZE, NINIT, WINIT, LSIZE
library/src/dealer_par.cpp BIGNUM
library/src/utility/debug.h 9 DDS_*_PREFIX / DDS_DEBUG_SUFFIX strings
library/src/system/scheduler.cpp dropped the now-broken #ifndef DDS_SCHEDULER_PREFIX fallback block; replaced with #include <utility/debug.h>
library/tests/calc_par_test.cpp R2..RA rank bitmasks
library/tests/args.cpp DTEST_NUM_OPTIONS
.github/copilot-instructions.md, specs/constants-and-debug.md style-guide / spec wording

Intentionally left as macros

DEBUG (play_analyser.cpp) and DDS_TEST_MEMORY_SANITIZER (test) — both used
in #if; function-like macros (HAND_ID, AB_COUNT, timer macros…); include
guards; and platform/export flags (DLLEXPORT, WINVER, debug feature flags…).
examples/hands.cpp is out of scope.

Verification

All values are unchanged, so no test edits were needed.

  • bazelisk build //... — full build incl. wasm/web/JNI/python ✅
  • bazelisk test //...93/93 pass
  • bazelisk build --define=scheduler=true / --define=ab_stats=true / --define=debug_all=true //library/src:dds
  • bazelisk test --define=ab_stats=true //library/tests/ab_search:ab_stats_test //library/tests/ab_search:tt_lookup_test

🤖 Generated with Claude Code

Closes #350

Convert object-like constant macros in library/src and library/tests to
typed constexpr definitions, matching the existing style in
utility/constants.h. ALL_CAPS names are kept (renaming the legacy public
API identifiers would be a break).

- api/dll.h: DDS_VERSION, MAXNOOF*, 28 RETURN_* -> constexpr int;
  28 TEXT_* -> inline constexpr const char* const
- api/dds.h: THREADMEM_*, MAXNODE/MINNODE, SIMILAR* -> constexpr int
- ab_stats.hpp DDS_MAXDEPTH, scheduler.hpp HASH_MAX,
  timer_group.cpp TIMER_DEPTH, dealer_par.cpp BIGNUM,
  trans_table_s.cpp N/W SIZE/INIT + LSIZE -> constexpr int
- system.cpp DDS_SYSTEM_THREAD_* -> constexpr int ([[maybe_unused]]:
  most are behind platform #ifdefs)
- utility/debug.h: DDS_*_PREFIX / DDS_DEBUG_SUFFIX -> inline constexpr
  const char* const; drop the now-redundant #ifndef fallbacks in
  scheduler.cpp in favour of #include <utility/debug.h>
- tests: calc_par_test.cpp R2..RA, args.cpp DTEST_NUM_OPTIONS

Left as macros: DEBUG and DDS_TEST_MEMORY_SANITIZER (used in #if),
function-like macros, include guards, and platform/export flags.

Values are unchanged; full bazel build (incl. wasm/web/jni/python) and
all 93 tests pass, including the scheduler/ab_stats/debug_all
define-gated builds.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HGCVvZx1GL4TxgHkVDU6bG
@zzcgumn zzcgumn self-assigned this Aug 27, 2026
@zzcgumn
zzcgumn requested a review from tameware August 27, 2026 18:38
- dll.h: keep DDS_VERSION / MAXNOOFBOARDS / MAXNOOFTABLES as #define. This
  is the frozen legacy C API header and external consumers conventionally
  test the version/limits in the preprocessor (#if DDS_VERSION >= ...).
- dll.h + debug.h: drop `inline` from the string constants. At namespace
  scope `constexpr const char* const` has internal linkage, matching both
  the macros they replace and the sibling `int` constants; `inline` would
  have promoted them to external-linkage inline variables on a public
  header pulled into every binding TU.
- system.cpp: scope [[maybe_unused]] to the platform-gated
  DDS_SYSTEM_THREAD_* subset; _BASIC and _SIZE are used unconditionally.
- cpp.instructions.md: mirror the "legacy public-API constants keep
  ALL_CAPS as constexpr" carve-out already added to copilot-instructions.
- DdsStatus.java, copilot-instructions.md: RETURN_* are now constexpr
  constants, not macros; fix stale wording / examples.
- timer_group.cpp: comment the relocated TIMER_DEPTH constant.

Full bazel build (incl. wasm/web/jni/python) and all 93 tests pass,
including the scheduler/ab_stats/debug_all define-gated builds and
jni/tests:export_set_test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HGCVvZx1GL4TxgHkVDU6bG

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors object-like #define constants into constexpr constants across the DDS library and tests, aligning the broader codebase with the existing library/src/utility/constants.h style and improving type-safety, scoping, and debuggability.

Changes:

  • Converted various numeric constants from macros to constexpr int in core library and tests.
  • Converted several debug-related string constants from macros to constexpr string constants and updated scheduler includes accordingly.
  • Updated specs and style-guide documentation to clarify the intended constants/macro conventions (including the legacy ALL_CAPS exception).

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
specs/constants-and-debug.md Updates spec wording to reflect debug flags/macros and constexpr debug prefixes.
library/tests/calc_par_test.cpp Replaces rank bitmask #defines with constexpr int test constants.
library/tests/args.cpp Replaces DTEST_NUM_OPTIONS macro with constexpr int.
library/src/utility/debug.h Replaces debug filename prefix/suffix macros with constexpr string constants.
library/src/trans_table/trans_table_s.cpp Replaces TT sizing macros (NSIZE, WSIZE, etc.) with constexpr int.
library/src/system/timer_group.cpp Replaces TIMER_DEPTH macro with constexpr int and adds clarifying comment.
library/src/system/system.cpp Replaces thread-backend index macros with constexpr int (with [[maybe_unused]] where appropriate).
library/src/system/scheduler.hpp Replaces HASH_MAX macro with constexpr int.
library/src/system/scheduler.cpp Removes macro fallback block and includes <utility/debug.h> under DDS_SCHEDULER.
library/src/dealer_par.cpp Replaces BIGNUM macro with constexpr int.
library/src/api/dll.h Keeps version/limits as macros; converts RETURN_/TEXT_ constants to constexpr declarations.
library/src/api/dds.h Replaces several configuration macros with constexpr int.
library/src/ab_stats.hpp Replaces DDS_MAXDEPTH macro with constexpr int.
jni/java/org/dds/ffm/DdsStatus.java Updates comments to refer to RETURN_* “constants” rather than “macros”.
.github/instructions/cpp.instructions.md Documents legacy ALL_CAPS constant exception after macro→constexpr conversions.
.github/copilot-instructions.md Mirrors the same naming guidance update for constants vs macros.
Suppressed comments (1)

library/src/api/dll.h:38

  • The PR description says DDS_VERSION/MAXNOOFBOARDS/MAXNOOFTABLES were converted to constexpr, but this file explicitly keeps them as preprocessor macros. Please update the PR description (or, if the intention is truly to convert them, remove this comment and convert these defines) so the change summary matches the actual behavior.
/* Version 3.1.0. Allowing for 2 digit minor versions */
// These three stay object-like macros: this is the frozen legacy C API
// header and external consumers conventionally test the version / limits in
// the preprocessor (e.g. #if DDS_VERSION >= 30100, #ifdef MAXNOOFBOARDS).
#define DDS_VERSION 30100

#define MAXNOOFBOARDS 200

#define MAXNOOFTABLES 40

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread library/src/api/dll.h Outdated
- dll.h: make the TEXT_* constants array-typed
  (constexpr const char TEXT_x[] = "...") instead of const char* const,
  per Copilot review. This restores the string-literal macro semantics the
  reviewer flagged (sizeof(TEXT_x) gives the length, array-of-char type)
  while still decaying to const char* for strcpy / EXPECT_STREQ. Kept
  non-inline so linkage stays internal/per-TU, matching both the replaced
  macros and the sibling RETURN_* int constants.

Full bazel build (incl. wasm/web/jni/python) and all 93 tests pass.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HGCVvZx1GL4TxgHkVDU6bG
@zzcgumn

zzcgumn commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator Author

@tameware , do you still have Copilot credits left for this month?

@tameware

tameware commented Aug 28, 2026 via email

Copy link
Copy Markdown
Collaborator

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

library/src/api/dll.h:36

  • The PR description’s “Changes” table says DDS_VERSION, MAXNOOFBOARDS, and MAXNOOFTABLES were converted to constexpr, but this header intentionally keeps them as #define macros. It also describes TEXT_* as inline constexpr const char* const, but the implementation is now constexpr const char TEXT_*[] (array-typed, non-inline). Please update the PR description (and/or the “Intentionally left as macros” section) to match the actual changes so reviewers/users aren’t misled.
// header and external consumers conventionally test the version / limits in
// the preprocessor (e.g. #if DDS_VERSION >= 30100, #ifdef MAXNOOFBOARDS).
#define DDS_VERSION 30100

#define MAXNOOFBOARDS 200

@zzcgumn zzcgumn added the Clean Copilot review Copilot reviewed and had neither new comments nor new suppressed comments. label Aug 28, 2026
@tameware

Copy link
Copy Markdown
Collaborator

The changes are certainly an improvement. I find the description confusing. It uses MAXNOOFBOARDS as an example of a macro that's been replaced, but then says:

ALL_CAPS names are kept…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Clean Copilot review Copilot reviewed and had neither new comments nor new suppressed comments.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor from #define SOMECONSTANT 42 to const_expr int some_const = 42;

3 participants