载入中...
搜索中...
未找到
PropertyAccess.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <utility>
6
8
10 const auto found = std::find_if(properties.begin(), properties.end(),
11 [&path](const PropertyDescriptor &property) { return property.path == path; });
14}
15
17 if (!std::isfinite(number))
18 return WriteResult::reject("property_access.property.finite", "Numeric values must be finite");
19 if (property.numeric.minimum && number < *property.numeric.minimum)
20 return WriteResult::reject("property_access.property.minimum", "Value is below the minimum");
21 if (property.numeric.maximum && number > *property.numeric.maximum)
22 return WriteResult::reject("property_access.property.maximum", "Value is above the maximum");
23 return WriteResult::success();
24}
25
27 std::size_t expected = 0;
28 switch (kind) {
29 case PropertyKind::Vec2: expected = 2; break;
30 case PropertyKind::Vec3: expected = 3; break;
32 case PropertyKind::Color: expected = 4; break;
33 default: return WriteResult::success();
34 }
35 if (items.size() != expected)
36 return WriteResult::reject("property_access.property.arity",
37 "Composite property has the wrong component count");
38 return WriteResult::success();
39}
40
41WriteResult validatePropertyValue(const PropertyDescriptor &property, const Value &value) {
43 return WriteResult::reject("property_access.property.read-only", "Property is read-only");
44
45 const Value::Type type = value.type();
46 bool compatible = false;
47 switch (property.kind) {
48 case PropertyKind::Auto: compatible = true; break;
49 case PropertyKind::Bool: compatible = type == Value::Type::Bool; break;
50 case PropertyKind::Integer: compatible = type == Value::Type::Integer; break;
56 case PropertyKind::ReadOnlyText: compatible = type == Value::Type::String; break;
61 case PropertyKind::Array: compatible = type == Value::Type::Array; break;
63 case PropertyKind::Map: compatible = type == Value::Type::Object; break;
64 case PropertyKind::Action: compatible = true; break;
65 }
66 if (!compatible)
67 return WriteResult::reject("property_access.property.type", "Property value type does not match schema");
68
69 if (property.kind == PropertyKind::Enum) {
70 const auto *selected = value.getIf<std::string>();
71 if (!selected ||
72 std::find(property.choices.begin(), property.choices.end(), *selected) == property.choices.end())
73 return WriteResult::reject("property_access.property.choice", "Value is not an allowed choice");
74 }
75
76 if (const auto *items = value.getIf<Value::Array>()) {
77 if (const WriteResult arity = validateArrayArity(property.kind, *items); !arity.accepted)
78 return arity;
79 if (property.kind == PropertyKind::Color || property.kind == PropertyKind::Vec2 ||
80 property.kind == PropertyKind::Vec3 || property.kind == PropertyKind::Vec4) {
81 for (const Value &component : *items) {
82 double number = 0.0;
83 if (const auto *integer = component.getIf<std::int64_t>()) {
84 number = static_cast<double>(*integer);
85 } else if (const auto *floating = component.getIf<double>()) {
86 number = *floating;
87 } else {
88 return WriteResult::reject("property_access.property.type",
89 "Composite components must be numeric");
90 }
91 if (const WriteResult componentResult = validateNumericComponent(property, number);
92 !componentResult.accepted)
93 return componentResult;
94 }
95 return WriteResult::success();
96 }
97 }
98
99 double number = 0.0;
100 bool hasNumber = false;
101 if (const auto *integer = value.getIf<std::int64_t>()) {
102 number = static_cast<double>(*integer);
103 hasNumber = true;
104 } else if (const auto *floating = value.getIf<double>()) {
105 number = *floating;
106 hasNumber = true;
107 }
108 if (hasNumber) return validateNumericComponent(property, number);
109 return WriteResult::success();
110}
111
112} // namespace eve::property_access
double value
TokenKind kind
std::string path
Definition PlayHost.cpp:110
double number
bool found
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
Type
Runtime kind of the owned value.
Definition Value.h:38
@ Integer
Compatibility spelling for Int64.
@ Number
Compatibility spelling for Double.
std::vector< Value > Array
Definition Value.h:33
WriteResult validateNumericComponent(const PropertyDescriptor &property, double number)
WriteResult validateArrayArity(PropertyKind kind, const Value::Array &items)
constexpr bool hasFlag(PropertyFlag value, PropertyFlag flag)
True when flag.
PropertyKind
Semantic property kind used by renderer-independent presenters.
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
One stable property description consumed by any UI shell.
eve::OptionalRef< const PropertyDescriptor > find(const std::string &path) const
Find a property by stable path, or an empty immediate borrow.
std::vector< PropertyDescriptor > properties
Result of a model write, including a stable diagnostic code.
static WriteResult success()
Success.
static WriteResult reject(std::string code, std::string message)
Reject.