summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/include/native_service/ns_utility.hpp
blob: 07fe4f55cc9cf9c17f6142a3acc668c93b1df480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

////////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_NS_UtilityCenter
/// \brief    This file contains declaration of common APIs for NS_UtilityCenter.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
//@{
/**
 * @file ns_utility.hpp
 * @brief \~english This file contains declaration of common APIs for NS_UtilityCenter.
 *
 */
/** @addtogroup BaseSystem
 *  @{
 */
/** @addtogroup native_service
 *  @ingroup BaseSystem
 *  @{
 */
/** @addtogroup framework_unified
 *  @ingroup native_service
 *  @{
 */
/** @addtogroup native
 *  @ingroup framework_unified
 *  @{
 */
#ifndef __NSFRAMEWORK_NSUTILITY_NSUTILITY__  // NOLINT  (build/header_guard)
#define __NSFRAMEWORK_NSUTILITY_NSUTILITY__

#include <native_service/frameworkunified_types.h>
#include <new>
#include <exception>
#include <algorithm>
#include <iterator>

#ifdef AGL_STUB
#include <stdexcept>
#endif

// Macros ///////////////////////////////////////////////////////////////

#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  TypeName(const TypeName&);               \
  void operator=(const TypeName& )

#define DEFINE_EXCEPTION( name, base ) \
class name : public base \
{ \
public: \
  name ( const char* str = "" ) : base ( str ) \
  {} \
}

// Helpful type definitions ////////////////////////////////////////////////
template <int v>
struct Int2Type {
  enum { value = v };  // NOLINT (readability/nolint)
};

template <class T>
struct Type2Type {
  typedef T Type;
};

// Helpful Fucntions /////////////////////////////////////////////////////////
template< class T, UI_32 N >
const T *ArrBeg(const T(&arr)[ N ]) {
  return &arr[ 0 ];
}

template< class T, UI_32 N >
const T *ArrEnd(const T(&arr)[ N ]) {
  return &arr[ 0 ] + N;
}

template< class O, class I >
O SimpleCast(I i) {
  return static_cast< O >(i);
}

template< class R, class B >
R UnalignedRet(B *b) {
  return *reinterpret_cast< R volatile * >(b);
}

template< class Cont, class Gen >
Cont genRange(UI_32 N, Gen genFn) {
  Cont c;
  std::generate_n(std::back_inserter(c), N, genFn);
  return c;
}

template< class OCont, class IIter, class MapFn >
OCont mapRange(const IIter &begin, const IIter &end, MapFn fn) {
  OCont c;
  std::transform(begin, end, std::inserter(c, c.begin()), fn);
  return c;
}
// Static Compile-time list creation /////////////////////////////////////////

#ifdef AGL_STUB
#else
////////////////////////
#include <_pack1.h>    //
//////////////////////////
#endif

template< class T, UI_32 N >
struct TList {
  T datum;
  TList < T, N - 1 > data;

  T *operator&() {  // NOLINT (readability/nolint)
    return reinterpret_cast< T * >(this);
  }
  const T *operator&() const {  // NOLINT (readability/nolint)
    return reinterpret_cast< const T * >(this);
  }
  static const UI_32 count = N;  // NOLINT (readability/nolint)

};

template< class T >
struct TList< T, 1 > {
  T datum;

  T *operator&() {  // NOLINT (readability/nolint)
    return reinterpret_cast< T * >(this);
  }
  const T *operator&() const {  // NOLINT (readability/nolint)
    return reinterpret_cast< const T * >(this);
  }
  static const UI_32 count = 1;  // NOLINT (readability/nolint)

};

#ifdef AGL_STUB
#else
/////////////////////////
#include <_packpop.h> //
///////////////////////
#endif

// Function Decomposition ///////////////////////////////////////////////////

template< class Sig >
struct FSig {
};

template<class R>
struct FSig< R(*)() > {
  typedef R RType;
  static const UI_32 argCount = 0;  // NOLINT (readability/nolint)
};

template<class R, class T1>
struct FSig< R(*)(T1) > {
  typedef R RType;
  typedef T1 TArg1;
  static const UI_32 argCount = 1;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2>
struct FSig< R(*)(T1, T2) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  static const UI_32 argCount = 2;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2, class T3>
struct FSig< R(*)(T1, T2, T3) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  typedef T3 TArg3;
  static const UI_32 argCount = 3;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2, class T3, class T4>
struct FSig< R(*)(T1, T2, T3, T4) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  typedef T3 TArg3;
  typedef T4 TArg4;
  static const UI_32 argCount = 4;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2, class T3, class T4, class T5>
struct FSig< R(*)(T1, T2, T3, T4, T5) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  typedef T3 TArg3;
  typedef T4 TArg4;
  typedef T5 TArg5;
  static const UI_32 argCount = 5;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2, class T3, class T4, class T5, class T6>
struct FSig< R(*)(T1, T2, T3, T4, T5, T6) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  typedef T3 TArg3;
  typedef T4 TArg4;
  typedef T5 TArg5;
  typedef T6 TArg6;
  static const UI_32 argCount = 6;  // NOLINT (readability/nolint)
};

template<class R, class T1, class T2, class T3, class T4, class T5, class T6, class T7>
struct FSig< R(*)(T1, T2, T3, T4, T5, T6, T7) > {
  typedef R RType;
  typedef T1 TArg1;
  typedef T2 TArg2;
  typedef T3 TArg3;
  typedef T4 TArg4;
  typedef T5 TArg5;
  typedef T6 TArg6;
  typedef T7 TArg7;
  static const UI_32 argCount = 7;  // NOLINT (readability/nolint)
};



// Accumulator definition //////////////////////////////////////////////////

/// \brief Accumultor type
///
/// An accumulator is an object that keeps an internal counter, and will increment its
/// counter by n, which defaults to 1, each time operator() is called on it.  Eample usage:
///
/// Accumulator< int > a( 42 );  // initialize a to 42
/// int v1 = a();   // <-- v1 == 43
/// int v2 = a();   // <-- v2 == 44
/// int v4 = a(10);  // <-- v4 == 54
/// int v5 = a();   // <-- v5 == 55
template<typename T>
class Accumulator {
 public:
  Accumulator(T n) : n(n) {}

  template<typename U>
  Accumulator(const Accumulator<U> &u) : n(u.n) {}

  T operator()(T i = 1) {  // NOLINT (readability/nolint)
    return operator()< T >(i);
  }

  template<typename U>
  T operator()(U i) {
    return n += i;
  }

  template< typename U >
  friend class Accumulator;

 private:
  T n;
};


/// \brief Accumulator utility function
///
/// Given a value n of type T, returns an accumulator of type Accumulator<T> initialized to value n
template<typename T>
Accumulator<T> MakeAccumulator(T n) {
  return Accumulator<T>(n);
}

// Rsrc Management helper class /////////////////////////////////////////////

template< class T >
class MemTraits {
 public:
  typedef T *Type;
  typedef std::bad_alloc Exception;
  static void Release(Type rsrc) {
    delete rsrc;
  }

  static BOOL BadValue(Type rsrc) {
    return NULL == rsrc;
  }
};

template< class RsrcTraits >
class RaiseExceptionPolicy {
 public:
  typedef typename RsrcTraits::Type Type;
  static Type check(Type t) {  // NOLINT (readability/nolint)
    if (RsrcTraits::BadValue(t)) {
      throw typename RsrcTraits::Exception();
    }

    return t;
  }
};

template< class RsrcTraits >
class CheckForErrorPolicy {  // no exceptions
 public:
  typedef typename RsrcTraits::Type Type;

  static Type check(Type t) {  // NOLINT (readability/nolint)
    return t;
  }

  static bool isValid(Type t) {  // NOLINT (readability/nolint)
    return ! RsrcTraits::BadValue(t);
  }
};

template < class T,
           class RsrcTraits                    = MemTraits< T >,
           template <class> class ErrorPolicy  = RaiseExceptionPolicy >
class ResourceMgr : public RsrcTraits {
 public:
  typedef typename RsrcTraits::Type Type;
  typedef ErrorPolicy< RsrcTraits > TFullErrorPolicy;

  ResourceMgr(Type rsrc) : m_rsrc(TFullErrorPolicy::check(rsrc)) {}

  ~ResourceMgr() {
    RsrcTraits::Release(m_rsrc);
  }

  operator Type() {  // NOLINT (readability/nolint)
    return m_rsrc;
  }

  operator const Type() const {  // NOLINT (readability/nolint)
    return m_rsrc;
  }

  bool isValid() const {  // NOLINT (readability/nolint)
    return TFullErrorPolicy::isValid(m_rsrc);
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(ResourceMgr);

  Type m_rsrc;
};

// Functor Helper classes /////////////////////////////////////
template< class R >
class IFunctor {
 public:
  IFunctor() {}
  virtual ~IFunctor() {}

  virtual R operator()() const = 0;  // NOLINT (readability/nolint)
  virtual UI_32 size() const = 0;  // NOLINT (readability/nolint)
};

template< class TFn >
class CFunctor0 : public IFunctor< typename FSig< TFn >::RType > {
 public:
  CFunctor0(TFn fn) : m_function(fn) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function();
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn);
  }

 private:
  TFn m_function;
};

template< class TFn, class Arg1 >
class CFunctor1 : public IFunctor< typename FSig< TFn >::RType > {
 public:

  CFunctor1(TFn fn, Arg1 arg1) : m_function(fn), m_arg1(arg1) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1);
  }

 private:
  TFn m_function;
  Arg1 m_arg1;
};

template< class TFn, class Arg1, class Arg2 >
class CFunctor2 : public IFunctor< typename FSig< TFn >::RType > {
 public:

  CFunctor2(TFn fn, Arg1 arg1, Arg2 arg2) : m_function(fn), m_arg1(arg1), m_arg2(arg2) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
};

template< class TFn, class Arg1, class Arg2, class Arg3 >
class CFunctor3 : public IFunctor< typename FSig< TFn >::RType > {
 public:

  CFunctor3(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3) : m_function(fn), m_arg1(arg1), m_arg2(arg2), m_arg3(arg3) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2, m_arg3);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
  Arg3 m_arg3;
};

template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4 >
class CFunctor4 : public IFunctor< typename FSig< TFn >::RType > {
 public:
  CFunctor4(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) : m_function(fn), m_arg1(arg1), m_arg2(arg2),
    m_arg3(arg3), m_arg4(arg4) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2, m_arg3, m_arg4);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
  Arg3 m_arg3;
  Arg4 m_arg4;
};

template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5 >
class CFunctor5 : public IFunctor< typename FSig< TFn >::RType > {
 public:
  CFunctor5(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) : m_function(fn), m_arg1(arg1), m_arg2(arg2),
    m_arg3(arg3), m_arg4(arg4), m_arg5(arg5) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
  Arg3 m_arg3;
  Arg4 m_arg4;
  Arg5 m_arg5;
};

template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6 >
class CFunctor6 : public IFunctor< typename FSig< TFn >::RType > {
 public:
  CFunctor6(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6) : m_function(fn), m_arg1(arg1),
    m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5), m_arg6(arg6) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5) + sizeof(Arg6);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
  Arg3 m_arg3;
  Arg4 m_arg4;
  Arg5 m_arg5;
  Arg6 m_arg6;
};

template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7 >
class CFunctor7 : public IFunctor< typename FSig< TFn >::RType > {
 public:
  CFunctor7(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
    : m_function(fn), m_arg1(arg1), m_arg2(arg2), m_arg3(arg3), m_arg4(arg4), m_arg5(arg5), m_arg6(arg6), m_arg7(arg7) {}
  typename FSig< TFn >::RType operator()() const {  // NOLINT (readability/nolint)
    return m_function(m_arg1, m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7);
  }
  UI_32 size() const {  // NOLINT (readability/nolint)
    return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5) + sizeof(Arg6) + sizeof(Arg7);
  }
 private:
  TFn m_function;
  Arg1 m_arg1;
  Arg2 m_arg2;
  Arg3 m_arg3;
  Arg4 m_arg4;
  Arg5 m_arg5;
  Arg6 m_arg6;
  Arg7 m_arg7;
};


template<class TFn>
CFunctor0<TFn> functor(TFn fn) {
  return CFunctor0<TFn>(fn);
}

template<class TFn, class Arg1>
CFunctor1<TFn, Arg1> functor(TFn fn, Arg1 arg1) {
  return CFunctor1<TFn, Arg1>(fn, arg1);
}

template<class TFn, class Arg1, class Arg2>
CFunctor2<TFn, Arg1, Arg2> functor(TFn fn, Arg1 arg1, Arg2 arg2) {
  return CFunctor2<TFn, Arg1, Arg2>(fn, arg1, arg2);
}

template<class TFn, class Arg1, class Arg2, class Arg3>
CFunctor3<TFn, Arg1, Arg2, Arg3> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
  return CFunctor3<TFn, Arg1, Arg2, Arg3>(fn, arg1, arg2, arg3);
}

template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4>
CFunctor4<TFn, Arg1, Arg2, Arg3, Arg4> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4) {
  return CFunctor4<TFn, Arg1, Arg2, Arg3, Arg4>(fn, arg1, arg2, arg3, arg4);
}

template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
CFunctor5<TFn, Arg1, Arg2, Arg3, Arg4, Arg5> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5) {
  return CFunctor5<TFn, Arg1, Arg2, Arg3, Arg4, Arg5>(fn, arg1, arg2, arg3, arg4, arg5);
}

template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
CFunctor6<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,
                                                           Arg5 arg5, Arg6 arg6) {
  return CFunctor6<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6>(fn, arg1, arg2, arg3, arg4, arg5, arg6);
}

template<class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
CFunctor7<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7> functor(TFn fn, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4,
                                                                 Arg5 arg5, Arg6 arg6, Arg7 arg7) {
  return CFunctor7<TFn, Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7>(fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}

#endif  // __NSFRAMEWORK_NSUTILITY_NSUTILITY__  NOLINT  (build/header_guard)
/** @}*/
/** @}*/
/** @}*/
/** @}*/
//@}