1 /*
2 * Copyright (c) 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 #include "object_container.h"
17
18 #include <meta/base/interface_utils.h>
19 #include <meta/interface/intf_containable.h>
20 #include <meta/interface/intf_content.h>
21 #include <meta/interface/property/intf_property.h>
22
META_BEGIN_INTERNAL_NAMESPACE()23 META_BEGIN_INTERNAL_NAMESPACE()
24
25 bool ObjectContainer::Build(const IMetadata::Ptr& data)
26 {
27 bool ret = Super::Build(data);
28 if (ret) {
29 parent_ = GetSelf<IContainer>();
30 SetImplementingIContainer(GetSelf<IObject>().get(), this);
31 }
32 return ret;
33 }
34
Destroy()35 void ObjectContainer::Destroy()
36 {
37 InternalRemoveAll();
38 Super::Destroy();
39 }
40
GetAboutToChange()41 IEvent::Ptr ObjectContainer::GetAboutToChange()
42 {
43 auto ev = GetEvent("ContainerAboutToChange", MetadataQuery::EXISTING);
44 return ev && ev->HasHandlers() ? ev : nullptr;
45 }
CallAboutToChange(ContainerChangeType type,const IObject::Ptr & object)46 bool ObjectContainer::CallAboutToChange(ContainerChangeType type, const IObject::Ptr& object)
47 {
48 bool success = true;
49 if (auto ev = GetAboutToChange()) {
50 Invoke<IOnChildChanging>(ev, ChildChangedInfo { type, object, parent_ }, success);
51 }
52 return success;
53 }
54
Add(const IObject::Ptr & object)55 bool ObjectContainer::Add(const IObject::Ptr& object)
56 {
57 return CallAboutToChange(ContainerChangeType::ADDING, object) && Container::Add(object);
58 }
Insert(SizeType index,const IObject::Ptr & object)59 bool ObjectContainer::Insert(SizeType index, const IObject::Ptr& object)
60 {
61 return CallAboutToChange(ContainerChangeType::ADDING, object) && Container::Insert(index, object);
62 }
Replace(const IObject::Ptr & child,const IObject::Ptr & replaceWith,bool addAlways)63 bool ObjectContainer::Replace(const IObject::Ptr& child, const IObject::Ptr& replaceWith, bool addAlways)
64 {
65 return CallAboutToChange(ContainerChangeType::REMOVING, child) &&
66 CallAboutToChange(ContainerChangeType::ADDING, replaceWith) &&
67 Container::Replace(child, replaceWith, addAlways);
68 }
Remove(SizeType index)69 bool ObjectContainer::Remove(SizeType index)
70 {
71 auto object = GetAt(index);
72 return object && Remove(object);
73 }
Remove(const IObject::Ptr & object)74 bool ObjectContainer::Remove(const IObject::Ptr& object)
75 {
76 return CallAboutToChange(ContainerChangeType::REMOVING, object) && Container::Remove(object);
77 }
RemoveAll()78 void ObjectContainer::RemoveAll()
79 {
80 bool success = true;
81 auto list = GetAll();
82 for (auto it = list.begin(); it != list.end() && success; ++it) {
83 success = success && CallAboutToChange(ContainerChangeType::REMOVING, *it);
84 }
85 if (success) {
86 Container::RemoveAll();
87 }
88 }
89
90 META_END_INTERNAL_NAMESPACE()
91