Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/bindings-cpp/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ outcome.error() // const std::string& - get error message (UB if succ
extern "C" __attribute__((export_name("__preinit__01_clear_global_state")))
void __preinit__01_clear_global_state() {
ClearV9Module(); // Reset module definition and handler registries
getV9TypeRegistration().clear(); // Reset type registry and error state
getModuleTypeRegistration().clear(); // Reset type registry and error state
}
```

Expand Down Expand Up @@ -308,15 +308,15 @@ if (constraint == FieldConstraint::PrimaryKey) {

### Phase 3: Type System Registration

**Component**: V9TypeRegistration system (`v9_type_registration.h`)
**Component**: ModuleTypeRegistration system (`module_type_registration.h`)

**Core Principle**: Only user-defined structs and enums get registered in the typespace. Primitives, arrays, Options, and special types are always inlined.

**Architecture Note**: V9Builder serves as the registration coordinator but delegates all type processing to the V9TypeRegistration system. This separation ensures a single, unified type registration pathway.
**Architecture Note**: V9Builder serves as the registration coordinator but delegates all type processing to the ModuleTypeRegistration system. This separation ensures a single, unified type registration pathway.

**Registration Flow**:
```cpp
class V9TypeRegistration {
class ModuleTypeRegistration {
AlgebraicType registerType(const bsatn::AlgebraicType& bsatn_type,
const std::string& explicit_name = "",
const std::type_info* cpp_type = nullptr) {
Expand Down Expand Up @@ -376,7 +376,7 @@ void __preinit__99_validate_types() {
}

// 3. Check for type registration errors
if (getV9TypeRegistration().hasError()) {
if (getModuleTypeRegistration().hasError()) {
createErrorModule("ERROR_TYPE_REGISTRATION_" + sanitize(error_message));
return;
}
Expand Down Expand Up @@ -427,7 +427,7 @@ namespace SpacetimeDB::detail {
```

#### 2. LazyTypeRegistrar Integration
**Location**: `v9_type_registration.h` - Compile-time namespace detection
**Location**: `module_type_registration.h` - Compile-time namespace detection

```cpp
template<typename T>
Expand All @@ -445,7 +445,7 @@ class LazyTypeRegistrar {
}

// Register with qualified name
type_index_ = getV9TypeRegistration().registerAndGetIndex(
type_index_ = getModuleTypeRegistration().registerAndGetIndex(
algebraic_type, qualified_name, &typeid(T));
}
};
Expand Down
3 changes: 2 additions & 1 deletion crates/bindings-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ set(LIBRARY_SOURCES
src/internal/Module.cpp
src/internal/AlgebraicType.cpp # Required for V9 autogen types
src/internal/v9_builder.cpp # V9 incremental module builder
src/internal/v9_type_registration.cpp # Unified type registration system
src/internal/v10_builder.cpp # V10 facade over module definition assembly
src/internal/module_type_registration.cpp # Unified type registration system
)

add_library(spacetimedb_cpp_library STATIC)
Expand Down
3 changes: 2 additions & 1 deletion crates/bindings-cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ LOG_PANIC("Fatal error message");
The library uses a sophisticated hybrid compile-time/runtime architecture:
- **Compile-Time Validation** (`table_with_constraints.h`): C++20 concepts and static assertions for constraint validation
- **V9 Type Registration System** (`internal/v9_type_registration.h`): Unified type registration with error detection and circular reference prevention
- **Module Type Registration System** (`internal/module_type_registration.h`): Unified type registration with error detection and circular reference prevention
- **Priority-Ordered Initialization** (`internal/Module.cpp`): __preinit__ functions with numbered priorities ensure correct registration order
- **Error Detection System** (`internal/Module.cpp`): Multi-layer validation with error module replacement for clear diagnostics
- **BSATN Serialization** (`bsatn/`): Binary serialization system with algebraic type support for all data types
Expand Down Expand Up @@ -274,3 +274,4 @@ See the `modules/*-cpp/src/` directory for example modules:
## Contributing
This library is part of the SpacetimeDB project. Please see the main repository for contribution guidelines.
5 changes: 2 additions & 3 deletions crates/bindings-cpp/include/spacetimedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ namespace spacetimedb {
* @return Serialized module definition
*/
inline std::vector<uint8_t> serialize_module_def() {
auto& module_def = SpacetimeDB::Internal::Module::GetModuleDef();
return module_def.serialize();
return SpacetimeDB::Internal::Module::SerializeModuleDef();
}
}

Expand Down Expand Up @@ -219,4 +218,4 @@ namespace spacetimedb {
// Include BSATN implementation files after all headers are defined
#include "spacetimedb/bsatn/types_impl.h"
#include "spacetimedb/bsatn/schedule_at_impl.h"
#include "spacetimedb/enum_macro.h"
#include "spacetimedb/enum_macro.h"
7 changes: 3 additions & 4 deletions crates/bindings-cpp/include/spacetimedb/bsatn/sum_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ struct bsatn_traits<SumType<Ts...>> {
using sum_type = SumType<Ts...>;

static AlgebraicType algebraic_type() {
// For now, return a string type as placeholder
// TODO: Implement proper sum type registration in V9TypeRegistration system
return AlgebraicType::String();
// Reuse the canonical std::variant sum-shape implementation.
return bsatn_traits<std::variant<Ts...>>::algebraic_type();
}
};

Expand Down Expand Up @@ -162,4 +161,4 @@ SumType<Ts...> deserialize(Reader& reader, std::type_identity<SumType<Ts...>>) {
} // namespace bsatn
} // namespace SpacetimeDB

#endif // SPACETIMEDB_BSATN_SUM_TYPE_H
#endif // SPACETIMEDB_BSATN_SUM_TYPE_H
21 changes: 10 additions & 11 deletions crates/bindings-cpp/include/spacetimedb/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace SpacetimeDB {
namespace Internal {
class Module;
struct RawModuleDef;
}

// Field constraint flags - must match Rust's ColumnAttribute bits
Expand Down Expand Up @@ -52,7 +51,7 @@ struct TableTag;


// Forward declaration for field tags
template<typename TableType, typename FieldType, FieldConstraint Constraint>
template<typename TableType, typename FieldType, FieldConstraint Constraint, bool IsEventTable>
struct FieldTag;

// Forward declarations for typed field accessors
Expand Down Expand Up @@ -237,23 +236,23 @@ class DatabaseContext {

// Field tag accessor - NEW: ctx.db[simple_table.id] syntax
// Overloaded for each field constraint type
template<typename TableType, typename FieldType>
TypedPrimaryKeyAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::PrimaryKey>& field_tag) const {
template<typename TableType, typename FieldType, bool IsEventTable>
TypedPrimaryKeyAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::PrimaryKey, IsEventTable>& field_tag) const {
return TypedPrimaryKeyAccessor<TableType, FieldType>(field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}

template<typename TableType, typename FieldType>
TypedUniqueAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::Unique>& field_tag) const {
template<typename TableType, typename FieldType, bool IsEventTable>
TypedUniqueAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::Unique, IsEventTable>& field_tag) const {
return TypedUniqueAccessor<TableType, FieldType>(field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}

template<typename TableType, typename FieldType>
TypedIndexedAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::Indexed>& field_tag) const {
template<typename TableType, typename FieldType, bool IsEventTable>
TypedIndexedAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::Indexed, IsEventTable>& field_tag) const {
return TypedIndexedAccessor<TableType, FieldType>(field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}

template<typename TableType, typename FieldType>
TypedRegularAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::None>& field_tag) const {
template<typename TableType, typename FieldType, bool IsEventTable>
TypedRegularAccessor<TableType, FieldType> operator[](const FieldTag<TableType, FieldType, FieldConstraint::None, IsEventTable>& field_tag) const {
return TypedRegularAccessor<TableType, FieldType>(field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}

Expand All @@ -273,4 +272,4 @@ namespace spacetimedb {
using TableAccessor = SpacetimeDB::TableAccessor<T>;
}

#endif // SPACETIMEDB_DATABASE_H
#endif // SPACETIMEDB_DATABASE_H
3 changes: 2 additions & 1 deletion crates/bindings-cpp/include/spacetimedb/enum_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "spacetimedb/bsatn/traits.h"
#include "spacetimedb/bsatn/sum_type.h"
#include "spacetimedb/macros.h"
#include "spacetimedb/internal/v9_type_registration.h"
#include "spacetimedb/internal/module_type_registration.h"

/**
* @file enum_macro.h
Expand Down Expand Up @@ -488,3 +488,4 @@ namespace SpacetimeDB::detail {
}



Loading
Loading