-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathModule.h
More file actions
221 lines (179 loc) · 7.51 KB
/
Module.h
File metadata and controls
221 lines (179 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#ifndef SPACETIMEDB_MODULE_H
#define SPACETIMEDB_MODULE_H
#include <string>
#include <vector>
#include <optional>
#include "../bsatn/types.h"
#include "../reducer_macros.h"
#include "../reducer_context.h"
#include "../abi/opaque_types.h"
#include <spacetimedb/abi/FFI.h> // For StatusCode
#include "autogen/TableAccess.g.h" // For TableAccess enum
#include "autogen/Lifecycle.g.h" // For Lifecycle enum
#include "autogen/CaseConversionPolicy.g.h" // For CaseConversionPolicy
namespace SpacetimeDB {
namespace bsatn {
class Writer; // Forward declaration
}
namespace Internal {
// Forward declarations are handled by includes
// Module class - singleton pattern similar to C# static class
class Module {
private:
Module() = default;
Module(const Module&) = delete;
Module& operator=(const Module&) = delete;
public:
static Module& Instance() {
static Module instance;
return instance;
}
// Initialize module (called once)
// Module description for FFI (matching existing signature)
static void __describe_module__(BytesSink sink);
static std::vector<uint8_t> SerializeModuleDef();
// Reducer invocation for FFI (matching existing signature)
static Status __call_reducer__(
uint32_t id,
uint64_t sender_0, uint64_t sender_1, uint64_t sender_2, uint64_t sender_3,
uint64_t conn_id_0, uint64_t conn_id_1,
Timestamp timestamp,
BytesSource args_source,
BytesSink error_sink
);
// View invocation for FFI (with sender)
static int16_t __call_view__(
uint32_t id,
uint64_t sender_0, uint64_t sender_1, uint64_t sender_2, uint64_t sender_3,
BytesSource args_source,
BytesSink result_sink
);
// View invocation for FFI (anonymous - no sender)
static int16_t __call_view_anon__(
uint32_t id,
BytesSource args_source,
BytesSink result_sink
);
// Procedure invocation for FFI
static int16_t __call_procedure__(
uint32_t id,
uint64_t sender_0, uint64_t sender_1, uint64_t sender_2, uint64_t sender_3,
uint64_t conn_id_0, uint64_t conn_id_1,
uint64_t timestamp_microseconds,
BytesSource args_source,
BytesSink result_sink
);
static int16_t __call_http_handler__(
uint32_t id,
uint64_t timestamp_microseconds,
BytesSource request_source,
BytesSource request_body_source,
BytesSink response_sink,
BytesSink response_body_sink
);
// Internal registration methods (inline to avoid linking issues)
template<typename T>
static void RegisterTableInternal(const char* name, bool is_public, bool is_event = false) {
// Forward declaration - implementation will be included at end of file
RegisterTableInternalImpl<T>(name, is_public, is_event);
}
template<typename Func>
static void RegisterReducerInternal(const std::string& name, Func func) {
// Forward declaration - implementation will be included at end of file
RegisterReducerInternalImpl<Func>(name, func);
}
// Implementation methods (will be defined after including Module_impl.h)
template<typename T>
static void RegisterTableInternalImpl(const char* name, bool is_public, bool is_event = false);
public:
// These need to be public for macro access
template<typename Func>
static void RegisterReducerInternalImpl(const std::string& name, Func func);
template<typename Func>
static void RegisterReducerInternalWithNames(const std::string& name, Func func, const std::vector<std::string>& param_names);
private:
public:
// Registration support routed through the V10 module-definition builder.
static void RegisterClientVisibilityFilter(const char* sql);
static void SetCaseConversionPolicy(CaseConversionPolicy policy);
static void RegisterExplicitTableName(const std::string& source_name, const std::string& canonical_name);
static void RegisterExplicitFunctionName(const std::string& source_name, const std::string& canonical_name);
static void RegisterExplicitIndexName(const std::string& source_name, const std::string& canonical_name);
};
// Helper functions for module description
std::vector<uint8_t> ConsumeBytes(BytesSource source);
void WriteBytes(BytesSink sink, const std::vector<uint8_t>& bytes);
void SetTableIsEventFlag(const std::string& table_name, bool is_event);
bool GetTableIsEventFlag(const std::string& table_name);
} // namespace Internal
// Public alias to mirror C# API shape (`SpacetimeDB.CaseConversionPolicy`).
using CaseConversionPolicy = Internal::CaseConversionPolicy;
// Public API similar to C# Module class
class Module {
public:
// Table registration
template<typename T>
static void RegisterTable(const char* name, bool is_public = true, bool is_event = false) {
Internal::Module::RegisterTableInternal<T>(name, is_public, is_event);
}
// Reducer registration
template<typename Func>
static void RegisterReducer(const char* name, Func func) {
Internal::Module::RegisterReducerInternal(name, func);
}
// Client visibility filter (similar to C# / Rust)
static void RegisterClientVisibilityFilter(const char* sql) {
Internal::Module::RegisterClientVisibilityFilter(sql);
}
// Module metadata (future extension)
static void SetMetadata([[maybe_unused]] const char* name, [[maybe_unused]] const char* version) {
// TODO: Implement module metadata
}
static void SetCaseConversionPolicy(CaseConversionPolicy policy) {
Internal::Module::SetCaseConversionPolicy(policy);
}
static void RegisterExplicitTableName(const char* source_name, const char* canonical_name) {
Internal::Module::RegisterExplicitTableName(source_name, canonical_name);
}
static void RegisterExplicitFunctionName(const char* source_name, const char* canonical_name) {
Internal::Module::RegisterExplicitFunctionName(source_name, canonical_name);
}
static void RegisterExplicitIndexName(const char* source_name, const char* canonical_name) {
Internal::Module::RegisterExplicitIndexName(source_name, canonical_name);
}
};
// Global registration functions for X-Macro support
template<typename T>
void register_table_impl(const char* name, bool is_public) {
Internal::Module::RegisterTableInternal<T>(name, is_public);
}
template<typename Func>
void register_reducer_impl(const std::string& name, Func func) {
Internal::Module::RegisterReducerInternal(name, func);
}
// Initialize module - no-op; preinit functions handle registration.
inline void initialize_module() {
// No-op.
}
// Write module definition (for FFI)
inline void spacetimedb_write_module_def(uint32_t sink) {
BytesSink bs{sink};
Internal::Module::__describe_module__(bs);
}
// Call reducer (for FFI)
inline int16_t spacetimedb_call_reducer(uint32_t id, uint32_t args,
uint64_t sender_0, uint64_t sender_1,
uint64_t sender_2, uint64_t sender_3) {
// Create a simple timestamp
Timestamp ts(0);
BytesSource args_source{args};
BytesSink error_sink{0}; // Null sink for now
auto status = Internal::Module::__call_reducer__(
id, sender_0, sender_1, sender_2, sender_3,
0, 0, ts, args_source, error_sink);
return is_ok(status) ? 0 : -1;
}
} // namespace SpacetimeDB
// Include the template implementations
#include "Module_impl.h"
#endif // SPACETIMEDB_MODULE_H