载入中...
搜索中...
未找到
NumericRead.h
浏览该文件的文档.
1#pragma once
2
3#include "common/ValueRead.h"
4
5#include <cmath>
6#include <limits>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <utility>
11
13
25 std::string_view name, std::string_view path,
26 std::string_view source) {
27 const auto found = object.find(std::string(name));
28 if (found == object.end()) return Result<std::optional<double>>::success(std::nullopt);
29 auto parsed = readNumericDoubleValue(found->second);
30 if (!parsed)
32 DiagnosticCode::InvalidArgument, "Expected number", std::string(path), {}, std::string(source)));
33 if (!std::isfinite(parsed.value()))
35 DiagnosticCode::InvalidArgument, "Expected finite number", std::string(path), {}, std::string(source)));
36 return Result<std::optional<double>>::success(parsed.value());
37}
38
49[[nodiscard]] inline Result<std::optional<int>> readOptionalInt(const Value::Object& object, std::string_view name,
50 std::string_view path, std::string_view source) {
51 const auto found = object.find(std::string(name));
52 if (found == object.end()) return Result<std::optional<int>>::success(std::nullopt);
53 const auto failure = [&](std::string message) {
55 DiagnosticCode::InvalidArgument, std::move(message), std::string(path), {}, std::string(source)));
56 };
57 if (const auto* integer = found->second.getIf<std::int64_t>()) {
58 if (*integer < std::numeric_limits<int>::min() || *integer > std::numeric_limits<int>::max())
59 return failure("Integer out of range");
60 return Result<std::optional<int>>::success(static_cast<int>(*integer));
61 }
62 if (const auto* number = found->second.getIf<double>()) {
63 if (!std::isfinite(*number) || *number != std::floor(*number)) return failure("Expected integer");
64 if (*number < static_cast<double>(std::numeric_limits<int>::min()) ||
65 *number > static_cast<double>(std::numeric_limits<int>::max()))
66 return failure("Integer out of range");
67 return Result<std::optional<int>>::success(static_cast<int>(*number));
68 }
69 return failure("Expected integer");
70}
71
72} // namespace eve::weapon::detail
std::string name
Definition ECS.cpp:154
std::string message
std::string path
Definition PlayHost.cpp:111
double number
bool found
std::string_view source
size_t end
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
std::map< std::string, Value > Object
Definition Value.h:34
Result< std::optional< double > > readOptionalDouble(const Value::Object &object, std::string_view name, std::string_view path, std::string_view source)
Decode an optional finite number for the weapon codecs.
Definition NumericRead.h:24
Result< std::optional< int > > readOptionalInt(const Value::Object &object, std::string_view name, std::string_view path, std::string_view source)
Decode an optional native int, also accepting integral finite Doubles, for the weapon codecs.
Definition NumericRead.h:49
Result< double > readNumericDoubleValue(const Value &value)
Read an Int64 or Double as a double without imposing a finite-value policy.
Definition Value.cpp:74