Files
SpacetimeDB/crates/bindings-cpp/include/spacetimedb/readonly_database_context.h
Jason Larabie 52b6c66fa1 Add C++ Bindings (#3544)
# Description of Changes

This adds C++ server bindings (/crate/bindings-cpp) to allow writing C++
20 modules.

- Emscripten WASM build system integration with CMake
- Macro-based code generation (SPACETIMEDB_TABLE, SPACETIMEDB_REDUCER,
etc)
- All SpacetimeDB types supported (primitives, Timestamp, Identity,
Uuid, etc)
- Product types via SPACETIMEDB_STRUCT
- Sum types via SPACETIMEDB_ENUM
- Constraints marked with FIELD* macros

# API and ABI breaking changes

None

# Expected complexity level and risk

2 - Doesn't heavily impact any other areas but is complex macro C++
structure to support a similar developer experience, did have a small
impact on init command

# Testing

- [x] modules/module-test-cpp - heavily tested every reducer
- [x] modules/benchmarks-cpp - tested through the standalone (~6x faster
than C#, ~6x slower than Rust)
- [x] modules/sdk-test-cpp
- [x] modules/sdk-test-procedure-cpp
- [x] modules/sdk-test-view-cpp  
- [x] Wrote several test modules myself
- [x] Quickstart smoketest [Currently in progress]
- [ ] Write Blackholio C++ server module

---------

Signed-off-by: Jason Larabie <jason@clockworklabs.io>
Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com>
Co-authored-by: Ryan <r.ekhoff@clockworklabs.io>
Co-authored-by: John Detter <4099508+jdetter@users.noreply.github.com>
2026-02-07 04:26:45 +00:00

99 lines
3.7 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 {
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>
ReadOnlyPrimaryKeyAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::PrimaryKey>& field_tag) const {
return ReadOnlyPrimaryKeyAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType>
ReadOnlyUniqueAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::Unique>& field_tag) const {
return ReadOnlyUniqueAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType>
ReadOnlyIndexedAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::Indexed>& field_tag) const {
return ReadOnlyIndexedAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
template<typename TableType, typename FieldType>
ReadOnlyRegularAccessor<TableType, FieldType> operator[](
const FieldTag<TableType, FieldType, FieldConstraint::None>& field_tag) const {
return ReadOnlyRegularAccessor<TableType, FieldType>(
field_tag.table_name, field_tag.field_name, field_tag.member_ptr);
}
};
} // namespace SpacetimeDB
#endif // SPACETIMEDB_READONLY_DATABASE_CONTEXT_H