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

109 lines
4.3 KiB
C++

#ifndef SPACETIMEDB_READONLY_DATABASE_CONTEXT_H
#define SPACETIMEDB_READONLY_DATABASE_CONTEXT_H
#include "readonly_table_accessor.h"
#include "readonly_field_accessors.h"
#include "database.h"
#include <string>
namespace SpacetimeDB {
/**
* @brief Read-only database context for views
*
* ReadOnlyDatabaseContext provides a read-only interface to the database
* for use in views. It prevents all mutation operations at compile-time.
*
* Key differences from DatabaseContext:
* - No insert/update/delete operations
* - No direct table iteration (prevents inefficient full table scans)
* - Table data accessible ONLY through indexed field accessors
* - Enforces efficient query patterns using indexes
*
* This is a completely separate type from DatabaseContext (no inheritance)
* to match Rust's LocalReadOnly vs Local pattern.
*
* Example usage:
* @code
* SPACETIMEDB_VIEW(std::vector<User>, get_adults, Public, ViewContext ctx) {
* // Can only access via indexed fields
* std::vector<User> adults;
* for (const auto& user : ctx.db[user_age].filter(range_from(18u))) {
* adults.push_back(user);
* }
* return Ok(adults);
* }
* @endcode
*/
class ReadOnlyDatabaseContext {
public:
// Generic table accessor method (type-only, requires explicit table name later)
template<typename T>
ReadOnlyTableAccessor<T> table() const {
return ReadOnlyTableAccessor<T>{};
}
// Name-based accessor that returns a configured table accessor
template<typename T>
ReadOnlyTableAccessor<T> table(const char* name) const {
return ReadOnlyTableAccessor<T>(std::string(name));
}
// String overload
template<typename T>
ReadOnlyTableAccessor<T> table(const std::string& name) const {
return table<T>(name.c_str());
}
// Tag-based accessor using operator[] (SpacetimeDB standard)
template<typename Tag>
ReadOnlyTableAccessor<typename Tag::type> operator[](const Tag&) const {
static_assert(!Tag::__is_event_internal,
"Event tables are not accessible from views.");
return ReadOnlyTableAccessor<typename Tag::type>(std::string(Tag::__table_name_internal));
}
// Field tag accessors - read-only versions
// These return read-only field accessors that only support querying, not mutation
template<typename TableType, typename FieldType, bool IsEventTable>
ReadOnlyPrimaryKeyAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::PrimaryKey, IsEventTable>& field_tag) const {
static_assert(!IsEventTable,
"Event tables are not accessible from views.");
return ReadOnlyPrimaryKeyAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType, bool IsEventTable>
ReadOnlyUniqueAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::Unique, IsEventTable>& field_tag) const {
static_assert(!IsEventTable,
"Event tables are not accessible from views.");
return ReadOnlyUniqueAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType, bool IsEventTable>
ReadOnlyIndexedAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::Indexed, IsEventTable>& field_tag) const {
static_assert(!IsEventTable,
"Event tables are not accessible from views.");
return ReadOnlyIndexedAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType, bool IsEventTable>
ReadOnlyRegularAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::None, IsEventTable>& field_tag) const {
static_assert(!IsEventTable,
"Event tables are not accessible from views.");
return ReadOnlyRegularAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
};
} // namespace SpacetimeDB
#endif // SPACETIMEDB_READONLY_DATABASE_CONTEXT_H