载入中...
搜索中...
未找到
WorldSnapshotParsing.cpp
浏览该文件的文档.
2
3#include <charconv>
4#include <cmath>
5#include <limits>
6
8
9bool hasExactFields(const eve::Value::Object& object, std::initializer_list<std::string_view> expected) {
10 if (object.size() != expected.size()) return false;
11 for (const std::string_view field : expected) {
12 if (!object.contains(std::string(field))) return false;
13 }
14 return true;
15}
16
17const eve::Value* field(const eve::Value::Object& object, std::string_view name) {
18 const auto found = object.find(std::string(name));
19 return found == object.end() ? nullptr : &found->second;
20}
21
23 const eve::Value* value = field(object, name);
24 if (!value || !value->isString())
26 eve::DiagnosticCode::ParseError, "snapshot field must be a string", std::string(name)));
27 return eve::Result<std::string>::success(value->asString());
28}
29
31 auto text = readString(object, name);
32 if (!text) return eve::Result<std::uint64_t>::failure(text.status());
33 std::uint64_t output = 0;
34 const std::string& value = text.value();
35 const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), output);
36 if (value.empty() || error != std::errc{} || end != value.data() + value.size())
38 eve::DiagnosticCode::ParseError, "snapshot integer is not a uint64 decimal string", std::string(name)));
40}
41
43 auto text = readString(object, name);
44 if (!text) return eve::Result<std::int64_t>::failure(text.status());
45 std::int64_t output = 0;
46 const std::string& value = text.value();
47 const auto [end, error] = std::from_chars(value.data(), value.data() + value.size(), output);
48 if (value.empty() || error != std::errc{} || end != value.data() + value.size())
50 eve::DiagnosticCode::ParseError, "snapshot integer is not an int64 decimal string", std::string(name)));
52}
53
54eve::Result<double> readNumber(const eve::Value::Object& object, std::string_view name) {
55 const eve::Value* value = field(object, name);
56 if (!value || !value->isNumeric())
58 eve::DiagnosticCode::ParseError, "snapshot field must be numeric", std::string(name)));
59 const double result = value->isDouble() ? value->asDouble() : static_cast<double>(value->asInt());
60 if (!std::isfinite(result))
62 eve::DiagnosticCode::InvalidArgument, "snapshot numeric field must be finite", std::string(name)));
64}
65
66eve::Result<float> readFloat(const eve::Value::Object& object, std::string_view name) {
67 auto number = readNumber(object, name);
68 if (!number) return eve::Result<float>::failure(number.status());
69 if (number.value() < -std::numeric_limits<float>::max() || number.value() > std::numeric_limits<float>::max())
71 eve::DiagnosticCode::InvalidArgument, "snapshot numeric field is outside float range", std::string(name)));
72 return eve::Result<float>::success(static_cast<float>(number.value()));
73}
74
75eve::Result<bool> readBool(const eve::Value::Object& object, std::string_view name) {
76 const eve::Value* value = field(object, name);
77 if (!value || !value->isBool())
79 "snapshot field must be boolean", std::string(name)));
80 return eve::Result<bool>::success(value->asBool());
81}
82
83} // namespace eve::physics::snapshot_detail
std::string output
std::string name
Definition ECS.cpp:154
Poco::JSON::Object::Ptr result
std::string error
Definition Package.cpp:174
Object::Ptr value
double number
bool found
std::string text
size_t end
float size
Definition TreeMesh.cpp:158
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
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
Result< float > readFloat(const Value::Object &object, std::string_view name)
Reads a required finite number representable by float.
const Value * field(const Value::Object &object, std::string_view name)
Borrow an optional field only during the current synchronous codec call. @ownership Object owns the r...
bool hasExactFields(const Value::Object &object, std::initializer_list< std::string_view > expected)
Internal exact-field schema predicate.
Result< std::uint64_t > readUint64(const Value::Object &object, std::string_view name)
Reads a required unsigned decimal integer string.
Result< std::int64_t > readInt64(const Value::Object &object, std::string_view name)
Reads a required signed decimal integer string.
Result< bool > readBool(const Value::Object &object, std::string_view name)
Reads a required boolean.
eve::Result< double > readNumber(const eve::Value::Object &object, std::string_view name)
Reads a required finite number.
Result< std::string > readString(const Value::Object &object, std::string_view name)
Reads a required owning string.