1 /*
2 * Copyright (c) 2023 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 #include "text.h"
17
18 namespace OHOS {
19 namespace UDMF {
Text()20 Text::Text() : UnifiedRecord(TEXT)
21 {
22 }
23
Text(UDDetails & variantMap)24 Text::Text(UDDetails &variantMap) : UnifiedRecord(TEXT)
25 {
26 this->details_ = variantMap;
27 }
28
Text(UDType type,ValueType value)29 Text::Text(UDType type, ValueType value) : UnifiedRecord(type, value)
30 {
31 SetType(TEXT);
32 if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
33 auto object = std::get<std::shared_ptr<Object>>(value);
34 std::shared_ptr<Object> detailObj = nullptr;
35 if (object->GetValue(DETAILS, detailObj)) {
36 details_ = ObjectUtils::ConvertToUDDetails(detailObj);
37 }
38 }
39 }
40
GetSize()41 int64_t Text::GetSize()
42 {
43 return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_)) + GetInnerEntriesSize();
44 }
45
SetDetails(UDDetails & variantMap)46 void Text::SetDetails(UDDetails &variantMap)
47 {
48 this->details_ = variantMap;
49 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
50 std::get<std::shared_ptr<Object>>(value_)->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
51 }
52 }
53
GetDetails() const54 UDDetails Text::GetDetails() const
55 {
56 return this->details_;
57 }
58
InitObject()59 void Text::InitObject()
60 {
61 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
62 auto value = value_;
63 value_ = std::make_shared<Object>();
64 auto object = std::get<std::shared_ptr<Object>>(value_);
65 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
66 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
67 }
68 }
69 } // namespace UDMF
70 } // namespace OHOS
71