Files
Jason Larabie 14f79910ee Update C++ module bindings to RawModuleDefV10 (#4461)
# Description of Changes
- Migrated the C++ module-definition assembly path to V10-first
internals:
      - Added v10_builder and module_type_registration systems.
- Switched Module::__describe_module__ to serialize RawModuleDef with
V10 payload.
      - Updated macro registration pipeline to register through V10
- Added explicit naming support across macro surface (*_NAMED variants
for reducer/procedure/
        view and field/index macros).
- Reworked multi-column index macros (FIELD_MultiColumnIndex,
FIELD_MultiColumnIndex_NAMED) with
        migration alias.
- Added SPACETIMEDB_SETTING_CASE_CONVERSION(...) to support case
conversion policy
- Error-path hardening by adding explicit constraint-registration error
tracking and preinit validation
  - Codegen updates:
      - Updated C++ moduledef regen to V10 builder types.
- Adjusted C++ codegen duplicate-variant wrapper generation to emit
proper product-type
        wrappers.
  - Test/harness updates:
- type-isolation-test runner now defaults to focused V10 regression
checks; --v9 runs broader
        legacy/full suite.
      - Added focused modules for positive/negative V10 checks:
          - test_multicolumn_index_valid
          - error_multicolumn_missing_field
          - error_default_missing_field
- Re-enabled C++ paths in sdks/rust/tests/test.rs procedure/view/test
suites.

# API and ABI breaking changes

- Refactor of the underlying module definition
- New *_NAMED variant macros for explicit canonical naming
- FIELD_NamedMultiColumnIndex renamed to FIELD_MultiColumnIndex

# Expected complexity level and risk

3 - Large set of changes moving over to V10 with underlying changes to
make future updates a little easier

# Testing
- [x] Ran the type isolation test and expanded it
- [x] Ran the spacetimedb-sdk test framework to confirm no more drift
between C++ and other module languages
- [x] Ran Unreal test suite though not really applicable
- [x] New app creation with `spacetime init --template basic-cpp`
- [x] Ran describe module tests against Rust + C# matching with C++ on
the /modules/sdk-test* modules to find any possible mis-alignment

# Review
- [x] Another look at the new features with C++
- [x] Thoughts on *_NAMED macros, I couldn't come up with a better
solution with C++20
2026-02-28 07:05:50 +00:00

134 lines
4.3 KiB
C++

#pragma once
// Autogenerated Type Support
// This file provides base classes and macros exclusively for the autogenerated
// internal types used in the V9 module definition system.
#include <variant>
#include <cstdint>
#include <tuple>
#include "../bsatn/bsatn.h"
namespace SpacetimeDB::Internal {
// =============================================================================
// TAGGED ENUM BASE CLASS (for autogenerated sum types)
// =============================================================================
template<typename... Variants>
class TaggedEnumBase {
static_assert(sizeof...(Variants) > 0, "TaggedEnum must have at least one variant");
protected:
std::variant<Variants...> data_;
public:
// Default constructor - initializes to first variant
TaggedEnumBase() : data_{} {}
// Get current tag (variant index)
uint8_t get_tag() const noexcept {
return static_cast<uint8_t>(data_.index());
}
// Get variant by index (const)
template<size_t Index>
const auto& get() const {
return std::get<Index>(data_);
}
// Get variant by index (mutable)
template<size_t Index>
auto& get() {
return std::get<Index>(data_);
}
// Set variant by index
template<size_t Index, typename T>
void set(T&& value) {
data_.template emplace<Index>(std::forward<T>(value));
}
// Check if currently holds specific index
template<size_t Index>
bool is() const noexcept {
return data_.index() == Index;
}
// Visit pattern - used for serialization
template<typename Visitor>
auto visit(Visitor&& visitor) const {
return std::visit(std::forward<Visitor>(visitor), data_);
}
// Equality
bool operator==(const TaggedEnumBase& other) const noexcept {
return data_ == other.data_;
}
bool operator!=(const TaggedEnumBase& other) const noexcept {
return !(*this == other);
}
};
} // namespace SpacetimeDB::Internal
// =============================================================================
// MACROS FOR AUTOGENERATED INTERNAL TYPES
// =============================================================================
// Macro for internal tagged enums (sum types)
// Generates a class that inherits from TaggedEnumBase and adds BSATN serialization
#define SPACETIMEDB_INTERNAL_TAGGED_ENUM(TypeName, ...) \
class TypeName : public ::SpacetimeDB::Internal::TaggedEnumBase<__VA_ARGS__> { \
private: \
using Base = ::SpacetimeDB::Internal::TaggedEnumBase<__VA_ARGS__>; \
public: \
using Base::Base; \
using Base::get_tag; \
using Base::get; \
using Base::set; \
using Base::is; \
using Base::visit; \
using Base::operator==; \
using Base::operator!=; \
\
void bsatn_serialize(::SpacetimeDB::bsatn::Writer& writer) const { \
/* Write tag (variant index) */ \
writer.write_u8(this->get_tag()); \
/* Write variant data based on current tag */ \
this->visit([&writer](const auto& variant) { \
using VariantType = std::decay_t<decltype(variant)>; \
if constexpr (!std::is_same_v<VariantType, std::monostate>) { \
/* Non-unit variant - serialize the data */ \
::SpacetimeDB::bsatn::serialize(writer, variant); \
} \
/* Unit variant - write nothing */ \
}); \
} \
};
// Macro for internal product types (structs)
// Just creates a simple struct - no base class needed
#define SPACETIMEDB_INTERNAL_PRODUCT_TYPE(TypeName) \
struct TypeName
// Public aliases used by generated non-internal code paths.
#ifndef SPACETIMEDB_PRODUCT_TYPE
#define SPACETIMEDB_PRODUCT_TYPE(TypeName) SPACETIMEDB_INTERNAL_PRODUCT_TYPE(TypeName)
#endif
#ifndef SPACETIMEDB_TAGGED_ENUM
#define SPACETIMEDB_TAGGED_ENUM(TypeName, ...) SPACETIMEDB_INTERNAL_TAGGED_ENUM(TypeName, __VA_ARGS__)
#endif
// Macro for product type equality - generates == and != operators
// This generates a simple equality comparison using std::tie
#define SPACETIMEDB_PRODUCT_TYPE_EQUALITY(...) \
bool operator==(const auto& other) const noexcept { \
return std::tie(__VA_ARGS__) == std::tie(other.__VA_ARGS__); \
} \
bool operator!=(const auto& other) const noexcept { \
return !(*this == other); \
}