载入中...
搜索中...
未找到
EditingValue.cpp
浏览该文件的文档.
2
3#include <type_traits>
4
5namespace eve::editing {
6
7Value::Type Value::type() const { return static_cast<Type>(storage_.index()); }
8
9namespace {
10
11struct ValueBudget {
12 size_t elements = 0;
13 size_t stringBytes = 0;
14};
15
16bool checkValue(const Value& value, size_t depth, size_t maxDepth, size_t maxElements, size_t maxStringBytes,
17 ValueBudget& budget) {
18 if (depth > maxDepth) return false;
19 return std::visit(
20 [&](const auto& current) -> bool {
21 using T = std::decay_t<decltype(current)>;
22 if constexpr (std::is_same_v<T, std::string>) {
23 budget.stringBytes += current.size();
24 return budget.stringBytes <= maxStringBytes;
25 } else if constexpr (std::is_same_v<T, Value::Array>) {
26 budget.elements += current.size();
27 if (budget.elements > maxElements) return false;
28 for (const auto& entry : current)
29 if (!checkValue(entry, depth + 1, maxDepth, maxElements, maxStringBytes, budget)) return false;
30 return true;
31 } else if constexpr (std::is_same_v<T, Value::Object>) {
32 budget.elements += current.size();
33 if (budget.elements > maxElements) return false;
34 for (const auto& [key, entry] : current) {
35 budget.stringBytes += key.size();
36 if (budget.stringBytes > maxStringBytes ||
37 !checkValue(entry, depth + 1, maxDepth, maxElements, maxStringBytes, budget))
38 return false;
39 }
40 return true;
41 } else {
42 return true;
43 }
44 },
45 value.storage());
46}
47
48} // namespace
49
50bool Value::isWithinLimits(size_t maxDepth, size_t maxElements, size_t maxStringBytes) const {
51 if (maxDepth == 0) return false;
52 ValueBudget budget;
53 return checkValue(*this, 1, maxDepth, maxElements, maxStringBytes, budget);
54}
55
56} // namespace eve::editing
double value
size_t stringBytes
std::uint32_t key
double current
std::size_t elements
std::uint32_t depth
Type
Type public API.
bool isWithinLimits(size_t maxDepth, size_t maxElements, size_t maxStringBytes) const
Validate nesting, element count and approximate string byte limits.
Type type() const
Return the active value type.