1 //----------------------------------------------------------------------
2 // This software is part of the OpenBeOS distribution and is covered
4 //---------------------------------------------------------------------
6 \file ResourcesContainer.cpp
7 ResourcesContainer implementation.
12 #include "ResourcesContainer.h"
14 #include "ResourceItem.h"
20 ResourcesContainer::ResourcesContainer()
27 ResourcesContainer::~ResourcesContainer()
34 // Returns false, if item is NULL or memory is insufficient, true otherwise.
36 ResourcesContainer::AddResource(ResourceItem
*item
, int32 index
,
41 // replace an item with the same type and id
43 delete RemoveResource(IndexOf(item
->Type(), item
->ID()));
44 int32 count
= CountResources();
45 if (index
< 0 || index
> count
)
47 result
= fResources
.AddItem(item
, count
);
55 ResourcesContainer::RemoveResource(int32 index
)
57 ResourceItem
* item
= (ResourceItem
*)fResources
.RemoveItem(index
);
65 ResourcesContainer::RemoveResource(ResourceItem
*item
)
67 return RemoveResource(IndexOf(item
));
72 ResourcesContainer::MakeEmpty()
74 for (int32 i
= 0; ResourceItem
*item
= ResourceAt(i
); i
++)
76 fResources
.MakeEmpty();
80 // AssimilateResources
82 ResourcesContainer::AssimilateResources(ResourcesContainer
&container
)
84 // Resistance is futile! ;-)
85 int32 newCount
= container
.CountResources();
86 for (int32 i
= 0; i
< newCount
; i
++) {
87 ResourceItem
*item
= container
.ResourceAt(i
);
91 // That should not happen.
92 // Delete the item to have a consistent behavior.
96 container
.fResources
.MakeEmpty();
97 container
.SetModified(true);
103 ResourcesContainer::IndexOf(ResourceItem
*item
) const
105 return fResources
.IndexOf(item
);
110 ResourcesContainer::IndexOf(const void *data
) const
114 int32 count
= CountResources();
115 for (int32 i
= 0; index
== -1 && i
< count
; i
++) {
116 if (ResourceAt(i
)->Data() == data
)
125 ResourcesContainer::IndexOf(type_code type
, int32 id
) const
128 int32 count
= CountResources();
129 for (int32 i
= 0; index
== -1 && i
< count
; i
++) {
130 ResourceItem
*item
= ResourceAt(i
);
131 if (item
->Type() == type
&& item
->ID() == id
)
139 ResourcesContainer::IndexOf(type_code type
, const char *name
) const
142 int32 count
= CountResources();
143 for (int32 i
= 0; index
== -1 && i
< count
; i
++) {
144 ResourceItem
*item
= ResourceAt(i
);
145 const char *itemName
= item
->Name();
146 if (item
->Type() == type
&& ((name
== NULL
&& itemName
== NULL
)
147 || (name
!= NULL
&& itemName
!= NULL
148 && strcmp(name
, itemName
) == 0))) {
157 ResourcesContainer::IndexOfType(type_code type
, int32 typeIndex
) const
160 int32 count
= CountResources();
161 for (int32 i
= 0; index
== -1 && i
< count
; i
++) {
162 ResourceItem
*item
= ResourceAt(i
);
163 if (item
->Type() == type
) {
174 ResourcesContainer::ResourceAt(int32 index
) const
176 return (ResourceItem
*)fResources
.ItemAt(index
);
181 ResourcesContainer::CountResources() const
183 return fResources
.CountItems();
188 ResourcesContainer::SetModified(bool modified
)
190 fIsModified
= modified
;
191 // If unmodified, set the resource item's modified flag as well.
193 int32 count
= CountResources();
194 for (int32 i
= 0; i
< count
; i
++)
195 ResourceAt(i
)->SetModified(false);
201 ResourcesContainer::IsModified() const
203 bool isModified
= fIsModified
;
204 int32 count
= CountResources();
205 for (int32 i
= 0; !isModified
&& i
< count
; i
++)
206 isModified
|= ResourceAt(i
)->IsModified();
211 }; // namespace Storage
212 }; // namespace BPrivate