Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / include / native_service / ns_utility.hpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 /// \ingroup  tag_NS_UtilityCenter
19 /// \brief    This file contains declaration of common APIs for NS_UtilityCenter.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22 //@{
23 /**
24  * @file ns_utility.hpp
25  * @brief \~english This file contains declaration of common APIs for NS_UtilityCenter.
26  *
27  */
28 /** @addtogroup BaseSystem
29  *  @{
30  */
31 /** @addtogroup native_service
32  *  @ingroup BaseSystem
33  *  @{
34  */
35 /** @addtogroup framework_unified
36  *  @ingroup native_service
37  *  @{
38  */
39 /** @addtogroup native
40  *  @ingroup framework_unified
41  *  @{
42  */
43 #ifndef __NSFRAMEWORK_NSUTILITY_NSUTILITY__  // NOLINT  (build/header_guard)
44 #define __NSFRAMEWORK_NSUTILITY_NSUTILITY__
45
46 #include <native_service/frameworkunified_types.h>
47 #include <new>
48 #include <exception>
49 #include <algorithm>
50 #include <iterator>
51
52 #ifdef AGL_STUB
53 #include <stdexcept>
54 #endif
55
56 // Macros ///////////////////////////////////////////////////////////////
57
58 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
59   TypeName(const TypeName&);               \
60   void operator=(const TypeName& )
61
62 #define DEFINE_EXCEPTION( name, base ) \
63 class name : public base \
64 { \
65 public: \
66   name ( const char* str = "" ) : base ( str ) \
67   {} \
68 }
69
70 // Helpful type definitions ////////////////////////////////////////////////
71 template <int v>
72 struct Int2Type {
73   enum { value = v };  // NOLINT (readability/nolint)
74 };
75
76 template <class T>
77 struct Type2Type {
78   typedef T Type;
79 };
80
81 // Helpful Fucntions /////////////////////////////////////////////////////////
82 template< class T, UI_32 N >
83 const T *ArrBeg(const T(&arr)[ N ]) {
84   return &arr[ 0 ];
85 }
86
87 template< class T, UI_32 N >
88 const T *ArrEnd(const T(&arr)[ N ]) {
89   return &arr[ 0 ] + N;
90 }
91
92 template< class O, class I >
93 O SimpleCast(I i) {
94   return static_cast< O >(i);
95 }
96
97 template< class R, class B >
98 R UnalignedRet(B *b) {
99   return *reinterpret_cast< R volatile * >(b);
100 }
101
102 template< class Cont, class Gen >
103 Cont genRange(UI_32 N, Gen genFn) {
104   Cont c;
105   std::generate_n(std::back_inserter(c), N, genFn);
106   return c;
107 }
108
109 template< class OCont, class IIter, class MapFn >
110 OCont mapRange(const IIter &begin, const IIter &end, MapFn fn) {
111   OCont c;
112   std::transform(begin, end, std::inserter(c, c.begin()), fn);
113   return c;
114 }
115 // Static Compile-time list creation /////////////////////////////////////////
116
117 #ifdef AGL_STUB
118 #else
119 ////////////////////////
120 #include <_pack1.h>    //
121 //////////////////////////
122 #endif
123
124 template< class T, UI_32 N >
125 struct TList {
126   T datum;
127   TList < T, N - 1 > data;
128
129   T *operator&() {  // NOLINT (readability/nolint)
130     return reinterpret_cast< T * >(this);
131   }
132   const T *operator&() const {  // NOLINT (readability/nolint)
133     return reinterpret_cast< const T * >(this);
134   }
135   static const UI_32 count = N;  // NOLINT (readability/nolint)
136
137 };
138
139 template< class T >
140 struct TList< T, 1 > {
141   T datum;
142
143   T *operator&() {  // NOLINT (readability/nolint)
144     return reinterpret_cast< T * >(this);
145   }
146   const T *operator&() const {  // NOLINT (readability/nolint)
147     return reinterpret_cast< const T * >(this);
148   }
149   static const UI_32 count = 1;  // NOLINT (readability/nolint)
150
151 };
152
153 #ifdef AGL_STUB
154 #else
155 /////////////////////////
156 #include <_packpop.h> //
157 ///////////////////////
158 #endif
159
160 // Function Decomposition ///////////////////////////////////////////////////
161
162 template< class Sig >
163 struct FSig {
164 };
165
166 template<class R>
167 struct FSig< R(*)() > {
168   typedef R RType;
169   static const UI_32 argCount = 0;  // NOLINT (readability/nolint)
170 };
171
172 template<class R, class T1>
173 struct FSig< R(*)(T1) > {
174   typedef R RType;
175   typedef T1 TArg1;
176   static const UI_32 argCount = 1;  // NOLINT (readability/nolint)
177 };
178
179 template<class R, class T1, class T2>
180 struct FSig< R(*)(T1, T2) > {
181   typedef R RType;
182   typedef T1 TArg1;
183   typedef T2 TArg2;
184   static const UI_32 argCount = 2;  // NOLINT (readability/nolint)
185 };
186
187 template<class R, class T1, class T2, class T3>
188 struct FSig< R(*)(T1, T2, T3) > {
189   typedef R RType;
190   typedef T1 TArg1;
191   typedef T2 TArg2;
192   typedef T3 TArg3;
193   static const UI_32 argCount = 3;  // NOLINT (readability/nolint)
194 };
195
196 template<class R, class T1, class T2, class T3, class T4>
197 struct FSig< R(*)(T1, T2, T3, T4) > {
198   typedef R RType;
199   typedef T1 TArg1;
200   typedef T2 TArg2;
201   typedef T3 TArg3;
202   typedef T4 TArg4;
203   static const UI_32 argCount = 4;  // NOLINT (readability/nolint)
204 };
205
206 template<class R, class T1, class T2, class T3, class T4, class T5>
207 struct FSig< R(*)(T1, T2, T3, T4, T5) > {
208   typedef R RType;
209   typedef T1 TArg1;
210   typedef T2 TArg2;
211   typedef T3 TArg3;
212   typedef T4 TArg4;
213   typedef T5 TArg5;
214   static const UI_32 argCount = 5;  // NOLINT (readability/nolint)
215 };
216
217 template<class R, class T1, class T2, class T3, class T4, class T5, class T6>
218 struct FSig< R(*)(T1, T2, T3, T4, T5, T6) > {
219   typedef R RType;
220   typedef T1 TArg1;
221   typedef T2 TArg2;
222   typedef T3 TArg3;
223   typedef T4 TArg4;
224   typedef T5 TArg5;
225   typedef T6 TArg6;
226   static const UI_32 argCount = 6;  // NOLINT (readability/nolint)
227 };
228
229 template<class R, class T1, class T2, class T3, class T4, class T5, class T6, class T7>
230 struct FSig< R(*)(T1, T2, T3, T4, T5, T6, T7) > {
231   typedef R RType;
232   typedef T1 TArg1;
233   typedef T2 TArg2;
234   typedef T3 TArg3;
235   typedef T4 TArg4;
236   typedef T5 TArg5;
237   typedef T6 TArg6;
238   typedef T7 TArg7;
239   static const UI_32 argCount = 7;  // NOLINT (readability/nolint)
240 };
241
242
243
244 // Accumulator definition //////////////////////////////////////////////////
245
246 /// \brief Accumultor type
247 ///
248 /// An accumulator is an object that keeps an internal counter, and will increment its
249 /// counter by n, which defaults to 1, each time operator() is called on it.  Eample usage:
250 ///
251 /// Accumulator< int > a( 42 );  // initialize a to 42
252 /// int v1 = a();   // <-- v1 == 43
253 /// int v2 = a();   // <-- v2 == 44
254 /// int v4 = a(10);  // <-- v4 == 54
255 /// int v5 = a();   // <-- v5 == 55
256 template<typename T>
257 class Accumulator {
258  public:
259   Accumulator(T n) : n(n) {}
260
261   template<typename U>
262   Accumulator(const Accumulator<U> &u) : n(u.n) {}
263
264   T operator()(T i = 1) {  // NOLINT (readability/nolint)
265     return operator()< T >(i);
266   }
267
268   template<typename U>
269   T operator()(U i) {
270     return n += i;
271   }
272
273   template< typename U >
274   friend class Accumulator;
275
276  private:
277   T n;
278 };
279
280
281 /// \brief Accumulator utility function
282 ///
283 /// Given a value n of type T, returns an accumulator of type Accumulator<T> initialized to value n
284 template<typename T>
285 Accumulator<T> MakeAccumulator(T n) {
286   return Accumulator<T>(n);
287 }
288
289 // Rsrc Management helper class /////////////////////////////////////////////
290
291 template< class T >
292 class MemTraits {
293  public:
294   typedef T *Type;
295   typedef std::bad_alloc Exception;
296   static void Release(Type rsrc) {
297     delete rsrc;
298   }
299
300   static BOOL BadValue(Type rsrc) {
301     return NULL == rsrc;
302   }
303 };
304
305 template< class RsrcTraits >
306 class RaiseExceptionPolicy {
307  public:
308   typedef typename RsrcTraits::Type Type;
309   static Type check(Type t) {  // NOLINT (readability/nolint)
310     if (RsrcTraits::BadValue(t)) {
311       throw typename RsrcTraits::Exception();
312     }
313
314     return t;
315   }
316 };
317
318 template< class RsrcTraits >
319 class CheckForErrorPolicy {  // no exceptions
320  public:
321   typedef typename RsrcTraits::Type Type;
322
323   static Type check(Type t) {  // NOLINT (readability/nolint)
324     return t;
325   }
326
327   static bool isValid(Type t) {  // NOLINT (readability/nolint)
328     return ! RsrcTraits::BadValue(t);
329   }
330 };
331
332 template < class T,
333            class RsrcTraits                    = MemTraits< T >,
334            template <class> class ErrorPolicy  = RaiseExceptionPolicy >
335 class ResourceMgr : public RsrcTraits {
336  public:
337   typedef typename RsrcTraits::Type Type;
338   typedef ErrorPolicy< RsrcTraits > TFullErrorPolicy;
339
340   ResourceMgr(Type rsrc) : m_rsrc(TFullErrorPolicy::check(rsrc)) {}
341
342   ~ResourceMgr() {
343     RsrcTraits::Release(m_rsrc);
344   }
345
346   operator Type() {  // NOLINT (readability/nolint)
347     return m_rsrc;
348   }
349
350   operator const Type() const {  // NOLINT (readability/nolint)
351     return m_rsrc;
352   }
353
354   bool isValid() const {  // NOLINT (readability/nolint)
355     return TFullErrorPolicy::isValid(m_rsrc);
356   }
357
358  private:
359   DISALLOW_COPY_AND_ASSIGN(ResourceMgr);
360
361   Type m_rsrc;
362 };
363
364 // Functor Helper classes /////////////////////////////////////
365 template< class R >
366 class IFunctor {
367  public:
368   IFunctor() {}
369   virtual ~IFunctor() {}
370
371   virtual R operator()() const = 0;  // NOLINT (readability/nolint)
372   virtual UI_32 size() const = 0;  // NOLINT (readability/nolint)
373 };
374
375 template< class TFn >
376 class CFunctor0 : public IFunctor< typename FSig< TFn >::RType > {
377  public:
378   CFunctor0(TFn fn) : m_function(fn) {}
379   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
380     return m_function();
381   }
382   UI_32 size() const {  // NOLINT (readability/nolint)
383     return sizeof(TFn);
384   }
385
386  private:
387   TFn m_function;
388 };
389
390 template< class TFn, class Arg1 >
391 class CFunctor1 : public IFunctor< typename FSig< TFn >::RType > {
392  public:
393
394   CFunctor1(TFn fn, Arg1 arg1) : m_function(fn), m_arg1(arg1) {}
395   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
396     return m_function(m_arg1);
397   }
398   UI_32 size() const {  // NOLINT (readability/nolint)
399     return sizeof(TFn) + sizeof(Arg1);
400   }
401
402  private:
403   TFn m_function;
404   Arg1 m_arg1;
405 };
406
407 template< class TFn, class Arg1, class Arg2 >
408 class CFunctor2 : public IFunctor< typename FSig< TFn >::RType > {
409  public:
410
411   CFunctor2(TFn fn, Arg1 arg1, Arg2 arg2) : m_function(fn), m_arg1(arg1), m_arg2(arg2) {}
412   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
413     return m_function(m_arg1, m_arg2);
414   }
415   UI_32 size() const {  // NOLINT (readability/nolint)
416     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2);
417   }
418  private:
419   TFn m_function;
420   Arg1 m_arg1;
421   Arg2 m_arg2;
422 };
423
424 template< class TFn, class Arg1, class Arg2, class Arg3 >
425 class CFunctor3 : public IFunctor< typename FSig< TFn >::RType > {
426  public:
427
428   CFunctor3(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3) : m_function(fn), m_arg1(arg1), m_arg2(arg2), m_arg3(arg3) {}
429   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
430     return m_function(m_arg1, m_arg2, m_arg3);
431   }
432   UI_32 size() const {  // NOLINT (readability/nolint)
433     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3);
434   }
435  private:
436   TFn m_function;
437   Arg1 m_arg1;
438   Arg2 m_arg2;
439   Arg3 m_arg3;
440 };
441
442 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4 >
443 class CFunctor4 : public IFunctor< typename FSig< TFn >::RType > {
444  public:
445   CFunctor4(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) : m_function(fn), m_arg1(arg1), m_arg2(arg2),
446     m_arg3(arg3), m_arg4(arg4) {}
447   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
448     return m_function(m_arg1, m_arg2, m_arg3, m_arg4);
449   }
450   UI_32 size() const {  // NOLINT (readability/nolint)
451     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4);
452   }
453  private:
454   TFn m_function;
455   Arg1 m_arg1;
456   Arg2 m_arg2;
457   Arg3 m_arg3;
458   Arg4 m_arg4;
459 };
460
461 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5 >
462 class CFunctor5 : public IFunctor< typename FSig< TFn >::RType > {
463  public:
464   CFunctor5(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) : m_function(fn), m_arg1(arg1), m_arg2(arg2),
465     m_arg3(arg3), m_arg4(arg4), m_arg5(arg5) {}
466   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
467     return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5);
468   }
469   UI_32 size() const {  // NOLINT (readability/nolint)
470     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5);
471   }
472  private:
473   TFn m_function;
474   Arg1 m_arg1;
475   Arg2 m_arg2;
476   Arg3 m_arg3;
477   Arg4 m_arg4;
478   Arg5 m_arg5;
479 };
480
481 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6 >
482 class CFunctor6 : public IFunctor< typename FSig< TFn >::RType > {
483  public:
484   CFunctor6(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6) : m_function(fn), m_arg1(arg1),
485     m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5), m_arg6(arg6) {}
486   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
487     return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6);
488   }
489   UI_32 size() const {  // NOLINT (readability/nolint)
490     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5) + sizeof(Arg6);
491   }
492  private:
493   TFn m_function;
494   Arg1 m_arg1;
495   Arg2 m_arg2;
496   Arg3 m_arg3;
497   Arg4 m_arg4;
498   Arg5 m_arg5;
499   Arg6 m_arg6;
500 };
501
502 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7 >
503 class CFunctor7 : public IFunctor< typename FSig< TFn >::RType > {
504  public:
505   CFunctor7(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
506     : m_function(fn), m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5), m_arg6(arg6), m_arg7(arg7) {}
507   typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
508     return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7);
509   }
510   UI_32 size() const {  // NOLINT (readability/nolint)
511     return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5) + sizeof(Arg6) + sizeof(Arg7);
512   }
513  private:
514   TFn m_function;
515   Arg1 m_arg1;
516   Arg2 m_arg2;
517   Arg3 m_arg3;
518   Arg4 m_arg4;
519   Arg5 m_arg5;
520   Arg6 m_arg6;
521   Arg7 m_arg7;
522 };
523
524
525 template<class TFn>
526 CFunctor0<TFn> functor(TFn fn) {
527   return CFunctor0<TFn>(fn);
528 }
529
530 template<class TFn, class Arg1>
531 CFunctor1<TFn, Arg1> functor(TFn fn, Arg1 arg1) {
532   return CFunctor1<TFn, Arg1>(fn, arg1);
533 }
534
535 template<class TFn, class Arg1, class Arg2>
536 CFunctor2<TFn, Arg1, Arg2> functor(TFn fn, Arg1 arg1, Arg2 arg2) {
537   return CFunctor2<TFn, Arg1, Arg2>(fn, arg1, arg2);
538 }
539
540 template<class TFn, class Arg1, class Arg2, class Arg3>
541 CFunctor3<TFn, Arg1, Arg2, Arg3> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
542   return CFunctor3<TFn, Arg1, Arg2, Arg3>(fn, arg1, arg2, arg3);
543 }
544
545 template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4>
546 CFunctor4<TFn, Arg1, Arg2, Arg3, Arg4> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) {
547   return CFunctor4<TFn, Arg1, Arg2, Arg3, Arg4>(fn, arg1, arg2, arg3, arg4);
548 }
549
550 template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
551 CFunctor5<TFn, Arg1, Arg2, Arg3, Arg4, Arg5> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) {
552   return CFunctor5<TFn, Arg1, Arg2, Arg3, Arg4, Arg5>(fn, arg1, arg2, arg3, arg4, arg5);
553 }
554
555 template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
556 CFunctor6<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,
557                                                            Arg5 arg5, Arg6 arg6) {
558   return CFunctor6<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(fn, arg1, arg2, arg3, arg4, arg5, arg6);
559 }
560
561 template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
562 CFunctor7<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,
563                                                                  Arg5 arg5, Arg6 arg6, Arg7 arg7) {
564   return CFunctor7<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
565 }
566
567 #endif  // __NSFRAMEWORK_NSUTILITY_NSUTILITY__  NOLINT  (build/header_guard)
568 /** @}*/
569 /** @}*/
570 /** @}*/
571 /** @}*/
572 //@}