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 "utils/expected.h"
20 #include "utils/json_parser.h"
21 #include "utils/string_helpers.h"
22
23 #include <cstddef>
24 #include <cstdint>
25 #include <limits>
26 #include <string>
27 #include <type_traits>
28
29 namespace ark::tooling::inspector {
30 using BreakpointId = size_t;
31 using FrameId = uint32_t;
32 using RemoteObjectId = size_t;
33 using ScriptId = size_t;
34
35 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
ParseNumericId(const JsonObject & object,const char * propertyName)36 Expected<T, std::string> ParseNumericId(const JsonObject &object, const char *propertyName)
37 {
38 auto property = object.GetValue<JsonObject::StringT>(propertyName);
39 if (!property) {
40 return Unexpected(std::string("No such property: ") + propertyName);
41 }
42
43 intmax_t value;
44 if (!helpers::string::ParseInt(*property, &value, INTMAX_C(0)) || value < 0 ||
45 std::numeric_limits<T>::max() < static_cast<uintmax_t>(value)) {
46 return Unexpected("Invalid id: " + *property);
47 }
48
49 return static_cast<T>(value);
50 }
51 } // namespace ark::tooling::inspector
52
53 #endif // PANDA_TOOLING_INSPECTOR_TYPES_NUMERIC_ID_H
54