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>
31 namespace // unnamed namespace
34 // predicate for std::remove_if algorithm for compressing observer list
35 bool isNULL(void* ptr)
40 } // unnamed namespace
48 ResourceTicket::ResourceTicket( ResourceTicketLifetimeObserver& observer, unsigned int id, ResourceTypePath& typePath )
49 : mLifetimeObserver( &observer ),
51 mTypePath( typePath ),
52 mLoadingState( ResourceLoading )
56 ResourceTicket::~ResourceTicket()
58 // Guard to allow handle destruction after Core has been destroyed
59 if ( Stage::IsInstalled() &&
62 mLifetimeObserver->ResourceTicketDiscarded( *this );
66 void ResourceTicket::AddObserver(ResourceTicketObserver& observer)
68 mObservers.push_back(&observer);
71 void ResourceTicket::RemoveObserver(ResourceTicketObserver& observer)
73 ObserverIter iter = find(mObservers.begin(), mObservers.end(), &observer);
74 DALI_ASSERT_DEBUG( mObservers.end() != iter );
75 if( mObservers.end() != iter )
81 void ResourceTicket::LoadingSucceeded()
83 DALI_ASSERT_DEBUG(mLoadingState == ResourceLoading);
85 mLoadingState = ResourceLoadingSucceeded;
87 // Using array operator as the call back out to application code might call back in
88 // and corrupt the mObservers list. Presumption is the operator gets the current address
89 // and adds an offset so a push_back() triggered reallocation should still work.
90 size_t count = mObservers.size();
91 for(size_t i = 0; i < count; i++)
93 if( mObservers[i] != NULL)
95 mObservers[i]->ResourceLoadingSucceeded(*this);
99 // Move NULL pointers to the end...
100 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
102 // ...and remove them
103 mObservers.erase( endIter, mObservers.end() );
106 void ResourceTicket::LoadingFailed()
108 DALI_ASSERT_DEBUG(mLoadingState == ResourceLoading);
110 mLoadingState = ResourceLoadingFailed;
112 // Using array operator as the call back out to application code might call back in
113 // and corrupt the mObservers list. Presumption is the operator gets the current address
114 // and adds an offset so a push_back() triggered reallocation should still work.
115 size_t count = mObservers.size();
116 for(size_t i = 0; i < count; i++)
118 if( mObservers[i] != NULL)
120 mObservers[i]->ResourceLoadingFailed(*this);
124 // Move NULL pointers to the end...
125 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
127 // ...and remove them
128 mObservers.erase( endIter, mObservers.end() );
131 void ResourceTicket::Loading()
133 mLoadingState = ResourceLoading;
135 // Move NULL pointers to the end...
136 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
138 // ...and remove them
139 mObservers.erase( endIter, mObservers.end() );
142 void ResourceTicket::Uploaded()
144 // Using array operator as the call back out to application code might call back in
145 // and corrupt the mObservers list. Presumption is the operator gets the current address
146 // and adds an offset so a push_back() triggered reallocation should still work.
147 size_t count = mObservers.size();
148 for(size_t i = 0; i < count; i++)
150 if( mObservers[i] != NULL)
152 mObservers[i]->ResourceUploaded(*this);
156 // Move NULL pointers to the end...
157 ObserverIter endIter = remove_if( mObservers.begin(), mObservers.end(), isNULL );
159 // ...and remove them
160 mObservers.erase( endIter, mObservers.end() );
163 void ResourceTicket::StopLifetimeObservation()
165 mLifetimeObserver = NULL;
168 } // namespace Internal