载入中...
搜索中...
未找到
McpSceneLiteral.hpp
浏览该文件的文档.
1#pragma once
2#include <Poco/Dynamic/Var.h>
3#include <Poco/JSON/Array.h>
4#include <Poco/JSON/Object.h>
5#include <cmath>
6#include <iomanip>
7#include <limits>
8#include <locale>
9#include <sstream>
10#include "common/Result.h"
11#include "devtools/McpJson.hpp"
12
13namespace eve::dev {
19[[nodiscard]] inline Result<std::string> sceneLiteral(const Poco::Dynamic::Var& value, int depth = 0) {
20 const auto reject = [] {
22 DiagnosticCode::InvalidArgument, "Scene parameters require finite JSON values with depth at most 16",
23 "devtools.scene.parameters"));
24 };
25 if (depth > 16) return reject();
26 if (value.isEmpty()) return Result<std::string>::success("null");
27 if (value.isBoolean()) return Result<std::string>::success(value.convert<bool>() ? "true" : "false");
28 if (value.isInteger()) return Result<std::string>::success(std::to_string(value.convert<Poco::Int64>()));
29 if (value.isNumeric()) {
30 const double number = value.convert<double>();
31 if (!std::isfinite(number)) return reject();
32 std::ostringstream stream;
33 stream.imbue(std::locale::classic());
34 stream << std::setprecision(std::numeric_limits<double>::max_digits10) << number;
35 auto text = stream.str();
36 if (text.find_first_of(".eE") == std::string::npos) text += ".0";
37 return Result<std::string>::success(std::move(text));
38 }
39 if (value.isString())
40 return Result<std::string>::success("\"" + mcpJsonEscape(value.convert<std::string>()) + "\"");
41 if (value.type() == typeid(Poco::JSON::Array::Ptr)) {
42 const auto array = value.extract<Poco::JSON::Array::Ptr>();
43 if (!array) return reject();
44 std::string text = "[";
45 for (std::size_t i = 0; i < array->size(); ++i) {
46 auto item = sceneLiteral(array->get(static_cast<unsigned int>(i)), depth + 1);
47 if (!item.ok()) return Result<std::string>::failure(item.status());
48 if (i) text += ",";
49 text += item.value();
50 }
52 }
53 if (value.type() == typeid(Poco::JSON::Object::Ptr)) {
54 const auto object = value.extract<Poco::JSON::Object::Ptr>();
55 if (!object) return reject();
56 std::string text = "{";
57 bool first = true;
58 for (const auto& [key, value] : *object) {
59 auto item = sceneLiteral(value, depth + 1);
60 if (!item.ok()) return Result<std::string>::failure(item.status());
61 if (!first) text += ",";
62 first = false;
63 text += "[\"" + mcpJsonEscape(key) + "\"]=" + item.value();
64 }
66 }
67 return reject();
68}
69} // namespace eve::dev
SQObjectPtr key
Definition ECS.cpp:155
std::int32_t first
Object::Ptr value
Move-only, checked operation results for the common layer.
double number
std::string text
Json object
std::uint32_t depth
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
std::string mcpJsonEscape(const std::string &value)
Escape a raw string for embedding inside a JSON string literal.
Definition McpJson.hpp:31
Result< std::string > sceneLiteral(const Poco::Dynamic::Var &value, int depth=0)
Encode owning JSON data as a Squirrel literal without losing pointer containers or precision.