2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/resources/resource-ticket.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/images/image.h>
27 #include <dali/internal/event/common/stage-impl.h>
28 #include <dali/internal/event/resources/resource-ticket-observer.h>
29 #include <dali/internal/event/resources/resource-ticket-lifetime-observer.h>
33 namespace // unnamed namespace
36 // predicate for std::remove_if algorithm for compressing observer list
37 bool isNULL(void* ptr)
42 } // unnamed namespace
50 ResourceTicket::ResourceTicket( ResourceTicketLifetimeObserver& observer, unsigned int id, ResourceTypePath& typePath )
51 : mLifetimeObserver( &observer ),
53 mTypePath( typePath ),
54 mLoadingState( ResourceLoading )
58 ResourceTicket::~ResourceTicket()
60 // Guard to allow handle destruction after Core has been destroyed
61 if ( Stage::IsInstalled() &&
64 mLifetimeObserver->ResourceTicketDiscarded( *this );
68 void ResourceTicket::AddObserver(ResourceTicketObserver& observer)
70 mObservers.push_back(&observer);
73 void ResourceTicket::RemoveObserver(ResourceTicketObserver& observer)
75 ObserverIter iter = find(mObservers.begin(), mObservers.end(), &observer);
76 DALI_ASSERT_DEBUG( mObservers.end() != iter );
77 if( mObservers.end() != iter )
83 void ResourceTicket::LoadingSucceeded()
85 DALI_ASSERT_DEBUG(mLoadingState == ResourceLoading);
87 mLoadingState = ResourceLoadingSucceeded;
89 // Using array operator as the call back out to application code might call back in
90 // and corrupt the mObservers list. Presumption is the operator gets the current address
91 // and adds an offset so a push_back() triggered reallocation should still work.
92 size_t count = mObservers.size();
93 for(size_t i = 0; i < count; i++)
95 if( mObservers[i] != NULL)
97 mObservers[i]->ResourceLoadingSucceeded(*this);
101 // Move NULL pointers to the end...
102 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
104 // ...and remove them
105 mObservers.erase( endIter, mObservers.end() );
108 void ResourceTicket::LoadingFailed()
110 DALI_ASSERT_DEBUG(mLoadingState == ResourceLoading);
112 mLoadingState = ResourceLoadingFailed;
114 // Using array operator as the call back out to application code might call back in
115 // and corrupt the mObservers list. Presumption is the operator gets the current address
116 // and adds an offset so a push_back() triggered reallocation should still work.
117 size_t count = mObservers.size();
118 for(size_t i = 0; i < count; i++)
120 if( mObservers[i] != NULL)
122 mObservers[i]->ResourceLoadingFailed(*this);
126 // Move NULL pointers to the end...
127 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
129 // ...and remove them
130 mObservers.erase( endIter, mObservers.end() );
133 void ResourceTicket::Loading()
135 mLoadingState = ResourceLoading;
137 // Move NULL pointers to the end...
138 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
140 // ...and remove them
141 mObservers.erase( endIter, mObservers.end() );
144 void ResourceTicket::Uploaded()
146 // Using array operator as the call back out to application code might call back in
147 // and corrupt the mObservers list. Presumption is the operator gets the current address
148 // and adds an offset so a push_back() triggered reallocation should still work.
149 size_t count = mObservers.size();
150 for(size_t i = 0; i < count; i++)
152 if( mObservers[i] != NULL)
154 mObservers[i]->ResourceUploaded(*this);
158 // Move NULL pointers to the end...
159 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
161 // ...and remove them
162 mObservers.erase( endIter, mObservers.end() );
165 void ResourceTicket::SavingSucceeded()
167 // Using array operator as the call back out to application code might call back in
168 // and corrupt the mObservers list. Presumption is the operator gets the current address
169 // and adds an offset so a push_back() triggered reallocation should still work.
170 size_t count = mObservers.size();
171 for(size_t i = 0; i < count; i++)
173 if( mObservers[i] != NULL)
175 mObservers[i]->ResourceSavingSucceeded(*this);
179 // Move NULL pointers to the end...
180 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
182 // ...and remove them
183 mObservers.erase( endIter, mObservers.end() );
186 void ResourceTicket::SavingFailed()
188 // Using array operator as the call back out to application code might call back in
189 // and corrupt the mObservers list. Presumption is the operator gets the current address
190 // and adds an offset so a push_back() triggered reallocation should still work.
191 size_t count = mObservers.size();
192 for(size_t i = 0; i < count; i++)
194 if( mObservers[i] != NULL)
196 mObservers[i]->ResourceSavingFailed(*this);
200 // Move NULL pointers to the end...
201 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
203 // ...and remove them
204 mObservers.erase( endIter, mObservers.end() );
207 void ResourceTicket::StopLifetimeObservation()
209 mLifetimeObserver = NULL;
212 } // namespace Internal