Files
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

81 lines
2.4 KiB
C++

#ifndef SPACETIMEDB_VIEW_CONTEXT_H
#define SPACETIMEDB_VIEW_CONTEXT_H
#include <spacetimedb/bsatn/types.h> // For Identity
#include <spacetimedb/bsatn/timestamp.h> // For Timestamp
#include <spacetimedb/readonly_database_context.h> // For ReadOnlyDatabaseContext
#include <array>
namespace SpacetimeDB {
/**
* @brief Context for views with caller identity
*
* ViewContext provides read-only database access along with the identity
* of the caller who invoked the view. Use this when the view needs to
* filter or customize results based on who is calling it.
*
* Key differences from ReducerContext:
* - db is ReadOnlyDatabaseContext (no mutations allowed)
* - No connection_id (views are stateless, don't track connections)
* - No rng() method (views should be deterministic)
*
* Example usage:
* @code
* SPACETIMEDB_VIEW(std::vector<Item>, get_my_items, Public, ViewContext ctx) {
* std::vector<Item> my_items;
* // Filter by caller's identity using indexed field
* for (const auto& item : ctx.db[item_owner].filter(ctx.sender)) {
* my_items.push_back(item);
* }
* return Ok(my_items);
* }
* @endcode
*/
struct ViewContext {
// Caller's identity - who invoked this view
Identity sender;
// Read-only database access - no mutations allowed
ReadOnlyDatabaseContext db;
// Constructors
ViewContext() = default;
explicit ViewContext(Identity s)
: sender(s) {}
};
/**
* @brief Context for anonymous views without caller identity
*
* AnonymousViewContext provides read-only database access without
* exposing the caller's identity. Use this for views that return
* the same data regardless of who calls them.
*
* This is more efficient than ViewContext as it doesn't require
* identity information to be passed from the host.
*
* Key differences from ViewContext:
* - No sender field (caller identity not available)
* - Otherwise identical functionality
*
* Example usage:
* @code
* SPACETIMEDB_VIEW(std::optional<uint64_t>, count_users, Public, AnonymousViewContext ctx) {
* return Ok(std::optional<uint64_t>(ctx.db[user].count()));
* }
* @endcode
*/
struct AnonymousViewContext {
// Read-only database access - no mutations allowed
ReadOnlyDatabaseContext db;
// Constructors
AnonymousViewContext() = default;
};
} // namespace SpacetimeDB
#endif // SPACETIMEDB_VIEW_CONTEXT_H