#ifndef SPACETIMEDB_HANDLER_CONTEXT_H #define SPACETIMEDB_HANDLER_CONTEXT_H #ifndef SPACETIMEDB_UNSTABLE_FEATURES #error "spacetimedb/handler_context.h requires SPACETIMEDB_UNSTABLE_FEATURES to be enabled" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include namespace SpacetimeDB { struct HandlerContext { Timestamp timestamp; HttpClient http; private: mutable std::shared_ptr rng_instance; mutable uint32_t counter_uuid_ = 0; public: HandlerContext() = default; explicit HandlerContext(Timestamp t) : timestamp(t) {} Identity identity() const { std::array id_bytes; ::identity(id_bytes.data()); return Identity(id_bytes); } StdbRng& rng() const { if (!rng_instance) { rng_instance = std::make_shared(timestamp); } return *rng_instance; } Uuid new_uuid_v4() const { std::array random_bytes; rng().fill_bytes(random_bytes.data(), random_bytes.size()); return Uuid::from_random_bytes_v4(random_bytes); } Uuid new_uuid_v7() const { std::array random_bytes; rng().fill_bytes(random_bytes.data(), random_bytes.size()); return Uuid::from_counter_v7(counter_uuid_, timestamp, random_bytes); } #ifdef SPACETIMEDB_UNSTABLE_FEATURES template auto with_tx(Func&& body) -> decltype(body(std::declval())) { auto make_reducer_ctx = [](Timestamp tx_timestamp) { return ReducerContext( Identity{}, std::nullopt, tx_timestamp, AuthCtx::internal() ); }; return Internal::with_tx(make_reducer_ctx, body); } template auto try_with_tx(Func&& body) -> decltype(body(std::declval())) { auto make_reducer_ctx = [](Timestamp tx_timestamp) { return ReducerContext( Identity{}, std::nullopt, tx_timestamp, AuthCtx::internal() ); }; return Internal::try_with_tx(make_reducer_ctx, body); } #endif }; } // namespace SpacetimeDB #endif // SPACETIMEDB_HANDLER_CONTEXT_H