• 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 
16 #ifndef PANDA_TOOLING_INSPECTOR_TYPES_PROPERTY_DESCRIPTOR_H
17 #define PANDA_TOOLING_INSPECTOR_TYPES_PROPERTY_DESCRIPTOR_H
18 
19 #include "remote_object.h"
20 
21 #include "macros.h"
22 #include "utils/json_builder.h"
23 
24 #include <functional>
25 #include <string>
26 #include <utility>
27 
28 namespace ark::tooling::inspector {
29 class PropertyDescriptor {
30 public:
31     PropertyDescriptor(std::string name, RemoteObject value, bool isEntry = false)
name_(std::move (name))32         : name_(std::move(name)), value_(std::move(value)), isEntry_(isEntry)
33     {
34     }
35 
Accessor(std::string name,RemoteObject getter)36     static PropertyDescriptor Accessor(std::string name, RemoteObject getter)
37     {
38         PropertyDescriptor property(std::move(name), std::move(getter));
39         property.isAccessor_ = true;
40         return property;
41     }
42 
IsAccessor()43     bool IsAccessor() const
44     {
45         return isAccessor_;
46     }
47 
IsConfigurable()48     bool IsConfigurable() const
49     {
50         return configurable_;
51     }
52 
IsEntry()53     bool IsEntry() const
54     {
55         return isEntry_;
56     }
57 
IsEnumerable()58     bool IsEnumerable() const
59     {
60         return enumerable_;
61     }
62 
IsWritable()63     bool IsWritable() const
64     {
65         ASSERT(!IsAccessor());
66         return writable_;
67     }
68 
GetGetter()69     const RemoteObject &GetGetter() const
70     {
71         ASSERT(IsAccessor());
72         return value_;
73     }
74 
GetName()75     const std::string &GetName() const
76     {
77         return name_;
78     }
79 
GetSymbol()80     const std::optional<RemoteObject> &GetSymbol() const
81     {
82         return symbol_;
83     }
84 
GetValue()85     const RemoteObject &GetValue() const
86     {
87         ASSERT(!IsAccessor());
88         return value_;
89     }
90 
SetConfigurable(bool configurable)91     void SetConfigurable(bool configurable)
92     {
93         configurable_ = configurable;
94     }
95 
SetEnumerable(bool enumerable)96     void SetEnumerable(bool enumerable)
97     {
98         enumerable_ = enumerable;
99     }
100 
SetSymbol(RemoteObject symbol)101     void SetSymbol(RemoteObject symbol)
102     {
103         symbol_.emplace(std::move(symbol));
104     }
105 
SetWritable(bool writable)106     void SetWritable(bool writable)
107     {
108         ASSERT(!IsAccessor());
109         writable_ = writable;
110     }
111 
ToJson()112     std::function<void(JsonObjectBuilder &)> ToJson() const
113     {
114         return [this](JsonObjectBuilder &jsonBuilder) {
115             jsonBuilder.AddProperty("name", name_);
116 
117             if (symbol_) {
118                 jsonBuilder.AddProperty("symbol", symbol_->ToJson());
119             }
120 
121             if (isAccessor_) {
122                 jsonBuilder.AddProperty("get", value_.ToJson());
123             } else {
124                 jsonBuilder.AddProperty("value", value_.ToJson());
125                 jsonBuilder.AddProperty("writable", writable_);
126             }
127 
128             jsonBuilder.AddProperty("configurable", configurable_);
129             jsonBuilder.AddProperty("enumerable", enumerable_);
130         };
131     }
132 
133 private:
134     std::string name_;
135     std::optional<RemoteObject> symbol_;
136     RemoteObject value_;
137     bool isAccessor_ {false};
138     bool isEntry_ {false};
139     bool configurable_ {false};
140     bool enumerable_ {true};
141     bool writable_ {true};
142 };
143 }  // namespace ark::tooling::inspector
144 
145 #endif  // PANDA_TOOLING_INSPECTOR_TYPES_PROPERTY_DESCRIPTOR_H
146