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