Facebook
From Twist and Shout, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 51
  1. namespace
  2. {
  3.   namespace DetailsImpl // This suggest that you shouldn't use what's inside directly
  4.   {
  5.     template<class... T> struct contains_impl;
  6.  
  7.     template<class F> struct contains_impl<F>
  8.     {
  9.         static constexpr  bool _v = false;
  10.     };
  11.  
  12.     template<class F,class T1, class... T> struct contains_impl<F,T1, T...>
  13.     {
  14.         static constexpr bool _v = std::is_same<F,T1>::value || contains_impl<F, T...>::_v;
  15.     };
  16.  
  17.     template<class F, class T, class...Types>
  18.     struct contains
  19.     {
  20.         static constexpr bool _v = contains_impl<F, Types...>::_v;
  21.     };
  22.  
  23.     template<typename ... Types> struct RemoteTypes;
  24.  
  25.     template<typename...T>
  26.     struct MustBeRemoted
  27.     {
  28.       constexpr static bool value = false;
  29.     };
  30.  
  31.     template<typename T, typename First, class...Types>
  32.     struct MustBeRemoted<T,RemoteTypes<First,Types...>>
  33.     {
  34.         constexpr static bool value = contains<T, First, Types...>::_v;
  35.     };
  36.  
  37.     // Add here (or remove) every type to be NRemote::Set()ed
  38.     using RemoteList = RemoteTypes
  39.       <
  40.         IMapSelectionEngine,
  41.         CTTPlusManager,
  42.         TomTom::NavKit::UserProfile::IHybridStorage,
  43.         NMapInfo::IMapInfoEngine,
  44.         NNavKit::NMapMatching::CMapMatchingComponent,
  45.         //TomTom::NavKit::Http::Client::IHttpRequestManager, //NRemote managed
  46.         TLanguageHandler,
  47.         CPOIFilesChangesMonitor
  48.       >;
  49.  
  50.     // We need to convert to the std::unique_ptr<O> type to work around some problems
  51.     // with QNX's Dinkum library
  52.     template<typename T, typename O, typename D, typename... Args>
  53.     std::unique_ptr<O, D> make_unique_ptr_wd(D d, Args&&... args)
  54.     {
  55.       return std::unique_ptr<O, D>(static_cast<O*>(NEW T(std::forward<Args>(args)...)), d);
  56.     }
  57.   }
  58.  
  59.   template<typename T, bool Del = true>
  60.   void RemoteUnregister(T* obj)
  61.   {
  62.     if ( DetailsImpl::MustBeRemoted<T, DetailsImpl::RemoteList>::value )
  63.     {
  64.       NRemoting::Set<T>(nullptr);
  65.     }
  66.     if ( Del == true )
  67.     {
  68.       delete obj;
  69.     }
  70.   }
  71.  
  72.   template<>
  73.   void RemoteUnregister<TomTom::CTimerEvent>( TomTom::CTimerEvent* obj )
  74.   {
  75.     TomTom::CTimerEvent::Delete( obj );
  76.   }
  77.  
  78.   // T is the default type for Output, so if you need one don't forget to set it
  79.   template<typename T, typename O = T, typename... Args>
  80.   std::unique_ptr<O, Details::DeleterType<O>> make_unique_ptr(Args&&... args)
  81.   {
  82.     static_assert( std::is_base_of<O, T>::value, "O must be a base of T" );
  83.     Details::DeleterType<O> d = RemoteUnregister<O>;
  84.     auto r = DetailsImpl::make_unique_ptr_wd<T,O,Details::DeleterType<O>>(d, std::forward<Args>(args)...);
  85.     if ( DetailsImpl::MustBeRemoted<O>::value )
  86.     {
  87.       NRemoting::Set<O>(r.get());
  88.     }
  89.     return r;
  90.   }
  91.  
  92.   // Use this for pointers that we own and manage but that we don't create
  93.   template<typename T, typename O = T>
  94.   std::unique_ptr<O, Details::DeleterType<O>> manage_unique_ptr(T* item)
  95.   {
  96.     Details::DeleterType<O> d = RemoteUnregister<O, false>;
  97.     std::unique_ptr<O, Details::DeleterType<O>> r{item, d};
  98.     if ( DetailsImpl::MustBeRemoted<T>::value )
  99.     {
  100.       NRemoting::Set<O>(r.get());
  101.     }
  102.     return r;
  103.   }
  104. }
  105.