License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / event / resources / resource-ticket.h
1 #ifndef __DALI_INTERNAL_RESOURCE_TICKET_H__
2 #define __DALI_INTERNAL_RESOURCE_TICKET_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/common/map-wrapper.h>
24 #include <dali/public-api/common/loading-state.h>
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/internal/event/resources/resource-type-path.h>
27 #include <dali/integration-api/resource-cache.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 class ResourceTicket;
36 typedef IntrusivePtr<ResourceTicket> ResourceTicketPtr;
37 typedef std::vector<ResourceTicketPtr> ResourceTicketContainer;
38
39 class ResourceTicketObserver;
40 class ResourceTicketLifetimeObserver;
41
42 /**
43  * ResourceTicket records the lifetime of a resource loading request.
44  * When a resource is requested from ResourceClient, you get a ticket.
45  * The state of the loading operation, can be observed using the ticket.
46  * After the ticket is destroyed, the resource will be discarded.
47  *
48  * Multi-threading note: this class should be used in the main thread only
49  * i.e. not from within Dali::Integration::Core::Render().
50  */
51 class ResourceTicket : public RefObject
52 {
53 public:
54
55   /**
56    * Create a resource request.
57    * This should only be done by the ResourceTicketRegistry.
58    * @param [in] observer The object which observes ticket lifetimes.
59    * @param [in] id A unique ID for this ticket (loading request).
60    * @param [in] typePath The resource Type & Path pair.
61    */
62   ResourceTicket( ResourceTicketLifetimeObserver& observer, unsigned int id, ResourceTypePath& typePath );
63
64   /**
65    * Retrieve the unique ID of the resource request.
66    * This can be shared with Nodes etc. which will require the resource when rendering.
67    * @return The unique ID for this request.
68    */
69   unsigned int GetId() const { return mId; }
70
71   /**
72    * Retrieve the type and path of the resource request.
73    * @return The type and path.
74    */
75   const ResourceTypePath& GetTypePath() const { return mTypePath; }
76
77   /**
78    * Query the state of the resource loading request.
79    * @return The state.
80    */
81   Dali::LoadingState GetLoadingState() const { return mLoadingState; }
82
83   /**
84    * Adds an observer.
85    * @param[in] observer The observer.
86    */
87   void AddObserver(ResourceTicketObserver& observer);
88
89   /**
90    * Removes an observer.
91    * @param[in] observer The observer.
92    */
93   void RemoveObserver(ResourceTicketObserver& observer);
94
95 public: // Used by the ResourceClient
96
97   /**
98    * Called when a resource loads successfully.
99    * The ResourceTicketObservers will be notified.
100    */
101   void LoadingSucceeded();
102
103   /**
104    * Called when a resource fails to load.
105    * The ResourceTicketObservers will be notified.
106    */
107   void LoadingFailed();
108
109   /**
110    * Reset state to ResourceLoading.
111    */
112   void Loading();
113
114   /**
115    * Notification when a resource has been uploaded to GL.
116    * The ResourceTicketObservers will be notified.
117    */
118   void Uploaded();
119
120   /**
121    * Called when a resource is saved successfully.
122    * The ResourceTicketObservers will be notified.
123    */
124   void SavingSucceeded();
125
126   /**
127    * Called when a resource fails to save.
128    * The ResourceTicketObservers will be notified.
129    */
130   void SavingFailed();
131
132   /**
133    * Called when the ResourceTicketLifetimeObserver is being destroyed.
134    * This method should only be called during destruction of the Dali core.
135    */
136   void StopLifetimeObservation();
137
138 protected:
139
140   /**
141    * A reference counted object may only be deleted by calling Unreference()
142    */
143   virtual ~ResourceTicket();
144
145 private:
146
147   /**
148    * Undefined copy constructor.
149    */
150   ResourceTicket(const ResourceTicket&);
151
152   /**
153    * Undefined assignment operator.
154    */
155   ResourceTicket& operator=(const ResourceTicket& rhs);
156
157 private:
158
159   ResourceTicketLifetimeObserver* mLifetimeObserver; ///< reference to the lifetime-observer; not owned
160
161   unsigned int mId;
162   ResourceTypePath mTypePath;
163
164   Dali::LoadingState mLoadingState;
165
166   // These ticket observers are not owned by the ticket.
167   // Observers must remove themselves by calling RemoveObserver e.g. during destruction.
168   typedef std::vector<ResourceTicketObserver*> ObserverContainer;
169   typedef ObserverContainer::iterator ObserverIter;
170
171   ObserverContainer mObservers;
172 };
173
174 } // namespace Internal
175
176 } // namespace Dali
177
178 #endif // __DALI_INTERNAL_RESOURCE_MANAGER_H__