1/*
2 * Copyright (c) 2021-2022 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#include "common/option_args.h"
16#include "hilog_wrapper.h"
17namespace OHOS {
18namespace HiviewDFX {
19OptionArgs::OptionArgs()
20{
21}
22
23OptionArgs::~OptionArgs()
24{
25    list_.clear();
26    names_.clear();
27    args_.clear();
28}
29
30OptionArgs& OptionArgs::operator=(const OptionArgs& optArgs)
31{
32    type_ = optArgs.type_;
33
34    hasPid_ = optArgs.hasPid_;
35    pid_ = optArgs.pid_;
36    uid_ = optArgs.uid_;
37
38    hasCpuId_ = optArgs.hasCpuId_;
39    cpuId_ = optArgs.cpuId_;
40
41    hasStr_ = optArgs.hasStr_;
42    str_ = optArgs.str_;
43
44    hasList_ = optArgs.hasList_;
45    list_.assign(optArgs.list_.begin(), optArgs.list_.end());
46
47    hasNamesAndArgs_ = optArgs.hasNamesAndArgs_;
48    names_.assign(optArgs.names_.begin(), optArgs.names_.end());
49    args_.assign(optArgs.args_.begin(), optArgs.args_.end());
50
51    return *this;
52}
53
54bool OptionArgs::HasPid() const
55{
56    return hasPid_;
57}
58
59void OptionArgs::SetPid(int pid, int uid)
60{
61    hasPid_ = true;
62    pid_ = pid;
63    uid_ = uid;
64}
65
66int OptionArgs::GetPid() const
67{
68    return pid_;
69}
70
71int OptionArgs::GetUid() const
72{
73    return uid_;
74}
75
76bool OptionArgs::HasCpuId() const
77{
78    return hasCpuId_;
79}
80
81void OptionArgs::SetCpuId(int cpuid)
82{
83    hasCpuId_ = true;
84    cpuId_ = cpuid;
85}
86
87int OptionArgs::GetCpuId() const
88{
89    return cpuId_;
90}
91
92bool OptionArgs::HasStr() const
93{
94    return hasStr_;
95}
96
97void OptionArgs::SetStr(const std::string& str)
98{
99    hasStr_ = true;
100    str_ = str;
101}
102
103const std::string& OptionArgs::GetStr() const
104{
105    return str_;
106}
107
108bool OptionArgs::HasStrList() const
109{
110    return hasList_;
111}
112
113void OptionArgs::SetStrList(const std::vector<std::string>& list)
114{
115    hasList_ = true;
116    list_.assign(list.begin(), list.end());
117}
118
119const std::vector<std::string>& OptionArgs::GetStrList() const
120{
121    return list_;
122}
123
124bool OptionArgs::HasNamesAndArgs() const
125{
126    return hasNamesAndArgs_;
127}
128
129void OptionArgs::SetNamesAndArgs(const std::vector<std::string>& names, const std::vector<std::string>& args)
130{
131    hasNamesAndArgs_ = true;
132    names_.assign(names.begin(), names.end());
133    args_.assign(args.begin(), args.end());
134}
135
136const std::vector<std::string>& OptionArgs::GetNameList() const
137{
138    return names_;
139}
140
141const std::vector<std::string>& OptionArgs::GetArgList() const
142{
143    return args_;
144}
145
146void OptionArgs::Dump() const
147{
148    if (HasPid()) {
149        DUMPER_HILOGD(MODULE_COMMON, "debug|    pid=%{public}d, uid=%{public}d", pid_, uid_);
150    }
151    if (HasCpuId()) {
152        DUMPER_HILOGD(MODULE_COMMON, "debug|    cpuId=%{public}d", cpuId_);
153    }
154    if (HasStr()) {
155        DUMPER_HILOGD(MODULE_COMMON, "debug|    str=%{public}s", str_.c_str());
156    }
157    if (HasStrList()) {
158        for (size_t i = 0; i < list_.size(); i++) {
159            DUMPER_HILOGD(MODULE_COMMON, "debug|    list[%{public}zu]=%{public}s", i, list_[i].c_str());
160        }
161    }
162    if (HasNamesAndArgs()) {
163        for (size_t i = 0; i < names_.size(); i++) {
164            DUMPER_HILOGD(MODULE_COMMON, "debug|    names[%{public}zu]=%{public}s", i, names_[i].c_str());
165        }
166        for (size_t i = 0; i < args_.size(); i++) {
167            DUMPER_HILOGD(MODULE_COMMON, "debug|    args[%{public}zu]=%{public}s", i, args_[i].c_str());
168        }
169    }
170}
171
172std::shared_ptr<OptionArgs> OptionArgs::Clone(std::shared_ptr<OptionArgs>& optArgs)
173{
174    auto ret = Create();
175    if (optArgs != nullptr) {
176        ret->type_ = optArgs->type_;
177        ret->hasPid_ = optArgs->hasPid_;
178        ret->pid_ = optArgs->pid_;
179        ret->uid_ = optArgs->uid_;
180        ret->hasCpuId_ = optArgs->hasCpuId_;
181        ret->cpuId_ = optArgs->cpuId_;
182        ret->hasStr_ = optArgs->hasStr_;
183        ret->str_ = optArgs->str_;
184        ret->hasList_ = optArgs->hasList_;
185        ret->list_.assign(optArgs->list_.begin(), optArgs->list_.end());
186        ret->hasNamesAndArgs_ = optArgs->hasNamesAndArgs_;
187        ret->names_.assign(optArgs->names_.begin(), optArgs->names_.end());
188        ret->args_.assign(optArgs->args_.begin(), optArgs->args_.end());
189    }
190    return ret;
191}
192
193std::shared_ptr<OptionArgs> OptionArgs::Create()
194{
195    return std::make_shared<OptionArgs>();
196}
197} // namespace HiviewDFX
198} // namespace OHOS
199