载入中...
搜索中...
未找到
CommandQueue.cpp
浏览该文件的文档.
44 result = it->second.isDouble() ? it->second.asDouble() : static_cast<double>(it->second.asInt());
130 eve::DiagnosticCode::InvalidArgument, "order payload key must not be empty", "key", {}, "orders"));
140 eve::DiagnosticCode::InvalidArgument, "order payload key must not be empty", "key", {}, "orders"));
169std::string CommandQueue::create(const std::string& kind, int priority, double timeoutSeconds, bool activateNow) {
181 {{nextSequence_++, {}}, orders_[index].id, orders_[index].kind, OrderState::Queued, OrderState::Queued});
194std::string CommandQueue::appendUnchecked(const std::string& kind, int priority, double timeoutSeconds) {
198std::string CommandQueue::replaceUnchecked(const std::string& kind, int priority, double timeoutSeconds) {
204std::string CommandQueue::interruptUnchecked(const std::string& kind, int priority, double timeoutSeconds) {
255 std::find_if(queued_.begin(), queued_.end(), [this, &id](size_t index) { return orders_[index].id == id; });
271eve::Result<std::string> CommandQueue::append(const std::string& kind, int priority, double timeoutSeconds) {
276 return eve::Result<std::string>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument,
283 return eve::Result<std::string>::success(std::string(id), eve::Status::success(eve::StatusCode::Applied));
286eve::Result<std::string> CommandQueue::replace(const std::string& kind, int priority, double timeoutSeconds) {
291 return eve::Result<std::string>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument,
297 eve::DiagnosticCode::Failed, "order replacement did not produce an identity", {}, {}, "orders"));
298 return eve::Result<std::string>::success(std::string(id), eve::Status::success(eve::StatusCode::Applied));
301eve::Result<std::string> CommandQueue::interrupt(const std::string& kind, int priority, double timeoutSeconds) {
306 return eve::Result<std::string>::failure(eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument,
311 eve::DiagnosticCode::Conflict, "order priority cannot preempt the active order", "priority", {}, "orders"));
316 return eve::Result<std::string>::success(std::string(id), eve::Status::success(eve::StatusCode::Applied));
325 eve::DiagnosticCode::Conflict, "order is not the active unfinished order", "id", {}, "orders"));
335 eve::DiagnosticCode::Conflict, "order is not the active unfinished order", "id", {}, "orders"));
357 eve::Status::success(dtSeconds == 0.0 || !hadActiveOrder ? eve::StatusCode::NoOp : eve::StatusCode::Applied));
424 root.emplace("active", active_ == noIndex ? eve::Value() : eve::Value(static_cast<std::int64_t>(active_)));
433 if (index >= orders_.size() || index > static_cast<size_t>(std::numeric_limits<std::int64_t>::max()))
483 eve::DiagnosticCode::ParseError, "command queue snapshot must be an object", {}, {}, "orders.snapshot"));
497 if (ordersIt == root->end() || queuedIt == root->end() || activeIt == root->end() || eventsIt == root->end() ||
518 eve::DiagnosticCode::ParseError, "order entry must be an object", "orders", {}, "orders.snapshot"));
522 if (!readObjectMember(*object, "id", order.id) || !readObjectMember(*object, "kind", order.kind) ||
523 !readObjectMember(*object, "priority", priority) || !readOrderState(*object, "state", order.state) ||
528 eve::DiagnosticCode::ParseError, "order entry has an invalid field", "orders", {}, "orders.snapshot"));
535 order.id.empty() || order.kind.empty() || order.timeoutSeconds < 0.0 || order.elapsedSeconds < 0.0 ||
538 eve::DiagnosticCode::ParseError, "order entry violates an invariant", "orders", {}, "orders.snapshot"));
567 (candidate.orders_[previous].priority == candidate.orders_[index].priority && previous > index))
581 eve::DiagnosticCode::ParseError, "active order violates an invariant", "active", {}, "orders.snapshot"));
598 eve::DiagnosticCode::ParseError, "event entry must be an object", "events", {}, "orders.snapshot"));
602 if (!readCounter(*object, "sequence", event.sequence) || !readObjectMember(*object, "orderId", event.orderId) ||
606 event.sequence <= previousSequence || event.sequence >= candidate.nextSequence_ || event.kind.empty() ||
609 eve::DiagnosticCode::ParseError, "event entry violates an invariant", "events", {}, "orders.snapshot"));
648 "OrderPayload", std::function<OrderPayload*()>([]() -> OrderPayload* { return nullptr; }), false);
653 payload.addFunc("setJson", [vm](OrderPayload* value, const std::string& key, const std::string& json) {
666 eve::DiagnosticCode::InvalidArgument, "order payload must not be null", "payload", {}, "orders")),
668 return eve::script::projectResult(vm, value->erase(key), [](bool removed) { return eve::Value(removed); });
674 auto order = table.addClass<Order>("Order", std::function<Order*()>([]() -> Order* { return nullptr; }), false);
678 order.addFunc("getState", [](Order* value) { return value ? stateName(value->state) : std::string{}; });
679 order.addFunc("getTimeout", [](Order* value) { return value ? static_cast<float>(value->timeoutSeconds) : 0.0f; });
680 order.addFunc("getElapsed", [](Order* value) { return value ? static_cast<float>(value->elapsedSeconds) : 0.0f; });
682 order.addFunc("getPayload", [](Order* value) -> OrderPayload* { return value ? &value->payload : nullptr; });
688 event.addFunc("getOrderId", [](OrderEvent* value) { return value ? value->orderId : std::string{}; });
689 event.addFunc("getKind", [](OrderEvent* value) { return value ? value->kind : std::string{}; });
690 event.addFunc("getFrom", [](OrderEvent* value) { return value ? stateName(value->from) : std::string{}; });
691 event.addFunc("getTo", [](OrderEvent* value) { return value ? stateName(value->to) : std::string{}; });
692 event.addFunc("getReason", [](OrderEvent* value) { return value ? value->reason : std::string{}; });
695 "CommandQueue", std::function<CommandQueue*()>([]() -> CommandQueue* { return nullptr; }), false);
696 queue.addFunc("append", [vm](CommandQueue* value, const std::string& kind, int priority, float timeout) {
706 queue.addFunc("replace", [vm](CommandQueue* value, const std::string& kind, int priority, float timeout) {
716 queue.addFunc("interrupt", [vm](CommandQueue* value, const std::string& kind, int priority, float timeout) {
733 queue.addFunc("fail", [vm](CommandQueue* value, const std::string& id, const std::string& reason) {
740 queue.addFunc("cancel", [vm](CommandQueue* value, const std::string& id, const std::string& reason) {
784 "OwnedOrderQueue", std::function<ScriptCommandQueue*()>([]() -> ScriptCommandQueue* { return nullptr; }),
790 [](ScriptCommandQueue* value) { return value ? value->reference.packed() : std::uint64_t{0}; });
804 ownedQueue.addFunc("append", [vm](ScriptCommandQueue* value, const std::string& kind, int priority, float timeout) {
821 ownedQueue.addFunc("replace", [vm](ScriptCommandQueue* value, const std::string& kind, int priority, float timeout) {
838 ownedQueue.addFunc("interrupt", [vm](ScriptCommandQueue* value, const std::string& kind, int priority,
856 ownedQueue.addFunc("fail", [vm](ScriptCommandQueue* value, const std::string& id, const std::string& reason) {
866 eve::DiagnosticCode::StaleHandle, "owned order queue handle is stale", "queue", {}, "orders")));
869 ownedQueue.addFunc("cancel", [vm](ScriptCommandQueue* value, const std::string& id, const std::string& reason) {
879 eve::DiagnosticCode::StaleHandle, "owned order queue handle is stale", "queue", {}, "orders")));
906 eve::DiagnosticCode::StaleHandle, "owned order queue handle is stale", "queue", {}, "orders")));
936 eve::DiagnosticCode::StaleHandle, "owned order queue handle is stale", "queue", {}, "orders")));
949 eve::DiagnosticCode::StaleHandle, "owned order queue handle is stale", "queue", {}, "orders")));
971 eve::script::makeOwnedSquirrelInstance<ScriptCommandQueue>(vm, std::make_unique<ScriptCommandQueue>(ref));
978 auto result = eve::script::projectStatusResult(vm, eve::Status::success(eve::StatusCode::Applied));
The single Squirrel projection for common Result, Status and Value.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Definition Result.h:537
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Result< std::string > toJson() const
Serialize this value as deterministic compact JSON.
Definition Value.cpp:67
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:57
const T * getIf() const noexcept
Return a typed pointer, or nullptr when the kind differs.
Definition Value.h:180
Generic priority order queue with no gameplay or entity semantics.
Definition CommandQueue.h:89
void clear()
Resets the queue, retained history, ids, and event sequences.
Definition CommandQueue.cpp:367
eve::Result< void > restore(std::string_view json)
Restores a command queue transactionally from a JSON snapshot.
Definition CommandQueue.cpp:476
eve::OptionalRef< Order > find(const std::string &id)
Returns an order by stable id, including terminal history.
Definition CommandQueue.cpp:384
eve::Result< void > fail(const std::string &id, const std::string &reason)
Fails the active command.
Definition CommandQueue.cpp:329
eve::OptionalRef< OrderEvent > eventAt(int index)
Returns an event by sequence order, or null for an invalid index.
Definition CommandQueue.cpp:410
eve::Result< std::string > snapshot() const
Serializes the complete command queue as deterministic JSON.
Definition CommandQueue.cpp:422
eve::OptionalRef< Order > current()
Returns the active order, or a null observation when the queue is idle.
Definition CommandQueue.cpp:376
eve::Result< void > complete(const std::string &id)
Completes the active command.
Definition CommandQueue.cpp:319
eve::Result< std::string > interrupt(const std::string &kind, int priority=0, double timeoutSeconds=0.0)
Preempts the active command when priority permits.
Definition CommandQueue.cpp:301
eve::Result< std::string > append(const std::string &kind, int priority=0, double timeoutSeconds=0.0)
Appends a command and returns its stable identity or validation failure.
Definition CommandQueue.cpp:271
eve::Result< void > update(double dtSeconds)
Checked simulation update.
Definition CommandQueue.cpp:349
eve::OptionalRef< Order > orderAt(int index)
Returns an order in creation order, or null for an invalid index.
Definition CommandQueue.cpp:398
eve::Result< std::string > replace(const std::string &kind, int priority=0, double timeoutSeconds=0.0)
Cancels unfinished commands and starts a replacement.
Definition CommandQueue.cpp:286
int eventCount() const
Returns the number of retained deterministic events.
Definition CommandQueue.cpp:408
int queuedCount() const
Returns the number of queued orders, excluding the active order.
Definition CommandQueue.cpp:394
void clearEvents()
Clears retained events without resetting their sequence counter.
Definition CommandQueue.cpp:420
eve::Result< void > cancel(const std::string &id, const std::string &reason="cancelled")
Cancels an active or queued command.
Definition CommandQueue.cpp:339
JSON-compatible key/value payload carried by an order.
Definition CommandQueue.h:36
eve::Result< bool > erase(const std::string &key)
Removes key and reports whether it existed.
Definition CommandQueue.cpp:137
void setBool(const std::string &key, bool value)
Sets a JSON boolean value.
Definition CommandQueue.cpp:113
void setNumber(const std::string &key, double value)
Sets a JSON number value.
Definition CommandQueue.cpp:108
bool has(const std::string &key) const
Returns true when the payload contains key.
Definition CommandQueue.cpp:135
eve::Result< void > setJson(const std::string &key, std::string_view json)
Parses and stores one unchecked JSON value.
Definition CommandQueue.cpp:121
eve::Result< void > set(const std::string &key, eve::Value value)
Stores an already validated canonical value.
Definition CommandQueue.cpp:127
std::string toJson() const
Serializes the complete payload as a deterministic JSON object.
Definition CommandQueue.cpp:153
void setString(const std::string &key, const std::string &value)
Sets a JSON string value.
Definition CommandQueue.cpp:104
std::string getJson(const std::string &key) const
Returns the canonical JSON fragment for key, or an empty string.
Definition CommandQueue.cpp:146
Script module factory for generic CommandQueue objects.
Definition CommandQueue.h:230
static eve::Result< CommandQueueHandleRef > newQueueHandle()
Allocates a queue and returns its generation-qualified ownership reference.
Definition CommandQueue.cpp:618
static bool isStale(CommandQueueHandleRef reference) noexcept
Reports whether a queue reference is invalid for the current module.
Definition CommandQueue.cpp:637
static eve::script::Borrowed< CommandQueue > resolve(CommandQueueHandleRef reference) noexcept
Resolves a live queue as a non-owning observation.
Definition CommandQueue.cpp:623
static eve::Result< void > release(CommandQueueHandleRef reference)
Releases a queue owned by the Orders module.
Definition CommandQueue.cpp:629
Non-owning C++ observation with no implicit lifetime extension.
Definition RuntimeRegistry.h:42
Result< std::uint32_t > readIndex(const Accessor &accessor, std::uint32_t element)
Read one bounded unsigned scalar index.
Definition GltfDecode.cpp:234
Definition QueueInspector.h:11
@ Completed
@ Active
@ Queued
@ Cancelled
@ Failed
eve::script::RuntimeHandleRef< CommandQueueHandleTag > CommandQueueHandleRef
Generation-qualified reference to a module-owned order queue.
Definition CommandQueue.h:25
std::string stateName(OrderState state)
Returns the stable lowercase name of a state.
Definition CommandQueue.cpp:158
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
Definition SquirrelBinding.cpp:454
const char * objectSemanticName(ObjectSemantic semantic) noexcept
Returns the stable name used by diagnostics and documentation.
Definition SquirrelOwnership.h:52
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
Definition SquirrelBinding.cpp:470
@ InvalidArgument
@ NotFound
@ Failed
@ ParseError
@ StaleHandle
@ Conflict
@ NoOp
@ Applied
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
Definition Container.h:602
Deterministically sequenced order lifecycle event.
Definition CommandQueue.h:76
One entity-agnostic command and its lifecycle metadata.
Definition CommandQueue.h:67
Handle plus owner-lifetime epoch for a non-ECS registry object.
Definition RuntimeRegistry.h:91