• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PANDA_TOOLING_INSPECTOR_TYPES_REMOTE_OBJECT_H
16 #define PANDA_TOOLING_INSPECTOR_TYPES_REMOTE_OBJECT_H
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <functional>
21 #include <optional>
22 #include <string>
23 #include <type_traits>
24 #include <utility>
25 #include <vector>
26 
27 #include "types/numeric_id.h"
28 #include "types/remote_object_type.h"
29 #include "types/object_preview.h"
30 
31 namespace ark {
32 class JsonObjectBuilder;
33 }  // namespace ark
34 
35 namespace ark::tooling::inspector {
36 
37 class RemoteObject final : public JsonSerializable {
38 public:
Undefined()39     static RemoteObject Undefined()
40     {
41         return RemoteObject();
42     }
43 
Null()44     static RemoteObject Null()
45     {
46         return RemoteObject(nullptr);
47     }
48 
Boolean(bool boolean)49     static RemoteObject Boolean(bool boolean)
50     {
51         return RemoteObject(boolean);
52     }
53 
Number(int32_t number)54     static RemoteObject Number(int32_t number)
55     {
56         return RemoteObject(RemoteObjectType::NumberT {number});
57     }
58 
59     template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
Number(T number)60     static RemoteObject Number(T number)
61     {
62         return RemoteObject(RemoteObjectType::NumberT {number});
63     }
64 
65     template <typename T,
66               std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T> && sizeof(int32_t) < sizeof(T), int> = 0>
67     static RemoteObject Number(T number)
68     {
69         if (INT32_MIN <= number && number <= INT32_MAX) {
70             return RemoteObject(RemoteObjectType::NumberT {static_cast<int32_t>(number)});
71         }
72         if (number < 0) {
73             return RemoteObject(RemoteObjectType::BigIntT {-1, -static_cast<uintmax_t>(number)});
74         }
75         return RemoteObject(RemoteObjectType::BigIntT {1, static_cast<uintmax_t>(number)});
76     }
77 
78     template <typename T, std::enable_if_t<std::is_unsigned_v<T> && sizeof(int32_t) <= sizeof(T), int> = 0>
Number(T number)79     static RemoteObject Number(T number)
80     {
81         if (number <= INT32_MAX) {
82             return RemoteObject(RemoteObjectType::NumberT {static_cast<int32_t>(number)});
83         }
84         return RemoteObject(RemoteObjectType::BigIntT {1, number});
85     }
86 
String(std::string string)87     static RemoteObject String(std::string string)
88     {
89         return RemoteObject(std::move(string));
90     }
91 
Symbol(std::string description)92     static RemoteObject Symbol(std::string description)
93     {
94         return RemoteObject(RemoteObjectType::SymbolT {std::move(description)});
95     }
96 
97     static RemoteObject Object(std::string className, std::optional<RemoteObjectId> objectId = std::nullopt,
98                                std::optional<std::string> description = std::nullopt)
99     {
100         return RemoteObject(RemoteObjectType::ObjectT {std::move(className), objectId, std::move(description)});
101     }
102 
103     static RemoteObject Array(std::string className, size_t length,
104                               std::optional<RemoteObjectId> objectId = std::nullopt)
105     {
106         return RemoteObject(RemoteObjectType::ArrayT {std::move(className), objectId, length});
107     }
108 
109     static RemoteObject Function(std::string className, std::string name, size_t length,
110                                  std::optional<RemoteObjectId> objectId = std::nullopt)
111     {
112         return RemoteObject(RemoteObjectType::FunctionT {std::move(className), objectId, std::move(name), length});
113     }
114 
115     std::optional<RemoteObjectId> GetObjectId() const;
116 
117     void Serialize(JsonObjectBuilder &builder) const override;
118 
119     RemoteObjectType GetType() const;
120 
SetObjectPreview(ObjectPreview preview)121     void SetObjectPreview(ObjectPreview preview)
122     {
123         preview_ = std::move(preview);
124     }
125 
GetValue()126     RemoteObjectType::TypeValue &GetValue()
127     {
128         return value_;
129     }
130 
GetValue()131     const RemoteObjectType::TypeValue &GetValue() const
132     {
133         return value_;
134     }
135 
136     static std::string GetDescription(const RemoteObjectType::BigIntT &bigint);
137     static std::string GetDescription(const RemoteObjectType::ObjectT &object);
138     static std::string GetDescription(const RemoteObjectType::ArrayT &array);
139     static std::string GetDescription(const RemoteObjectType::FunctionT &function);
140 
141 private:
142     template <typename... T>
RemoteObject(T &&...value)143     explicit RemoteObject(T &&...value) : value_(std::forward<T>(value)...)
144     {
145     }
146 
147 private:
148     RemoteObjectType::TypeValue value_;
149 
150     std::optional<ObjectPreview> preview_;
151 };
152 }  // namespace ark::tooling::inspector
153 
154 #endif  // PANDA_TOOLING_INSPECTOR_TYPES_REMOTE_OBJECT_H
155