[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-core.git] / dali / internal / common / message.h
1 #ifndef __DALI_INTERNAL_MESSAGE_H__
2 #define __DALI_INTERNAL_MESSAGE_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/internal/common/buffer-index.h>
22 #include <dali/internal/common/type-abstraction.h>
23 #include <dali/internal/update/common/scene-graph-buffers.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 /**
32  * An abstract base class for messages queued across threads.
33  * Messages are only allowed to contain value objects, either copies of the parameters or pointers
34  * If message parameter type is & or const& the message will try to take a copy of the actual type
35  */
36 class MessageBase
37 {
38 public:
39
40   /**
41    * Construct the message base.
42    */
43   MessageBase( )
44   {
45   }
46
47   /**
48    * Virtual destructor
49    */
50   virtual ~MessageBase()
51   {
52   }
53
54   /**
55    * Called to process the message.
56    * @param [in] bufferIndex The current update/render buffer index (depending on which thread processes the message).
57    */
58   virtual void Process( BufferIndex bufferIndex ) = 0;
59
60 private:
61 };
62
63 /**
64  * Templated message which calls a member function of an object.
65  * This allows nodes etc. to be modified in a thread-safe manner, when the update occurs in a separate thread.
66  * The object lifetime must controlled i.e. not destroyed before the message is processed.
67  */
68 template< typename T >
69 class Message : public MessageBase
70 {
71 public:
72
73   typedef void(T::*MemberFunction)();
74
75   /**
76    * Create a message.
77    * @note The object is expected to be const in the thread which sends this message.
78    * However it can be modified when Process() is called in a different thread.
79    * @param[in] obj The object to be updated in a separate thread.
80    * @param[in] member The member function of the object.
81    */
82   Message( const T* obj, MemberFunction member )
83   : MessageBase(),
84     object( const_cast< T* >( obj ) ),
85     memberFunction( member )
86   {
87   }
88
89   /**
90    * Virtual destructor
91    */
92   virtual ~Message()
93   {
94   }
95
96   /**
97    * @copydoc MessageBase::Process
98    */
99   virtual void Process( BufferIndex /*bufferIndex*/ )
100   {
101     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
102     (object->*memberFunction)();
103   }
104
105 private:
106
107   T* object;
108   MemberFunction memberFunction;
109
110 };
111
112 /**
113  * Templated message which calls a member function of an object.
114  * This overload passes one value-type parameter.
115  * Template parameters need to match the MemberFunction!
116  * The message will contain copy of the value (in case of & or const&)
117  */
118 template< typename T, typename P >
119 class MessageValue1 : public MessageBase
120 {
121 public:
122
123   typedef void(T::*MemberFunction)( typename ParameterType< P >::PassingType );
124
125   /**
126    * Create a message.
127    * @note The object is expected to be const in the thread which sends this message.
128    * However it can be modified when Process() is called in a different thread.
129    * @param[in] obj The object.
130    * @param[in] member The member function of the object.
131    * @param[in] p1 The first value-type parameter to pass to the member function.
132    */
133   MessageValue1( const T* obj,
134                  MemberFunction member,
135                  typename ParameterType< P >::PassingType p1 )
136   : MessageBase(),
137     object( const_cast< T* >( obj ) ),
138     memberFunction( member ),
139     param1( p1 )
140   {
141   }
142
143   /**
144    * Virtual destructor
145    */
146   virtual ~MessageValue1()
147   {
148   }
149
150   /**
151    * @copydoc MessageBase::Process
152    */
153   virtual void Process( BufferIndex /*bufferIndex*/ )
154   {
155     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
156     (object->*memberFunction)( ParameterType< P >::PassObject( param1 ) );
157   }
158
159 private:
160
161   T* object;
162   MemberFunction memberFunction;
163   typename ParameterType< P >::HolderType param1;
164
165 };
166
167 /**
168  * Templated message which calls a member function of an object.
169  * This overload passes two value-type parameters.
170  * Template parameters need to match the MemberFunction!
171  * The message will contain copy of the value (in case of & or const&)
172  */
173
174 template< typename T, typename P1, typename P2 >
175 class MessageValue2 : public MessageBase
176 {
177 public:
178
179   typedef void(T::*MemberFunction)(
180       typename ParameterType< P1 >::PassingType,
181       typename ParameterType< P2 >::PassingType );
182
183   /**
184    * Create a message.
185    * @note The object is expected to be const in the thread which sends this message.
186    * However it can be modified when Process() is called in a different thread.
187    * @param[in] obj The object.
188    * @param[in] member The member function of the object.
189    * @param[in] p1 The first parameter to pass to the member function.
190    * @param[in] p2 The second parameter to pass to the member function.
191    */
192   MessageValue2( const T* obj,
193                  MemberFunction member,
194                  typename ParameterType< P1 >::PassingType p1,
195                  typename ParameterType< P2 >::PassingType p2 )
196   : MessageBase(),
197     object( const_cast< T* >( obj ) ),
198     memberFunction( member ),
199     param1( p1 ),
200     param2( p2 )
201   {
202   }
203
204   /**
205    * Virtual destructor
206    */
207   virtual ~MessageValue2()
208   {
209   }
210
211   /**
212    * @copydoc MessageBase::Process
213    */
214   virtual void Process( BufferIndex /*bufferIndex*/ )
215   {
216     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
217     (object->*memberFunction)(
218         ParameterType< P1 >::PassObject( param1 ),
219         ParameterType< P2 >::PassObject( param2 ) );
220   }
221
222 private:
223
224   T* object;
225   MemberFunction memberFunction;
226   typename ParameterType< P1 >::HolderType param1;
227   typename ParameterType< P2 >::HolderType param2;
228
229 };
230
231 /**
232  * Templated message which calls a member function of an object.
233  * This overload passes three value-type parameters.
234  * Template parameters need to match the MemberFunction!
235  * The message will contain copy of the value (in case of & or const&)
236  */
237 template< typename T, typename P1, typename P2, typename P3 >
238 class MessageValue3 : public MessageBase
239 {
240 public:
241
242   typedef void(T::*MemberFunction)(
243       typename ParameterType< P1 >::PassingType,
244       typename ParameterType< P2 >::PassingType,
245       typename ParameterType< P3 >::PassingType );
246
247   /**
248    * Create a message.
249    * @note The object is expected to be const in the thread which sends this message.
250    * However it can be modified when Process() is called in a different thread.
251    * @param[in] obj The object.
252    * @param[in] member The member function of the object.
253    * @param[in] p1 The first parameter to pass to the member function.
254    * @param[in] p2 The second parameter to pass to the member function.
255    * @param[in] p3 The third parameter to pass to the member function.
256    */
257   MessageValue3( const T* obj,
258                  MemberFunction member,
259                  typename ParameterType< P1 >::PassingType p1,
260                  typename ParameterType< P2 >::PassingType p2,
261                  typename ParameterType< P3 >::PassingType p3 )
262   : MessageBase(),
263     object( const_cast< T* >( obj ) ),
264     memberFunction( member ),
265     param1( p1 ),
266     param2( p2 ),
267     param3( p3 )
268   {
269   }
270
271   /**
272    * Virtual destructor
273    */
274   virtual ~MessageValue3()
275   {
276   }
277
278   /**
279    * @copydoc MessageBase::Process
280    */
281   virtual void Process( BufferIndex /*bufferIndex*/ )
282   {
283     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
284     (object->*memberFunction)(
285         ParameterType< P1 >::PassObject( param1 ),
286         ParameterType< P2 >::PassObject( param2 ),
287         ParameterType< P3 >::PassObject( param3 ) );
288   }
289
290 private:
291
292   T* object;
293   MemberFunction memberFunction;
294   typename ParameterType< P1 >::HolderType param1;
295   typename ParameterType< P2 >::HolderType param2;
296   typename ParameterType< P3 >::HolderType param3;
297
298 };
299
300 /**
301  * Templated message which calls a member function of an object.
302  * This overload passes four value-type parameters.
303  * Template parameters need to match the MemberFunction!
304  * The message will contain copy of the value (in case of & or const&)
305  */
306 template< typename T, typename P1, typename P2, typename P3, typename P4 >
307 class MessageValue4 : public MessageBase
308 {
309 public:
310
311   typedef void(T::*MemberFunction)(
312       typename ParameterType< P1 >::PassingType,
313       typename ParameterType< P2 >::PassingType,
314       typename ParameterType< P3 >::PassingType,
315       typename ParameterType< P4 >::PassingType );
316
317   /**
318    * Create a message.
319    * @note The object is expected to be const in the thread which sends this message.
320    * However it can be modified when Process() is called in a different thread.
321    * @param[in] obj The object.
322    * @param[in] member The member function of the object.
323    * @param[in] p1 The first parameter to pass to the member function.
324    * @param[in] p2 The second parameter to pass to the member function.
325    * @param[in] p3 The third parameter to pass to the member function.
326    * @param[in] p4 The fourth parameter to pass to the member function.
327    */
328   MessageValue4( const T* obj,
329                  MemberFunction member,
330                  typename ParameterType< P1 >::PassingType p1,
331                  typename ParameterType< P2 >::PassingType p2,
332                  typename ParameterType< P3 >::PassingType p3,
333                  typename ParameterType< P4 >::PassingType p4 )
334   : MessageBase(),
335     object( const_cast< T* >( obj ) ),
336     memberFunction( member ),
337     param1( p1 ),
338     param2( p2 ),
339     param3( p3 ),
340     param4( p4 )
341   {
342   }
343
344   /**
345    * Virtual destructor
346    */
347   virtual ~MessageValue4()
348   {
349   }
350
351   /**
352    * @copydoc MessageBase::Process
353    */
354   virtual void Process( BufferIndex /*bufferIndex*/ )
355   {
356     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
357     (object->*memberFunction)(
358         ParameterType< P1 >::PassObject( param1 ),
359         ParameterType< P2 >::PassObject( param2 ),
360         ParameterType< P3 >::PassObject( param3 ),
361         ParameterType< P4 >::PassObject( param4 ) );
362   }
363
364 private:
365
366   T* object;
367   MemberFunction memberFunction;
368   typename ParameterType< P1 >::HolderType param1;
369   typename ParameterType< P2 >::HolderType param2;
370   typename ParameterType< P3 >::HolderType param3;
371   typename ParameterType< P4 >::HolderType param4;
372
373 };
374
375 /**
376  * Templated message which calls a member function of an object.
377  * This overload passes five value-type parameters.
378  * Template parameters need to match the MemberFunction!
379  * The message will contain copy of the value (in case of & or const&)
380  */
381 template< typename T, typename P1, typename P2, typename P3, typename P4, typename P5 >
382 class MessageValue5 : public MessageBase
383 {
384 public:
385
386   typedef void(T::*MemberFunction)(
387       typename ParameterType< P1 >::PassingType,
388       typename ParameterType< P2 >::PassingType,
389       typename ParameterType< P3 >::PassingType,
390       typename ParameterType< P4 >::PassingType,
391       typename ParameterType< P5 >::PassingType );
392
393   /**
394    * Create a message.
395    * @note The object is expected to be const in the thread which sends this message.
396    * However it can be modified when Process() is called in a different thread.
397    * @param[in] obj The object.
398    * @param[in] member The member function of the object.
399    * @param[in] p1 The first parameter to pass to the member function.
400    * @param[in] p2 The second parameter to pass to the member function.
401    * @param[in] p3 The third parameter to pass to the member function.
402    * @param[in] p4 The fourth parameter to pass to the member function.
403    * @param[in] p5 The fifth parameter to pass to the member function.
404    */
405   MessageValue5( const T* obj,
406                  MemberFunction member,
407                  typename ParameterType< P1 >::PassingType p1,
408                  typename ParameterType< P2 >::PassingType p2,
409                  typename ParameterType< P3 >::PassingType p3,
410                  typename ParameterType< P4 >::PassingType p4,
411                  typename ParameterType< P5 >::PassingType p5 )
412   : MessageBase(),
413     object( const_cast< T* >( obj ) ),
414     memberFunction( member ),
415     param1( p1 ),
416     param2( p2 ),
417     param3( p3 ),
418     param4( p4 ),
419     param5( p5 )
420   {
421   }
422
423   /**
424    * Virtual destructor
425    */
426   virtual ~MessageValue5()
427   {
428   }
429
430   /**
431    * @copydoc MessageBase::Process
432    */
433   virtual void Process( BufferIndex /*bufferIndex*/ )
434   {
435     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
436     (object->*memberFunction)(
437         ParameterType< P1 >::PassObject( param1 ),
438         ParameterType< P2 >::PassObject( param2 ),
439         ParameterType< P3 >::PassObject( param3 ),
440         ParameterType< P4 >::PassObject( param4 ),
441         ParameterType< P5 >::PassObject( param5 ) );
442
443   }
444
445 private:
446
447   T* object;
448   MemberFunction memberFunction;
449   typename ParameterType< P1 >::HolderType param1;
450   typename ParameterType< P2 >::HolderType param2;
451   typename ParameterType< P3 >::HolderType param3;
452   typename ParameterType< P4 >::HolderType param4;
453   typename ParameterType< P5 >::HolderType param5;
454
455 };
456
457 /**
458  * Templated message which calls a member function of an object.
459  * This overload passes a value-type to set a double-buffered property.
460  * Template parameters need to match the MemberFunction!
461  * The message will contain copy of the value (in case of & or const&)
462  */
463 template< typename T, typename P >
464 class MessageDoubleBuffered1 : public MessageBase
465 {
466 public:
467
468   typedef void(T::*MemberFunction)(
469       BufferIndex,
470       typename ParameterType< P >::PassingType );
471
472   /**
473    * Create a message.
474    * @note The object is expected to be const in the thread which sends this message.
475    * However it can be modified when Process() is called in a different thread.
476    * @param[in] obj The object.
477    * @param[in] member The member function of the object.
478    * @param[in] p The second parameter to pass.
479    */
480   MessageDoubleBuffered1( const T* obj,
481                           MemberFunction member,
482                           typename ParameterType< P >::PassingType p )
483   : MessageBase(),
484     object( const_cast< T* >( obj ) ),
485     memberFunction( member ),
486     param( p )
487   {
488   }
489
490   /**
491    * Virtual destructor
492    */
493   virtual ~MessageDoubleBuffered1()
494   {
495   }
496
497   /**
498    * @copydoc MessageBase::Process
499    */
500   virtual void Process( BufferIndex bufferIndex )
501   {
502     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
503     (object->*memberFunction)(
504         bufferIndex,
505         ParameterType< P >::PassObject( param ) );
506   }
507
508 private:
509
510   T* object;
511   MemberFunction memberFunction;
512   typename ParameterType< P >::HolderType param;
513
514 };
515
516 /**
517  * Templated message which calls a member function of an object.
518  * This overload passes two value-types to set double-buffered properties.
519  * Template parameters need to match the MemberFunction!
520  * The message will contain copy of the value (in case of & or const&)
521  */
522 template< typename T, typename P2, typename P3 >
523 class MessageDoubleBuffered2 : public MessageBase
524 {
525 public:
526
527   typedef void(T::*MemberFunction)(
528       BufferIndex,
529       typename ParameterType< P2 >::PassingType,
530       typename ParameterType< P3 >::PassingType );
531
532   /**
533    * Create a message.
534    * @note The object is expected to be const in the thread which sends this message.
535    * However it can be modified when Process() is called in a different thread.
536    * @param[in] obj The object.
537    * @param[in] member The member function of the object.
538    * @param[in] p2 The second parameter to pass to the function.
539    * @param[in] p3 The third parameter to pass to the function.
540    */
541   MessageDoubleBuffered2( const T* obj,
542                           MemberFunction member,
543                           typename ParameterType< P2 >::PassingType p2,
544                           typename ParameterType< P3 >::PassingType p3 )
545   : MessageBase(),
546     object( const_cast< T* >( obj ) ),
547     memberFunction( member ),
548     param2( p2 ),
549     param3( p3 )
550   {
551   }
552
553   /**
554    * Virtual destructor
555    */
556   virtual ~MessageDoubleBuffered2()
557   {
558   }
559
560   /**
561    * @copydoc MessageBase::Process
562    */
563   virtual void Process( BufferIndex bufferIndex )
564   {
565     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
566     (object->*memberFunction)(
567         bufferIndex,
568         ParameterType< P2 >::PassObject( param2 ),
569         ParameterType< P3 >::PassObject( param3 ) );
570   }
571
572 private:
573
574   T* object;
575   MemberFunction memberFunction;
576   typename ParameterType< P2 >::HolderType param2;
577   typename ParameterType< P3 >::HolderType param3;
578
579 };
580
581
582 /**
583  * Templated message which calls a member function of an object.
584  * This overload passes three value-types to set double-buffered properties.
585  * Template parameters need to match the MemberFunction!
586  * The message will contain copy of the value (in case of & or const&)
587  */
588 template< typename T, typename P2, typename P3, typename P4 >
589 class MessageDoubleBuffered3 : public MessageBase
590 {
591 public:
592
593   typedef void(T::*MemberFunction)(
594       BufferIndex,
595       typename ParameterType< P2 >::PassingType,
596       typename ParameterType< P3 >::PassingType,
597       typename ParameterType< P4 >::PassingType );
598
599   /**
600    * Create a message.
601    * @note The object is expected to be const in the thread which sends this message.
602    * However it can be modified when Process() is called in a different thread.
603    * @param[in] obj The object.
604    * @param[in] member The member function of the object.
605    * @param[in] p2 The second parameter to pass.
606    * @param[in] p3 The third parameter to pass.
607    * @param[in] p4 The forth parameter to pass.
608    */
609   MessageDoubleBuffered3( const T* obj,
610                           MemberFunction member,
611                           typename ParameterType< P2 >::PassingType p2,
612                           typename ParameterType< P3 >::PassingType p3,
613                           typename ParameterType< P4 >::PassingType p4 )
614   : MessageBase(),
615     object( const_cast< T* >( obj ) ),
616     memberFunction( member ),
617     param2( p2 ),
618     param3( p3 ),
619     param4( p4 )
620   {
621   }
622
623   /**
624    * Virtual destructor
625    */
626   virtual ~MessageDoubleBuffered3()
627   {
628   }
629
630   /**
631    * @copydoc MessageBase::Process
632    */
633   virtual void Process( BufferIndex bufferIndex )
634   {
635     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
636     (object->*memberFunction)(
637         bufferIndex,
638         ParameterType< P2 >::PassObject( param2 ),
639         ParameterType< P3 >::PassObject( param3 ),
640         ParameterType< P4 >::PassObject( param4 ) );
641   }
642
643 private:
644
645   T* object;
646   MemberFunction memberFunction;
647   typename ParameterType< P2 >::HolderType param2;
648   typename ParameterType< P3 >::HolderType param3;
649   typename ParameterType< P4 >::HolderType param4;
650
651 };
652
653 /**
654  * Templated message which calls a member function of an object.
655  * This overload passes four value-types to set double-buffered properties.
656  * Template parameters need to match the MemberFunction!
657  * The message will contain copy of the value (in case of & or const&)
658  */
659 template< typename T, typename P2, typename P3, typename P4, typename P5 >
660 class MessageDoubleBuffered4 : public MessageBase
661 {
662 public:
663
664   typedef void(T::*MemberFunction)(
665       BufferIndex,
666       typename ParameterType< P2 >::PassingType,
667       typename ParameterType< P3 >::PassingType,
668       typename ParameterType< P4 >::PassingType,
669       typename ParameterType< P5 >::PassingType );
670
671   /**
672    * Create a message.
673    * @note The object is expected to be const in the thread which sends this message.
674    * However it can be modified when Process() is called in a different thread.
675    * @param[in] obj The object.
676    * @param[in] member The member function of the object.
677    * @param[in] p2 The second parameter to pass.
678    * @param[in] p3 The third parameter to pass.
679    * @param[in] p4 The forth parameter to pass.
680    * @param[in] p5 The fifth parameter to pass.
681    */
682   MessageDoubleBuffered4( const T* obj,
683                           MemberFunction member,
684                           typename ParameterType< P2 >::PassingType p2,
685                           typename ParameterType< P3 >::PassingType p3,
686                           typename ParameterType< P4 >::PassingType p4,
687                           typename ParameterType< P5 >::PassingType p5 )
688   : MessageBase(),
689     object( const_cast< T* >( obj ) ),
690     memberFunction( member ),
691     param2( p2 ),
692     param3( p3 ),
693     param4( p4 ),
694     param5( p5 )
695   {
696   }
697
698   /**
699    * Virtual destructor
700    */
701   virtual ~MessageDoubleBuffered4()
702   {
703   }
704
705   /**
706    * @copydoc MessageBase::Process
707    */
708   virtual void Process( BufferIndex bufferIndex )
709   {
710     DALI_ASSERT_DEBUG( object && "Message does not have an object" );
711     (object->*memberFunction)(
712         bufferIndex,
713         ParameterType< P2 >::PassObject( param2 ),
714         ParameterType< P3 >::PassObject( param3 ),
715         ParameterType< P4 >::PassObject( param4 ),
716         ParameterType< P5 >::PassObject( param5 ) );
717   }
718
719 private:
720
721   T* object;
722   MemberFunction memberFunction;
723   typename ParameterType< P2 >::HolderType param2;
724   typename ParameterType< P3 >::HolderType param3;
725   typename ParameterType< P4 >::HolderType param4;
726   typename ParameterType< P5 >::HolderType param5;
727
728 };
729
730 } // namespace Internal
731
732 } // namespace Dali
733
734 #endif // __DALI_INTERNAL_MESSAGE_H__