1 /**
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_TOOLING_INSPECTOR_TYPES_NUMERIC_ID_H
17 #define PANDA_TOOLING_INSPECTOR_TYPES_NUMERIC_ID_H
18
19 #include <cstddef>
20 #include <cstdint>
21 #include <limits>
22 #include <string>
23 #include <type_traits>
24
25 #include "utils/expected.h"
26 #include "utils/json_parser.h"
27 #include "utils/string_helpers.h"
28
29 namespace ark::tooling::inspector {
30 using BreakpointId = size_t;
31 using ExecutionContextId = uint32_t;
32 using FrameId = uint32_t;
33 using RemoteObjectId = size_t;
34 using ScriptId = size_t;
35
36 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
ParseNumericId(const JsonObject & object,const char * propertyName)37 Expected<T, std::string> ParseNumericId(const JsonObject &object, const char *propertyName)
38 {
39 auto property = object.GetValue<JsonObject::StringT>(propertyName);
40 if (!property) {
41 return Unexpected(std::string("No such property: ") + propertyName);
42 }
43
44 intmax_t value;
45 if (!helpers::string::ParseInt(*property, &value, INTMAX_C(0)) || value < 0 ||
46 std::numeric_limits<T>::max() < static_cast<uintmax_t>(value)) {
47 return Unexpected("Invalid id: " + *property);
48 }
49
50 return static_cast<T>(value);
51 }
52 } // namespace ark::tooling::inspector
53
54 #endif // PANDA_TOOLING_INSPECTOR_TYPES_NUMERIC_ID_H
55