Dali 3D User Interface Engine
uv-callback-manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 
18 // CLASS HEADER
19 #include "uv-callback-manager.h"
20 
21 // EXTERNAL INCLUDES
22 #include <uv.h>
24 
25 // INTERNAL INCLUDES
26 
27 
28 namespace Dali
29 {
30 
31 namespace Internal
32 {
33 
34 namespace Adaptor
35 {
36 
37 namespace
38 {
39 
40 static void FreeHandleCallback(uv_handle_t* handle )
41 {
42  delete handle;
43 }
44 
45 }
49 struct CallbackData
50 {
51  typedef void (*CallbackFunction)(uv_idle_t*);
52 
57  : mCallback(callback),
59  mIdleHandle( NULL),
60  mExecute(true)
61  {
62  }
63 
67  void AddIdle( CallbackFunction callback)
68  {
69  // heap allocate a handle as it will be alive after the CallbackData object is deleted.
70  mIdleHandle = new uv_idle_t;
71 
72  // Node.JS uses uv_default_loop
73  uv_idle_init( uv_default_loop() , mIdleHandle );
74 
75  mIdleHandle->data = this;
76 
77  uv_idle_start( mIdleHandle, callback);
78  }
79 
84  {
85  // the handle will still be alive for a short period after calling uv_close
86  // set the data to NULL to avoid a dangling pointer
87  mIdleHandle->data = NULL;
88 
89  uv_idle_stop( mIdleHandle );
90 
91  uv_close( reinterpret_cast< uv_handle_t*>( mIdleHandle ) , FreeHandleCallback );
92 
93  delete mCallback;
95  }
96 
97  // Data
100  uv_idle_t* mIdleHandle;
101  bool mExecute;
102 
103 };
104 
105 namespace
106 {
107 void IdleCallback( uv_idle_t* handle )
108 {
109  CallbackData *callbackData = static_cast<CallbackData *>(handle->data);
110 
111  // remove callback data from the container first in case our callback tries to modify the container
112  CallbackBase::Execute( *callbackData->mRemoveFromContainerFunction, callbackData );
113 
114  // run the function
115  CallbackBase::Execute( *callbackData->mCallback );
116 
117  // will clear up the handle
118  delete callbackData;
119 
120 }
121 }
122 
124 :mRunning(false)
125 {
126 }
127 
129 {
130  DALI_ASSERT_DEBUG( mRunning == false );
131  mRunning = true;
132 }
133 
135 {
136  // make sure we're not called twice
137  DALI_ASSERT_DEBUG( mRunning == true );
138 
139  mRunning = false;
140 
141  for( CallbackList::iterator iter = mCallbackContainer.begin(); iter != mCallbackContainer.end(); ++iter)
142  {
143  CallbackData* data = (*iter);
144 
145  delete data;
146  }
147  mCallbackContainer.clear();
148 }
149 
151 {
152  if( !mRunning )
153  {
154  return false;
155  }
156 
157  CallbackData *callbackData = new CallbackData(callback );
158 
159  // To inform the manager a callback has finished, we get it to call RemoveCallbackFromContainer
160  callbackData->mRemoveFromContainerFunction = MakeCallback( this, &UvCallbackManager::RemoveCallbackFromContainer );
161 
162  // add the call back to the container
163  mCallbackContainer.push_front(callbackData);
164 
165  // init the callback
166  callbackData->AddIdle( &IdleCallback );
167 
168  return true;
169 }
170 
172 {
173  mCallbackContainer.remove(callbackData);
174 }
175 
176 // Creates a concrete interface for CallbackManager
178 {
179  return new UvCallbackManager;
180 }
181 
182 } // namespace Adaptor
183 
184 } // namespace Internal
185 
186 } // namespace Dali
Dali Docs Home
Read more about Dali