2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NS_UtilityCenter
19 /// \brief This file contains declaration of common APIs for NS_UtilityCenter.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 * @file ns_utility.hpp
25 * @brief \~english This file contains declaration of common APIs for NS_UtilityCenter.
28 /** @addtogroup BaseSystem
31 /** @addtogroup native_service
35 /** @addtogroup framework_unified
36 * @ingroup native_service
39 /** @addtogroup native
40 * @ingroup framework_unified
43 #ifndef __NSFRAMEWORK_NSUTILITY_NSUTILITY__ // NOLINT (build/header_guard)
44 #define __NSFRAMEWORK_NSUTILITY_NSUTILITY__
46 #include <native_service/frameworkunified_types.h>
56 // Macros ///////////////////////////////////////////////////////////////
58 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
59 TypeName(const TypeName&); \
60 void operator=(const TypeName& )
62 #define DEFINE_EXCEPTION( name, base ) \
63 class name : public base \
66 name ( const char* str = "" ) : base ( str ) \
70 // Helpful type definitions ////////////////////////////////////////////////
73 enum { value = v }; // NOLINT (readability/nolint)
81 // Helpful Fucntions /////////////////////////////////////////////////////////
82 template< class T, UI_32 N >
83 const T *ArrBeg(const T(&arr)[ N ]) {
87 template< class T, UI_32 N >
88 const T *ArrEnd(const T(&arr)[ N ]) {
92 template< class O, class I >
94 return static_cast< O >(i);
97 template< class R, class B >
98 R UnalignedRet(B *b) {
99 return *reinterpret_cast< R volatile * >(b);
102 template< class Cont, class Gen >
103 Cont genRange(UI_32 N, Gen genFn) {
105 std::generate_n(std::back_inserter(c), N, genFn);
109 template< class OCont, class IIter, class MapFn >
110 OCont mapRange(const IIter &begin, const IIter &end, MapFn fn) {
112 std::transform(begin, end, std::inserter(c, c.begin()), fn);
115 // Static Compile-time list creation /////////////////////////////////////////
119 ////////////////////////
120 #include <_pack1.h> //
121 //////////////////////////
124 template< class T, UI_32 N >
127 TList < T, N - 1 > data;
129 T *operator&() { // NOLINT (readability/nolint)
130 return reinterpret_cast< T * >(this);
132 const T *operator&() const { // NOLINT (readability/nolint)
133 return reinterpret_cast< const T * >(this);
135 static const UI_32 count = N; // NOLINT (readability/nolint)
140 struct TList< T, 1 > {
143 T *operator&() { // NOLINT (readability/nolint)
144 return reinterpret_cast< T * >(this);
146 const T *operator&() const { // NOLINT (readability/nolint)
147 return reinterpret_cast< const T * >(this);
149 static const UI_32 count = 1; // NOLINT (readability/nolint)
155 /////////////////////////
156 #include <_packpop.h> //
157 ///////////////////////
160 // Function Decomposition ///////////////////////////////////////////////////
162 template< class Sig >
167 struct FSig< R(*)() > {
169 static const UI_32 argCount = 0; // NOLINT (readability/nolint)
172 template<class R, class T1>
173 struct FSig< R(*)(T1) > {
176 static const UI_32 argCount = 1; // NOLINT (readability/nolint)
179 template<class R, class T1, class T2>
180 struct FSig< R(*)(T1, T2) > {
184 static const UI_32 argCount = 2; // NOLINT (readability/nolint)
187 template<class R, class T1, class T2, class T3>
188 struct FSig< R(*)(T1, T2, T3) > {
193 static const UI_32 argCount = 3; // NOLINT (readability/nolint)
196 template<class R, class T1, class T2, class T3, class T4>
197 struct FSig< R(*)(T1, T2, T3, T4) > {
203 static const UI_32 argCount = 4; // NOLINT (readability/nolint)
206 template<class R, class T1, class T2, class T3, class T4, class T5>
207 struct FSig< R(*)(T1, T2, T3, T4, T5) > {
214 static const UI_32 argCount = 5; // NOLINT (readability/nolint)
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) > {
226 static const UI_32 argCount = 6; // NOLINT (readability/nolint)
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) > {
239 static const UI_32 argCount = 7; // NOLINT (readability/nolint)
244 // Accumulator definition //////////////////////////////////////////////////
246 /// \brief Accumultor type
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:
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
259 Accumulator(T n) : n(n) {}
262 Accumulator(const Accumulator<U> &u) : n(u.n) {}
264 T operator()(T i = 1) { // NOLINT (readability/nolint)
265 return operator()< T >(i);
273 template< typename U >
274 friend class Accumulator;
281 /// \brief Accumulator utility function
283 /// Given a value n of type T, returns an accumulator of type Accumulator<T> initialized to value n
285 Accumulator<T> MakeAccumulator(T n) {
286 return Accumulator<T>(n);
289 // Rsrc Management helper class /////////////////////////////////////////////
295 typedef std::bad_alloc Exception;
296 static void Release(Type rsrc) {
300 static BOOL BadValue(Type rsrc) {
305 template< class RsrcTraits >
306 class RaiseExceptionPolicy {
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();
318 template< class RsrcTraits >
319 class CheckForErrorPolicy { // no exceptions
321 typedef typename RsrcTraits::Type Type;
323 static Type check(Type t) { // NOLINT (readability/nolint)
327 static bool isValid(Type t) { // NOLINT (readability/nolint)
328 return ! RsrcTraits::BadValue(t);
333 class RsrcTraits = MemTraits< T >,
334 template <class> class ErrorPolicy = RaiseExceptionPolicy >
335 class ResourceMgr : public RsrcTraits {
337 typedef typename RsrcTraits::Type Type;
338 typedef ErrorPolicy< RsrcTraits > TFullErrorPolicy;
340 ResourceMgr(Type rsrc) : m_rsrc(TFullErrorPolicy::check(rsrc)) {}
343 RsrcTraits::Release(m_rsrc);
346 operator Type() { // NOLINT (readability/nolint)
350 operator const Type() const { // NOLINT (readability/nolint)
354 bool isValid() const { // NOLINT (readability/nolint)
355 return TFullErrorPolicy::isValid(m_rsrc);
359 DISALLOW_COPY_AND_ASSIGN(ResourceMgr);
364 // Functor Helper classes /////////////////////////////////////
369 virtual ~IFunctor() {}
371 virtual R operator()() const = 0; // NOLINT (readability/nolint)
372 virtual UI_32 size() const = 0; // NOLINT (readability/nolint)
375 template< class TFn >
376 class CFunctor0 : public IFunctor< typename FSig< TFn >::RType > {
378 CFunctor0(TFn fn) : m_function(fn) {}
379 typename FSig< TFn >::RType operator()() const { // NOLINT (readability/nolint)
382 UI_32 size() const { // NOLINT (readability/nolint)
390 template< class TFn, class Arg1 >
391 class CFunctor1 : public IFunctor< typename FSig< TFn >::RType > {
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);
398 UI_32 size() const { // NOLINT (readability/nolint)
399 return sizeof(TFn) + sizeof(Arg1);
407 template< class TFn, class Arg1, class Arg2 >
408 class CFunctor2 : public IFunctor< typename FSig< TFn >::RType > {
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);
415 UI_32 size() const { // NOLINT (readability/nolint)
416 return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2);
424 template< class TFn, class Arg1, class Arg2, class Arg3 >
425 class CFunctor3 : public IFunctor< typename FSig< TFn >::RType > {
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);
432 UI_32 size() const { // NOLINT (readability/nolint)
433 return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3);
442 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4 >
443 class CFunctor4 : public IFunctor< typename FSig< TFn >::RType > {
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);
450 UI_32 size() const { // NOLINT (readability/nolint)
451 return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4);
461 template< class TFn, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5 >
462 class CFunctor5 : public IFunctor< typename FSig< TFn >::RType > {
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);
469 UI_32 size() const { // NOLINT (readability/nolint)
470 return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5);
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 > {
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);
489 UI_32 size() const { // NOLINT (readability/nolint)
490 return sizeof(TFn) + sizeof(Arg1) + sizeof(Arg2) + sizeof(Arg3) + sizeof(Arg4) + sizeof(Arg5) + sizeof(Arg6);
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 > {
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);
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);
526 CFunctor0<TFn> functor(TFn fn) {
527 return CFunctor0<TFn>(fn);
530 template<class TFn, class Arg1>
531 CFunctor1<TFn, Arg1> functor(TFn fn, Arg1 arg1) {
532 return CFunctor1<TFn, Arg1>(fn, arg1);
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);
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);
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);
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);
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);
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);
567 #endif // __NSFRAMEWORK_NSUTILITY_NSUTILITY__ NOLINT (build/header_guard)