From bef24e7b4001c7c7cd7a03a22f8eaf7c8af3c4b7 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期四, 20 一月 2022 14:08:35 +0800 Subject: [PATCH] performance --- src/common.h | 53 ++++++++++++++++++++++++++++------------------------- 1 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/common.h b/src/common.h index db4761d..253d124 100644 --- a/src/common.h +++ b/src/common.h @@ -16,23 +16,6 @@ #include <nng/nng.h> namespace nng_wrap { -template <class... T> struct make_void{typedef void type;}; -template <class... T> using void_t = typename make_void<T...>::type; -template <class T, typename = void> struct is_default_c : std::false_type{}; -template <class T> struct is_default_c<T, void_t<decltype(T())>> : std::true_type{}; -template<class T> using is_function_t = typename std::is_function<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::type; -template <bool, class T> struct is_callable_h : is_function_t<T>{}; -template <class T> struct is_callable_h<true, T>{ -private: - struct FB{void operator()();}; - struct D : T, FB{}; - template<typename U, U> struct c; - template<class> static std::true_type t(...); - template<class C> static std::false_type t(c<void(FB::*)(), &C::operator()>*); -public: - using type = decltype(t<D>(nullptr)); -}; -template <class T> using is_callable = typename is_callable_h<std::is_class<typename std::remove_reference<T>::type>::value, typename std::remove_reference<T>::type>::type; static thread_local std::string verbose_info{}; #ifndef PRNTVITAG @@ -91,7 +74,25 @@ return {}; } -static constexpr int timeout_req_rep = 5162; +template <class... T> struct make_void{typedef void type;}; +template <class... T> using void_t = typename make_void<T...>::type; +template <class T, typename = void> struct is_default_c : std::false_type{}; +template <class T> struct is_default_c<T, void_t<decltype(T()),decltype(std::declval<T>().operator()())>> : std::true_type{}; +template<class T> using is_function_t = typename std::is_function<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::type; +template <bool, class T> struct is_callable_h : is_function_t<T>{}; +template <class T> struct is_callable_h<true, T>{ +private: + struct FB{void operator()();}; + struct D : T, FB{}; + template<typename U, U> struct c; + template<class> static std::true_type t(...); + template<class C> static std::false_type t(c<void(FB::*)(), &C::operator()>*); +public: + using type = decltype(t<D>(nullptr)); +}; +template <class T> using is_callable = typename is_callable_h<std::is_class<typename std::remove_reference<T>::type>::value, typename std::remove_reference<T>::type>::type; + +static constexpr int timeout_req_rep = 6251; inline void remove_exist(const std::string& url){ if (url.find("ipc://") == 0){ @@ -136,8 +137,10 @@ public: DISABLE_COPY_AND_ASSIGN(_ps); _ps()=default; + int operator()(){return msg_.size();} virtual ~_ps(){ t_quit_.store(true, std::memory_order_relaxed); + cv_msg_.notify_all(); if (t_.joinable()) t_.join(); } @@ -153,7 +156,7 @@ DISABLE_COPY_AND_ASSIGN(_ps_sub); _ps_sub()=default; ~_ps_sub()=default; - + std::mutex& operator()(){return mtx_topics_;} std::unordered_set<std::string> topics_{}; std::mutex mtx_topics_{}; std::unordered_set<std::string> failed_topics_{}; @@ -164,6 +167,7 @@ public: DISABLE_COPY_AND_ASSIGN(_sv); _sv()=default; + std::deque<std::string> operator()(){return {fixed_msg_};} ~_sv(){ t_quit_.store(true, std::memory_order_relaxed); if (t_.joinable()) t_.join(); @@ -189,6 +193,7 @@ public: DISABLE_COPY_AND_ASSIGN(_rr); _rr()=default; + std::tuple<uint64_t&,std::unordered_map<uint64_t, std::string>&> operator()(){return std::tie(work_index_, msg_);} ~_rr(){ if(std::get<0>(socks_).id > 0) nng_close(std::get<0>(socks_)); if(std::get<0>(std::get<1>(socks_)).id > 0) nng_close(std::get<0>(std::get<1>(socks_))); @@ -219,12 +224,10 @@ }; -template<class T, typename std::enable_if<is_default_c<T>::value, int>::type=0> inline T* singleton(){ static auto t = std::make_unique<T>(); return t.get(); } - -template <class T, class... Args, typename std::enable_if<is_callable<T>::value, int>::type=0> -inline std::thread get_thread(T&& t, Args&&... args){ - return std::thread(std::forward<T>(t), std::forward<Args>(args)...); -} +template<class T, typename std::enable_if<is_default_c<T>::value, int>::type=0> +inline T* singleton(){ static auto t = std::make_unique<T>(); return t.get(); } +template <class T, class... Args, typename std::enable_if<is_callable<T>::value&&sizeof...(Args)==1, int>::type=0> +inline std::thread get_thread(T&& t, Args&&... args){return std::thread(std::forward<T>(t), std::forward<Args>(args)...);} } #endif \ No newline at end of file -- Gitblit v1.8.0